[Mlir-commits] [mlir] [mlir][python] automatic location inference (PR #151246)

Anatoly Myachev llvmlistbot at llvm.org
Wed Sep 3 08:14:22 PDT 2025


================
@@ -0,0 +1,101 @@
+# RUN: %PYTHON %s | FileCheck %s
+# REQUIRES: python-ge-311
+import gc
+from contextlib import contextmanager
+
+from mlir.ir import *
+from mlir.dialects._ods_common import _cext
+from mlir.dialects import arith, _arith_ops_gen
+
+
+def run(f):
+    print("\nTEST:", f.__name__)
+    f()
+    gc.collect()
+    assert Context._get_live_count() == 0
+
+
+ at contextmanager
+def with_infer_location():
+    _cext.globals.set_loc_tracebacks_enabled(True)
+    yield
+    _cext.globals.set_loc_tracebacks_enabled(False)
+
+
+# CHECK-LABEL: TEST: testInferLocations
+ at run
+def testInferLocations():
+    with Context() as ctx, with_infer_location():
+        ctx.allow_unregistered_dialects = True
+
+        op = Operation.create("custom.op1")
+        one = arith.constant(IndexType.get(), 1)
+        _cext.globals.register_traceback_file_exclusion(arith.__file__)
+        two = arith.constant(IndexType.get(), 2)
+
+        # fmt: off
+        # CHECK: loc(callsite("testInferLocations"("{{.*}}[[SEP:[/\\]]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":31:13 to :43) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))
----------------
anmyachev wrote:

I managed to fix it with the following regexp: `[[SEP:(/|\\\\)]]`

In the previous approach, only one `\` character was written to `SEP`.


I checked it with `lit` and the following file:
```txt
// RUN: cat %s | FileCheck %s



# CHECK: loc(callsite("testInferLocations"("{{.*}}[[SEP:(/|\\\\)]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":31:13 to :43) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))

1:  
2: TEST: testInferLocations 
3: loc(callsite("testInferLocations"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":31:13 to :43) at callsite("run"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":13:4 to :7) at "<module>"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":26:1 to :4)))) 

``` 

https://github.com/llvm/llvm-project/pull/151246


More information about the Mlir-commits mailing list