-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dead values before shape refinement
Some programs have a lot of dead values, causing shape refinement to fail to converge. Indeed, even if there is no shape refinement to do, greedy pattern rewrites keep iterating as long as the IR changes, which includes removing dead values. PiperOrigin-RevId: 619606321
- Loading branch information
Michael Levesque-Dion
authored and
TensorFlow MLIR Team
committed
Mar 27, 2024
1 parent
0272de9
commit 0b73f94
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
stablehlo/stablehlo/experimental/transforms/StablehloTrivialDce.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* Copyright 2022 The StableHLO Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include <cstdint> | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "stablehlo/experimental/transforms/Passes.h" | ||
|
||
namespace mlir { | ||
namespace stablehlo { | ||
namespace experimental { | ||
|
||
#define GEN_PASS_DEF_STABLEHLOTRIVIALDCEPASS | ||
#include "stablehlo/experimental/transforms/Passes.h.inc" | ||
|
||
namespace { | ||
|
||
struct StablehloTrivialDcePass | ||
: public impl::StablehloTrivialDcePassBase<StablehloTrivialDcePass> { | ||
using StablehloTrivialDcePassBase::StablehloTrivialDcePassBase; | ||
|
||
void runOnOperation() override { | ||
GreedyRewriteConfig config; | ||
|
||
// Hardcode defaults for stability. | ||
config.enableRegionSimplification = true; | ||
config.maxNumRewrites = GreedyRewriteConfig::kNoLimit; | ||
config.strictMode = GreedyRewriteStrictness::AnyOp; | ||
|
||
// Run a single bottom up pass. | ||
config.useTopDownTraversal = false; | ||
config.maxIterations = 1; | ||
|
||
// Running a greedy rewrite will cause trivially dead values to be removed. | ||
// Doing it without patterns ensures that no other changes are made to the | ||
// IR. Doing it bottom-up ensures that values that are transitively dead are | ||
// also removed. Although 1 pass should be enough, | ||
// applyPatternsAndFoldGreedily will want to run at least 1 more iteration | ||
// to confirm convergence, but we don't need to check for convergence, so we | ||
// ignore the return value. | ||
(void)applyPatternsAndFoldGreedily(getOperation(), RewritePatternSet(&getContext()), config); | ||
} | ||
}; | ||
|
||
} // namespace | ||
} // namespace experimental | ||
} // namespace stablehlo | ||
} // namespace mlir |