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) {
return 0; }
51 llvm::function_ref<
void(uint64_t, uint64_t)> addMatch) {
52 addMatch(
match(op), 0);
58 virtual LogicalResult
rewrite(Operation *op) {
return failure(); }
107template <
typename OpTy>
109 uint64_t
match(Operation *op)
override {
110 if (
auto concreteOp = dyn_cast<OpTy>(op))
111 return match(concreteOp);
114 LogicalResult
rewrite(Operation *op)
override {
115 return rewrite(cast<OpTy>(op));
118 virtual uint64_t
match(OpTy op) {
return 1; }
126 uint64_t
match(Operation *op)
override;
127 LogicalResult
rewrite(Operation *op)
override;
128 std::string
getName()
const override;
134 std::unique_ptr<mlir::PassManager>
pm;
142 template <
typename R,
unsigned Benefit,
typename... Args>
143 void add(Args &&...args) {
145 {std::make_unique<R>(std::forward<Args>(args)...), Benefit});
155 SmallVector<std::pair<std::unique_ptr<Reduction>,
unsigned>>
168 :
public mlir::DialectInterfaceCollection<ReducePatternDialectInterface> {
assert(baseType &&"element must be base type")
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
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 LogicalResult rewrite(Operation *op)
Apply the reduction 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 rewriteMatches(Operation *op, ArrayRef< uint64_t > matches)
Apply a set of matches of this 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.
virtual uint64_t match(Operation *op)
Check if the reduction can apply to a specific operation.
void notifyOpErased(Operation *op)
virtual std::string getName() const =0
Return a human-readable name for this reduction pattern.
virtual void matches(Operation *op, llvm::function_ref< void(uint64_t, uint64_t)> addMatch)
Collect all ways how this reduction can apply to a specific operation.
virtual void beforeReduction(mlir::ModuleOp)
Called before the reduction is applied to a new subset of operations.