Skip to content

Commit

Permalink
Add a test case for applyPatternsAndFoldGreedily to support the rev…
Browse files Browse the repository at this point in the history
…ert of 59bbc7a

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
  • Loading branch information
joker-eph committed Apr 1, 2022
1 parent ba43d6f commit 43f0d5f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions mlir/test/Transforms/test-operation-folder-commutative.mlir
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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]> {
Expand Down
28 changes: 25 additions & 3 deletions mlir/test/lib/Dialect/Test/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 43f0d5f

Please sign in to comment.