[Mlir-commits] [mlir] [MLIR][Python] Make location optional in Python-defined dialect loading (PR #186172)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 12 09:33:51 PDT 2026


https://github.com/PragmaTwice created https://github.com/llvm/llvm-project/pull/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 a 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.

>From 8148ae23d24ecd874fa61d92057a40a28ece1773 Mon Sep 17 00:00:00 2001
From: PragmaTwice <twice at apache.org>
Date: Fri, 13 Mar 2026 00:30:10 +0800
Subject: [PATCH] [MLIR][Python] Make location optional in Python-defined
 dialect loading

---
 mlir/python/mlir/dialects/ext.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

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
 



More information about the Mlir-commits mailing list