21 #include "llvm/Support/Debug.h" 23 #define DEBUG_TYPE "mlir-scf-affine-utils" 26 using namespace presburger;
30 target = llvm::to_vector<4>(llvm::map_range(source, [](
Optional<Value> val) {
31 return val.hasValue() ? *val :
Value();
51 for (
unsigned i = syms.size(); i < newSyms.size(); ++i)
53 return constraints.
addBound(type, pos, alignedMap);
60 newResults.push_back(r + val);
107 RewriterBase::InsertionGuard guard(rewriter);
113 unsigned resultDimStart = constraints.
appendDimVar(numResults);
128 AffineMap sliceBound = isMin ? opUb[0] : opLb[0];
158 for (
unsigned i = resultDimStart; i < resultDimStart + numResults; ++i) {
166 map.
getSubMap({i - resultDimStart}), operands)))
174 ineq[dimOpBound] = isMin ? 1 : -1;
175 ineq[i] = isMin ? -1 : 1;
188 for (int64_t i = 0, e = constraints.
getNumVars(); i < e; ++i) {
213 auto lbv = lb.dyn_cast<
Value>();
216 auto ubv = ub.dyn_cast<
Value>();
236 if (lbInt && ubInt && (*lbInt + *stepInt >= *ubInt)) {
249 ivUb = exprLb + 1 + (*stepInt * ((exprUb - exprLb - 1).
floorDiv(*stepInt)));
275 for (
Value operand : operands) {
277 if (llvm::is_contained(allIvs, operand))
284 if (
failed(loopMatcher(operand, lb, ub, step)))
324 Value step,
bool insideLoop) {
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
static LogicalResult alignAndAddBound(FlatAffineValueConstraints &constraints, IntegerPolyhedron::BoundType type, unsigned pos, AffineMap map, ValueRange operands)
Bound an identifier pos in a given FlatAffineValueConstraints with constraints drawn from an affine m...
MLIRContext * getContext() const
unsigned appendSymbolVar(ValueRange vals)
Operation is a basic unit of execution within MLIR.
AffineMap getSubMap(ArrayRef< unsigned > resultPos) const
Returns the map consisting of the resultPos subset.
unsigned getNumSymbols() const
unsigned getNumDims() const
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.
LogicalResult canonicalizeMinMaxOpInLoop(RewriterBase &rewriter, Operation *op, AffineMap map, ValueRange operands, bool isMin, LoopMatcherFn loopMatcher)
Try to canonicalize an min/max operations in the context of for loops with a known range...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
AffineMap alignAffineMapWithValues(AffineMap map, ValueRange operands, ValueRange dims, ValueRange syms, SmallVector< Value > *newSyms=nullptr)
Re-indexes the dimensions and symbols of an affine map with given operands values to align with dims ...
unsigned appendDimVar(ValueRange vals)
Append variables of the specified kind after the last variable of that kind.
AffineMap shiftDims(unsigned shift, unsigned offset=0) const
Replace dims[offset ...
BoundType
The type of bound: equal, lower bound or upper bound.
static AffineMap addConstToResults(AffineMap map, int64_t val)
Add val to each result of map.
void addInequality(ArrayRef< int64_t > inEq)
Adds an inequality (>= 0) from the coefficients specified in inEq.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This class represents an efficient way to signal success or failure.
ArrayRef< Optional< Value > > getMaybeValues() const
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
int64_t floorDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR's floordiv operation on constants.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
static LogicalResult addLoopRangeConstraints(FlatAffineValueConstraints &constraints, Value iv, OpFoldResult lb, OpFoldResult ub, OpFoldResult step, RewriterBase &rewriter)
unsigned getNumVars() const
unsigned getNumSymbolVars() const
Base type for affine expression.
void canonicalizeMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands)
Modifies both map and operands in-place so as to:
unsigned getNumResults() const
Location getLoc()
The source location the operation was defined or derived from.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued...
ArrayRef< AffineExpr > getResults() const
unsigned getNumDimVars() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
AffineExpr getAffineConstantExpr(int64_t constant)
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
FlatAffineValueConstraints represents an extension of IntegerPolyhedron where each non-local variable...
Specialization of arith.constant op that returns an integer of index type.
bool isEmpty() const
Checks for emptiness by performing variable elimination on all variables, running the GCD test on eac...
unsigned getNumCols() const
Returns the number of columns in the constraint system.
AffineExpr getAffineDimExpr(unsigned position)
LogicalResult rewritePeeledMinMaxOp(RewriterBase &rewriter, Operation *op, AffineMap map, ValueRange operands, bool isMin, Value iv, Value ub, Value step, bool insideLoop)
Try to simplify a min/max operation op after loop peeling.
static void unpackOptionalValues(ArrayRef< Optional< Value >> source, SmallVector< Value > &target)
MLIRContext * getContext() const
static LogicalResult canonicalizeMinMaxOp(RewriterBase &rewriter, Operation *op, AffineMap map, ValueRange operands, bool isMin, FlatAffineValueConstraints constraints)
This function tries to canonicalize min/max operations by proving that their value is bounded by the ...
Optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
This class provides an abstraction over the different types of ranges over Values.
Optional< int64_t > getConstantBound(BoundType type, unsigned pos) const
Returns the constant bound for the pos^th variable if there is one; None otherwise.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
void getSliceBounds(unsigned offset, unsigned num, MLIRContext *context, SmallVectorImpl< AffineMap > *lbMaps, SmallVectorImpl< AffineMap > *ubMaps, bool getClosedUB=false)
Computes the lower and upper bounds of the first num dimensional variables (starting at offset) as an...
LogicalResult addBound(BoundType type, unsigned pos, AffineMap boundMap, bool isClosedBound)
Adds a bound for the variable at the specified position with constraints being drawn from the specifi...