[Mlir-commits] [mlir] eb687fb - [MLIR][Python] Make location optional in Python-defined dialect loading (#186172)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Mar 12 19:17:31 PDT 2026
Author: Twice
Date: 2026-03-13T10:17:26+08:00
New Revision: eb687fb10613573b5d60dfbbb146e15f07809d82
URL: https://github.com/llvm/llvm-project/commit/eb687fb10613573b5d60dfbbb146e15f07809d82
DIFF: https://github.com/llvm/llvm-project/commit/eb687fb10613573b5d60dfbbb146e15f07809d82.diff
LOG: [MLIR][Python] Make location optional in Python-defined dialect loading (#186172)
Now we need to provide a location when call `load()`, e.g.
```python
with Context(), Location.unknown():
MyDialect.load()
```
But it's actually weird: IRDL is just an implementation details, so for
users they don't know why they need to provide a location for loading a
dialect, which is unrelated to constructing an IR module.
This PR made location optional for dialect loading.
Added:
Modified:
mlir/python/mlir/dialects/ext.py
mlir/test/python/dialects/ext.py
Removed:
################################################################################
diff --git a/mlir/python/mlir/dialects/ext.py b/mlir/python/mlir/dialects/ext.py
index 5bcc595220f69..15651a1c4e858 100644
--- a/mlir/python/mlir/dialects/ext.py
+++ b/mlir/python/mlir/dialects/ext.py
@@ -22,6 +22,7 @@
from ._ods_common import _cext, segmented_accessor
from .irdl import Variadicity
from ..passmanager import PassManager
+from contextlib import nullcontext
ir = _cext.ir
@@ -804,9 +805,10 @@ def _emit_dialect(cls) -> None:
@classmethod
def _emit_module(cls) -> ir.Module:
- m = ir.Module.create()
- with ir.InsertionPoint(m.body):
- cls._emit_dialect()
+ with ir.Location.unknown() if not ir.Location.current else nullcontext():
+ m = ir.Module.create()
+ with ir.InsertionPoint(m.body):
+ cls._emit_dialect()
return m
diff --git a/mlir/test/python/dialects/ext.py b/mlir/test/python/dialects/ext.py
index 30275cc53d096..30132f891faec 100644
--- a/mlir/test/python/dialects/ext.py
+++ b/mlir/test/python/dialects/ext.py
@@ -43,7 +43,7 @@ class AddOp(Operation, dialect=MyInt, name="add"):
# CHECK: irdl.results(res: %0)
# CHECK: }
# CHECK: }
- with Context(), Location.unknown():
+ with Context():
MyInt.load()
print(MyInt._mlir_module)
@@ -51,13 +51,14 @@ class AddOp(Operation, dialect=MyInt, name="add"):
print([i._op_name for i in MyInt.operations])
i32 = IntegerType.get_signless(32)
- module = Module.create()
- with InsertionPoint(module.body):
- two = ConstantOp(IntegerAttr.get(i32, 2))
- three = ConstantOp(IntegerAttr.get(i32, 3))
- add1 = AddOp(two, three)
- add2 = AddOp(add1, two)
- add3 = AddOp(add2, three)
+ with Location.unknown():
+ module = Module.create()
+ with InsertionPoint(module.body):
+ two = ConstantOp(IntegerAttr.get(i32, 2))
+ three = ConstantOp(IntegerAttr.get(i32, 3))
+ add1 = AddOp(two, three)
+ add2 = AddOp(add1, two)
+ add3 = AddOp(add2, three)
# CHECK: %0 = "myint.constant"() {value = 2 : i32} : () -> i32
# CHECK: %1 = "myint.constant"() {value = 3 : i32} : () -> i32
More information about the Mlir-commits
mailing list