[Mlir-commits] [mlir] 43f0d5f - Add a test case for `applyPatternsAndFoldGreedily` to support the revert of 59bbc7a08

Mehdi Amini llvmlistbot at llvm.org
Thu Mar 31 23:20:52 PDT 2022


Author: Mehdi Amini
Date: 2022-04-01T06:17:07Z
New Revision: 43f0d5f934b0f9200f8c661fe7350c71bcabe285

URL: https://github.com/llvm/llvm-project/commit/43f0d5f934b0f9200f8c661fe7350c71bcabe285
DIFF: https://github.com/llvm/llvm-project/commit/43f0d5f934b0f9200f8c661fe7350c71bcabe285.diff

LOG: Add a test case for `applyPatternsAndFoldGreedily` to support the revert of 59bbc7a08

This shows that pushing constant to the right in a commutative op leads
to `applyPatternsAndFoldGreedily` to converge without applying all the
patterns.

Differential Revision: https://reviews.llvm.org/D122870

Added: 
    mlir/test/Transforms/test-operation-folder-commutative.mlir

Modified: 
    mlir/test/lib/Dialect/Test/TestOps.td
    mlir/test/lib/Dialect/Test/TestPatterns.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/test/Transforms/test-operation-folder-commutative.mlir b/mlir/test/Transforms/test-operation-folder-commutative.mlir
new file mode 100644
index 0000000000000..9640e7968669e
--- /dev/null
+++ b/mlir/test/Transforms/test-operation-folder-commutative.mlir
@@ -0,0 +1,11 @@
+// RUN: mlir-opt --pass-pipeline="func.func(test-patterns)" %s | FileCheck %s
+
+// CHECK-LABEL: func @test_reorder_constants_and_match
+func @test_reorder_constants_and_match(%arg0 : i32) -> (i32) {
+  // CHECK: %[[CST:.+]] = arith.constant 43
+  %cst = arith.constant 43 : i32
+  // CHECK: return %[[CST]]
+  %y = "test.op_commutative2"(%cst, %arg0) : (i32, i32) -> i32
+  %x = "test.op_commutative2"(%y, %arg0) : (i32, i32) -> i32
+  return %x : i32
+}

diff  --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 5bb397353af28..b157d5da742a5 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -1089,6 +1089,11 @@ def TestCommutativeOp : TEST_Op<"op_commutative", [Commutative]> {
   let results = (outs I32);
 }
 
+def TestCommutative2Op : TEST_Op<"op_commutative2", [Commutative]> {
+  let arguments = (ins I32:$op1, I32:$op2);
+  let results = (outs I32);
+}
+
 def TestIdempotentTraitOp
  : TEST_Op<"op_idempotent_trait",
            [SameOperandsAndResultType, NoSideEffect, Idempotent]> {

diff  --git a/mlir/test/lib/Dialect/Test/TestPatterns.cpp b/mlir/test/lib/Dialect/Test/TestPatterns.cpp
index 28a988412f69e..bd80ea793e597 100644
--- a/mlir/test/lib/Dialect/Test/TestPatterns.cpp
+++ b/mlir/test/lib/Dialect/Test/TestPatterns.cpp
@@ -124,18 +124,40 @@ struct FolderInsertBeforePreviouslyFoldedConstantPattern
   }
 };
 
+/// This pattern matches test.op_commutative2 with the first operand being
+/// another test.op_commutative2 with a constant on the right side and fold it
+/// away by propagating it as its result. This is intend to check that patterns
+/// are applied after the commutative property moves constant to the right.
+struct FolderCommutativeOp2WithConstant
+    : public OpRewritePattern<TestCommutative2Op> {
+public:
+  using OpRewritePattern<TestCommutative2Op>::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(TestCommutative2Op op,
+                                PatternRewriter &rewriter) const override {
+    auto operand =
+        dyn_cast_or_null<TestCommutative2Op>(op->getOperand(0).getDefiningOp());
+    if (!operand)
+      return failure();
+    Attribute constInput;
+    if (!matchPattern(operand->getOperand(1), m_Constant(&constInput)))
+      return failure();
+    rewriter.replaceOp(op, operand->getOperand(1));
+    return success();
+  }
+};
+
 struct TestPatternDriver
     : public PassWrapper<TestPatternDriver, OperationPass<FuncOp>> {
   StringRef getArgument() const final { return "test-patterns"; }
   StringRef getDescription() const final { return "Run test dialect patterns"; }
   void runOnOperation() override {
     mlir::RewritePatternSet patterns(&getContext());
-    populateWithGenerated(patterns);
 
     // Verify named pattern is generated with expected name.
     patterns.add<FoldingPattern, TestNamedPatternRule,
-                 FolderInsertBeforePreviouslyFoldedConstantPattern>(
-        &getContext());
+                 FolderInsertBeforePreviouslyFoldedConstantPattern,
+                 FolderCommutativeOp2WithConstant>(&getContext());
 
     (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
   }


        


More information about the Mlir-commits mailing list