Skip to content

[MLIR][PDL] Skip over all results in the PDL Bytecode if a Constraint/Rewrite failed #139255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions mlir/lib/Rewrite/ByteCode.cpp
Original file line number Diff line number Diff line change
@@ -1496,22 +1496,24 @@ LogicalResult ByteCodeExecutor::executeApplyRewrite(PatternRewriter &rewriter) {
void ByteCodeExecutor::processNativeFunResults(
ByteCodeRewriteResultList &results, unsigned numResults,
LogicalResult &rewriteResult) {
// Store the results in the bytecode memory or handle missing results on
// failure.
for (unsigned resultIdx = 0; resultIdx < numResults; resultIdx++) {
PDLValue::Kind resultKind = read<PDLValue::Kind>();

if (failed(rewriteResult)) {
// Skip the according number of values on the buffer on failure and exit
// early as there are no results to process.
if (failed(rewriteResult)) {
for (unsigned resultIdx = 0; resultIdx < numResults; resultIdx++) {
const PDLValue::Kind resultKind = read<PDLValue::Kind>();
if (resultKind == PDLValue::Kind::TypeRange ||
resultKind == PDLValue::Kind::ValueRange) {
skip(2);
} else {
skip(1);
}
return;
}
return;
}

// Store the results in the bytecode memory
for (unsigned resultIdx = 0; resultIdx < numResults; resultIdx++) {
PDLValue::Kind resultKind = read<PDLValue::Kind>();
PDLValue result = results.getResults()[resultIdx];
LLVM_DEBUG(llvm::dbgs() << " * Result: " << result << "\n");
assert(result.getKind() == resultKind &&
30 changes: 30 additions & 0 deletions mlir/test/Rewrite/pdl-bytecode.mlir
Original file line number Diff line number Diff line change
@@ -143,6 +143,36 @@ module @ir attributes { test.apply_constraint_4 } {

// -----

// Test returning a type from a native constraint.
module @patterns {
pdl_interp.func @matcher(%root : !pdl.operation) {
%new_type:2 = pdl_interp.apply_constraint "op_multiple_returns_failure"(%root : !pdl.operation) : !pdl.type, !pdl.type -> ^pat2, ^end

^pat2:
pdl_interp.record_match @rewriters::@success(%root, %new_type#0 : !pdl.operation, !pdl.type) : benefit(1), loc([%root]) -> ^end

^end:
pdl_interp.finalize
}

module @rewriters {
pdl_interp.func @success(%root : !pdl.operation, %new_type : !pdl.type) {
%op = pdl_interp.create_operation "test.replaced_by_pattern" -> (%new_type : !pdl.type)
pdl_interp.erase %root
pdl_interp.finalize
}
}
}

// CHECK-LABEL: test.apply_constraint_multi_result_failure
// CHECK-NOT: "test.replaced_by_pattern"
// CHECK: "test.success_op"
module @ir attributes { test.apply_constraint_multi_result_failure } {
"test.success_op"() : () -> ()
}

// -----

// Test success and failure cases of native constraints with pdl.range results.
module @patterns {
pdl_interp.func @matcher(%root : !pdl.operation) {
9 changes: 9 additions & 0 deletions mlir/test/lib/Rewrite/TestPDLByteCode.cpp
Original file line number Diff line number Diff line change
@@ -55,6 +55,13 @@ static LogicalResult customTypeResultConstraint(PatternRewriter &rewriter,
return failure();
}

// Custom constraint that always returns failure
static LogicalResult customConstraintFailure(PatternRewriter & /*rewriter*/,
PDLResultList & /*results*/,
ArrayRef<PDLValue> /*args*/) {
return failure();
}

// Custom constraint that returns a type range of variable length if the op is
// named test.success_op
static LogicalResult customTypeRangeResultConstraint(PatternRewriter &rewriter,
@@ -150,6 +157,8 @@ struct TestPDLByteCodePass
customValueResultConstraint);
pdlPattern.registerConstraintFunction("op_constr_return_type",
customTypeResultConstraint);
pdlPattern.registerConstraintFunction("op_multiple_returns_failure",
customConstraintFailure);
pdlPattern.registerConstraintFunction("op_constr_return_type_range",
customTypeRangeResultConstraint);
pdlPattern.registerRewriteFunction("creator", customCreate);