21 #include "llvm/ADT/IntEqClasses.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/InterleavedRange.h"
25 #define DEBUG_TYPE "affine-min-max"
26 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ")
33 template <
typename AffineOp>
40 static constexpr
bool isMin = std::is_same_v<AffineOp, AffineMinOp>;
42 LLVM_DEBUG({
DBGS() <<
"analyzing value: `" << affineOp <<
"`\n"; });
47 llvm::iota_range<unsigned>(0u, affineMap.
getNumResults(),
false),
49 return Variable(affineMap.getSliceMap(i, 1), operands);
52 DBGS() <<
"- constructed variables are: "
53 << llvm::interleaved_array(llvm::map_range(
54 variables, [](
const Variable &v) {
return v.getMap(); }))
59 ComparisonOperator cmpOp =
60 isMin ? ComparisonOperator::LT : ComparisonOperator::GT;
63 llvm::IntEqClasses boundedClasses(variables.size());
66 unsigned eqClass = boundedClasses.findLeader(i);
69 if (bounds.contains(eqClass))
76 DBGS() <<
"- inspecting variable: #" << i <<
", with map: `" << v.getMap()
81 for (
size_t j = i + 1;
j < variables.size(); ++
j) {
82 unsigned jEqClass = boundedClasses.findLeader(
j);
84 if (jEqClass == eqClass)
88 Variable *nv = bounds.lookup_or(jEqClass, &variables[
j]);
91 DBGS() <<
"- comparing with variable: #" << jEqClass
92 <<
", with map: " << nv->getMap() <<
"\n";
96 FailureOr<bool> cmpResult =
100 if (failed(cmpResult)) {
102 DBGS() <<
"-- classes: #" << i <<
", #" << jEqClass
103 <<
" cannot be merged\n";
110 DBGS() <<
"-- merging classes: #" << i <<
", #" << jEqClass
111 <<
", is cmp(lhs, rhs): " << *cmpResult <<
"`\n";
114 boundedClasses.join(eqClass, jEqClass);
119 boundedClasses.join(eqClass, jEqClass);
122 bounds[boundedClasses.findLeader(i)] = bound;
128 {
DBGS() <<
"- the affine operation couldn't get simplified\n"; });
134 results.reserve(bounds.size());
135 for (
auto [k, bound] : bounds)
136 results.push_back(bound->getMap().getResult(0));
139 DBGS() <<
"- starting from map: " << affineMap <<
"\n";
140 DBGS() <<
"- creating new map with: \n";
143 DBGS() <<
"--- res: " << llvm::interleaved_array(results) <<
"\n";
151 rewriter.
modifyOpInPlace(affineOp, [&]() { affineOp.setMap(affineMap); });
152 LLVM_DEBUG({
DBGS() <<
"- simplified affine op: `" << affineOp <<
"`\n"; });
169 if (
auto minOp = dyn_cast<AffineMinOp>(op))
171 else if (
auto maxOp = cast<AffineMaxOp>(op))
200 LogicalResult matchAndRewrite(AffineMaxOp affineOp,
209 LogicalResult matchAndRewrite(AffineMinOp affineOp,
218 LogicalResult matchAndRewrite(AffineApplyOp affineOp,
222 affineOp->getOperands().end()};
227 if (map == affineOp.getAffineMap())
231 affineOp.setMap(map);
232 affineOp->setOperands(operands);
242 #define GEN_PASS_DEF_SIMPLIFYAFFINEMINMAXPASS
243 #include "mlir/Dialect/Affine/Passes.h.inc"
249 :
public affine::impl::SimplifyAffineMinMaxPassBase<
250 SimplifyAffineMinMaxPass> {
251 void runOnOperation()
override;
255 FunctionOpInterface func = getOperation();
257 AffineMaxOp::getCanonicalizationPatterns(
patterns, func.getContext());
258 AffineMinOp::getCanonicalizationPatterns(
patterns, func.getContext());
259 patterns.add<SimplifyAffineMaxOp, SimplifyAffineMinOp, SimplifyAffineApplyOp>(
263 return signalPassFailure();
static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp)
Simplifies an affine min/max operation by proving there's a lower or upper bound.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
unsigned getNumSymbols() const
unsigned getNumDims() const
unsigned getNumResults() const
MLIRContext * getContext() const
This class represents a frozen set of patterns that can be processed by a pattern applicator.
This class allows control over how the GreedyPatternRewriteDriver works.
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
Operation is the basic unit of execution within MLIR.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
A variable that can be added to the constraint set as a "column".
static llvm::FailureOr< bool > strongCompare(const Variable &lhs, ComparisonOperator cmp, const Variable &rhs)
This function is similar to ValueBoundsConstraintSet::compare, except that it returns false if !...
ComparisonOperator
Comparison operator for ValueBoundsConstraintSet::compare.
This class provides an abstraction over the different types of ranges over Values.
bool simplifyAffineMaxOp(RewriterBase &rewriter, AffineMaxOp op)
This transform tries to simplify the affine max operation op, by finding a common upper bound for a s...
void fullyComposeAffineMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands, bool composeAffineMin=false)
Given an affine map map and its input operands, this method composes into map, maps of AffineApplyOps...
bool simplifyAffineMinOp(RewriterBase &rewriter, AffineMinOp op)
This transform tries to simplify the affine min operation op, by finding a common lower bound for a s...
LogicalResult simplifyAffineMinMaxOps(RewriterBase &rewriter, ArrayRef< Operation * > ops, bool *modified=nullptr)
This transform applies simplifyAffineMinOp and simplifyAffineMaxOp to all the affine....
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig bool * changed
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
const FrozenRewritePatternSet & patterns
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
Creates a simplification pass for affine min/max/apply.
void runOnOperation() override
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.