Skip to content

Commit 09dfc57

Browse files
authored
[mlir] Enable decoupling two kinds of greedy behavior. (#104649)
The greedy rewriter is used in many different flows and it has a lot of convenience (work list management, debugging actions, tracing, etc). But it combines two kinds of greedy behavior 1) how ops are matched, 2) folding wherever it can. These are independent forms of greedy and leads to inefficiency. E.g., cases where one need to create different phases in lowering and is required to applying patterns in specific order split across different passes. Using the driver one ends up needlessly retrying folding/having multiple rounds of folding attempts, where one final run would have sufficed. Of course folks can locally avoid this behavior by just building their own, but this is also a common requested feature that folks keep on working around locally in suboptimal ways. For downstream users, there should be no behavioral change. Updating from the deprecated should just be a find and replace (e.g., `find ./ -type f -exec sed -i 's|applyPatternsAndFoldGreedily|applyPatternsGreedily|g' {} \;` variety) as the API arguments hasn't changed between the two.
1 parent 412e1af commit 09dfc57

File tree

110 files changed

+313
-246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+313
-246
lines changed

flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class InlineElementalsPass
125125
mlir::RewritePatternSet patterns(context);
126126
patterns.insert<InlineElementalConversion>(context);
127127

128-
if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
128+
if (mlir::failed(mlir::applyPatternsGreedily(
129129
getOperation(), std::move(patterns), config))) {
130130
mlir::emitError(getOperation()->getLoc(),
131131
"failure in HLFIR elemental inlining");

flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ class LowerHLFIRIntrinsics
520520
config.enableRegionSimplification =
521521
mlir::GreedySimplifyRegionLevel::Disabled;
522522

523-
if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
524-
module, std::move(patterns), config))) {
523+
if (mlir::failed(
524+
mlir::applyPatternsGreedily(module, std::move(patterns), config))) {
525525
mlir::emitError(mlir::UnknownLoc::get(context),
526526
"failure in HLFIR intrinsic lowering");
527527
signalPassFailure();

flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ class OptimizedBufferizationPass
13721372
// patterns.insert<ReductionMaskConversion<hlfir::MaxvalOp>>(context);
13731373
// patterns.insert<ReductionMaskConversion<hlfir::MinvalOp>>(context);
13741374

1375-
if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
1375+
if (mlir::failed(mlir::applyPatternsGreedily(
13761376
getOperation(), std::move(patterns), config))) {
13771377
mlir::emitError(getOperation()->getLoc(),
13781378
"failure in HLFIR optimized bufferization");

flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class SimplifyHLFIRIntrinsics
491491
patterns.insert<SumAsElementalConversion>(context);
492492
patterns.insert<CShiftAsElementalConversion>(context);
493493

494-
if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
494+
if (mlir::failed(mlir::applyPatternsGreedily(
495495
getOperation(), std::move(patterns), config))) {
496496
mlir::emitError(getOperation()->getLoc(),
497497
"failure in HLFIR intrinsic simplification");

flang/lib/Optimizer/Transforms/AlgebraicSimplification.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ struct AlgebraicSimplification
3939
void AlgebraicSimplification::runOnOperation() {
4040
RewritePatternSet patterns(&getContext());
4141
populateMathAlgebraicSimplificationPatterns(patterns);
42-
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns),
43-
config);
42+
(void)applyPatternsGreedily(getOperation(), std::move(patterns), config);
4443
}
4544

4645
std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass() {

flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class AssumedRankOpConversion
154154
mlir::GreedyRewriteConfig config;
155155
config.enableRegionSimplification =
156156
mlir::GreedySimplifyRegionLevel::Disabled;
157-
(void)applyPatternsAndFoldGreedily(mod, std::move(patterns), config);
157+
(void)applyPatternsGreedily(mod, std::move(patterns), config);
158158
}
159159
};
160160
} // namespace

flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ class ConstantArgumentGlobalisationOpt
173173
config.strictMode = mlir::GreedyRewriteStrictness::ExistingOps;
174174

175175
patterns.insert<CallOpRewriter>(context, *di);
176-
if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
177-
mod, std::move(patterns), config))) {
176+
if (mlir::failed(
177+
mlir::applyPatternsGreedily(mod, std::move(patterns), config))) {
178178
mlir::emitError(mod.getLoc(),
179179
"error in constant globalisation optimization\n");
180180
signalPassFailure();

flang/lib/Optimizer/Transforms/StackArrays.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ void StackArraysPass::runOnOperation() {
793793
config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled;
794794

795795
patterns.insert<AllocMemConversion>(&context, *candidateOps);
796-
if (mlir::failed(mlir::applyOpPatternsAndFold(opsToConvert,
797-
std::move(patterns), config))) {
796+
if (mlir::failed(mlir::applyOpPatternsGreedily(
797+
opsToConvert, std::move(patterns), config))) {
798798
mlir::emitError(func->getLoc(), "error in stack arrays optimization\n");
799799
signalPassFailure();
800800
}

mlir/docs/PatternRewriter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ which point the driver finishes.
358358

359359
This driver comes in two fashions:
360360

361-
* `applyPatternsAndFoldGreedily` ("region-based driver") applies patterns to
361+
* `applyPatternsGreedily` ("region-based driver") applies patterns to
362362
all ops in a given region or a given container op (but not the container op
363363
itself). I.e., the worklist is initialized with all containing ops.
364364
* `applyOpPatternsAndFold` ("op-based driver") applies patterns to the

mlir/examples/standalone/lib/Standalone/StandalonePasses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class StandaloneSwitchBarFoo
3939
RewritePatternSet patterns(&getContext());
4040
patterns.add<StandaloneSwitchBarFooRewriter>(&getContext());
4141
FrozenRewritePatternSet patternSet(std::move(patterns));
42-
if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet)))
42+
if (failed(applyPatternsGreedily(getOperation(), patternSet)))
4343
signalPassFailure();
4444
}
4545
};

0 commit comments

Comments
 (0)