Skip to content

Commit 6d2b2b8

Browse files
committed
[MLIR][PDL] Add Bytecode support for negated native constraints
Differential Revision: https://reviews.llvm.org/D153878
1 parent 08da343 commit 6d2b2b8

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

mlir/lib/Rewrite/ByteCode.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ void Generator::generate(pdl_interp::ApplyConstraintOp op,
773773
"expected index for constraint function");
774774
writer.append(OpCode::ApplyConstraint, constraintToMemIndex[op.getName()]);
775775
writer.appendPDLValueList(op.getArgs());
776+
writer.append(ByteCodeField(op.getIsNegated()));
776777
writer.append(op.getSuccessors());
777778
}
778779
void Generator::generate(pdl_interp::ApplyRewriteOp op,
@@ -1413,10 +1414,16 @@ void ByteCodeExecutor::executeApplyConstraint(PatternRewriter &rewriter) {
14131414
LLVM_DEBUG({
14141415
llvm::dbgs() << " * Arguments: ";
14151416
llvm::interleaveComma(args, llvm::dbgs());
1417+
llvm::dbgs() << "\n";
14161418
});
14171419

1420+
ByteCodeField isNegated = read();
1421+
LLVM_DEBUG({
1422+
llvm::dbgs() << " * isNegated: " << isNegated << "\n";
1423+
llvm::interleaveComma(args, llvm::dbgs());
1424+
});
14181425
// Invoke the constraint and jump to the proper destination.
1419-
selectJump(succeeded(constraintFn(rewriter, args)));
1426+
selectJump(isNegated != succeeded(constraintFn(rewriter, args)));
14201427
}
14211428

14221429
LogicalResult ByteCodeExecutor::executeApplyRewrite(PatternRewriter &rewriter) {

mlir/test/Rewrite/pdl-bytecode.mlir

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@ module @ir attributes { test.apply_constraint_2 } {
7272

7373
// -----
7474

75+
// Test support for negated constraints.
76+
module @patterns {
77+
pdl_interp.func @matcher(%root : !pdl.operation) {
78+
%test_attr = pdl_interp.create_attribute unit
79+
%attr = pdl_interp.get_attribute "test_attr" of %root
80+
pdl_interp.are_equal %test_attr, %attr : !pdl.attribute -> ^pat, ^end
81+
82+
^pat:
83+
pdl_interp.apply_constraint "single_entity_constraint"(%root : !pdl.operation) {isNegated = true} -> ^pat1, ^end
84+
85+
^pat1:
86+
pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end
87+
88+
^end:
89+
pdl_interp.finalize
90+
}
91+
92+
module @rewriters {
93+
pdl_interp.func @success(%root : !pdl.operation) {
94+
%op = pdl_interp.create_operation "test.replaced_by_pattern"
95+
pdl_interp.erase %root
96+
pdl_interp.finalize
97+
}
98+
}
99+
}
100+
101+
// CHECK-LABEL: test.apply_constraint_3
102+
// CHECK-NEXT: "test.replaced_by_pattern"
103+
// CHECK-NOT: "test.replaced_by_pattern"
104+
105+
module @ir attributes { test.apply_constraint_3 } {
106+
"test.foo"() { test_attr } : () -> ()
107+
"test.op"() { test_attr } : () -> ()
108+
}
109+
110+
// -----
111+
112+
75113
//===----------------------------------------------------------------------===//
76114
// pdl_interp::ApplyRewriteOp
77115
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)