25 #include "llvm/Support/Debug.h"
27 #define DEBUG_TYPE "mlir-scf-affine-utils"
30 using namespace affine;
31 using namespace presburger;
36 lb = forOp.getLowerBound();
37 ub = forOp.getUpperBound();
38 step = forOp.getStep();
42 for (
unsigned idx = 0; idx < parOp.getNumLoops(); ++idx) {
43 if (parOp.getInductionVars()[idx] == iv) {
44 lb = parOp.getLowerBound()[idx];
45 ub = parOp.getUpperBound()[idx];
46 step = parOp.getStep()[idx];
53 for (int64_t idx = 0; idx < forallOp.getRank(); ++idx) {
54 if (forallOp.getInductionVar(idx) == iv) {
55 lb = forallOp.getMixedLowerBound()[idx];
56 ub = forallOp.getMixedUpperBound()[idx];
57 step = forallOp.getMixedStep()[idx];
66 static FailureOr<AffineApplyOp>
69 RewriterBase::InsertionGuard guard(rewriter);
71 FailureOr<AffineValueMap> simplified =
73 if (failed(simplified))
76 op, simplified->getAffineMap(), simplified->
getOperands());
91 auto lbv = llvm::dyn_cast_if_present<Value>(lb);
94 auto ubv = llvm::dyn_cast_if_present<Value>(ub);
102 cstr.
addBound(BoundType::EQ, symLb, *lbInt);
104 cstr.
addBound(BoundType::EQ, symUb, *ubInt);
114 if (lbInt && ubInt && (*lbInt + *stepInt >= *ubInt)) {
129 ivUb = exprLb + 1 + (*stepInt * ((exprUb - exprLb - 1).floorDiv(*stepInt)));
135 return cstr.
addBound(BoundType::UB, dimIv, map);
156 if (allIvs.contains(operand))
163 if (failed(loopMatcher(operand, lb, ub, step)))
206 constraints.
addBound(BoundType::EQ, 1, *constUb);
208 constraints.
addBound(BoundType::EQ, 2, *constStep);
static FailureOr< AffineApplyOp > canonicalizeMinMaxOp(RewriterBase &rewriter, Operation *op, FlatAffineValueConstraints constraints)
Base type for affine expression.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
This class is a general helper class for creating context-global objects like types,...
AffineExpr getAffineSymbolExpr(unsigned position)
AffineExpr getAffineConstantExpr(int64_t constant)
unsigned appendDimVar(ValueRange vals)
unsigned appendSymbolVar(ValueRange vals)
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
operand_range getOperands()
Returns an iterator on the underlying Value's.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
FlatAffineValueConstraints is an extension of FlatLinearValueConstraints with helper functions for Af...
LogicalResult addBound(presburger::BoundType type, unsigned pos, AffineMap boundMap, ValueRange operands)
Adds a bound for the variable at the specified position with constraints being drawn from the specifi...
unsigned getNumSymbolVars() const
unsigned getNumCols() const
Returns the number of columns in the constraint system.
void addInequality(ArrayRef< DynamicAPInt > inEq)
Adds an inequality (>= 0) from the coefficients specified in inEq.
unsigned getNumDimVars() const
FailureOr< AffineValueMap > simplifyConstrainedMinMaxOp(Operation *op, FlatAffineValueConstraints constraints)
Try to simplify the given affine.min or affine.max op to an affine map with a single result and opera...
ParallelOp getParallelForInductionVarOwner(Value val)
Returns the parallel loop parent of an induction variable.
LogicalResult matchForLikeLoop(Value iv, OpFoldResult &lb, OpFoldResult &ub, OpFoldResult &step)
Match "for loop"-like operations from the SCF dialect.
LogicalResult canonicalizeMinMaxOpInLoop(RewriterBase &rewriter, Operation *op, LoopMatcherFn loopMatcher)
Try to canonicalize the given affine.min/max operation in the context of for loops with a known range...
LogicalResult rewritePeeledMinMaxOp(RewriterBase &rewriter, Operation *op, Value iv, Value ub, Value step, bool insideLoop)
Try to simplify the given affine.min/max operation op after loop peeling.
LogicalResult addLoopRangeConstraints(affine::FlatAffineValueConstraints &cstr, Value iv, OpFoldResult lb, OpFoldResult ub, OpFoldResult step)
Populate the given constraint set with induction variable constraints of a "for" loop with the given ...
ForOp getForInductionVarOwner(Value val)
Returns the loop parent of an induction variable.
ForallOp getForallOpThreadIndexOwner(Value val)
Returns the ForallOp parent of an thread index variable.
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.