[Mlir-commits] [mlir] [MLIR][Python] Add a DSL for defining dialects in Python bindings (PR #169045)
Rolf Morel
llvmlistbot at llvm.org
Sun Jan 18 10:35:33 PST 2026
================
@@ -0,0 +1,322 @@
+# RUN: %PYTHON %s 2>&1 | FileCheck %s
+
+from mlir.ir import *
+from mlir.dialects import arith
+from mlir.dialects.ext import *
+from typing import Any, Optional, Sequence, TypeVar, Union
+import sys
+
+
+def run(f):
+ print("\nTEST:", f.__name__)
+ f()
+
+
+# CHECK: TEST: testMyInt
+ at run
+def testMyInt():
+ class MyInt(Dialect, name="myint"):
+ pass
+
+ i32 = IntegerType[32]
+
+ class ConstantOp(MyInt.Operation, name="constant"):
+ value: IntegerAttr
+ cst: Result[i32]
+
+ class AddOp(MyInt.Operation, name="add"):
+ lhs: Operand[i32]
+ rhs: Operand[i32]
+ res: Result[i32]
+
+ # CHECK: irdl.dialect @myint {
+ # CHECK: irdl.operation @constant {
+ # CHECK: %0 = irdl.base "#builtin.integer"
+ # CHECK: irdl.attributes {"value" = %0}
+ # CHECK: %1 = irdl.is i32
+ # CHECK: irdl.results(cst: %1)
+ # CHECK: }
+ # CHECK: irdl.operation @add {
+ # CHECK: %0 = irdl.is i32
+ # CHECK: irdl.operands(lhs: %0, rhs: %0)
+ # CHECK: irdl.results(res: %0)
+ # CHECK: }
+ # CHECK: }
+ with Context(), Location.unknown():
+ MyInt.load()
+ print(MyInt.mlir_module)
+
+ # CHECK: ['constant', 'add']
+ print([i._op_name for i in MyInt.operations])
+ i32 = IntegerType.get_signless(32)
+
+ module = Module.create()
+ with InsertionPoint(module.body):
+ two = ConstantOp(i32, IntegerAttr.get(i32, 2))
+ three = ConstantOp(i32, IntegerAttr.get(i32, 3))
+ add1 = AddOp(i32, two, three)
+ add2 = AddOp(i32, add1, two)
+ add3 = AddOp(i32, add2, three)
----------------
rolfmorel wrote:
I would prefer to address things that affect the (base) API already now (e.g. most wouldn't like needing to change their downstream code that uses this API when we come around to updating the API).
In general, consistency with the generated OpView classes is the goal, IMHO. We can deviate from if we have good reason, though here it doesn't seem that hard to fix/get right. Let me know if that's not the case though.
https://github.com/llvm/llvm-project/pull/169045
More information about the Mlir-commits
mailing list