Skip to content

[mlir][Transforms] GreedyPatternRewriteDriver: Add flag to control constant CSE'ing #89552

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
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
4 changes: 4 additions & 0 deletions mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class GreedyRewriteConfig {
/// Note: Only applicable when simplifying entire regions.
bool enableRegionSimplification = true;

/// If set to "true", constants are CSE'd (even across multiple regions that
/// are in a parent-ancestor relationship).
bool cseConstants = true;

/// This specifies the maximum number of times the rewriter will iterate
/// between applying patterns and simplifying regions. Use `kNoLimit` to
/// disable this iteration limit.
Expand Down
2 changes: 2 additions & 0 deletions mlir/include/mlir/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def Canonicalizer : Pass<"canonicalize"> {
Option<"enableRegionSimplification", "region-simplify", "bool",
/*default=*/"true",
"Perform control flow optimizations to the region tree">,
Option<"cseConstants", "cse-constants", "bool", /*default=*/"true",
"CSE constant operations">,
Option<"maxIterations", "max-iterations", "int64_t",
/*default=*/"10",
"Max. iterations between applying patterns / simplifying regions">,
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Transforms/Canonicalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Canonicalizer : public impl::CanonicalizerBase<Canonicalizer> {
: config(config) {
this->topDownProcessingEnabled = config.useTopDownTraversal;
this->enableRegionSimplification = config.enableRegionSimplification;
this->cseConstants = config.cseConstants;
this->maxIterations = config.maxIterations;
this->maxNumRewrites = config.maxNumRewrites;
this->disabledPatterns = disabledPatterns;
Expand All @@ -45,6 +46,7 @@ struct Canonicalizer : public impl::CanonicalizerBase<Canonicalizer> {
// Set the config from possible pass options set in the meantime.
config.useTopDownTraversal = topDownProcessingEnabled;
config.enableRegionSimplification = enableRegionSimplification;
config.cseConstants = cseConstants;
config.maxIterations = maxIterations;
config.maxNumRewrites = maxNumRewrites;

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,13 @@ LogicalResult RegionPatternRewriteDriver::simplify(bool *changed) && {
if (!config.useTopDownTraversal) {
// Add operations to the worklist in postorder.
region.walk([&](Operation *op) {
if (!insertKnownConstant(op))
if (!config.cseConstants || !insertKnownConstant(op))
addToWorklist(op);
});
} else {
// Add all nested operations to the worklist in preorder.
region.walk<WalkOrder::PreOrder>([&](Operation *op) {
if (!insertKnownConstant(op)) {
if (!config.cseConstants || !insertKnownConstant(op)) {
addToWorklist(op);
return WalkResult::advance();
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Pass/run-reproducer.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func.func @bar() {
external_resources: {
mlir_reproducer: {
verify_each: true,
// CHECK: builtin.module(func.func(cse,canonicalize{ max-iterations=1 max-num-rewrites=-1 region-simplify=false test-convergence=false top-down=false}))
// CHECK: builtin.module(func.func(cse,canonicalize{cse-constants=true max-iterations=1 max-num-rewrites=-1 region-simplify=false test-convergence=false top-down=false}))
pipeline: "builtin.module(func.func(cse,canonicalize{max-iterations=1 max-num-rewrites=-1 region-simplify=false top-down=false}))",
disable_threading: true
}
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/Transforms/test-canonicalize.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize))' | FileCheck %s
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize{region-simplify=false}))' | FileCheck %s --check-prefixes=CHECK,NO-RS
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize{cse-constants=false}))' | FileCheck %s --check-prefixes=NO-CSE

// CHECK-LABEL: func @remove_op_with_inner_ops_pattern
func.func @remove_op_with_inner_ops_pattern() {
Expand Down Expand Up @@ -89,3 +90,16 @@ func.func @test_region_simplify() {
^bb1:
return
}

// CHECK-LABEL: do_not_cse_constant
// CHECK: %[[c0:.*]] = arith.constant 0 : index
// CHECK: return %[[c0]], %[[c0]]
// NO-CSE-LABEL: do_not_cse_constant
// NO-CSE: %[[c0:.*]] = arith.constant 0 : index
// NO-CSE: %[[c1:.*]] = arith.constant 0 : index
// NO-CSE: return %[[c0]], %[[c1]]
func.func @do_not_cse_constant() -> (index, index) {
%0 = arith.constant 0 : index
%1 = arith.constant 0 : index
return %0, %1 : index, index
}