Skip to content

[mlir] transform.apply_patterns support more config options #88484

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion mlir/include/mlir/Dialect/Transform/IR/TransformOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ def ApplyPatternsOp : TransformDialectOp<"apply_patterns",
}];

let arguments = (ins
TransformHandleTypeInterface:$target, UnitAttr:$apply_cse);
TransformHandleTypeInterface:$target,
UnitAttr:$apply_cse,
DefaultValuedAttr<I64Attr, "static_cast<uint64_t>(-1)">:$max_iterations,
DefaultValuedAttr<I64Attr, "static_cast<uint64_t>(-1)">:$max_num_rewrites);
let results = (outs);
let regions = (region MaxSizedRegion<1>:$patterns);

Expand Down
7 changes: 7 additions & 0 deletions mlir/lib/Dialect/Transform/IR/TransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ DiagnosedSilenceableFailure transform::ApplyPatternsOp::applyToOne(
static_cast<RewriterBase::Listener *>(rewriter.getListener());
FrozenRewritePatternSet frozenPatterns(std::move(patterns));

config.maxIterations = getMaxIterations() == static_cast<uint64_t>(-1)
? GreedyRewriteConfig::kNoLimit
: getMaxIterations();
config.maxNumRewrites = getMaxNumRewrites() == static_cast<uint64_t>(-1)
? GreedyRewriteConfig::kNoLimit
: getMaxNumRewrites();

// Apply patterns and CSE repetitively until a fixpoint is reached. If no CSE
// was requested, apply the greedy pattern rewrite only once. (The greedy
// pattern rewrite driver already iterates to a fixpoint internally.)
Expand Down
30 changes: 30 additions & 0 deletions mlir/test/Dialect/Transform/test-pattern-application.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ module attributes {transform.with_named_sequence} {

// -----

// CHECK-LABEL: @limited_updates
func.func @limited_updates() {
"test.container"() ({
// Only one is replaced.
// CHECK: "test.foo"() {replace_with_new_op = "test.foo"}
// CHECK: "test.foo"() : ()
%0 = "test.foo"() {replace_with_new_op = "test.foo"} : () -> (i32)
%1 = "test.foo"() {replace_with_new_op = "test.foo"} : () -> (i32)
}) : () -> ()
return
}

module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg0: !transform.any_op) {
// Pattern application will fail because of the upper limit, wrap in
// sequence to suppress the error message.
transform.sequence %arg0 : !transform.any_op failures(suppress) {
^bb0(%arg1: !transform.any_op):
%0 = transform.structured.match ops{["test.container"]} in %arg1 : (!transform.any_op) -> !transform.any_op
%1 = transform.structured.match ops{["test.foo"]} in %arg1 : (!transform.any_op) -> !transform.any_op
transform.apply_patterns to %0 {
transform.apply_patterns.transform.test_patterns
} {max_num_rewrites = 1} : !transform.any_op
}
transform.yield
}
}

// -----

func.func @replacement_op_not_found() {
"test.container"() ({
// expected-note @below {{[0] replaced op}}
Expand Down