[Mlir-commits] [mlir] [mlir][python] C++ API demo (PR #71133)

Maksim Levental llvmlistbot at llvm.org
Mon Nov 6 13:30:55 PST 2023


================
@@ -551,3 +552,37 @@ def testInferTypeOpInterface():
             two_operands = test.InferResultsVariadicInputsOp(single=zero, doubled=zero)
             # CHECK: f32
             print(two_operands.result.type)
+
+
+# CHECK-LABEL: testPythonPassDemo
+ at run
+def testPythonPassDemo():
+    def print_ops(op):
+        print(op.name)
+
+    module = """
+    module {
+      func.func @main() {
+        %memref = memref.alloca() : memref<1xi64>
+        %c0 = arith.constant 0 : index
+        %c1 = arith.constant 1 : i64
+        memref.store %c1, %memref[%c0] : memref<1xi64>
+        %u_memref = memref.cast %memref : memref<1xi64> to memref<*xi64>
+        return
+      }
+    }
+    """
+
+    # CHECK: memref.alloca
+    # CHECK: arith.constant
+    # CHECK: arith.constant
+    # CHECK: memref.store
+    # CHECK: memref.cast
+    # CHECK: func.return
+    # CHECK: func.func
+    # CHECK: builtin.module
+    with Context() as ctx, Location.unknown():
+        test.register_python_test_dialect(ctx)
+        test.register_python_test_pass_demo_pass(print_ops)
----------------
makslevental wrote:

> That seems like a dangerous pattern to me: what happens when print_ops goes out of scope? (I mean it can't here, but you're registering a `PyObject` so it could...)

The bandaid is to put `print_ops` into `threading.local()` right?
 
> Also can you write in the same file a second run of the pipeline with a different `print_ops` which would print with a prefix for example? I suspect the registration being global that won't work...

Yea sure but you can't register a single pass (in asserts mode...) multiple times anyway so we shouldn't expect that  to work "afortiori".



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


More information about the Mlir-commits mailing list