13 #ifndef CIRCT_REDUCE_REDUCTION_H
14 #define CIRCT_REDUCE_REDUCTION_H
17 #include "mlir/IR/BuiltinOps.h"
18 #include "mlir/Pass/PassManager.h"
19 #include "llvm/ADT/SmallVector.h"
41 virtual uint64_t
match(Operation *op) = 0;
46 virtual LogicalResult
rewrite(Operation *op) = 0;
83 template <
typename OpTy>
85 uint64_t
match(Operation *op)
override {
86 if (
auto concreteOp = dyn_cast<OpTy>(op))
87 return match(concreteOp);
90 LogicalResult
rewrite(Operation *op)
override {
94 virtual uint64_t
match(OpTy op) {
return 1; }
95 virtual LogicalResult
rewrite(OpTy op) = 0;
102 uint64_t
match(Operation *op)
override;
103 LogicalResult
rewrite(Operation *op)
override;
104 std::string
getName()
const override;
110 std::unique_ptr<mlir::PassManager>
pm;
118 template <
typename R,
unsigned Benefit,
typename... Args>
119 void add(Args &&...args) {
121 {std::make_unique<R>(std::forward<Args>(args)...), Benefit});
131 SmallVector<std::pair<std::unique_ptr<Reduction>,
unsigned>>
144 :
public mlir::DialectInterfaceCollection<ReducePatternDialectInterface> {
SmallVector< std::pair< std::unique_ptr< Reduction >, unsigned > > reducePatternsWithBenefit
void filter(const std::function< bool(const Reduction &)> &pred)
Reduction & operator[](size_t idx) const
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
LogicalResult rewrite(Operation *op) override
Apply the reduction to a specific operation.
uint64_t match(Operation *op) override
Check if the reduction can apply to a specific operation.
virtual LogicalResult rewrite(OpTy op)=0
virtual uint64_t match(OpTy op)
A reduction pattern that applies an mlir::Pass.
bool isOneShot() const override
Return true if the tool should not try to reapply this reduction after it has been successful.
LogicalResult rewrite(Operation *op) override
Apply the reduction to a specific operation.
uint64_t match(Operation *op) override
Check if the reduction can apply to a specific operation.
MLIRContext *const context
std::string getName() const override
Return a human-readable name for this reduction pattern.
bool acceptSizeIncrease() const override
Return true if the tool should accept the transformation this reduction performs on the module even i...
std::unique_ptr< mlir::PassManager > pm
PassReduction(MLIRContext *context, std::unique_ptr< Pass > pass, bool canIncreaseSize=false, bool oneShot=false)
A dialect interface to provide reduction patterns to a reducer tool.
ReducePatternDialectInterface(Dialect *dialect)
virtual void populateReducePatterns(ReducePatternSet &patterns) const =0
void populateReducePatterns(ReducePatternSet &patterns) const
An abstract reduction pattern.
virtual uint64_t match(Operation *op)=0
Check if the reduction can apply to a specific operation.
virtual void afterReduction(mlir::ModuleOp)
Called after the reduction has been applied to a subset of operations.
std::function< void(Operation *)> notifyOpErasedCallback
An optional callback for reductions to communicate removal of operations.
virtual bool acceptSizeIncrease() const
Return true if the tool should accept the transformation this reduction performs on the module even i...
virtual LogicalResult rewrite(Operation *op)=0
Apply the reduction to a specific operation.
virtual bool isOneShot() const
Return true if the tool should not try to reapply this reduction after it has been successful.
void notifyOpErased(Operation *op)
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.