[Mlir-commits] [mlir] 6d2b2b8 - [MLIR][PDL] Add Bytecode support for negated native constraints

Martin Lücke llvmlistbot at llvm.org
Mon Sep 11 05:58:32 PDT 2023


Author: Martin Lücke
Date: 2023-09-11T12:57:41Z
New Revision: 6d2b2b8eafbee4e35c9e666ca92d6d28f9253203

URL: https://github.com/llvm/llvm-project/commit/6d2b2b8eafbee4e35c9e666ca92d6d28f9253203
DIFF: https://github.com/llvm/llvm-project/commit/6d2b2b8eafbee4e35c9e666ca92d6d28f9253203.diff

LOG: [MLIR][PDL] Add Bytecode support for negated native constraints

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

Added: 
    

Modified: 
    mlir/lib/Rewrite/ByteCode.cpp
    mlir/test/Rewrite/pdl-bytecode.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Rewrite/ByteCode.cpp b/mlir/lib/Rewrite/ByteCode.cpp
index e7d4c4089a991e1..6e6992dcdeea784 100644
--- a/mlir/lib/Rewrite/ByteCode.cpp
+++ b/mlir/lib/Rewrite/ByteCode.cpp
@@ -773,6 +773,7 @@ void Generator::generate(pdl_interp::ApplyConstraintOp op,
          "expected index for constraint function");
   writer.append(OpCode::ApplyConstraint, constraintToMemIndex[op.getName()]);
   writer.appendPDLValueList(op.getArgs());
+  writer.append(ByteCodeField(op.getIsNegated()));
   writer.append(op.getSuccessors());
 }
 void Generator::generate(pdl_interp::ApplyRewriteOp op,
@@ -1413,10 +1414,16 @@ void ByteCodeExecutor::executeApplyConstraint(PatternRewriter &rewriter) {
   LLVM_DEBUG({
     llvm::dbgs() << "  * Arguments: ";
     llvm::interleaveComma(args, llvm::dbgs());
+    llvm::dbgs() << "\n";
   });
 
+  ByteCodeField isNegated = read();
+  LLVM_DEBUG({
+    llvm::dbgs() << "  * isNegated: " << isNegated << "\n";
+    llvm::interleaveComma(args, llvm::dbgs());
+  });
   // Invoke the constraint and jump to the proper destination.
-  selectJump(succeeded(constraintFn(rewriter, args)));
+  selectJump(isNegated != succeeded(constraintFn(rewriter, args)));
 }
 
 LogicalResult ByteCodeExecutor::executeApplyRewrite(PatternRewriter &rewriter) {

diff  --git a/mlir/test/Rewrite/pdl-bytecode.mlir b/mlir/test/Rewrite/pdl-bytecode.mlir
index 513ff3c40bc64f0..ae61c1a079548ae 100644
--- a/mlir/test/Rewrite/pdl-bytecode.mlir
+++ b/mlir/test/Rewrite/pdl-bytecode.mlir
@@ -72,6 +72,44 @@ module @ir attributes { test.apply_constraint_2 } {
 
 // -----
 
+// Test support for negated constraints.
+module @patterns {
+  pdl_interp.func @matcher(%root : !pdl.operation) {
+    %test_attr = pdl_interp.create_attribute unit
+    %attr = pdl_interp.get_attribute "test_attr" of %root
+    pdl_interp.are_equal %test_attr, %attr : !pdl.attribute -> ^pat, ^end
+
+  ^pat:
+    pdl_interp.apply_constraint "single_entity_constraint"(%root : !pdl.operation) {isNegated = true} -> ^pat1, ^end
+
+  ^pat1:
+    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end
+
+  ^end:
+    pdl_interp.finalize
+  }
+
+  module @rewriters {
+    pdl_interp.func @success(%root : !pdl.operation) {
+      %op = pdl_interp.create_operation "test.replaced_by_pattern"
+      pdl_interp.erase %root
+      pdl_interp.finalize
+    }
+  }
+}
+
+// CHECK-LABEL: test.apply_constraint_3
+// CHECK-NEXT: "test.replaced_by_pattern"
+// CHECK-NOT: "test.replaced_by_pattern"
+
+module @ir attributes { test.apply_constraint_3 } {
+  "test.foo"() { test_attr } : () -> ()
+  "test.op"() { test_attr } : () -> ()
+}
+
+// -----
+
+
 //===----------------------------------------------------------------------===//
 // pdl_interp::ApplyRewriteOp
 //===----------------------------------------------------------------------===//


        


More information about the Mlir-commits mailing list