--- /dev/null
+// 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
+}
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]> {
}
};
+/// 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));
}