[Mlir-commits] [llvm] [mlir] [mlir] Add PDL C & Python usage (PR #94714)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jun 6 18:41:24 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {darker}-->


:warning: Python code formatter, darker found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
darker --check --diff -r 765206e050453018e861637a08a4520f29238074...f70e85e83e36e1c786d607fa89b126d248bc6f3f mlir/python/mlir/rewrite.py mlir/test/python/integration/dialects/pdl.py
``````````

</details>

<details>
<summary>
View the diff from darker here.
</summary>

``````````diff
--- test/python/integration/dialects/pdl.py	2024-06-07 01:35:58.000000 +0000
+++ test/python/integration/dialects/pdl.py	2024-06-07 01:41:00.953646 +0000
@@ -5,63 +5,63 @@
 from mlir.ir import *
 from mlir.rewrite import *
 
 
 def construct_and_print_in_module(f):
-  print("\nTEST:", f.__name__)
-  with Context(), Location.unknown():
-    module = Module.create()
-    with InsertionPoint(module.body):
-      module = f(module)
-    if module is not None:
-      print(module)
-  return f
+    print("\nTEST:", f.__name__)
+    with Context(), Location.unknown():
+        module = Module.create()
+        with InsertionPoint(module.body):
+            module = f(module)
+        if module is not None:
+            print(module)
+    return f
 
 
 # CHECK-LABEL: TEST: test_add_to_mul
 # CHECK: arith.muli
 @construct_and_print_in_module
 def test_add_to_mul(module_):
-  index_type = IndexType.get()
+    index_type = IndexType.get()
 
-  # Create a test case.
-  @module(sym_name="ir")
-  def ir():
-    @func.func(index_type, index_type)
-    def add_func(a, b):
-      return arith.addi(a, b)
+    # Create a test case.
+    @module(sym_name="ir")
+    def ir():
+        @func.func(index_type, index_type)
+        def add_func(a, b):
+            return arith.addi(a, b)
 
-  # Create a rewrite from add to mul. This will match
-  # - operation name is arith.addi
-  # - operands are index types.
-  # - there are two operands.
-  with Location.unknown():
-    m = Module.create()
-    with InsertionPoint(m.body):
-      # Change all arith.addi with index types to arith.muli.
-      pattern = pdl.PatternOp(1, "addi_to_mul")
-      with InsertionPoint(pattern.body):
-        # Match arith.addi with index types.
-        index_type = pdl.TypeOp(IndexType.get())
-        operand0 = pdl.OperandOp(index_type)
-        operand1 = pdl.OperandOp(index_type)
-        op0 = pdl.OperationOp(
-            name="arith.addi", args=[operand0, operand1], types=[index_type]
-        )
+    # Create a rewrite from add to mul. This will match
+    # - operation name is arith.addi
+    # - operands are index types.
+    # - there are two operands.
+    with Location.unknown():
+        m = Module.create()
+        with InsertionPoint(m.body):
+            # Change all arith.addi with index types to arith.muli.
+            pattern = pdl.PatternOp(1, "addi_to_mul")
+            with InsertionPoint(pattern.body):
+                # Match arith.addi with index types.
+                index_type = pdl.TypeOp(IndexType.get())
+                operand0 = pdl.OperandOp(index_type)
+                operand1 = pdl.OperandOp(index_type)
+                op0 = pdl.OperationOp(
+                    name="arith.addi", args=[operand0, operand1], types=[index_type]
+                )
 
-        # Replace the matched op with arith.muli.
-        rewrite = pdl.RewriteOp(op0)
-        with InsertionPoint(rewrite.add_body()):
-          newOp = pdl.OperationOp(
-              name="arith.muli", args=[operand0, operand1], types=[index_type]
-          )
-          pdl.ReplaceOp(op0, with_op=newOp)
+                # Replace the matched op with arith.muli.
+                rewrite = pdl.RewriteOp(op0)
+                with InsertionPoint(rewrite.add_body()):
+                    newOp = pdl.OperationOp(
+                        name="arith.muli", args=[operand0, operand1], types=[index_type]
+                    )
+                    pdl.ReplaceOp(op0, with_op=newOp)
 
-  # Create a PDL module from module and freeze it. At this point the ownership
-  # of the module is transferred to the PDL module. This ownership transfer is
-  # not yet captured Python side/has sharp edges. So best to construct the
-  # module and PDL module in same scope.
-  # FIXME: This should be made more robust.
-  frozen = PDLModule(m).freeze()
-  # Could apply frozen pattern set multiple times.
-  apply_patterns_and_fold_greedily(module_, frozen)
-  return module_
+    # Create a PDL module from module and freeze it. At this point the ownership
+    # of the module is transferred to the PDL module. This ownership transfer is
+    # not yet captured Python side/has sharp edges. So best to construct the
+    # module and PDL module in same scope.
+    # FIXME: This should be made more robust.
+    frozen = PDLModule(m).freeze()
+    # Could apply frozen pattern set multiple times.
+    apply_patterns_and_fold_greedily(module_, frozen)
+    return module_

``````````

</details>


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


More information about the Mlir-commits mailing list