egglog 3.0.0

egglog is a language that combines the benefits of equality saturation and datalog. It can be used for analysis, optimization, and synthesis of programs. It is the successor to the popular rust library egg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Datatype declarations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(datatype Loc 
    (Mem) ; This is where the generic computation happens
    (AMX) ; AMX tiles
    (WMMA_A) ; CUDA Warp-level Matrix Multiplication registers
    (WMMA_B)
    (WMMA_C)
)

(datatype BinOp
    (Add)
    (Sub)
    (Mul)
    (Div)
    (Mod)
    (Min)
    (Max)
    (EQ)
    (NE)
    (LT)
    (LE)
    (GT)
    (GE)
    (And)
    (Or))
(datatype UnaOp
    (Not))

; TODO
(datatype Buffer)
(datatype Parameter)
(datatype Type
    ;;  bits x lanes
    (Int i64 i64)
    (UInt i64 i64)
    (Float i64 i64)
    (BFloat i64 i64)
    (Handle i64)
)
(datatype CallType
    (Image)
    (Extern)
    (ExternCPlusPlus)
    (PureExtern)
    (Halide)
    (Intrinsic)
    (PureIntrinsic)
)

(sort Stmt)
(sort VecInt (Vec i64))
(sort Expr)
(sort VecExpr (Vec Expr))

(sort Variable)
(constructor V (String) Variable)

;; Expr
(constructor Cast (Type Expr) Expr)
(constructor Reinterpret (Type Expr) Expr)
(constructor Bop (BinOp Expr Expr) Expr)
(constructor Uop (UnaOp Expr) Expr)
;; condition true_val false_val
(constructor Select (Expr Expr Expr) Expr)
;; name index
(constructor Load (Type Variable Expr) Expr)
;; base stride lanes
(constructor Ramp (Expr Expr i64) Expr :cost 100)
;; value lanes
(constructor Broadcast (Expr i64) Expr :cost 100)
;; name value body
;; TODO: Not sure if we need this one, binding is tricky
(constructor Let (String Expr Expr) Expr)
;; This one is also tricky
(constructor Call (String Type VecExpr CallType) Expr)
;; name
;; TODO: there are some arguments that are ignored here
(constructor Var (Type Variable) Expr)
(constructor Shuffle (VecExpr VecInt) Expr)
(constructor VectorReduce (Type BinOp Expr) Expr)
;;                bits val
(constructor IntImm (i64  i64) Expr)
; u64 is not supported in egglog
(constructor UIntImm (i64  i64) Expr)
(constructor FloatImm (i64 f64) Expr)


;; Stmt

;;                name  value index
(constructor Store (String Expr  Expr) Stmt)
(constructor Evaluate (Expr) Stmt)


;; Some secondary stmts that we may not use
;; Not supported: Provide, Realize, Acquire, Prefetch, HoistedStorage

;; TODO: MemoryType, ForType, Partition, DeviceAPI
; (constructor LetStmt (String Expr Stmt) Stmt)
; (constructor AssertStmt (Expr Expr) Stmt)
; (constructor ProducerConsumer (String bool Stmt) Stmt)
; ;;                 name    type mem_type   extents cond body 
; (constructor Allocate (String Type MemoryType VecExpr Expr Stmt) Stmt)
; (constructor Free (String) Stmt)
; (constructor Block (Stmt Stmt) Stmt)
; (constructor Fork (Stmt Stmt) Stmt)
; (constructor IfThenElse (Expr Stmt Stmt) Stmt)
; (constructor For (String Expr Expr ForType Partition DeviceAPI Stmt) Stmt)
; (constructor Atomic (String String Stmt) Stmt)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Extensions to the IR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(constructor Loc2Loc (Loc Loc Expr) Expr :unextractable)
; (constructor Mem2AMX (Expr) Expr :unextractable)
(constructor Mem2AMX (Expr) Expr :cost 100000)
; (constructor AMX2Mem (Expr) Expr :unextractable)
(constructor AMX2Mem (Expr) Expr :cost 100000)
(constructor WMMA2Mem (Expr) Expr :cost 100000)
(constructor Mem2WMMA (Expr) Expr :cost 100000)

(birewrite (Mem2AMX e) (Loc2Loc (Mem) (AMX) e))
(birewrite (AMX2Mem e) (Loc2Loc (AMX) (Mem) e))
(birewrite (WMMA2Mem e) (Loc2Loc (WMMA_C) (Mem) e))
(birewrite (Mem2WMMA e) (Loc2Loc (Mem) (WMMA_C) e))
(rewrite (Loc2Loc a b (Loc2Loc b a e)) e)

(constructor ExprVar (Loc Expr) Variable)
;; See `optimization/vector_axioms.egg` for conversion between `Broadcast` and `BroadcastPer`
(constructor BroadcastPer (i64 Expr i64) Expr :cost 200) ;; cost 200 to prioritize `Broadcast` over `BroadcastPer`

;; list of intrinsics
; KWayInterleave(i64 k, Expr base, i64 lanes): i64x(height*width)

;; We don't need StoreAndLoad... Use ExprVar instead
; ;; takes an expression over loc1, stores it in loc1, 
; ;; and then loads it back in loc2
; ;; There is actually no load happening here:
; ;;       *StoreAndLoad returns the base address and by itself
; ;;       it does not load the value
; ;;       It may be better named as StoreAndReturnBaseAddress (This name is suggested by Copilot)
; ;;       Or Spill
; (constructor GenericStoreAndLoad (Loc Loc Expr) Expr)
; ;; Takes a Mem expression, stores it in memory, and then loads it back
; (constructor StoreAndLoad (Expr) Expr)
; ;; takes an AMX expression, tile_store it in memory, 
; ;; and then load this memory back
; (constructor AMXStoreAndLoad (Expr) Expr)
; (birewrite (AMXStoreAndLoad e) (GenericStoreAndLoad (AMX) (Mem) e))
; (birewrite (StoreAndLoad e) (GenericStoreAndLoad (Mem) (Mem) e))


;; This is for use with StoreAndLoad
(constructor Computed () Expr)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; true if the type is signed
(constructor UIntOrInt (bool i64 i64) Type)
(birewrite (UIntOrInt false i l) (UInt i l))
(birewrite (UIntOrInt true i l) (Int i l))

(constructor UIntOrIntImm (bool i64 i64) Expr)
(birewrite (UIntOrIntImm false b x) (UIntImm b x))
(birewrite (UIntOrIntImm true b x) (IntImm b x))

(constructor IntImm64 (i64) Expr)
(birewrite (IntImm64 x) (IntImm 64 x))
(constructor IntImm32 (i64) Expr)
(birewrite (IntImm32 x) (IntImm 32 x))
(constructor IntImm16 (i64) Expr)
(birewrite (IntImm16 x) (IntImm 16 x))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Rulesets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(ruleset amx)
; (ruleset amx-alwaysrun)

(ruleset canonicalize)
(ruleset assemble)
(ruleset typechecking)
(ruleset push-down-vector-reduce)

; 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Properties
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(relation CommBop (BinOp))
(rule () (
    (CommBop (Add))
    (CommBop (Mul))
    (CommBop (And))
    (CommBop (Or))
    (CommBop (Min))
    (CommBop (Max))
    (CommBop (EQ))
    (CommBop (NE))
))

(relation AddOrSub (BinOp))
(rule () (
    (AddOrSub (Add))
    (AddOrSub (Sub))
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Analysis rules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; IsExpr
(relation IsExpr (Expr))
(rule ((= e (Cast _type _expr))) ((IsExpr e)))
(rule ((= e (Reinterpret _type _expr))) ((IsExpr e)))
(rule ((= e (Bop _binop _expr1 _expr2))) ((IsExpr e)))
(rule ((= e (Select _expr1 _expr2 _expr3))) ((IsExpr e)))
(rule ((= e (Load _type _string _expr))) ((IsExpr e)))
(rule ((= e (Ramp _expr1 _expr2 _i64))) ((IsExpr e)))
(rule ((= e (Broadcast _expr _i64))) ((IsExpr e)))
(rule ((= e (Let _string _expr1 _expr2))) ((IsExpr e)))
(rule ((= e (Call _string _type _vecexpr _calltype))) ((IsExpr e)))
(rule ((= e (Var _type _string))) ((IsExpr e)))
(rule ((= e (Shuffle _vecexpr _vecint))) ((IsExpr e)))
(rule ((= e (VectorReduce _type _binop _expr))) ((IsExpr e)))
(rule ((= e (IntImm _bits _i64))) ((IsExpr e)))

; 


;; Type checking
(constructor MultipliedLanes (Type i64) Type)
(rewrite (MultipliedLanes (Int i l) x) (Int i (* l x)) :ruleset typechecking)
(rewrite (MultipliedLanes (UInt i l) x) (UInt i (* l x)) :ruleset typechecking)
(rewrite (MultipliedLanes (Float i l) x) (Float i (* l x)) :ruleset typechecking)
(rewrite (MultipliedLanes (BFloat i l) x) (BFloat i (* l x)) :ruleset typechecking)
(rewrite (MultipliedLanes (Handle l) x) (Handle (* l x)) :ruleset typechecking)

(function LanesInType (Type) i64 :no-merge)
(rule ((= t (Int i l)))
      ((set (LanesInType t) l)) :ruleset typechecking)
(rule ((= t (UInt i l)))
      ((set (LanesInType t) l)) :ruleset typechecking)
(rule ((= t (Float i l)))
      ((set (LanesInType t) l)) :ruleset typechecking)
(rule ((= t (BFloat i l)))
      ((set (LanesInType t) l)) :ruleset typechecking)
(rule ((= t (Handle l)))
      ((set (LanesInType t) l)) :ruleset typechecking)

(constructor WithLanes (Type i64) Type)
(rewrite (WithLanes (Int i l) x) (Int i x) :ruleset typechecking)
(rewrite (WithLanes (UInt i l) x) (UInt i x) :ruleset typechecking)
(rewrite (WithLanes (Float i l) x) (Float i x) :ruleset typechecking)
(rewrite (WithLanes (BFloat i l) x) (BFloat i x) :ruleset typechecking)
(rewrite (WithLanes (Handle l) x) (Handle x) :ruleset typechecking)

(relation has-type (Expr Type))
(rule ((= e (Cast ty expr))) ((has-type e ty)) :ruleset typechecking)
(rule ((= e (Reinterpret ty expr))) ((has-type e ty)) :ruleset typechecking)
(rule (
    (= e (Bop bop e1 e2))
    (has-type e1 t)
    (has-type e2 t)
) ((has-type e t)) :ruleset typechecking)
(rule (
    (= e (Uop uop e1))
    (has-type e1 t)
) ((has-type e t)) :ruleset typechecking)
(rule ((= e (Select cond tval fval))
    (has-type cond (UInt 1 _lanes))
    (has-type tval ty)
    (has-type fval ty)
) ((has-type e ty)) :ruleset typechecking)
(rule ((= e (Load ty name child))) ((has-type e ty)) :ruleset typechecking)
(rule ((= e (Ramp child s l)) 
    (has-type child ty)
) ((has-type e (MultipliedLanes ty l))) :ruleset typechecking)
(rule ((= e (Broadcast child l)) (has-type child ty)) 
    ((has-type e (MultipliedLanes ty l))) :ruleset typechecking)
(rule ((= e (Let x e1 e2))) 
    ((panic "let not supported")) :ruleset typechecking)
(rule ((= e (Call fn out-type args calltype))
) ((has-type e out-type)) :ruleset typechecking)
(rule ((= e (Var ty x))) ((has-type e ty)) :ruleset typechecking)
(rule ((= e (Shuffle vecexpr vecint))) 
    ((panic "not supported")) :ruleset typechecking)
(rule ((= e (VectorReduce ty binop expr))) 
    ((has-type e ty)) :ruleset typechecking)
(rule ((= e (IntImm bits i64))) ((has-type e (Int bits 1))) :ruleset typechecking)
(rule ((= e (UIntImm bits i64))) ((has-type e (UInt bits 1))) :ruleset typechecking)
(rule ((= e (FloatImm bits f64))) ((has-type e (Float bits 1))) :ruleset typechecking)

;; extensions
(rule (
    (= e (Loc2Loc l1 l2 e1))
    (has-type e1 t)
) (
    (has-type e t)
) :ruleset typechecking)
(rule (
    (= e (BroadcastPer p e1 l))
    (has-type e1 t)
) (
    (has-type e (MultipliedLanes t l))
) :ruleset typechecking)

(rule ((has-type e t1) (has-type e t2) (!= t1 t2)) ((panic "type error")))

; 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; !!Dangerous!!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(rewrite
    (VectorReduce t-outer bop (Cast t-inner e))
    (Cast t-outer (VectorReduce (WithLanes t-e l-outer) bop e))
  :when (
    (has-type e t-e) 
    (= l-outer (LanesInType t-outer))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Axiomatic rewrites
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Conversion between `Broadcast` and `BroadcastPer`
(rule (
    (= bc (Broadcast e lo))
    (has-type e t)
    (= p (LanesInType t))
) (
    (union bc (BroadcastPer p e lo))
))
(rule (
    (= bcp (BroadcastPer p e lo))
    (has-type e t)
    (= p (LanesInType t))
) (
    (union bcp (Broadcast e lo))
))
;; end conversion

(rewrite 
    (Broadcast (Broadcast x l0) l1)
    (Broadcast x (* l0 l1))
)

(rewrite (Bop bop a b) (Bop bop b a) :when ((CommBop bop)))


;; broadcast related axioms
;; TODO: these rules are still not replaceable, why??
;; TODO: missing the cast-broadcastper rule for the degenerate rules
(rewrite (Broadcast x 1) x)
(rewrite (Broadcast (Load type name index) lanes) 
         (Load (MultipliedLanes type lanes) name (Broadcast index lanes)))
(rewrite (Broadcast (Cast type expr) lanes) 
         (Cast (MultipliedLanes type lanes) (Broadcast expr lanes)))
;; same rules generalized to BroadcastPer
(rewrite (BroadcastPer p x 1) x)
(rewrite (BroadcastPer p (Load type name index) lanes) 
         (Load (MultipliedLanes type lanes) name (BroadcastPer p index lanes)))
(rewrite (BroadcastPer p (Cast type expr) lanes) 
         (Cast (MultipliedLanes type lanes) (BroadcastPer p expr lanes)))
(rewrite (Cast type (BroadcastPer p x l))
         (BroadcastPer p (Cast (WithLanes type lx) x) l)
    :when ((has-type x t)
           (= (LanesInType t) lx)))

;; Reverse the pulled out BroadcastPer
;; TODO: in its own ruleset
(rewrite (BroadcastPer p (Broadcast x l0) l1)
         (Broadcast (BroadcastPer p x l1) l0)
    :when ((has-type x t)
           (= curr-p (LanesInType t))
           (>= curr-p p)))
(rewrite (BroadcastPer p (Ramp x (Broadcast s curr-p) l0) l1)
         (Ramp (BroadcastPer p x l1) (Broadcast s (* curr-p l1)) l0)
    :when ((has-type x t)
           (= curr-p (LanesInType t))
           (>= curr-p p)))
;; in the case the larger broadcast cannot be compressed
;; this seems to be "confluent" with even considering multiple variants in the E-class,
;; because this same rule can be achieved by running other rules on an equivalent version of the expression
;; (that unnests the inner Broadcast to two Broadcasts)
(rewrite (BroadcastPer p (Broadcast x l0) l1)
         (Broadcast x (* l0 l1))
    :when ((has-type x t)
           (= curr-p (LanesInType t))
           (< curr-p p)
           (> (* curr-p l0) p)))
(rewrite (BroadcastPer p (Ramp x (Broadcast s curr-p) l0) l1)
         (Ramp 
            (Broadcast 
                (Ramp 
                    x 
                    ; TODO: need to make sure all the lanes of the stride of the Ramp 
                    ; must be the lanes of the type of `x`
                    (Broadcast s curr-p) 
                    (/ p curr-p))
                l1)
            (Broadcast (Bop (Mul) s (UIntOrIntImm signed bits (/ p curr-p))) (* p l1))
            (/ l0 (/ p curr-p)))
    :when ((has-type x (UIntOrInt signed bits curr-p))
           (< curr-p p)
           (> (* curr-p l0) p)
           (= 0 (% p curr-p))
           (= 0 (% l0 (/ p curr-p)))
           ))

;; More broadcast axioms
(rewrite (Bop bop (Broadcast a l1) (Broadcast b l2))
         (Broadcast (Bop bop (Broadcast a (/ l1 l2)) b) l2)
    :when ((> l1 l2)
           (= 0 (% l1 l2))))

;; Also see `make-aligned` for the more nested version
(rewrite (Bop bop (Broadcast a l1) (Broadcast b l2))
         (Broadcast (Bop bop a (Broadcast b (/ l2 l1))) l1)
    :when ((< l1 l2)
           (= 0 (% l2 l1))))

(rewrite (Bop bop (Broadcast a l1) (Ramp x s l2))
         (Bop bop (Broadcast (Broadcast a (/ l1 l2)) l2) (Ramp x s l2))
    :when ((> l1 l2)
           (= 0 (% l1 l2))))

(rewrite (Bop bop (Ramp x s l2) (Broadcast a l1))
         (Bop bop (Ramp x s l2) (Broadcast (Broadcast a (/ l1 l2)) l2))
    :when ((> l1 l2)
           (= 0 (% l1 l2))))


(birewrite (Bop bop (BroadcastPer p a l) (BroadcastPer p b l))
         (BroadcastPer p (Bop bop a b) l))

(rewrite (Bop add-or-sub (Ramp base stride lanes) (Broadcast x lanes))
         (Ramp (Bop add-or-sub base x) stride lanes)
    :when ((AddOrSub add-or-sub)))
(rewrite (Bop add-or-sub (Ramp b1 s1 l) (Ramp b2 s2 l))
         (Ramp (Bop add-or-sub b1 b2) (Bop add-or-sub s1 s2) l)
    :when ((AddOrSub add-or-sub)))
(rewrite (Bop (Mul) (Ramp b s l) (Broadcast x l))
         (Ramp (Bop (Mul) b x) (Bop (Mul) s x) l))

; TODO: this can be generalized
(rewrite (Bop (Mod) (Ramp (IntImm bits base) (IntImm bits 1) lanes) (Broadcast (IntImm bits x) lanes))
         (Broadcast (Ramp (IntImm bits 0) (IntImm bits 1) x) (/ lanes x))
    :when ((= (% lanes x) 0)
           (= (% base x) 0)))
; (0:1:8) / x8(4) -> (x4(0):x4(1):2)
(rewrite (Bop (Div) (Ramp (IntImm bits base) (IntImm bits 1) lanes) (Broadcast (IntImm bits x) lanes))
         (Ramp (Broadcast (IntImm bits (/ base x)) x) (Broadcast (IntImm bits 1) x) (/ lanes x))
        :when ((= (% lanes x) 0)
               (= (% base x) 0)))


(rewrite (Bop add-or-sub (Ramp base stride r-lanes) (Broadcast x b-lanes))
         (Ramp (Bop add-or-sub base (Broadcast x (/ b-lanes r-lanes))) stride r-lanes)
    :when ((= 0 (% b-lanes r-lanes))
           (AddOrSub add-or-sub) ))
(rewrite (Ramp x s 1) x)

(rewrite x (Ramp x (Broadcast (IntImm b 0) l) 1) 
    :when (
        (IsExpr x) 
        (has-type x (Int b l))))
(rewrite x (Broadcast x 1) :when ((IsExpr x)))
; reorganize a ramp as a 4-column tiles
; TODO: do we need these rules now we have `make-aligned`?
; Yes - otherwise performance/tiled_matmul.cpp does not pass
(rewrite (Ramp e (IntImm b 1) l) 
         (Ramp (Ramp e (IntImm b 1) 4) (Broadcast (IntImm b 4) 4) (/ l 4)) 
  :when ((= 0 (% l 4))))
(rewrite (Ramp e (IntImm b 1) l) 
         (Ramp (Ramp e (IntImm b 1) 2) (Broadcast (IntImm b 2) 2) (/ l 2)) 
  :when ((= 0 (% l 2))))
(rewrite (Ramp e (IntImm b 1) l) 
         (Ramp (Ramp e (IntImm b 1) 16) (Broadcast (IntImm b 16) 16) (/ l 16)) 
  :when ((= 0 (% l 16))))

;; Cast identity
(rule (
    (has-type e t)
) (
    (union e (Cast t e))
))


; 


(rule (
    (has-type e (UIntOrInt sign bits lanes))
) (
    (union e (Bop (Add) e (Broadcast (UIntOrIntImm sign bits 0) lanes)))
))

; 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constant folding
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(rewrite (Bop (Add) (IntImm bits a) (IntImm bits b)) (IntImm bits (+ a b)))
(rewrite (Bop (Sub) (IntImm bits a) (IntImm bits b)) (IntImm bits (- a b)))
(rewrite (Bop (Mul) (IntImm bits a) (IntImm bits b)) (IntImm bits (* a b)))
(rewrite (Bop (Div) (IntImm bits a) (IntImm bits b)) (IntImm bits (/ a b)))
(rewrite (Bop (Mod) (IntImm bits a) (IntImm bits b)) (IntImm bits (% a b)))
(rewrite (Bop (Min) (IntImm bits a) (IntImm bits b)) (IntImm bits (min a b)))
(rewrite (Bop (Max) (IntImm bits a) (IntImm bits b)) (IntImm bits (max a b)))
(rewrite (Bop (EQ) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((= a b)))
(rewrite (Bop (EQ) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((!= a b)))
(rewrite (Bop (NE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((= a b)))
(rewrite (Bop (NE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((!= a b)))
(rewrite (Bop (LT) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((< a b)))
(rewrite (Bop (LT) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((>= a b)))
(rewrite (Bop (LE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((<= a b)))
(rewrite (Bop (LE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((> a b)))
(rewrite (Bop (GT) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((> a b)))
(rewrite (Bop (GT) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((<= a b)))
(rewrite (Bop (GE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 1) :when ((>= a b)))
(rewrite (Bop (GE) (IntImm bits a) (IntImm bits b)) (UIntImm 1 0) :when ((< a b)))
; TODO: logical operators

; 


;; Given two index expressions, make-aligned tries to 
;; un-nest Broadcast and Ramp expressions

(relation make-aligned (Expr Expr))

(rule (
    (make-aligned a b)
) (
    (make-aligned b a)
) :ruleset canonicalize)

;; can do prime factorization to further generalize these rules
(rule (
    (make-aligned (Broadcast e1 b1) (Broadcast e2 b2))
    (< b1 b2)
    (= (% b2 b1) 0)
    (!= b1 1)
) (
    (union (Broadcast e2 b2) (Broadcast (Broadcast e2 (/ b2 b1)) b1))
    ; (subsume (Broadcast e2 b2))
) :ruleset canonicalize)

(rule (
    (make-aligned (Ramp e1 s1 l1) (Broadcast e2 b2))
    (< l1 b2)
    (= (% b2 l1) 0)
    (!= l1 1)
) (
    (union (Broadcast e2 b2) (Broadcast (Broadcast e2 (/ b2 l1)) l1))
    ; (subsume (Broadcast e2 b2))
) :ruleset canonicalize)

(rule (
    (make-aligned (Broadcast e1 b1) (Ramp e2 s2 l2))
    (< b1 l2)
    (= (% l2 b1) 0)
    (!= b1 1)

    (has-type e2 (UIntOrInt sign bits lanes))
) (
    (union (Ramp e2 s2 l2) 
           (Ramp (Ramp e2 s2 (/ l2 b1))
                 (Broadcast (Bop (Mul) s2 (Broadcast (UIntOrIntImm sign bits (/ l2 b1)) lanes)) (/ l2 b1))
                 b1))
    ; (subsume (Ramp e2 s2 l2))
) :ruleset canonicalize)

(rule (
    (make-aligned (Ramp e1 s1 l1) (Ramp e2 s2 l2))
    (< l1 l2)
    (= (% l2 l1) 0)
    (!= l1 1)

    (has-type e2 (UIntOrInt sign bits lanes))
) (
    (union (Ramp e2 s2 l2) 
           (Ramp (Ramp e2 s2 (/ l2 l1))
                 (Bop (Mul) s2 (Broadcast (UIntOrIntImm sign bits (/ l2 l1)) lanes))
                 l1))
    ; (subsume (Ramp e2 s2 l2))
) :ruleset canonicalize)

;; Propagating down make-aligned
;; TODO: should we ban the case when l is equal to 1??

(rule (
    (make-aligned (Ramp e1 s1 l) (Ramp e2 s2 l))
) (
    (make-aligned e1 e2)
) :ruleset canonicalize)

(rule (
    (make-aligned (Ramp e1 s1 l) (Broadcast e2 l))
) (
    (make-aligned e1 e2)
) :ruleset canonicalize)

(rule (
    (make-aligned (Broadcast e1 b) (Broadcast e2 b))
) (
    (make-aligned e1 e2)
) :ruleset canonicalize)

;; Instantiation of make-aligned for general matrix multiplication
(rule (
    (= e (VectorReduce out-type (Add)
        (Bop (Mul)
            (Cast lhs-op-type lhs)
            (Cast rhs-op-type rhs))))
    (= lhs (Load lhs-type lhs-name lhs-idx))
    (= rhs (Load rhs-type rhs-name rhs-idx))
) (
    (make-aligned lhs-idx rhs-idx)
) :ruleset canonicalize)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pulling out aligned broadcasts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(sort I64ExprBinFn (UnstableFn (i64 i64 Expr Expr) Expr))
(datatype InvertedIndex
    (InvRamp InvertedIndex Expr i64)
    (InvBroadcast InvertedIndex i64)
    (InvertedIndexEnd))
(constructor assemble-inv-ind (
    i64 ;; multiplicity
    Expr 
    InvertedIndex) Expr :unextractable)
(rewrite (assemble-inv-ind m e (InvertedIndexEnd)) e 
    :ruleset assemble)
(rewrite (assemble-inv-ind m e (InvRamp i (Broadcast s sl) l)) 
         (assemble-inv-ind m (Ramp e (Broadcast s (/ sl m)) l) i)
    :when ((= (% sl m) 0))
    :ruleset assemble)
(rewrite (assemble-inv-ind m e (InvBroadcast i l))
         (assemble-inv-ind m (Broadcast e l) i)
    :ruleset assemble)

;; Main interface
;;
;; This relation kicks off the following procedure:
;;  1. it scans lhs and rhs in lock step, finds the first
;;     "co-located" Broadcasts with same broadcast lanes, and removes them.
;;  2. it apply the assemble constructor to the modified lhs and rhs.
;;  3. at this stage, the result should be equivalent to the original expression,
;;     and remove-aligned-bc will union the result with the original expression.
(relation remove-aligned-bc (
    ;; origin
    Expr 
    ;; assmble constructor for lhs and rhs
    I64ExprBinFn 
    ;; lhs
    Expr
    ;; rhs
    Expr 
))

(relation remove-aligned-bc-impl (
    ;; origin
    Expr 
    ;; assmble constructor for lhs and rhs
    I64ExprBinFn 
    ;; lhs
    Expr
    ;; rhs
    Expr 
    ;; continuation for lhs
    InvertedIndex 
    ;; continuation for rhs
    InvertedIndex
))

(rule (
    (remove-aligned-bc origin modified left right)
) (
    (remove-aligned-bc-impl origin modified left right (InvertedIndexEnd) (InvertedIndexEnd))
) :ruleset canonicalize)

(rule (
    (remove-aligned-bc-impl origin modified left right inv-left inv-right)

    (= (Broadcast e1+ l) left)
    (= (Broadcast e2+ l) right)
    (!= l 1)
    (has-type e1+ t)
    (= broadcast-per (LanesInType t))
) (
    (let new-left (assemble-inv-ind l e1+ inv-left))
    (let new-right (assemble-inv-ind l e2+ inv-right))
    (union origin (unstable-app modified broadcast-per l new-left new-right))
) :ruleset canonicalize)

(rule (
    (remove-aligned-bc-impl origin modified left right inv-left inv-right)

    (= (Ramp e1+ s1 l) left)
    (= (Broadcast e2+ l) right)
    (!= l 1)
) (
    (remove-aligned-bc-impl origin modified e1+ e2+ 
        (InvRamp inv-left s1 l) 
        (InvBroadcast inv-right l))
) :ruleset canonicalize)

(rule (
    (remove-aligned-bc-impl origin modified left right inv-left inv-right)

    (= (Broadcast e1+ l) left)
    (= (Ramp e2+ s2 l) right)
    (!= l 1)
) (
    (remove-aligned-bc-impl origin modified e1+ e2+ 
        (InvBroadcast inv-left l)
        (InvRamp inv-right s2 l))
) :ruleset canonicalize)

(rule (
    (remove-aligned-bc-impl origin modified left right inv-left inv-right)

    (= (Ramp e1+ s1 l) left)
    (= (Ramp e2+ s2 l) right)
    (!= l 1)
) (
    (remove-aligned-bc-impl origin modified e1+ e2+ 
        (InvRamp inv-left s1 l)
        (InvRamp inv-right s2 l))
) :ruleset canonicalize)

;; Instantiation of remove-aligned-bc for general matrix multiplication
(constructor matrix-multiplication-int/uint-cont (
    ;; signedness
    bool
    ;; lhs-name
    Variable
    ;; rhs-name
    Variable
    ;; out-lanes
    i64
    ;; tot-lanes
    i64
    ;; broadcast-per
    i64
    ;; multiplicity
    i64
    ;; lhs
    Expr
    ;; rhs
    Expr
    ) Expr :unextractable)

;; TODO: explain what multiplicity does
(constructor matrix-multiplication-float-cont (
    ;; lhs-name
    Variable
    ;; rhs-name
    Variable
    ;; out-lanes
    i64
    ;; tot-lanes
    i64
    ;; broadcast-per
    i64
    ;; multiplicity
    i64
    ;; lhs
    Expr
    ;; rhs
    Expr
    ) Expr
    :unextractable)

(constructor matrix-multiplication-float-16-cont (
    ;; lhs-name
    Variable
    ;; rhs-name
    Variable
    ;; out-lanes
    i64
    ;; tot-lanes
    i64
    ;; broadcast-per
    i64
    ;; multiplicity
    i64
    ;; lhs
    Expr
    ;; rhs
    Expr
    ) Expr
    :unextractable)

(rewrite (matrix-multiplication-int/uint-cont signed lhs-name rhs-name old-out-lanes old-tot-lanes broadcast-per m lhs-idx rhs-idx)
         ;; (/ tot-lanes out-lanes) computes how many values are reduced into one value
         ;; We divide broadcast-per by this to account for the actual broadcasting after reduction
         (BroadcastPer (/ broadcast-per (/ tot-lanes out-lanes))
            (VectorReduce (UIntOrInt signed 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (UIntOrInt signed 32 tot-lanes)
                        (Load (UIntOrInt signed 8 tot-lanes) lhs-name lhs-idx))
                    (Cast (UIntOrInt signed 32 tot-lanes) 
                        (Load (UIntOrInt signed 8 tot-lanes) rhs-name rhs-idx)
                        )))
            m
         )
    :when ((= out-lanes (/ old-out-lanes m))
           (= tot-lanes (/ old-tot-lanes m)))
    :ruleset assemble
    )

(rewrite (matrix-multiplication-float-cont lhs-name rhs-name old-out-lanes old-tot-lanes broadcast-per m lhs-idx rhs-idx)
         (BroadcastPer (/ broadcast-per (/ tot-lanes out-lanes))
            (VectorReduce (Float 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float 32 tot-lanes)
                        (Load (BFloat 16 tot-lanes) lhs-name lhs-idx))
                    (Cast (Float 32 tot-lanes)
                        (Load (BFloat 16 tot-lanes) rhs-name rhs-idx)
                        )))
            m
         )
    :when ((= out-lanes (/ old-out-lanes m))
           (= tot-lanes (/ old-tot-lanes m)))
    :ruleset assemble
    )

(rewrite (matrix-multiplication-float-16-cont lhs-name rhs-name old-out-lanes old-tot-lanes broadcast-per m lhs-idx rhs-idx)
         (BroadcastPer (/ broadcast-per (/ tot-lanes out-lanes))
            (VectorReduce (Float 16 out-lanes) (Add)
                (Bop (Mul)
                    (Load (Float 16 tot-lanes) lhs-name lhs-idx)
                    (Load (Float 16 tot-lanes) rhs-name rhs-idx)))
            m
         )
    :when ((= out-lanes (/ old-out-lanes m))
           (= tot-lanes (/ old-tot-lanes m)))
    :ruleset assemble
    )

(rule (
    (= e (VectorReduce (UIntOrInt signed 32 out-lanes) (Add)
        (Bop (Mul)
            (Cast (UIntOrInt signed 32 tot-lanes) lhs)
            (Cast (UIntOrInt signed 32 tot-lanes) rhs))))
    (= lhs (Load (UIntOrInt signed 8 tot-lanes) lhs-name lhs-idx))
    (= rhs (Load (UIntOrInt signed 8 tot-lanes) rhs-name rhs-idx))
) (
    (remove-aligned-bc e (unstable-fn "matrix-multiplication-int/uint-cont" signed lhs-name rhs-name out-lanes tot-lanes) lhs-idx rhs-idx)
) :ruleset canonicalize)

(rule (
    (= e (VectorReduce (Float 32 out-lanes) (Add)
        (Bop (Mul)
            (Cast (Float 32 tot-lanes) lhs)
            (Cast (Float 32 tot-lanes) rhs))))
    (= lhs (Load (BFloat 16 tot-lanes) lhs-name lhs-idx))
    (= rhs (Load (BFloat 16 tot-lanes) rhs-name rhs-idx))
) (
    (remove-aligned-bc e (unstable-fn "matrix-multiplication-float-cont" lhs-name rhs-name out-lanes tot-lanes) lhs-idx rhs-idx)
) :ruleset canonicalize)

(rule (
    (= e (VectorReduce (Float 16 out-lanes) (Add)
        (Bop (Mul)
            lhs
            rhs)))
    (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-idx))
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-idx))
) (
    (remove-aligned-bc e (unstable-fn "matrix-multiplication-float-16-cont" lhs-name rhs-name out-lanes tot-lanes) lhs-idx rhs-idx)
) :ruleset canonicalize)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Push down broadcasts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; See `vector_axioms.egg`

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; push down VectorReduce
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO: decompose remove-innermost-bc into pull-out-bc rule and match rule
(relation remove-innermost-bc-demand (Expr))
(relation remove-innermost-bc (
    ;; original
    Expr
    ;; removed
    Expr
    ;; multiplicity shrinked
    i64
    ))

(rule (
    (VectorReduce ty (Add) (Bop (Mul) a b))
) (
    ;; We only do right child because of commutativity
    (remove-innermost-bc-demand b)
) :ruleset push-down-vector-reduce)

(rule (
    (= e (VectorReduce ty (Add)
        (Bop (Mul) a b)
    ))

    (remove-innermost-bc b shrinked-b m)
    (has-type shrinked-b arg-type)
) (
    ;; Subsuming the top level of the original lhs:
    ;; There are likely no accelerable pattern where 
    ;; the innermost expression is a broadcast
    (subsume (VectorReduce ty (Add) (Bop (Mul) a b)))
    (union e (VectorReduce ty (Add)
        (Bop (Mul) 
            (VectorReduce arg-type (Add) a)
            shrinked-b
            )))
) :ruleset push-down-vector-reduce)

;; Going down

(rule (
    (remove-innermost-bc-demand (Cast ty e))
) (
    (remove-innermost-bc-demand e)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand (Load ty x e))
) (
    (remove-innermost-bc-demand e)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand (Ramp e s l))
    (!= l 1)
) (
    (remove-innermost-bc-demand e)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand (Broadcast e l))
    (has-type e ty)
    (> (LanesInType ty) 1)
) (
    (remove-innermost-bc-demand e)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand (Broadcast e l))
    (has-type e ty)
    (= (LanesInType ty) 1)
    (!= l 1)
) (
    (remove-innermost-bc (Broadcast e l) e l)
) :ruleset push-down-vector-reduce)

;; Going back up

(rule (
    (remove-innermost-bc-demand e)
    (= e (Cast ty e1))
    (remove-innermost-bc e1 removed m)

    (= l (LanesInType ty))
    (= (% l m) 0)
) (
    (remove-innermost-bc e (Cast (WithLanes ty (/ l m)) removed) m)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand e)
    (= e (Load ty x e1))
    (remove-innermost-bc e1 removed m)

    (= l (LanesInType ty))
    (= (% l m) 0)
) (
    (remove-innermost-bc e (Load (WithLanes ty (/ l m)) x removed) m)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand e)
    (= e (Broadcast e1 l))
    (remove-innermost-bc e1 removed m)

    (!= l 1)
) (
    (remove-innermost-bc e (Broadcast removed l) m)
) :ruleset push-down-vector-reduce)

(rule (
    (remove-innermost-bc-demand e)
    (= e (Ramp e1 (Broadcast s ml) l))
    (remove-innermost-bc e1 removed m)

    (!= l 1)
    (= (% ml m) 0)
) (
    (remove-innermost-bc e (Ramp removed (Broadcast s (/ ml l)) l) m)
) :ruleset push-down-vector-reduce)


; 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; AMX helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; {
    ; (relation _LoadAMXType (Type))
    ; (_LoadAMXType (Int 32 256))
    ; (_LoadAMXType (Float 32 256))
;; }
; (constructor LoadAMX (Type String) Expr :unextractable)
; (birewrite (LoadAMX ty x) (Load ty x R256) :when ((_LoadAMXType ty)))

; (constructor StoreAMX (String Expr) Stmt :unextractable)
; (birewrite (Store name value R256) (StoreAMX name value))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shape inference
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;                       var    rows colbytes
(relation AMXAllocation (String i64  i64))
(relation AMXShape (Expr i64  i64))

(rule (
    (AMXAllocation name rows colbytes)
    (= e (Var t (V name)))
) (
    (AMXShape e rows colbytes)
) :ruleset amx)
(rule (
    (AMXShape e rows colbytes)
    (Store name e index)
) (
    (AMXAllocation name rows colbytes)
) :ruleset amx)
(rule (
    (AMXAllocation name rows colbytes)
    (Store name e index)
) (
    (AMXShape e rows colbytes)
) :ruleset amx)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tile_matmul
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Abstract AMX left-hand side access patterns
(relation amx-int/uint-lhs (
    Expr ;; orig-expr
    i64 ;; x-lanes
    i64 ;; y-lanes
    i64 ;; r-lanes
    Expr ;; rhs-expr
))

(relation amx-float-lhs (
    Expr ;; orig-expr
    i64 ;; x-lanes
    i64 ;; y-lanes
    i64 ;; r-lanes
    Expr ;; rhs-expr
))


(rule (
    (= orig-lhs (Load (UIntOrInt lhs-signed 8 tot-lanes) lhs-name lhs-index))
    (= lhs-index
        (Ramp (Broadcast (Ramp lhs-base (IntImm32 1) r-lanes) y-lanes) 
              (Broadcast x-stride (* y-lanes r-lanes))
              x-lanes)
    )
) (
    (let new-lhs (Call "tile_load" (UIntOrInt lhs-signed 8 (* x-lanes r-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 r-lanes) (Var (Handle 1) lhs-name) lhs-base x-stride) (Intrinsic)))
    (amx-int/uint-lhs orig-lhs x-lanes y-lanes r-lanes new-lhs)
) :ruleset amx)

(rule (
    (= orig-lhs (Load (BFloat 16 tot-lanes) lhs-name lhs-index))
    (= lhs-index
        (Ramp (Broadcast (Ramp lhs-base (IntImm32 1) r-lanes) y-lanes) 
              (Broadcast x-stride (* y-lanes r-lanes))
              x-lanes)
    )
) (
    (let new-lhs (Call "tile_load" (BFloat 16 (* x-lanes r-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 r-lanes) (Var (Handle 1) lhs-name) lhs-base x-stride) (Intrinsic)))
    (amx-float-lhs orig-lhs x-lanes y-lanes r-lanes new-lhs)
) :ruleset amx)

;; Abstract AMX right-hand side access patterns

(relation amx-int/uint-rhs (
    Expr ;; orig-expr
    i64 ;; x-lanes
    i64 ;; y-lanes
    i64 ;; r-lanes
    Expr ;; rhs-expr
))

(relation amx-float-rhs (
    Expr ;; orig-expr
    i64 ;; x-lanes
    i64 ;; y-lanes
    i64 ;; r-lanes
    Expr ;; rhs-expr
))

(rule (
    (= orig-rhs (Load (UIntOrInt rhs-signed 8 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        ; 4 is for (u)int8
        (Broadcast
            (Ramp (Ramp (Ramp rhs-base (IntImm32 1) 4) 
                        (Broadcast rhs-r-stride 4) 
                        (/ r-lanes 4)
                        )
                  (Broadcast (IntImm32 4) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let amx-rhs (Call "tile_load" (UIntOrInt rhs-signed 8 (* r-lanes y-lanes)) (vec-of (IntImm16 (/ r-lanes 4)) (IntImm16 (* y-lanes 4)) (Var (Handle 1) rhs-name) rhs-base rhs-r-stride) (Intrinsic)))
    (amx-int/uint-rhs orig-rhs x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)

(rule (
    (= orig-rhs (Load (BFloat 16 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast
            (Ramp (Ramp (Ramp rhs-base (IntImm32 1) 2) 
                        (Broadcast rhs-r-stride 2)
                        (/ r-lanes 2)
                        )
                  (Broadcast (IntImm32 2) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let amx-rhs (Call "tile_load" (BFloat 16 (* r-lanes y-lanes)) (vec-of (IntImm16 (/ r-lanes 2)) (IntImm16 (* y-lanes 2)) (Var (Handle 1) rhs-name) rhs-base rhs-r-stride) (Intrinsic)))
    (amx-float-rhs orig-rhs x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)

(rule (
    (= orig-rhs-mem (AMX2Mem orig-rhs))
    (= orig-rhs (Load (UIntOrInt rhs-signed 8 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        ; 4 is for (u)int8
        (Broadcast
            (Ramp (Ramp (Ramp rhs-base (IntImm32 1) 4) 
                        (Broadcast rhs-r-stride 4) 
                        (/ r-lanes 4)
                        )
                  (Broadcast (IntImm32 4) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let amx-rhs (Load (UIntOrInt rhs-signed 8 (* r-lanes y-lanes)) rhs-name (Ramp rhs-base (IntImm32 1) (* r-lanes y-lanes))))
    (amx-int/uint-rhs orig-rhs-mem x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)


(rule (
    (= orig-rhs-mem (AMX2Mem orig-rhs))
    (= orig-rhs (Load (BFloat 16 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast
            (Ramp (Ramp (Ramp rhs-base (IntImm32 1) 2) 
                        (Broadcast rhs-r-stride 2)
                        (/ r-lanes 2)
                        )
                  (Broadcast (IntImm32 2) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let amx-rhs (Load (BFloat 16 (* r-lanes y-lanes)) rhs-name (Ramp rhs-base (IntImm32 1) (* r-lanes y-lanes))))
    (amx-float-rhs orig-rhs-mem x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)


(rule (
    (= orig-rhs (Load (UIntOrInt rhs-signed 8 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast
            (Ramp (Ramp rhs-base 
                        rhs-r-stride
                        r-lanes
                        )
                  (Broadcast (IntImm32 1) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let rhs-type (UIntOrInt rhs-signed 8 (* r-lanes y-lanes)))
    (let amx-rhs (Call "tile_load"
        rhs-type
        (vec-of
            (IntImm16 (/ r-lanes 4))
            (IntImm16 (* y-lanes 4))
            (Var (Handle 1) (ExprVar (Mem) 
                (Call "KWayInterleave" (BFloat 16 (* y-lanes r-lanes)) 
                    (vec-of (IntImm32 4) 
                            (Load rhs-type rhs-name (Ramp (Ramp rhs-base (IntImm32 1) y-lanes) (Broadcast rhs-r-stride y-lanes) r-lanes))
                            (IntImm32 r-lanes))
                    (Intrinsic))))
            (IntImm32 0)
            (IntImm32 (* y-lanes 4))
            )
        (Intrinsic)))
    (amx-int/uint-rhs orig-rhs x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)


(rule (
    (= orig-rhs (Load (BFloat 16 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast
            (Ramp (Ramp rhs-base 
                        rhs-r-stride
                        r-lanes
                        )
                  (Broadcast (IntImm32 1) r-lanes)
                  y-lanes
            )
            x-lanes
        )
    )
) (
    (let rhs-type (BFloat 16 (* r-lanes y-lanes)))
    (let amx-rhs (Call "tile_load"
        rhs-type
        (vec-of
            (IntImm16 (/ r-lanes 2))
            (IntImm16 (* y-lanes 2))
            (Var (Handle 1) (ExprVar (Mem) 
                (Call "KWayInterleave" (BFloat 16 (* y-lanes r-lanes)) 
                    (vec-of (IntImm32 2) 
                            (Load rhs-type rhs-name (Ramp (Ramp rhs-base (IntImm32 1) y-lanes) (Broadcast rhs-r-stride y-lanes) r-lanes))
                            (IntImm32 r-lanes))
                    (Intrinsic))))
            (IntImm32 0)
            (IntImm32 (* y-lanes 2))
            )
        (Intrinsic)))
    (amx-float-rhs orig-rhs x-lanes y-lanes r-lanes amx-rhs)
) :ruleset amx)


; Int8/UInt8
(rule (
    (= e (Bop (Add) 
            (VectorReduce (Int 32 out-lanes) (Add) 
                (Bop (Mul) 
                    (Cast (Int 32 tot-lanes) lhs) 
                    (Cast (Int 32 tot-lanes) rhs))
                )
            mat))
    (= out-lanes (* x-lanes y-lanes))
    (= tot-lanes (* out-lanes r-lanes))

    (amx-int/uint-lhs lhs x-lanes y-lanes r-lanes new-lhs)
    (amx-int/uint-rhs rhs x-lanes y-lanes r-lanes new-rhs)
) (
    (let new-e (Call "tile_matmul" (Int 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4)) (IntImm16 r-lanes) (Mem2AMX mat) new-lhs new-rhs) (Intrinsic)))
    (AMXShape new-e x-lanes (* y-lanes 4))
    (union e (AMX2Mem new-e))
) :ruleset amx)

;; Same rule, but does C = AxB instead of C = AxB+C
(rule (
    (= e (VectorReduce (Int 32 out-lanes) (Add) 
            (Bop (Mul) 
                (Cast (Int 32 tot-lanes) lhs) 
                (Cast (Int 32 tot-lanes) rhs))
            ))
    (= out-lanes (* x-lanes y-lanes))
    (= tot-lanes (* out-lanes r-lanes))

    (amx-int/uint-lhs lhs x-lanes y-lanes r-lanes new-lhs)
    (amx-int/uint-rhs rhs x-lanes y-lanes r-lanes new-rhs)
) (
    (let initial (Call "tile_zero" (Int 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4))) (Intrinsic)))
    (let new-e (Call "tile_matmul" (Int 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4)) (IntImm16 r-lanes) initial new-lhs new-rhs) (Intrinsic)))
    (AMXShape new-e x-lanes (* y-lanes 4))
    (union e (AMX2Mem new-e))
) :ruleset amx)

; Float16
(rule (
    (= e (Bop (Add) 
        (VectorReduce (Float 32 out-lanes) (Add) 
            (Bop (Mul) 
                (Cast (Float 32 tot-lanes) lhs)
                (Cast (Float 32 tot-lanes) rhs))
            )
        mat))
    (= out-lanes (* x-lanes y-lanes))
    (= tot-lanes (* out-lanes r-lanes))

    (amx-float-lhs lhs x-lanes y-lanes r-lanes new-lhs)
    (amx-float-rhs rhs x-lanes y-lanes r-lanes new-rhs)
) (
    (let new-e (Call "tile_matmul" (Float 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4)) (IntImm16 r-lanes) (Mem2AMX mat) new-lhs new-rhs) (Intrinsic)))
    (AMXShape new-e x-lanes (* y-lanes 4))
    (union e (AMX2Mem new-e))
) :ruleset amx)

(rule (
    (= e
        (VectorReduce (Float 32 out-lanes) (Add) 
            (Bop (Mul) 
                (Cast (Float 32 tot-lanes) lhs)
                (Cast (Float 32 tot-lanes) rhs))
            ))
    (= out-lanes (* x-lanes y-lanes))
    (= tot-lanes (* out-lanes r-lanes))

    (amx-float-lhs lhs x-lanes y-lanes r-lanes new-lhs)
    (amx-float-rhs rhs x-lanes y-lanes r-lanes new-rhs)
) (
    (let initial (Call "tile_zero" (Float 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4))) (Intrinsic)))
    (let new-e (Call "tile_matmul" (Float 32 (* x-lanes y-lanes)) (vec-of (IntImm16 x-lanes) (IntImm16 (* y-lanes 4)) (IntImm16 r-lanes) initial new-lhs new-rhs) (Intrinsic)))
    (AMXShape new-e x-lanes (* y-lanes 4))

    (union e (AMX2Mem new-e))
) :ruleset amx)

(birewrite (Cast ty (AMX2Mem e)) (AMX2Mem (Cast ty e)) :ruleset amx)
(birewrite (Cast ty (Mem2AMX e)) (Mem2AMX (Cast ty e)) :ruleset amx)
(birewrite (BroadcastPer p (Mem2AMX e) l) (Mem2AMX (BroadcastPer p e l)) :ruleset amx)
(birewrite (AMX2Mem (BroadcastPer p e l)) (BroadcastPer p (AMX2Mem e) l) :ruleset amx)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tilezero
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(rule (
    (= e (Mem2AMX
        (Broadcast
            (UIntOrIntImm _sign 32 0)
            lanes
        )))
    (AMXShape e rows colbytes)
) (
    (let new-e (Call "tile_zero" (Int 32 lanes) (vec-of (IntImm16 rows) (IntImm16 colbytes)) (Intrinsic)))
    (union e new-e)
) :ruleset amx)

(rule (
    (= e (Mem2AMX
        (Broadcast
            (FloatImm 32 0.0)
            lanes
        )))
    (AMXShape e rows colbytes)
) (
    (let new-e (Call "tile_zero" (Float 32 lanes) (vec-of (IntImm16 rows) (IntImm16 colbytes)) (Intrinsic)))
    (union e new-e)
) :ruleset amx)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tile_load
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; there are two ways to translate an Mem2AMX:
;; We can use computes the entire computation in memory (`(ExprVar (Mem) ...)`),
;; followed by an tileload,
;; Or if the argument to Mem2AMX is a load of a specific pattern,
;; then we can just to a tileload

; The generic case

(rule (
    (= e (Mem2AMX value))
    ; We need to know the shape of the computation 
    ; before loading it
    (AMXShape e row colbytes)
    (has-type value (UIntOrInt sign 8 lanes))
) (
    (let new-e (Call "tile_load" (UIntOrInt sign 8 lanes)
                   (vec-of (IntImm16 row)
                           (IntImm16 colbytes)
                           (Var (Handle 1) (ExprVar (Mem) value))
                           ;; (Computed)
                           (IntImm32 0) ;; offset
                           ;; (Computed)
                           (IntImm32 colbytes) ;; stride
                           )
                    (Intrinsic)))
    (union e new-e)
) :ruleset amx)

(rule (
    (= e (Mem2AMX value))
    ; We need to know the shape of the computation 
    ; before loading it
    (AMXShape e row colbytes)
    (has-type value (BFloat 16 lanes))
) (
    (let new-e (Call "tile_load" (BFloat 16 lanes)
                   (vec-of (IntImm16 row)
                           (IntImm16 colbytes)
                           (Var (Handle 1) (ExprVar (Mem) value))
                           ;; (Computed)
                           (IntImm32 0) ;; offset
                           ;; (Computed)
                           (IntImm32 colbytes) ;; stride
                           )
                    (Intrinsic)))
    (union e new-e)
) :ruleset amx)

; The specialized case

(rule (
    (= e (Mem2AMX
        (Load ty name index)))
    (= index (Ramp (Ramp base (IntImm32 1) m-lanes) (Broadcast n-stride m-lanes) n-lanes))
    (= ty (UIntOrInt _sign 8 _lanes))
) (
    (let new-e (Call "tile_load" ty (vec-of (IntImm16 n-lanes) (IntImm16 m-lanes) (Var (Handle 1) name) base n-stride) (Intrinsic)))
    (union e new-e)
) :ruleset amx)

(rule (
    (= e (Mem2AMX
        (Load ty name index)))
    (= index (Ramp (Ramp base (IntImm32 1) m-lanes) (Broadcast n-stride m-lanes) n-lanes))
    (= ty (BFloat 16 _lanes))
) (
    (let new-e (Call "tile_load" ty (vec-of (IntImm16 n-lanes) (IntImm16 (* m-lanes 2)) (Var (Handle 1) name) base n-stride) (Intrinsic)))
    (union e new-e)
) :ruleset amx)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tile_store
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; there are two ways to translate an AMX2Mem:
;; We can compute the expression as is, and stores the
;; AMX result in some temporary buffer contiguously,
;; Or if the statement has the form (Store name (AMX2Mem value) index),
;; and the index has a specific pattern, then we can directly tile_store it.

;; (rewrite (AMX2Mem value) (Load ty (ExprVar (AMX) value) (Computed))
;;     :when ((has-type value ty)))
(rule ((= e (AMX2Mem value))
       (has-type value ty)
       (= extent (LanesInType ty)))
      ((union e (Load ty (ExprVar (AMX) value) (Ramp (IntImm32 0) (IntImm32 1) extent))))
    :ruleset amx)
;; there are some problems here
;; ExprVar at least need to know the matrix dimension of `value`
;; to properly codegen.
(rewrite (AMX2Mem value) 
         (Load ty (ExprVar (AMX) value) (Ramp (IntImm32 0) (IntImm32 1) extent))
    :when (
        (has-type value ty)
        (= extent (LanesInType ty))
    )
    :ruleset amx)
; de-specialization
(rewrite (Load ty (ExprVar (AMX) value) (Ramp (IntImm32 0) (IntImm32 1) extent)) (AMX2Mem value)
    :ruleset amx)

(rule (
    (= s (Store store-name (AMX2Mem tile) index))
    (= index (Ramp (Ramp base (IntImm32 1) m-lanes) (Broadcast n-stride m-lanes) n-lanes))
    (= bytes 4)
    (has-type value ty)
) (
    (let new-s (Evaluate 
        (Call "tile_store" (Int 32 1) 
            (vec-of (IntImm16 n-lanes) 
                    (IntImm16 (* m-lanes bytes))
                    (Var (Handle 1) (V store-name))
                    (Bop (Mul) base (IntImm32 bytes))
                    (Bop (Mul) n-stride (IntImm32 bytes))
                    tile
                    )
            (Intrinsic))))
    (union s new-s)
) :ruleset amx)

; 



;; Specification of different tile sizes for WMMA
;; We leave the type (f16 vs bf16) and layout (row major vs column major) as a future TODO
(datatype WMMATileConfig 
    (Tile i64 i64 i64))
(datatype Layout
    (RowMajor)
    (ColMajor))

(relation WMMATileA (WMMATileConfig Layout String))
(WMMATileA (Tile 16 16 16) (RowMajor) "wmma.load.a.sync.aligned.row.m16n16k16.f16")
(WMMATileA (Tile 32 8 16) (RowMajor) "wmma.load.a.sync.aligned.row.m32n8k16.f16")
(WMMATileA (Tile 8 32 16) (RowMajor) "wmma.load.a.sync.aligned.row.m8n32k16.f16")
(relation WMMATileB (WMMATileConfig Layout String))
(WMMATileB (Tile 16 16 16) (RowMajor) "wmma.load.b.sync.aligned.row.m16n16k16.f16")
(WMMATileB (Tile 16 16 16) (ColMajor) "wmma.load.b.sync.aligned.col.m16n16k16.f16")
(WMMATileB (Tile 32 8 16) (RowMajor) "wmma.load.b.sync.aligned.row.m32n8k16.f16")
(WMMATileB (Tile 8 32 16) (RowMajor) "wmma.load.b.sync.aligned.row.m8n32k16.f16")
(relation WMMATileC (WMMATileConfig Layout String i64))
(WMMATileC (Tile 16 16 16) (RowMajor) "wmma.load.c.sync.aligned.row.m16n16k16.f32" 32)
(WMMATileC (Tile 32 8 16) (RowMajor) "wmma.load.c.sync.aligned.row.m32n8k16.f32" 32)
(WMMATileC (Tile 8 32 16) (RowMajor) "wmma.load.c.sync.aligned.row.m8n32k16.f32" 32)
(WMMATileC (Tile 16 16 16) (RowMajor) "wmma.load.c.sync.aligned.row.m16n16k16.f16" 16)
(WMMATileC (Tile 32 8 16) (RowMajor) "wmma.load.c.sync.aligned.row.m32n8k16.f16" 16)
(WMMATileC (Tile 8 32 16) (RowMajor) "wmma.load.c.sync.aligned.row.m8n32k16.f16" 16)
(relation WMMAGEMM (WMMATileConfig Layout Layout String i64))
(WMMAGEMM (Tile 16 16 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m16n16k16.f32.f32" 32)
(WMMAGEMM (Tile 16 16 16) (RowMajor) (ColMajor) "wmma.mma.sync.aligned.row.col.m16n16k16.f32.f32" 32)
(WMMAGEMM (Tile 32 8 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m32n8k16.f32.f32" 32)
(WMMAGEMM (Tile 8 32 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m8n32k16.f32.f32" 32)
(WMMAGEMM (Tile 16 16 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m16n16k16.f16.f16" 16)
(WMMAGEMM (Tile 16 16 16) (RowMajor) (ColMajor) "wmma.mma.sync.aligned.row.col.m16n16k16.f16.f16" 16)
(WMMAGEMM (Tile 32 8 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m32n8k16.f16.f16" 16)
(WMMAGEMM (Tile 8 32 16) (RowMajor) (RowMajor) "wmma.mma.sync.aligned.row.row.m8n32k16.f16.f16" 16)
(relation WMMATileD (WMMATileConfig Layout String i64))
(WMMATileD (Tile 16 16 16) (RowMajor) "wmma.store.d.sync.aligned.row.m16n16k16.f32" 32)
(WMMATileD (Tile 32 8 16) (RowMajor) "wmma.store.d.sync.aligned.row.m32n8k16.f32" 32)
(WMMATileD (Tile 8 32 16) (RowMajor) "wmma.store.d.sync.aligned.row.m8n32k16.f32" 32)
(WMMATileD (Tile 16 16 16) (RowMajor) "wmma.store.d.sync.aligned.row.m16n16k16.f16" 16)
(WMMATileD (Tile 32 8 16) (RowMajor) "wmma.store.d.sync.aligned.row.m32n8k16.f16" 16)
(WMMATileD (Tile 8 32 16) (RowMajor) "wmma.store.d.sync.aligned.row.m8n32k16.f16" 16)

;; different from WMMA, the last column denotes number of elements rather than colbytes
(relation WMMAAllocation (String i64 i64))
(relation WMMAShape (Expr i64 i64))

(rule (
        (WMMAAllocation name rows cols)
        (= e (Var t (V name)))
    ) (
        (WMMAShape e rows cols)
    ))
(rule (
        (WMMAShape e rows cols)
        (Store name e index)
    ) (
        (WMMAAllocation name rows cols)
    ))
; TODO: this rule is wrong: it requires the index to be a linear scan instead of an arbitrary expression
(rule (
        (WMMAAllocation name rows cols)
        (Store name e index)
    ) (
        (WMMAShape e rows cols)
    ))

;; Axiomatic rules about WMMA2Mem and Mem2WMMA

(birewrite (Cast ty (WMMA2Mem e)) (WMMA2Mem (Cast ty e)))
(birewrite (Cast ty (Mem2WMMA e)) (Mem2WMMA (Cast ty e)))
(birewrite (BroadcastPer p (Mem2WMMA e) l) (Mem2WMMA (BroadcastPer p e l)))
(birewrite (WMMA2Mem (BroadcastPer p e l)) (BroadcastPer p (WMMA2Mem e) l))
(birewrite (Broadcast (Mem2WMMA e) l) (Mem2WMMA (Broadcast e l)))
(birewrite (WMMA2Mem (Broadcast e l)) (Broadcast (WMMA2Mem e) l))


(relation wmma-f16-rhs (
    Expr ;; orig-expr
    WMMATileConfig
    Layout
    Expr ;; rhs-expr
))

(rule (
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))

    (= rhs-index
        (Broadcast
            (Ramp (Ramp rhs-base
                    rhs-r-stride
                    r-lanes
                )
                (Broadcast (IntImm32 1) r-lanes)
                y-lanes
            )
            x-lanes
        )
    )

    (= tile (Tile x-lanes y-lanes r-lanes))
    (WMMATileB tile (RowMajor) wmma-intrinsic)
) (
    (let new-rhs (Call wmma-intrinsic (Float 16 256) (vec-of (Var (Handle 1) rhs-name) rhs-base rhs-r-stride) (Intrinsic)))
    (wmma-f16-rhs rhs tile (RowMajor) new-rhs)
))

(rule (
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))

    (= rhs-index
        (Broadcast
            (Ramp (Ramp rhs-base
                    (IntImm32 1)
                    r-lanes
                )
                (Broadcast rhs-r-stride r-lanes)
                y-lanes
            )
            x-lanes
        )
    )

    (= tile (Tile x-lanes y-lanes r-lanes))
    (WMMATileB tile (ColMajor) wmma-intrinsic)
) (
    (let new-rhs (Call wmma-intrinsic (Float 16 256) (vec-of (Var (Handle 1) rhs-name) rhs-base rhs-r-stride) (Intrinsic)))
    (wmma-f16-rhs rhs tile (ColMajor) new-rhs)
))

; (wmma-f16-rhs 
;     (WMMA2Mem (Load ty name (Ramp (Ramp (IntImm32 0) (IntImm32 16) 16) (Broadcast (IntImm32 ) 16) 16))) 
;     16 16 16
;     (Load ty name (Ramp (Ramp (IntImm32 0) (IntImm32 16) 16) (Broadcast (IntImm32 1) 16) 16)))
(rule (
    (= orig-rhs-mem (WMMA2Mem orig-rhs))
    (= orig-rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast
            (Ramp (Ramp rhs-base rhs-r-stride r-lanes)
                  (Broadcast (IntImm32 1) r-lanes)
                   y-lanes)
            x-lanes))
) (
    (let new-rhs (Load (Float 16 (* r-lanes y-lanes)) rhs-name (Ramp rhs-base (IntImm32 1) (* r-lanes y-lanes))))
    (wmma-f16-rhs orig-rhs-mem (Tile x-lanes y-lanes r-lanes) (RowMajor) new-rhs)
))

(rule (
        (= e (Bop (Add)
                (VectorReduce (Float acc-bits out-lanes) (Add)
                    (Bop (Mul)
                        (Cast (Float acc-bits tot-lanes) lhs)
                        (Cast (Float acc-bits tot-lanes) rhs))
                )
                mat))
        (= out-lanes (* x-lanes y-lanes))
        (= tot-lanes (* out-lanes r-lanes))

        (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
        (= lhs-index
            (Ramp (Broadcast (Ramp lhs-base (IntImm32 1) r-lanes) y-lanes)
                (Broadcast x-stride (* y-lanes r-lanes))
                x-lanes)
        )

        (= tile (Tile x-lanes y-lanes r-lanes))
        (wmma-f16-rhs rhs tile rhs-layout new-rhs)

        (WMMATileA tile lhs-layout intrinsic-A)
        (WMMAGEMM tile lhs-layout rhs-layout intrinsic-GEMM acc-bits)

    ) (
        (let new-lhs (Call intrinsic-A (Float 16 256) (vec-of (Var (Handle 1) lhs-name) lhs-base x-stride) (Intrinsic)))
        (let new-mat (Mem2WMMA mat))
        (let new-e (Call intrinsic-GEMM (Float acc-bits 256) (vec-of new-lhs new-rhs new-mat) (Intrinsic)))
        (WMMAShape new-e x-lanes y-lanes)

        (union e (WMMA2Mem new-e))
    ))

(rule (
        (= e 
                (VectorReduce (Float acc-bits out-lanes) (Add)
                    (Bop (Mul)
                        (Cast (Float acc-bits tot-lanes) lhs)
                        (Cast (Float acc-bits tot-lanes) rhs))
                ))
        (= out-lanes (* x-lanes y-lanes))
        (= tot-lanes (* out-lanes r-lanes))

        (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
        (= lhs-index
            (Ramp (Broadcast (Ramp lhs-base (IntImm32 1) r-lanes) y-lanes)
                (Broadcast x-stride (* y-lanes r-lanes))
                x-lanes)
        )

        (= tile (Tile x-lanes y-lanes r-lanes))
        (wmma-f16-rhs rhs tile rhs-layout new-rhs)

        (WMMATileA tile lhs-layout intrinsic-A)
        (WMMAGEMM tile lhs-layout rhs-layout intrinsic-GEMM acc-bits)
        ;; The layout of the accumulator does not seem to affect the gemm intrinsic to be used
        (WMMATileC tile acc-layout intrinsic-C acc-bits)

    ) (
        (let initial (Call (+ intrinsic-C ".ZERO") (Float acc-bits 256) (vec-of (IntImm32 0)) (Intrinsic)))
        (let new-lhs (Call intrinsic-A (Float 16 256) (vec-of (Var (Handle 1) lhs-name) lhs-base x-stride) (Intrinsic)))
        (let new-e (Call intrinsic-GEMM (Float acc-bits 256) (vec-of new-lhs new-rhs initial) (Intrinsic)))
        (WMMAShape new-e x-lanes y-lanes)
        (union e (WMMA2Mem new-e))
    ))


(rule (
        (= e (Mem2WMMA
                (Broadcast
                    (FloatImm bits 0.0)
                    lanes
                )))
        (WMMAShape e rows cols)
        (= lanes (* rows cols))

        ;; we arbitrarily pick row major as the intrinsic for initialization
        (WMMATileC (Tile rows cols _k) (RowMajor) intrinsic bits)
    ) (
        (let new-e (Call (+ intrinsic ".ZERO") (Float bits lanes) (vec-of (IntImm32 0)) (Intrinsic)))
        ; (let new-e (Call intrinsic (Float 32 lanes) (vec-of (Var (Handle 1) (ExprVar (Mem) 
        (union e new-e)
    ))

(rule (
    (= e (WMMA2Mem wmma-expr))
    (WMMAShape wmma-expr n-lanes k-lanes)
    (has-type e (Float bits 256))
    (WMMATileD (Tile n-lanes _m k-lanes) (RowMajor) intrinsic bits)
) (
    (let load-var (ExprVar (Mem)
        (Call intrinsic (Int 32 1) (vec-of (Var (Handle 1) (V "DUMMY_STORE_NAME")) wmma-expr (IntImm32 0) (IntImm32 k-lanes)) (Intrinsic))))
    (let index (Ramp (Ramp (IntImm32 0) (IntImm32 1) k-lanes) (Broadcast (IntImm32 k-lanes) k-lanes) n-lanes))
    (let new-e (Load (Float bits (* n-lanes k-lanes)) load-var index))
    (union e new-e)
))

(rule (
        (= s (Store store-name (WMMA2Mem tile) index))
        (= tile (Load (Float bits _lanes) (V tile-name) load-index))
        (= index (Ramp (Ramp base (IntImm32 1) m-lanes) (Broadcast n-stride m-lanes) n-lanes))
        (has-type tile (Float bits _lanes))
        (WMMAAllocation tile-name n-lanes m-lanes)
        (WMMATileD (Tile n-lanes m-lanes _k) (RowMajor) intrinsic bits)
    ) (
        (let new-s (Evaluate
                (Call intrinsic (Int 32 1)
                    (vec-of (Var (Handle 1) (V store-name))
                        tile
                        base
                        n-stride
                    )
                    (Intrinsic))))
        (union s new-s)
    ))

(rule (
    (= s (Store store-name (WMMA2Mem tile) store-index))
    (= tile (Load (Float bits _lanes) (V tile-name) load-index))

    (= store-index (Ramp store-base (IntImm32 1) tot-lanes))
    (= load-index (Ramp tile-base (IntImm32 1) tot-lanes))
    (WMMAAllocation tile-name n-lanes m-lanes)

    (WMMATileD (Tile n-lanes m-lanes _k) (RowMajor) intrinsic bits)
) (
    (let new-s (Evaluate
            (Call intrinsic (Int 32 1)
                (vec-of (Var (Handle 1) (V store-name))
                    tile
                    store-base
                    (IntImm32 m-lanes)
                )
                (Intrinsic))))
    (union s new-s)
))


;; loading matrices A and B

(rule (
    (= e (Mem2WMMA
        (Load ty name index)))
    (= index (Ramp (Ramp base (IntImm32 1) m-lanes) (Broadcast n-stride m-lanes) n-lanes))
    (= ty (Float 16 _lanes))

    (WMMATileB (Tile _l m-lanes n-lanes) (RowMajor) wmma-intrinsic)
) (
    (let new-e (Call wmma-intrinsic ty (vec-of (Var (Handle 1) name) base n-stride) (Intrinsic)))
    (union e new-e)
))

(rule (
    (= e (Bop (Add)
            (VectorReduce (Float 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float 32 tot-lanes) lhs)
                    (Cast (Float 32 tot-lanes) rhs))
            )
            mat))
    (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))

    (= lhs-index 
        (Ramp 
            (Ramp 
                (Ramp bl s1a l1) 
                (Broadcast s2 l1) 
                l2) 
            (Broadcast s3 (* l1 l2)) 
            l3))
    (= rhs-index 
        (Broadcast 
            (Broadcast (Ramp br s1b l1) l2) l3))

    (= s2 s1a)
    (= s1a (IntImm32 1))
    (= lhs-inner-offset 1)
    ;; NB: if we relax the restriction to be that lhs-inner-offset 
    ;; can be greater than 0, then we need to pass that
    ;; lhs-inner-offset to ConvolutionShuffle
    ; (= (% s2 s1a) 0)
    ; (= (/ s1 s1a) lhs-inner-offset)
) (
    (let new-l1 (+ l1 (* lhs-inner-offset l2)))
    (let new-tot-lanes (* (* new-l1 l2) l3))
    (let new-lhs-index
        (Ramp 
            (Broadcast 
                (Ramp bl s1a new-l1)
                l2)
            (Broadcast s3 (* new-l1 l2))
            l3)
    )
    (let new-lhs (Load (Float 16 new-tot-lanes) lhs-name new-lhs-index))
    
    (let new-rhs-index 
        (Broadcast 
            (Ramp 
                (Ramp (IntImm32 0) (IntImm32 l2) new-l1)
                (Broadcast (IntImm32 1) new-l1)
                l2)
            l3))
    (let new-rhs-name
        (ExprVar (Mem)
            (Call "ConvolutionShuffle" (Float 16 (* new-l1 l2))
                (vec-of (Var (Handle 1) rhs-name) br s1b (IntImm32 l1) (IntImm32 l2) (IntImm32 1) (IntImm32 0))
                (Intrinsic))
        )
    )
    (let new-rhs (Load (Float 16 new-tot-lanes) new-rhs-name new-rhs-index))

    (let new-e 
        (Bop (Add)
                (VectorReduce (Float 32 out-lanes) (Add)
                    (Bop (Mul)
                        (Cast (Float 32 new-tot-lanes) new-lhs)
                        (Cast (Float 32 new-tot-lanes) new-rhs))
                )
                mat))

    (union e new-e)
))

(rule (
    (= e (Bop (Add)
            (VectorReduce (Float 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float 32 tot-lanes) lhs)
                    (Cast (Float 32 tot-lanes) rhs))
            )
            mat))
    (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))

    (= lhs-index 
        (Ramp 
            (Ramp bl s1a l1)
            (Broadcast s2 l1) 
            l2))
    (= rhs-index 
        (Broadcast (Ramp br s1b l1) l2))

    (= s2 s1a)
    (= s1a (IntImm32 1))
    (= lhs-inner-offset 1)
    (= 0 (% l2 32))
    (= 0 (% l2 8))
) (
    (let new-l2 (/ l2 32))
    (let new-l3 (/ l2 8))
    (let new-l1 (+ l1 (* lhs-inner-offset new-l2)))
    (let new-tot-lanes (* (* new-l1 new-l2) new-l3))
    (let new-lhs-index
        (Ramp 
            (Broadcast 
                (Ramp bl s1a new-l1)
                new-l2)
            (Broadcast (IntImm32 new-l2) (* new-l1 new-l2))
            new-l3)
    )
    (let new-lhs (Load (Float 16 new-tot-lanes) lhs-name new-lhs-index))
    
    (let new-rhs-index 
        (Broadcast 
            (Ramp 
                (Ramp (IntImm32 0) (IntImm32 new-l2) new-l1)
                (Broadcast (IntImm32 1) new-l1)
                new-l2)
            new-l3)
    )
    (let new-rhs-name
        (ExprVar (Mem)
            (Call "ConvolutionShuffle" (Float 16 (* new-l1 new-l2))
                (vec-of (Var (Handle 1) rhs-name) br s1b (IntImm32 l1) (IntImm32 new-l2) (IntImm32 1) (IntImm32 0))
                (Intrinsic))
        )
    )
    (let new-rhs (Load (Float 16 new-tot-lanes) new-rhs-name new-rhs-index))

    (let new-e 
        (Bop (Add)
                (VectorReduce (Float 32 out-lanes) (Add)
                    (Bop (Mul)
                        (Cast (Float 32 new-tot-lanes) new-lhs)
                        (Cast (Float 32 new-tot-lanes) new-rhs))
                )
                mat))

    (union e new-e)
))


;; Downsampling
(rule (
    (= e (Bop (Add)
            (VectorReduce (Float 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float 32 tot-lanes) lhs)
                    (Cast (Float 32 tot-lanes) rhs))
            )
            mat))
    (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))

    (= lhs-index 
        (Ramp 
            (Ramp bl s1a l1)
            (Broadcast s2 l1) 
            l2))
    (= rhs-index 
        (Broadcast (Ramp br s1b l1) l2))

    (= s2 (IntImm32 2))
    (= s1a (IntImm32 1))
    (= lhs-inner-offset 1)
    (= l1 16)
    (= l2 256)
) (
    (let new-l2 8)
    (let new-l3 32)
    (let new-l1 16)
    (let new-tot-lanes (* (* new-l1 new-l2) new-l3))

    ; the rhs index is invariant to both exprs
    (let new-rhs-index
        (Broadcast 
            (Ramp 
                (Ramp (IntImm32 0) (IntImm32 new-l2) new-l1)
                (Broadcast (IntImm32 1) new-l1)
                new-l2)
            new-l3)
    )

    (let new-lhs-index-1
        (Ramp 
            (Broadcast 
                (Ramp bl s1a new-l1)
                new-l2)
            (Broadcast (IntImm32 new-l1) (* new-l1 new-l2))
            new-l3)
    )
    (let new-lhs-index-2
        (Ramp 
            (Broadcast 
                (Ramp (Bop (Add) bl (IntImm32 new-l1)) s1a new-l1)
                new-l2)
            (Broadcast (IntImm32 new-l1) (* new-l1 new-l2))
            new-l3)
    )
    (let new-lhs-1 (Load (Float 16 new-tot-lanes) lhs-name new-lhs-index-1))
    (let new-lhs-2 (Load (Float 16 new-tot-lanes) lhs-name new-lhs-index-2))
    
    (let new-rhs-name-1
        (ExprVar (Mem)
            (Call "ConvolutionShuffle" (Float 16 (* new-l1 new-l2))
                (vec-of (Var (Handle 1) rhs-name) br s1b (IntImm32 16) (IntImm32 8) (IntImm32 2) (IntImm32 0))
                (Intrinsic))
        )
    )
    (let new-rhs-name-2
        (ExprVar (Mem)
            (Call "ConvolutionShuffle" (Float 16 (* new-l1 new-l2))
                (vec-of (Var (Handle 1) rhs-name) br s1b (IntImm32 16) (IntImm32 8) (IntImm32 2) (IntImm32 16))
                (Intrinsic))
        )
    )
    (let new-rhs-1 (Load (Float 16 new-tot-lanes) new-rhs-name-1 new-rhs-index))
    (let new-rhs-2 (Load (Float 16 new-tot-lanes) new-rhs-name-2 new-rhs-index))

    (let new-e-1
        (VectorReduce (Float 32 out-lanes) (Add)
            (Bop (Mul)
                (Cast (Float 32 new-tot-lanes) new-lhs-1)
                (Cast (Float 32 new-tot-lanes) new-rhs-1))
        ))
    (let new-e-2
        (VectorReduce (Float 32 out-lanes) (Add)
            (Bop (Mul)
                (Cast (Float 32 new-tot-lanes) new-lhs-2)
                (Cast (Float 32 new-tot-lanes) new-rhs-2))
        ))
    
    (let new-e (Bop (Add) new-e-1 (Bop (Add) new-e-2 mat)))

    (union e new-e)
))

(rule (
    (= e (Bop (Add)
            (VectorReduce (Float acc-bits out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float acc-bits tot-lanes) lhs)
                    (Cast (Float acc-bits tot-lanes) rhs))
            )
            mat))
    (= out-lanes (* x-lanes y-lanes))
    (= tot-lanes (* out-lanes r-lanes))

    (= lhs (Load (Float 16 tot-lanes) lhs-name lhs-index))
    (= lhs-index
        (Ramp 
            (Broadcast
                (Ramp lhs-base (IntImm32 1) r-lanes)
                x-lanes)
                ; step = 1 means this is actually a convolution
            (Broadcast (IntImm32 1) (* r-lanes x-lanes))
            y-lanes
        )
    )
    
    (= rhs (Load (Float 16 tot-lanes) rhs-name rhs-index))
    (= rhs-index
        (Broadcast (Ramp (Ramp rhs-base (IntImm32 1) r-lanes) 
            (Broadcast x-stride r-lanes)
            x-lanes)
        y-lanes)
    )
) (
    (let new-r-lanes (* r-lanes 2))
    (let new-y-lanes (/ y-lanes r-lanes))
    (let new-x-lanes (* x-lanes r-lanes))

    (let new-tot-lanes ( * (* new-x-lanes new-y-lanes) new-r-lanes))
    (let new-out-lanes (* new-x-lanes new-y-lanes))

    (let new-lhs-index
        (Ramp 
            (Broadcast 
                (Ramp lhs-base (IntImm32 1) new-r-lanes)
                new-x-lanes)
            (Broadcast (IntImm32 r-lanes) (* new-r-lanes new-x-lanes))
            new-y-lanes)
    )
    (let new-lhs (Load (Float 16 new-tot-lanes) lhs-name new-lhs-index)) ;; r-lanes so it's an overlapping load

    (let new-rhs-index
        (Broadcast 
            (Ramp 
                (Ramp (IntImm32 0) (IntImm32 new-x-lanes) new-r-lanes)
                (Broadcast (IntImm32 1) new-r-lanes)
                new-x-lanes)
            new-y-lanes))
    (let new-rhs-name
        (ExprVar (Mem)
            (Call "ConvolutionShuffle+" (Float 16 (* new-r-lanes new-x-lanes))
                (vec-of (Var (Handle 1) rhs-name) rhs-base (IntImm32 1) (IntImm32 r-lanes) (IntImm32 r-lanes) (IntImm32 1) (IntImm32 0) x-stride (IntImm32 x-lanes))
                (Intrinsic))
        )
    )
    (let new-rhs (Load (Float 16 new-tot-lanes) new-rhs-name new-rhs-index))
    (let new-e 
        (Bop (Add)
            (VectorReduce (Float 32 out-lanes) (Add)
                (Bop (Mul)
                    (Cast (Float 32 new-tot-lanes) new-lhs)
                    (Cast (Float 32 new-tot-lanes) new-rhs))
            )
            mat))
    (union e new-e)
))

; 
(let collectstoresplaceholderconvBBBeqsatBBB0 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Broadcast (FloatImm 32 0.000000) 256)) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB1 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Broadcast (FloatImm 32 0.000000) 256)) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB10 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 32)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 32) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB11 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 32)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 32) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB12 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 40)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 40) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB13 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 40)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 40) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB14 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 48)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 48) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB15 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 48)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 48) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB16 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 56)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 56) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB17 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 56)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 56) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB18 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 64)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 64) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB19 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 64)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 64) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB2 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 0) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB20 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 72)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 72) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB21 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 72)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 72) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB22 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 80)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 80) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB23 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 80)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 80) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB24 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 88)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 88) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB25 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 88)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 88) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB26 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 96)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 96) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB27 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 96)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 96) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB28 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 104)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 104) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB29 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 104)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 104) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB3 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 0) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB30 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 112)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 112) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB31 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 112)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 112) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB32 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 120)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 120) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB33 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 120)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 120) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB4 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 8)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 8) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB5 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 8)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 8) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB6 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 16)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 16) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB7 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 16)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 16) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB8 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 24)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 24) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))))) (Ramp (IntImm 32 0) (IntImm 32 1) 256)))
(let collectstoresplaceholderconvBBBeqsatBBB9 (Store "conv" (Loc2Loc (Mem) (WMMA_C) (Bop (Add) (VectorReduce (Float 32 256) (Add) (Bop (Mul) (Cast (Float 32 2048) (Load (Float 16 2048) (V "image" ) (Ramp (Ramp (Bop (Add) (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "image.min.1" )) (Var (Int 32 1) (V "image.stride.1" ))) (Var (Int 32 1) (V "image.min.0" )))) (IntImm 32 24)) (IntImm 32 1) 8) (Broadcast (IntImm 32 1) 8) 256))) (Broadcast (Cast (Float 32 8) (Load (Float 16 8) (V "kernel" ) (Ramp (Bop (Sub) (IntImm 32 24) (Var (Int 32 1) (V "kernel.min.0" ))) (IntImm 32 1) 8))) 256))) (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))))) (Ramp (IntImm 32 256) (IntImm 32 1) 256)))
(let collectstoresplaceholderoutputBBBeqsatBBB34 (Store "output" (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 0) (IntImm 32 1) 256))) (Ramp (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (Var (Int 32 1) (V "output.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.min.1" )) (Var (Int 32 1) (V "output.stride.1" ))) (Var (Int 32 1) (V "output.min.0" )))) (IntImm 32 1) 256)))
(let collectstoresplaceholderoutputBBBeqsatBBB35 (Store "output" (Loc2Loc (WMMA_C) (Mem) (Load (Float 32 256) (V "conv" ) (Ramp (IntImm 32 256) (IntImm 32 1) 256))) (Ramp (Bop (Sub) (Bop (Add) (Bop (Mul) (Bop (Add) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.s0.y.mmy.mmy" )) (IntImm 32 2)) (Var (Int 32 1) (V "output.s0.y.mmy.base" ))) (IntImm 32 1)) (Var (Int 32 1) (V "output.stride.1" ))) (Var (Int 32 1) (V "output.s0.x.mmx.base" ))) (Bop (Add) (Bop (Mul) (Var (Int 32 1) (V "output.min.1" )) (Var (Int 32 1) (V "output.stride.1" ))) (Var (Int 32 1) (V "output.min.0" )))) (IntImm 32 1) 256)))




(run-schedule 
    (repeat 20
        (saturate (run typechecking))
        (run)
        (run amx)))
; ;; These are not currently used
; (run-schedule (repeat 15 
;     (saturate (run typechecking))
;     (run push-down-vector-reduce)))

; ; (keep-best "collectstoresplaceholderconvBBBeqsatBBB0" "collectstoresplaceholderconvBBBeqsatBBB1" "collectstoresplaceholderconvBBBeqsatBBB10" "collectstoresplaceholderconvBBBeqsatBBB11" "collectstoresplaceholderconvBBBeqsatBBB12" "collectstoresplaceholderconvBBBeqsatBBB13" "collectstoresplaceholderconvBBBeqsatBBB14" "collectstoresplaceholderconvBBBeqsatBBB15" "collectstoresplaceholderconvBBBeqsatBBB16" "collectstoresplaceholderconvBBBeqsatBBB17" "collectstoresplaceholderconvBBBeqsatBBB18" "collectstoresplaceholderconvBBBeqsatBBB19" "collectstoresplaceholderconvBBBeqsatBBB2" "collectstoresplaceholderconvBBBeqsatBBB20" "collectstoresplaceholderconvBBBeqsatBBB21" "collectstoresplaceholderconvBBBeqsatBBB22" "collectstoresplaceholderconvBBBeqsatBBB23" "collectstoresplaceholderconvBBBeqsatBBB24" "collectstoresplaceholderconvBBBeqsatBBB25" "collectstoresplaceholderconvBBBeqsatBBB26" "collectstoresplaceholderconvBBBeqsatBBB27" "collectstoresplaceholderconvBBBeqsatBBB28" "collectstoresplaceholderconvBBBeqsatBBB29" "collectstoresplaceholderconvBBBeqsatBBB3" "collectstoresplaceholderconvBBBeqsatBBB30" "collectstoresplaceholderconvBBBeqsatBBB31" "collectstoresplaceholderconvBBBeqsatBBB32" "collectstoresplaceholderconvBBBeqsatBBB33" "collectstoresplaceholderconvBBBeqsatBBB4" "collectstoresplaceholderconvBBBeqsatBBB5" "collectstoresplaceholderconvBBBeqsatBBB6" "collectstoresplaceholderconvBBBeqsatBBB7" "collectstoresplaceholderconvBBBeqsatBBB8" "collectstoresplaceholderconvBBBeqsatBBB9" "collectstoresplaceholderoutputBBBeqsatBBB34" "collectstoresplaceholderoutputBBBeqsatBBB35"  "AMXShape" "AMXAllocation" "WMMAShape" "WMMAAllocation" "CommBop" "AddOrSub" "WMMATileA" "WMMATileB" "WMMATileC" "WMMAGEMM" "WMMATileD")

; (run-schedule (repeat 15
;     (saturate (run typechecking))
;     (repeat 1 (run))
;     ))
; (run-schedule (repeat 20
;     (saturate (run typechecking))
;     (repeat 1 (run canonicalize) (run))
;     ))
; (run-schedule (saturate
;     (saturate (run typechecking))
;     (repeat 1 (run assemble))
;     ))
; (run-schedule (repeat 6
;     (saturate (run typechecking))
;     (repeat 1 (run))
;     ))

; (keep-best "collectstoresplaceholderconvBBBeqsatBBB0" "collectstoresplaceholderconvBBBeqsatBBB1" "collectstoresplaceholderconvBBBeqsatBBB10" "collectstoresplaceholderconvBBBeqsatBBB11" "collectstoresplaceholderconvBBBeqsatBBB12" "collectstoresplaceholderconvBBBeqsatBBB13" "collectstoresplaceholderconvBBBeqsatBBB14" "collectstoresplaceholderconvBBBeqsatBBB15" "collectstoresplaceholderconvBBBeqsatBBB16" "collectstoresplaceholderconvBBBeqsatBBB17" "collectstoresplaceholderconvBBBeqsatBBB18" "collectstoresplaceholderconvBBBeqsatBBB19" "collectstoresplaceholderconvBBBeqsatBBB2" "collectstoresplaceholderconvBBBeqsatBBB20" "collectstoresplaceholderconvBBBeqsatBBB21" "collectstoresplaceholderconvBBBeqsatBBB22" "collectstoresplaceholderconvBBBeqsatBBB23" "collectstoresplaceholderconvBBBeqsatBBB24" "collectstoresplaceholderconvBBBeqsatBBB25" "collectstoresplaceholderconvBBBeqsatBBB26" "collectstoresplaceholderconvBBBeqsatBBB27" "collectstoresplaceholderconvBBBeqsatBBB28" "collectstoresplaceholderconvBBBeqsatBBB29" "collectstoresplaceholderconvBBBeqsatBBB3" "collectstoresplaceholderconvBBBeqsatBBB30" "collectstoresplaceholderconvBBBeqsatBBB31" "collectstoresplaceholderconvBBBeqsatBBB32" "collectstoresplaceholderconvBBBeqsatBBB33" "collectstoresplaceholderconvBBBeqsatBBB4" "collectstoresplaceholderconvBBBeqsatBBB5" "collectstoresplaceholderconvBBBeqsatBBB6" "collectstoresplaceholderconvBBBeqsatBBB7" "collectstoresplaceholderconvBBBeqsatBBB8" "collectstoresplaceholderconvBBBeqsatBBB9" "collectstoresplaceholderoutputBBBeqsatBBB34" "collectstoresplaceholderoutputBBBeqsatBBB35"  "AMXShape" "AMXAllocation" "WMMAShape" "WMMAAllocation" "CommBop" "AddOrSub" "WMMATileA" "WMMATileB" "WMMATileC" "WMMAGEMM" "WMMATileD")

; (run-schedule (repeat 8
;     (saturate (run typechecking))
;     (repeat 1 (run))
;     ))


; (ruleset subsume-invalid-exprs)

; 
;; (extract collectstoresplaceholderconvBBBeqsatBBB0)
;; (extract collectstoresplaceholderconvBBBeqsatBBB1)
;; (extract collectstoresplaceholderconvBBBeqsatBBB10)
;; (extract collectstoresplaceholderconvBBBeqsatBBB11)
;; (extract collectstoresplaceholderconvBBBeqsatBBB12)
;; (extract collectstoresplaceholderconvBBBeqsatBBB13)
;; (extract collectstoresplaceholderconvBBBeqsatBBB14)
;; (extract collectstoresplaceholderconvBBBeqsatBBB15)
;; (extract collectstoresplaceholderconvBBBeqsatBBB16)
;; (extract collectstoresplaceholderconvBBBeqsatBBB17)
;; (extract collectstoresplaceholderconvBBBeqsatBBB18)
;; (extract collectstoresplaceholderconvBBBeqsatBBB19)
;; (extract collectstoresplaceholderconvBBBeqsatBBB2)
;; (extract collectstoresplaceholderconvBBBeqsatBBB20)
;; (extract collectstoresplaceholderconvBBBeqsatBBB21)
;; (extract collectstoresplaceholderconvBBBeqsatBBB22)
;; (extract collectstoresplaceholderconvBBBeqsatBBB23)
;; (extract collectstoresplaceholderconvBBBeqsatBBB24)
;; (extract collectstoresplaceholderconvBBBeqsatBBB25)
;; (extract collectstoresplaceholderconvBBBeqsatBBB26)
;; (extract collectstoresplaceholderconvBBBeqsatBBB27)
;; (extract collectstoresplaceholderconvBBBeqsatBBB28)
;; (extract collectstoresplaceholderconvBBBeqsatBBB29)
;; (extract collectstoresplaceholderconvBBBeqsatBBB3)
;; (extract collectstoresplaceholderconvBBBeqsatBBB30)
;; (extract collectstoresplaceholderconvBBBeqsatBBB31)
;; (extract collectstoresplaceholderconvBBBeqsatBBB32)
;; (extract collectstoresplaceholderconvBBBeqsatBBB33)
;; (extract collectstoresplaceholderconvBBBeqsatBBB4)
;; (extract collectstoresplaceholderconvBBBeqsatBBB5)
;; (extract collectstoresplaceholderconvBBBeqsatBBB6)
;; (extract collectstoresplaceholderconvBBBeqsatBBB7)
;; (extract collectstoresplaceholderconvBBBeqsatBBB8)
;; (extract collectstoresplaceholderconvBBBeqsatBBB9)
;; (extract collectstoresplaceholderoutputBBBeqsatBBB34)
;; (extract collectstoresplaceholderoutputBBBeqsatBBB35)