11#include "mlir/IR/SymbolTable.h"
12#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
13#include "mlir/Transforms/Passes.h"
26struct OperationPruner :
public Reduction {
30 uint64_t
match(Operation *op)
override {
31 if (op->hasTrait<OpTrait::IsTerminator>())
33 if (isa<ModuleOp>(op))
35 if (op->getNumResults() > 0 && !op->use_empty())
37 if (symbolUses.hasRef(op))
41 LogicalResult
rewrite(Operation *op)
override {
45 std::string
getName()
const override {
return "operation-pruner"; }
51struct UnusedSymbolPruner :
public Reduction {
56 uint64_t
match(Operation *op)
override {
57 if (op->hasAttr(SymbolTable::getSymbolAttrName()))
58 if (!symbolUses.hasRef(op))
63 LogicalResult
rewrite(Operation *op)
override {
68 std::string
getName()
const override {
return "unused-symbol-pruner"; }
75struct MakeSymbolsPrivate :
public Reduction {
76 uint64_t
match(Operation *op)
override {
77 if (!op->getParentOp() || !isa<SymbolOpInterface>(op))
79 return SymbolTable::getSymbolVisibility(op) !=
80 SymbolTable::Visibility::Private;
83 LogicalResult
rewrite(Operation *op)
override {
85 SymbolTable::setSymbolVisibility(op, SymbolTable::Visibility::Private);
89 std::string
getName()
const override {
return "make-symbols-private"; }
99static std::unique_ptr<Pass>
101 GreedyRewriteConfig config;
102 config.setUseTopDownTraversal(
true);
103 config.setRegionSimplificationLevel(
104 mlir::GreedySimplifyRegionLevel::Disabled);
106 config.setMaxNumRewrites(*maxNumRewrites);
107 return createCanonicalizerPass(config);
112 std::optional<int64_t> maxNumRewrites) {
117 patterns.add<MakeSymbolsPrivate, 100>();
118 patterns.add<UnusedSymbolPruner, 99>();
void pruneUnusedOps(Operation *initialOp, Reduction &reduction)
Starting at the given op, traverse through it and its operands and erase operations that have no more...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void populateGenericReducePatterns(MLIRContext *context, ReducePatternSet &patterns, std::optional< int64_t > maxNumRewrites=std::nullopt)
Populate reduction patterns that are not specific to certain operations or dialects.
std::unique_ptr< Pass > createSimpleCanonicalizerPass()
Create a simple canonicalizer pass.
A reduction pattern that applies an mlir::Pass.
An abstract reduction pattern.
virtual LogicalResult rewrite(Operation *op)
Apply the reduction to a specific operation.
virtual bool acceptSizeIncrease() const
Return true if the tool should accept the transformation this reduction performs on the module even i...
virtual uint64_t match(Operation *op)
Check if the reduction can apply to a specific operation.
virtual std::string getName() const =0
Return a human-readable name for this reduction pattern.
virtual void beforeReduction(mlir::ModuleOp)
Called before the reduction is applied to a new subset of operations.
A helper struct that scans a root operation and all its nested operations for InnerRefAttrs.