Skip to content

mlir_codegen

MLIRBufferFType

Bases: BufferFType, MLIRArgumentFType, ABC

Abstract base class for the ftype of datastructures. The ftype defines how the data in an Buffer is organized and accessed.

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
class MLIRBufferFType(BufferFType, MLIRArgumentFType, ABC):
    """
    Abstract base class for the ftype of datastructures. The ftype defines how
    the data in an Buffer is organized and accessed.
    """

    @abstractmethod
    def mlir_length(self, ctx: MLIRContext, buffer):
        """
        Return MLIR code which loads a named buffer at the given index.
        """
        ...

    @abstractmethod
    def mlir_load(self, ctx: MLIRContext, buffer, index):
        """
        Return MLIR code which loads a named buffer at the given index.
        """
        ...

    @abstractmethod
    def mlir_store(self, ctx: MLIRContext, buffer, index, value=None):
        """
        Return MLIR code which stores a named buffer to the given index.
        """
        ...

    @abstractmethod
    def mlir_resize(self, ctx: MLIRContext, buffer, new_length):
        """Resize a buffer and store its updated descriptor in the slot box."""
        ...

mlir_length(ctx, buffer) abstractmethod

Return MLIR code which loads a named buffer at the given index.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1086
1087
1088
1089
1090
1091
@abstractmethod
def mlir_length(self, ctx: MLIRContext, buffer):
    """
    Return MLIR code which loads a named buffer at the given index.
    """
    ...

mlir_load(ctx, buffer, index) abstractmethod

Return MLIR code which loads a named buffer at the given index.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1093
1094
1095
1096
1097
1098
@abstractmethod
def mlir_load(self, ctx: MLIRContext, buffer, index):
    """
    Return MLIR code which loads a named buffer at the given index.
    """
    ...

mlir_resize(ctx, buffer, new_length) abstractmethod

Resize a buffer and store its updated descriptor in the slot box.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1107
1108
1109
1110
@abstractmethod
def mlir_resize(self, ctx: MLIRContext, buffer, new_length):
    """Resize a buffer and store its updated descriptor in the slot box."""
    ...

mlir_store(ctx, buffer, index, value=None) abstractmethod

Return MLIR code which stores a named buffer to the given index.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1100
1101
1102
1103
1104
1105
@abstractmethod
def mlir_store(self, ctx: MLIRContext, buffer, index, value=None):
    """
    Return MLIR code which stores a named buffer to the given index.
    """
    ...

MLIRCompiler

Bases: MLIRForm, AssemblyLoader

A class to compile and run FinchAssembly.

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
class MLIRCompiler(MLIRForm, asm.AssemblyLoader):
    """
    A class to compile and run FinchAssembly.

    """

    def __init__(self, ctx: MLIRLowerer | None = None):
        self.ctx: MLIRLowerer = MLIRGenerator() if ctx is None else ctx

    def lower(
        self, prgm: asm.Module, *, mode: CompilerMode | None = None
    ) -> MLIRLibrary:
        if prgm.head() != asm.Module:
            raise ValueError(
                "MLIRCompiler expects a Module as the head of the program, "
                f"got {type(prgm.head())}"
            )
        mlir_code = self.ctx(prgm, mode=mode).code
        logger.debug(f"Compiling MLIR code:\n{mlir_code}")
        context, module, engine = load_mlir_engine(mlir_code)
        kernels = {}
        for func in prgm.funcs:
            match func:
                case asm.Function(asm.Variable(func_name, func_type), _, _):
                    kernels[func_name] = MLIRKernel(
                        engine,
                        func_name,
                        func_type,
                    )
                case _:
                    raise NotImplementedError(
                        f"Unrecognized function type: {type(func)}"
                    )
        return MLIRLibrary((context, module, engine), kernels)

MLIRContext

Bases: Context

A context for generating MLIR code.

The context tracks SSA values, variable and slot bindings, indentation, nested blocks, constants, and operations emitted into the MLIR module.

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
class MLIRContext(Context):
    """
    A context for generating MLIR code.

    The context tracks SSA values, variable and slot bindings, indentation,
    nested blocks, constants, and operations emitted into the MLIR module.

    """

    def __init__(
        self,
        tab="  ",
        indent=1,
        bindings=None,
        slots=None,
        *,
        mode=None,
    ):
        if bindings is None:
            bindings = ScopedDict()
        if slots is None:
            slots = ScopedDict()

        super().__init__(mode=mode)
        self.tab = tab
        self.indent = indent
        self.bindings = bindings
        self.slots = slots

    @property
    def feed(self) -> str:
        return self.tab * self.indent

    def new_ssa(self) -> str:
        return "%" + self.freshen("v")

    def constant(self, value, type_: str) -> str:
        key = f".const:{value}:{type_}"
        if key in self.bindings:
            return self.bindings[key][0]
        ssa = self.new_ssa()
        self.exec(f"{self.feed}{ssa} = arith.constant {value} : {type_}")
        self.bindings[key] = (ssa, type_)
        return ssa

    def resolve(self, node):
        match node:
            case asm.Slot(var_n, _):
                if var_n in self.slots:
                    fields, _ = self.slots[var_n]
                    return fields
                raise KeyError(f"Slot {var_n} not found in context")
            case _:
                raise ValueError(f"Expected Slot, got: {type(node)}")

    def cache(self, name, val):
        if isinstance(val, asm.Literal | asm.Variable):
            return val
        var = asm.Variable(self.freshen(name), val.result_type)
        self(asm.Assign(var, val))
        return var

    def emit(self):
        return "\n".join([*self.preamble, *self.epilogue])

    def emit_global(self):
        body = self.emit()
        new_funcs = []

        for name, value in mlir_globals.items():
            if f"func.call @{name}(" in body:
                new_funcs.append(value)

        funcs = "\n".join(new_funcs)
        if funcs:
            funcs += "\n"

        return f"module {{\n{funcs}{body}\n}}\n"

    def block(self) -> MLIRContext:
        blk = super().block()
        blk.tab = self.tab
        blk.indent = self.indent
        blk.bindings = self.bindings
        blk.slots = self.slots
        return blk

    def subblock(self):
        blk = self.block()
        blk.indent = self.indent + 1
        blk.bindings = self.bindings.scope()
        blk.slots = self.slots.scope()
        return blk

    def loop_carried_names(self, body):
        names = []
        nodes = [body]

        while nodes:
            node = nodes.pop()
            match node:
                case asm.Assign(asm.Variable(name, _), _):
                    if name in self.bindings and name not in names:
                        names.append(name)
                case asm.SetAttr(obj, _, _):
                    while isinstance(obj, asm.GetAttr):
                        obj = obj.obj
                    if (
                        isinstance(obj, asm.Variable)
                        and obj.name in self.bindings
                        and obj.name not in names
                    ):
                        names.append(obj.name)
                case asm.Block(bodies):
                    nodes.extend(reversed(bodies))
                case asm.IfElse(_, if_body, else_body):
                    nodes.extend((else_body, if_body))
                case (
                    asm.If(_, nested)
                    | asm.ForLoop(_, _, _, nested)
                    | asm.BufferLoop(_, _, nested)
                    | asm.WhileLoop(_, nested)
                ):
                    nodes.append(nested)

        return names

    def __call__(self, prgm: asm.AssemblyNode):
        feed = self.feed
        match prgm:
            case asm.Assert(exp):
                condition = self(exp)
                message = json.dumps(f"Finch assertion failed: {exp}")
                self.exec(f"{feed}cf.assert {condition}, {message}")
                return None
            case asm.Literal(value):
                # A Scalar-wrapped literal (a sparse gap read) carries its dtype
                # in element_type; the Scalar's own ftype is a struct, which is
                # not a type `arith.constant` can be given.
                value_type = _promote_type_helper([prgm])
                value = getattr(value, "val", value)
                t = mlir_type(value_type)
                new = float(value) if MLIROperator.is_float(value_type) else int(value)
                return self.constant(new, t)

            case asm.Variable(name, _):
                if name not in self.bindings:
                    raise ValueError(f"Variable does not exist: {name!r}")
                value, type_ = self.bindings[name]
                if value.startswith("@"):
                    result = self.new_ssa()
                    self.exec(f"{feed}{result} = func.constant {value} : {type_}")
                    return result
                return value

            case asm.Assign(asm.Variable(var_n, var_t), val):
                v = self(val)
                self.bindings[var_n] = (v, mlir_type(var_t))
                return None

            case asm.Call(op, args):
                return mlir_function_call(op, self, *args)

            case asm.Length(buffer):
                buf_t = buffer.result_type
                if not isinstance(buf_t, MLIRBufferFType):
                    raise TypeError(f"Expected MLIR buffer type, got: {buf_t}")
                return buf_t.mlir_length(self, self.resolve(buffer))

            case asm.Load(buffer, index):
                buf_t = buffer.result_type
                if not isinstance(buf_t, MLIRBufferFType):
                    raise TypeError(f"Expected MLIR buffer type, got: {buf_t}")
                if self.mode.debug:
                    index = self.cache("index", index)
                    self(
                        asm.Assert(
                            asm.Call(
                                asm.Literal(ffuncs.and_),
                                (
                                    asm.Call(
                                        asm.Literal(ffuncs.ge),
                                        (index, asm.Literal(index.result_type(0))),
                                    ),
                                    asm.Call(
                                        asm.Literal(ffuncs.lt),
                                        (index, asm.Length(buffer)),
                                    ),
                                ),
                            )
                        )
                    )
                return buf_t.mlir_load(self, self.resolve(buffer), index)

            case asm.Store(buffer, index, value):
                buf_t = buffer.result_type
                if not isinstance(buf_t, MLIRBufferFType):
                    raise TypeError(f"Expected MLIR buffer type, got: {buf_t}")
                if self.mode.debug:
                    index = self.cache("index", index)
                    self(
                        asm.Assert(
                            asm.Call(
                                asm.Literal(ffuncs.and_),
                                (
                                    asm.Call(
                                        asm.Literal(ffuncs.ge),
                                        (index, asm.Literal(index.result_type(0))),
                                    ),
                                    asm.Call(
                                        asm.Literal(ffuncs.lt),
                                        (index, asm.Length(buffer)),
                                    ),
                                ),
                            )
                        )
                    )
                return buf_t.mlir_store(self, self.resolve(buffer), index, value)

            case asm.Resize(asm.Slot(var_n, _) as buffer, size):
                buf_t = buffer.result_type

                if not isinstance(buf_t, MLIRBufferFType):
                    raise TypeError(f"Expected MLIR buffer type, got {buf_t}")

                buf_t.mlir_resize(self, self.resolve(buffer), size)
                return None

            case asm.GetAttr(obj, attr):
                attrs = [attr.val]
                base = obj
                while True:
                    match base:
                        case asm.GetAttr(inner, a):
                            attrs.append(a.val)
                            base = inner
                        case _:
                            break
                attrs.reverse()
                obj_t = base.result_type
                if not isinstance(obj_t, StructFType):
                    raise TypeError(f"Expected struct type, got: {obj_t}")
                return mlir_getattr(obj_t, self, self(base), attrs)

            case asm.SetAttr(obj, asm.Literal(attr), value):
                match obj.result_type:
                    case MutableStructFType() as obj_t:
                        obj_value = self(obj)
                        val = self(value)
                        value_t = mlir_type(obj_t.struct_attrtype(attr))
                        stored_t = llvm_type(value_t)
                        if stored_t != value_t:
                            cast = self.new_ssa()
                            if value_t == "index":
                                self.exec(
                                    f"{feed}{cast} = arith.index_cast {val} "
                                    ": index to i64"
                                )
                            else:
                                self.exec(
                                    f"{feed}{cast} = "
                                    "builtin.unrealized_conversion_cast "
                                    f"{val} : {value_t} to {stored_t}"
                                )
                            val = cast
                        ptr = self.new_ssa()
                        self.exec(
                            f"{feed}{ptr} = llvm.getelementptr {obj_value}"
                            f"[0, {obj_t.struct_fieldnames.index(attr)}] "
                            f": (!llvm.ptr) -> !llvm.ptr, {mlir_struct_type(obj_t)}"
                        )
                        self.exec(
                            f"{feed}llvm.store {val}, {ptr} : {stored_t}, !llvm.ptr"
                        )
                        return None
                attrs = [attr]
                base = obj
                while isinstance(base, asm.GetAttr):
                    attrs.append(base.attr.val)
                    base = base.obj
                attrs.reverse()

                if not isinstance(base, asm.Variable):
                    raise TypeError(f"Expected variable-backed struct, got: {base}")

                name = base.name
                obj_t = base.result_type
                if not isinstance(obj_t, StructFType):
                    raise TypeError(f"Expected struct type, got: {obj_t}")

                indices = []
                field_t = obj_t
                for field in attrs:
                    if not isinstance(field_t, StructFType):
                        raise TypeError(f"Expected struct type, got: {field_t}")
                    indices.append(field_t.struct_fieldnames.index(field))
                    field_t = field_t.struct_attrtype(field)

                obj = self.bindings[name][0]
                val = self(value)
                value_t = mlir_type(field_t)
                stored_t = llvm_type(value_t)
                if stored_t != value_t:
                    cast = self.new_ssa()
                    if value_t == "index":
                        self.exec(
                            f"{feed}{cast} = arith.index_cast {val} : index to i64"
                        )
                    else:
                        self.exec(
                            f"{feed}{cast} = builtin.unrealized_conversion_cast "
                            f"{val} : {value_t} to {stored_t}"
                        )
                    val = cast

                result = self.new_ssa()

                self.exec(
                    f"{feed}{result} = llvm.insertvalue {val}, {obj}"
                    f"[{', '.join(map(str, indices))}] "
                    f": {mlir_type(obj_t)}"
                )

                self.bindings[name] = (result, mlir_type(obj_t))
                return None

            case asm.Unpack(asm.Slot(var_n, var_t), val):
                if val.result_type != var_t:
                    raise TypeError(f"Type mismatch: {val.result_type} != {var_t}")
                self.slots[var_n] = (var_t.mlir_unpack(self, var_n, val), var_t)
                return None

            case asm.Repack(asm.Slot(var_n, var_t)):
                obj, _ = self.slots[var_n]
                var_t.mlir_repack(self, var_n, obj)
                return None

            case asm.Block(bodies):
                ctx_2 = self.block()
                for body in bodies:
                    ctx_2(body)

                block = ctx_2.emit()
                if block:
                    self.exec(block)
                return None

            case asm.ForLoop(asm.Variable(var_n, var_t), start, end, body):
                lo, hi = self(start), self(end)
                for bound_t, name in (
                    (start.result_type, "lo"),
                    (end.result_type, "hi"),
                ):
                    if mlir_type(bound_t) != "index":
                        cast = self.new_ssa()
                        old = lo if name == "lo" else hi
                        self.exec(
                            f"{feed}{cast} = arith.index_cast {old} "
                            f": {mlir_type(bound_t)} to index"
                        )
                        if name == "lo":
                            lo = cast
                        else:
                            hi = cast
                step = self.constant(1, "index")
                iv = self.new_ssa()
                ctx_2 = self.subblock()
                loop_names = self.loop_carried_names(body)
                initial = [self.bindings[name] for name in loop_names]
                iter_args = [self.new_ssa() for _ in loop_names]
                for name, arg, (_, type_) in zip(
                    loop_names, iter_args, initial, strict=True
                ):
                    ctx_2.bindings[name] = (arg, type_)
                if mlir_type(var_t) == "index":
                    ctx_2.bindings[var_n] = (iv, "index")
                else:
                    iv_cast = ctx_2.new_ssa()
                    ctx_2.exec(
                        f"{ctx_2.feed}{iv_cast} = arith.index_cast {iv} "
                        f": index to {mlir_type(var_t)}"
                    )
                    ctx_2.bindings[var_n] = (iv_cast, mlir_type(var_t))
                ctx_2(body)

                if loop_names:
                    yield_val = [ctx_2.bindings[name][0] for name in loop_names]
                    yield_t = [type_ for _, type_ in initial]
                    ctx_2.exec(
                        f"{ctx_2.feed}scf.yield {', '.join(yield_val)} "
                        f": {', '.join(yield_t)}"
                    )
                    results = [self.new_ssa() for _ in loop_names]
                    result_sp = (
                        results[0]
                        if len(results) == 1
                        else f"{results[0]}:{len(results)}"
                    )
                    result_val = (
                        results
                        if len(results) == 1
                        else [f"{results[0]}#{i}" for i in range(len(results))]
                    )
                    iter_sp = [
                        f"{arg} = {value}"
                        for arg, (value, _) in zip(iter_args, initial, strict=True)
                    ]
                    self.exec(
                        f"{feed}{result_sp} = scf.for {iv} = {lo} to {hi} "
                        f"step {step} iter_args({', '.join(iter_sp)}) "
                        f"-> ({', '.join(yield_t)}) "
                        f"{{\n{ctx_2.emit()}\n{feed}}}"
                    )
                    for name, result, type_ in zip(
                        loop_names, result_val, yield_t, strict=True
                    ):
                        self.bindings[name] = (result, type_)
                    return None

                self.exec(
                    f"{feed}scf.for {iv} = {lo} to {hi} step {step} {{\n"
                    f"{ctx_2.emit()}\n"
                    f"{feed}}}"
                )
                return None

            case asm.BufferLoop(buffer, asm.Variable(var_n, var_t), body):
                buf_t = buffer.result_type
                if not isinstance(buf_t, MLIRBufferFType):
                    raise TypeError(f"Expected MLIR buffer type, got: {buf_t}")
                buf = self.resolve(buffer)
                length = buf_t.mlir_length(self, buf)

                zero = self.constant(0, "index")
                step = self.constant(1, "index")

                iv = self.new_ssa()
                ctx_2 = self.subblock()
                loop_names = self.loop_carried_names(body)
                initial = [self.bindings[name] for name in loop_names]
                iter_args = [self.new_ssa() for _ in loop_names]
                for name, arg, (_, type_) in zip(
                    loop_names, iter_args, initial, strict=True
                ):
                    ctx_2.bindings[name] = (arg, type_)

                idx_name = ctx_2.freshen(".buffer_index")
                ctx_2.bindings[idx_name] = (iv, "index")

                elem = buf_t.mlir_load(ctx_2, buf, asm.Variable(idx_name, algebra.intp))
                ctx_2.bindings[var_n] = (elem, mlir_type(var_t))
                ctx_2(body)

                if loop_names:
                    yield_val = [ctx_2.bindings[name][0] for name in loop_names]
                    yield_t = [type_ for _, type_ in initial]
                    ctx_2.exec(
                        f"{ctx_2.feed}scf.yield {', '.join(yield_val)} "
                        f": {', '.join(yield_t)}"
                    )
                    results = [self.new_ssa() for _ in loop_names]
                    result_sp = (
                        results[0]
                        if len(results) == 1
                        else f"{results[0]}:{len(results)}"
                    )
                    result_val = (
                        results
                        if len(results) == 1
                        else [f"{results[0]}#{i}" for i in range(len(results))]
                    )
                    iter_sp = [
                        f"{arg} = {value}"
                        for arg, (value, _) in zip(iter_args, initial, strict=True)
                    ]
                    self.exec(
                        f"{feed}{result_sp} = scf.for {iv} = {zero} to {length} "
                        f"step {step} iter_args({', '.join(iter_sp)}) "
                        f"-> ({', '.join(yield_t)}) "
                        f"{{\n{ctx_2.emit()}\n{feed}}}"
                    )
                    for name, result, type_ in zip(
                        loop_names, result_val, yield_t, strict=True
                    ):
                        self.bindings[name] = (result, type_)
                    return None

                self.exec(
                    f"{feed}scf.for {iv} = {zero} to {length} step {step} {{\n"
                    f"{ctx_2.emit()}\n"
                    f"{feed}}}"
                )
                return None

            case asm.If(condition, body):
                cond = self(condition)

                before = {}
                vars = self.bindings

                while vars is not None:
                    for i, j in vars.bindings.items():
                        if i not in before:
                            before[i] = j
                    vars = vars.parent

                new_ctx = self.subblock()
                new_ctx.bindings = ScopedDict(before.copy())
                new_ctx(body)
                body_bindings = new_ctx.bindings.bindings

                changed = {
                    name: body_bindings[name]
                    for name, old_binding in before.items()
                    if not name.startswith(".")
                    and body_bindings.get(name) != old_binding
                }
                if not changed:
                    self.exec(f"{feed}scf.if {cond} {{\n{new_ctx.emit()}\n{feed}}}")
                else:
                    names = list(changed)
                    new_result = [self.new_ssa() for _ in names]

                    new_vals = [changed[i][0] for i in names]
                    new_type = [changed[i][1] for i in names]
                    old_vals = [before[i][0] for i in names]

                    new_ctx.exec(
                        f"{new_ctx.feed}scf.yield "
                        f"{', '.join(new_vals)} : {', '.join(new_type)}"
                    )

                    self.exec(
                        f"{feed}{', '.join(new_result)} = scf.if {cond} "
                        f"-> ({', '.join(new_type)}) {{\n"
                        f"{new_ctx.emit()}\n"
                        f"{feed}}} else {{\n"
                        f"{new_ctx.feed}scf.yield {', '.join(old_vals)} : "
                        f"{', '.join(new_type)}\n"
                        f"{feed}}}"
                    )

                    for i, j, k in zip(
                        names,
                        new_result,
                        new_type,
                        strict=True,
                    ):
                        self.bindings[i] = (j, k)

                return None

            case asm.IfElse(condition, body, else_body):
                cond = self(condition)

                before = {}
                vars = self.bindings

                while vars is not None:
                    for i, j in vars.bindings.items():
                        if i not in before:
                            before[i] = j
                    vars = vars.parent

                new_ctx = self.subblock()
                new_ctx.bindings = ScopedDict(before.copy())
                new_ctx(body)
                body_bindings = new_ctx.bindings.bindings

                else_ctx = self.subblock()
                else_ctx.bindings = ScopedDict(before.copy())
                else_ctx(else_body)
                else_bindings = else_ctx.bindings.bindings

                names = sorted(
                    i
                    for i in set(body_bindings) & set(else_bindings)
                    if not i.startswith(".")
                    and (
                        body_bindings[i] != before.get(i)
                        or else_bindings[i] != before.get(i)
                    )
                )
                if not names:
                    self.exec(
                        f"{feed}scf.if {cond} {{\n"
                        f"{new_ctx.emit()}\n"
                        f"{feed}}} else {{\n"
                        f"{else_ctx.emit()}\n"
                        f"{feed}}}"
                    )
                else:
                    new_result = [self.new_ssa() for _ in names]

                    new_vals = [body_bindings[i][0] for i in names]
                    else_vals = [else_bindings[i][0] for i in names]
                    new_type = [body_bindings[i][1] for i in names]

                    new_ctx.exec(
                        f"{new_ctx.feed}scf.yield {', '.join(new_vals)} "
                        f": {', '.join(new_type)}"
                    )
                    else_ctx.exec(
                        f"{else_ctx.feed}scf.yield {', '.join(else_vals)} "
                        f": {', '.join(new_type)}"
                    )

                    self.exec(
                        f"{feed}{', '.join(new_result)} = "
                        f"scf.if {cond} -> ({', '.join(new_type)}) {{\n"
                        f"{new_ctx.emit()}\n"
                        f"{feed}}} else {{\n"
                        f"{else_ctx.emit()}\n"
                        f"{feed}}}"
                    )

                    for i, j, k in zip(
                        names,
                        new_result,
                        new_type,
                        strict=True,
                    ):
                        self.bindings[i] = (j, k)

                return None

            case asm.WhileLoop(condition, loop_body):
                loop_names = self.loop_carried_names(loop_body)

                if not loop_names:
                    condition_ctx = self.subblock()
                    cond = condition_ctx(condition)
                    condition_ctx.exec(f"{condition_ctx.feed}scf.condition({cond})")

                    body_ctx = self.subblock()
                    body_ctx(loop_body)
                    body_ctx.exec(f"{body_ctx.feed}scf.yield")

                    self.exec(
                        f"{feed}scf.while : () -> () {{\n"
                        f"{condition_ctx.emit()}\n"
                        f"{feed}}} do {{\n"
                        f"{body_ctx.emit()}\n"
                        f"{feed}}}"
                    )
                    return None

                before = {i: self.bindings[i] for i in loop_names}
                before_vals = [before[i][0] for i in loop_names]
                before_type = [before[i][1] for i in loop_names]
                loop_args = [self.new_ssa() for _ in loop_names]

                condition_ctx = self.subblock()
                for i, j, k in zip(
                    loop_names,
                    loop_args,
                    before_type,
                    strict=True,
                ):
                    condition_ctx.bindings.bindings[i] = (j, k)
                cond = condition_ctx(condition)
                condition_ctx.exec(
                    f"{condition_ctx.feed}scf.condition({cond}) "
                    f"{', '.join(loop_args)} : {', '.join(before_type)}"
                )

                body_ctx = self.subblock()
                for i, j, k in zip(
                    loop_names,
                    loop_args,
                    before_type,
                    strict=True,
                ):
                    body_ctx.bindings.bindings[i] = (j, k)
                body_ctx(loop_body)

                new_vals = [body_ctx.bindings[i][0] for i in loop_names]
                body_ctx.exec(
                    f"{body_ctx.feed}scf.yield "
                    f"{', '.join(new_vals)} : {', '.join(before_type)}"
                )

                arg_bindings = {
                    arg: (initial, type_)
                    for arg, initial, type_ in zip(
                        loop_args,
                        before_vals,
                        before_type,
                        strict=True,
                    )
                }

                new_ssa = ", ".join(
                    f"{arg} = {initial}" for arg, (initial, _) in arg_bindings.items()
                )
                body_args = ", ".join(
                    f"{arg}: {type_}" for arg, (_, type_) in arg_bindings.items()
                )
                result_types = ", ".join(type_ for _, type_ in arg_bindings.values())

                result = self.new_ssa()
                if len(arg_bindings) == 1:
                    result_name = result
                    result_vals = [result]
                else:
                    result_name = f"{result}:{len(arg_bindings)}"
                    result_vals = [f"{result}#{i}" for i in range(len(arg_bindings))]

                block = self.freshen("bb")
                self.exec(
                    f"{feed}{result_name} = scf.while ({new_ssa}) "
                    f": ({result_types}) -> ({result_types}) {{\n"
                    f"{condition_ctx.emit()}\n"
                    f"{feed}}} do {{\n"
                    f"{body_ctx.feed}^{block}({body_args}):\n"
                    f"{body_ctx.emit()}\n"
                    f"{feed}}}"
                )

                for i, j, (_, k) in zip(
                    loop_names,
                    result_vals[: len(loop_names)],
                    list(arg_bindings.values())[: len(loop_names)],
                    strict=True,
                ):
                    self.bindings[i] = (j, k)

                return None

            case asm.Function(
                asm.Variable(func_name, asm.AssemblyKernelFType(result_type=return_t)),
                args,
                body,
            ):
                ctx_2 = self.subblock()
                statement = []
                for arg in args:
                    match arg:
                        case asm.Variable(name, t):
                            ssa = "%" + name.replace("#", "_")
                            statement.append(f"{ssa}: {mlir_type(t)}")
                            ctx_2.bindings[name] = (ssa, mlir_type(t))
                        case _:
                            raise NotImplementedError(
                                f"Unrecognized argument type: {arg}"
                            )
                if isinstance(return_t, StructFType):
                    statement.append("%_ret: !llvm.ptr")
                    ctx_2.bindings[".ret_ptr"] = ("%_ret", "!llvm.ptr")
                    ret = ""
                else:
                    ret = (
                        ""
                        if return_t == algebra.none_
                        else f" -> {mlir_type(return_t)}"
                    )
                ctx_2(body)
                body_code = ctx_2.emit()
                feed = self.feed

                self.exec(
                    f"{feed}func.func @{func_name}({', '.join(statement)}){ret} "
                    f"attributes {{llvm.emit_c_interface}} {{\n"
                    f"{body_code}\n"
                    f"{feed}}}"
                )
                return None

            case asm.Return(value):
                if value.result_type == algebra.none_:
                    if not isinstance(value, asm.Literal):
                        self(value)
                    self.exec(f"{feed}func.return")
                elif isinstance(value.result_type, StructFType):
                    v = self(value)
                    ptr = self.bindings[".ret_ptr"][0]
                    self.exec(
                        f"{feed}llvm.store {v}, {ptr} "
                        f": {mlir_type(value.result_type)}, !llvm.ptr"
                    )
                    self.exec(f"{feed}func.return")
                else:
                    v = self(value)
                    self.exec(f"{feed}func.return {v} : {mlir_type(value.result_type)}")
                return None

            case asm.Module(funcs):
                for func in funcs:
                    self.bindings[func.name.name] = (
                        f"@{func.name.name}",
                        mlir_type(func.name.result_type),
                    )
                for func in funcs:
                    if not isinstance(func, asm.Function):
                        raise NotImplementedError(
                            f"Unrecognized function type: {type(func)}"
                        )
                    self(func)
                return None

            case node:
                raise NotImplementedError(f"Unrecognized node: {node}")

MLIRForm

Bases: Form

Validates that a FinchAssembly program only uses constructs the MLIR backend can lower, raising a named error for each known limitation instead of failing deep inside code generation.

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
class MLIRForm(Form):
    """
    Validates that a FinchAssembly program only uses constructs the MLIR
    backend can lower, raising a named error for each known limitation
    instead of failing deep inside code generation.
    """

    @classmethod
    def validate_inputs(cls, prgm):
        match prgm:
            case asm.Module(funcs):
                for func in funcs:
                    cls.validate_function(func)
            case _:
                raise TypeError(f"MLIR backend expects an asm.Module, got {type(prgm)}")

    @classmethod
    def validate_function(cls, func):
        match func:
            case asm.Function(
                asm.Variable(
                    func_name, asm.AssemblyKernelFType(result_type=return_type)
                ),
                args,
                body,
            ):
                pass
            case _:
                raise TypeError(f"MLIR backend expects asm.Function, got {func}")
        defined = {}
        for arg in args:
            match arg:
                case asm.Variable(name, t):
                    cls.validate_type(func_name, f"argument {name!r}", t)
                    defined[name] = 0
                case _:
                    raise TypeError(
                        f"MLIR backend expects variable arguments in "
                        f"{func_name!r}, got {arg}"
                    )
        if return_type != algebra.none_:
            cls.validate_type(func_name, "return value", return_type)
        cls.validate_stmt(func_name, body, defined, depth=0)

    @classmethod
    def validate_type(cls, func_name, what, t):
        try:
            mlir_type(t)
        except NotImplementedError as e:
            raise NotImplementedError(
                f"MLIR backend cannot lower the type of {what} "
                f"in function {func_name!r}: {t}"
            ) from e

    @classmethod
    def validate_stmt(cls, func_name, node, defined, depth):
        match node:
            case asm.Block(bodies):
                for body in bodies:
                    cls.validate_stmt(func_name, body, defined, depth)
            case asm.Assign(asm.Variable(name, _), _):
                defined.setdefault(name, depth)
            case asm.ForLoop(asm.Variable(name, _), _, _, body):
                inner = dict(defined)
                inner[name] = depth + 1
                cls.validate_stmt(func_name, body, inner, depth + 1)
            case asm.BufferLoop(_, asm.Variable(name, _), body):
                inner = dict(defined)
                inner[name] = depth + 1
                cls.validate_stmt(func_name, body, inner, depth + 1)
            case asm.If(_, body):
                cls.validate_stmt(func_name, body, defined, depth)
            case asm.IfElse(_, body, else_body):
                cls.validate_stmt(func_name, body, defined, depth)
                cls.validate_stmt(func_name, else_body, defined, depth)
            case asm.WhileLoop(_, body):
                cls.validate_stmt(func_name, body, defined, depth)
            case (
                asm.Unpack(_, _)
                | asm.Repack(_)
                | asm.SetAttr(_, _, _)
                | asm.Store(_, _, _)
                | asm.Resize(_, _)
                | asm.Return(_)
                | asm.Assert(_)
            ):
                pass

            case _:
                raise NotImplementedError(
                    f"MLIR backend does not yet support "
                    f"{type(node).__name__} (in function {func_name!r})"
                )

MLIRKernel

Bases: AssemblyKernel

A class to represent MLIR kernel. It validates and serializes arguments, invokes the compiled function, and reconstructs the result.

Source code in src/finch/codegen/mlir_codegen/mlir.py
 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
class MLIRKernel(asm.AssemblyKernel):
    """
    A class to represent MLIR kernel. It validates and serializes arguments,
    invokes the compiled function, and reconstructs the result.

    """

    def __init__(self, engine, func_name, type_):
        super().__init__(type_)
        self.engine = engine
        self.func_name = func_name
        self.ret_type = type_.result_type
        self.argtypes = type_.arg_types

    def __call__(self, *args):
        if len(args) != len(self.argtypes):
            raise ValueError(
                f"Expected {len(self.argtypes)} arguments, got {len(args)}"
            )
        for argtype, arg in zip(self.argtypes, args, strict=False):
            if not fisinstance(arg, argtype):
                raise TypeError(f"Expected argument of type {argtype}, got {type(arg)}")
        serial_args = list(map(serialize_to_mlir, self.argtypes, args))

        packed = []
        for t, sa in zip(self.argtypes, serial_args, strict=False):
            if isinstance(t, BufferFType) and mlir_type(t) != "!llvm.ptr":
                packed.append(ctypes.pointer(ctypes.pointer(sa)))
            else:
                packed.append(ctypes.pointer(sa))

        if self.ret_type == algebra.none_:
            res = None
            self.engine.invoke(self.func_name, *packed)
        elif isinstance(self.ret_type, StructFType):
            res = mlir_ctype(self.ret_type)()
            self.engine.invoke(
                self.func_name, *packed, ctypes.pointer(ctypes.pointer(res))
            )
        else:
            res = mlir_ctype(self.ret_type)()
            self.engine.invoke(self.func_name, *packed, ctypes.pointer(res))

        for arg_type, arg, serial_arg in zip(
            self.argtypes, args, serial_args, strict=False
        ):
            deserialize_from_mlir(arg_type, arg, serial_arg)

        if self.ret_type is type(None):
            return None
        return construct_from_mlir(self.ret_type, res)

MLIRLibrary

Bases: AssemblyLibrary

A class to represent MLIR module.

Source code in src/finch/codegen/mlir_codegen/mlir.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class MLIRLibrary(asm.AssemblyLibrary):
    """
    A class to represent MLIR module.

    """

    def __init__(self, mlir_module, kernels):
        self.mlir_module = mlir_module
        self.kernels = kernels

    def __getattr__(self, name):
        if name in self.kernels:
            return self.kernels[name]
        raise AttributeError(
            f"{type(self).__name__!r} object has no attribute {name!r}"
        )

MLIRLowerer

Bases: Stage

Source code in src/finch/codegen/mlir_codegen/stages.py
15
16
17
18
19
20
21
class MLIRLowerer(Stage):
    @abstractmethod
    def lower(self, prgm: asm.Module, *, mode: CompilerMode | None = None) -> MLIRCode:
        """
        Lower the given assembly program to textual MLIR.

        """

lower(prgm, *, mode=None) abstractmethod

Lower the given assembly program to textual MLIR.

Source code in src/finch/codegen/mlir_codegen/stages.py
16
17
18
19
20
21
@abstractmethod
def lower(self, prgm: asm.Module, *, mode: CompilerMode | None = None) -> MLIRCode:
    """
    Lower the given assembly program to textual MLIR.

    """

MLIRUnpackableFType

Bases: ABC

Abstract base class for unpackable formats in MLIR. Unpackable formats must also support other functions with unpacked inputs in addition to variable ones.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
class MLIRUnpackableFType(ABC):
    """
    Abstract base class for unpackable formats in MLIR. Unpackable formats must also
    support other functions with unpacked inputs in addition to variable ones.
    """

    @abstractmethod
    def mlir_unpack(self, ctx, lhs, rhs):
        """
        Convert a value to an unpacked representation in MLIR. Returns a NamedTuple
        of unpacked variable names, etc. The `lhs` is the variable namespace to
        assign to.
        """
        ...

    @abstractmethod
    def mlir_repack(self, ctx, lhs, rhs):
        """
        Update an object based on an unpacked representation. The `rhs` is the
        unpacked representation to update from, and `lhs` is a variable name referring
        to the original object to update.
        """
        ...

mlir_repack(ctx, lhs, rhs) abstractmethod

Update an object based on an unpacked representation. The rhs is the unpacked representation to update from, and lhs is a variable name referring to the original object to update.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1153
1154
1155
1156
1157
1158
1159
1160
@abstractmethod
def mlir_repack(self, ctx, lhs, rhs):
    """
    Update an object based on an unpacked representation. The `rhs` is the
    unpacked representation to update from, and `lhs` is a variable name referring
    to the original object to update.
    """
    ...

mlir_unpack(ctx, lhs, rhs) abstractmethod

Convert a value to an unpacked representation in MLIR. Returns a NamedTuple of unpacked variable names, etc. The lhs is the variable namespace to assign to.

Source code in src/finch/codegen/mlir_codegen/mlir.py
1144
1145
1146
1147
1148
1149
1150
1151
@abstractmethod
def mlir_unpack(self, ctx, lhs, rhs):
    """
    Convert a value to an unpacked representation in MLIR. Returns a NamedTuple
    of unpacked variable names, etc. The `lhs` is the variable namespace to
    assign to.
    """
    ...

mlir_ctype(s)

Return the ctypes equivalent of an FType or MLIR type string, so Python can pass arguments to and read results from a compiled MLIR kernel.

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
def mlir_ctype(s: FType | str):
    """
    Return the ctypes equivalent of an FType or MLIR type string, so Python
    can pass arguments to and read results from a compiled MLIR kernel.
    """
    match s:
        case MutableStructFType():
            return ctypes.POINTER(mlir_struct_ctype(s))
        case StructFType():
            return mlir_struct_ctype(s)
        case FType():
            return mlir_ctype(mlir_type(s))

    if s.startswith("memref<") and s.endswith(">"):
        res = mlir_memrefs.get(s)
        if res is None:
            from mlir.runtime import make_nd_memref_descriptor

            t = s[len("memref<") : -1].split("x")
            rank = 0
            while rank < len(t) - 1 and (t[rank] == "?" or t[rank].isdigit()):
                rank += 1
            elem = "x".join(t[rank:])
            res = make_nd_memref_descriptor(rank, mlir_ctype(elem))
            mlir_memrefs[s] = res
        return res

    match s:
        case "!llvm.ptr":
            return ctypes.c_void_p
        case "i1":
            return ctypes.c_bool
        case "i8":
            return ctypes.c_int8
        case "i16":
            return ctypes.c_int16
        case "i32":
            return ctypes.c_int32
        case "index" | "i64":
            return ctypes.c_int64
        case "f32":
            return ctypes.c_float
        case "f64":
            return ctypes.c_double
        case _:
            raise NotImplementedError(f"No ctypes mapping for MLIR type {s}")

mlir_type(t)

Convert an FType into the MLIR type string

Source code in src/finch/codegen/mlir_codegen/mlir.py
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
def mlir_type(t: FType):
    """
    Convert an FType into the MLIR type string
    """
    match t:
        case asm.AssemblyKernelFType():
            args = [mlir_type(arg) for arg in t.arg_types]
            if isinstance(t.result_type, StructFType):
                args.append("!llvm.ptr")
                ret = "()"
            else:
                ret = (
                    "()" if t.result_type == algebra.none_ else mlir_type(t.result_type)
                )
            return f"({', '.join(args)}) -> {ret}"
        case MLIRArgumentFType():
            return t.mlir_type()
        case algebra.bool_:
            return "i1"
        case algebra.intp | algebra.int_:
            # A Python `int` is a 64-bit integer, which this backend represents
            # as `index` like every other one. Mapping it to `i64` instead made
            # the emitted type depend on whether a literal or a variable came
            # first in a commutative call.
            return "index"
        case algebra.float_:
            return "f64"
        case algebra.ftypes.FDTypeNumpy():
            dt = np_dtype(t)
            match dt.kind:
                case "b":
                    return "i1"
                case "i" | "u":
                    return f"i{dt.itemsize * 8}"
                case "f":
                    return f"f{dt.itemsize * 8}"
                case _:
                    raise NotImplementedError(f"No MLIR type for numpy dtype {dt}")
        case MutableStructFType():
            return "!llvm.ptr"
        case StructFType():
            return mlir_struct_type(t)
        case _:
            raise NotImplementedError(f"No MLIR type mapping for {t}")