15#include "llvm/ADT/APSInt.h"
16#include "llvm/ADT/SmallVectorExtras.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/DebugLog.h"
22#define DEBUG_TYPE "value-bounds-op-interface"
29#include "mlir/Interfaces/ValueBoundsOpInterface.cpp.inc"
33 if (
auto bbArg = dyn_cast<BlockArgument>(value))
41 : mixedOffsets(offsets), mixedSizes(sizes), mixedStrides(strides) {
42 assert(offsets.size() == sizes.size() &&
43 "expected same number of offsets, sizes, strides");
44 assert(offsets.size() == strides.size() &&
45 "expected same number of offsets, sizes, strides");
50 : mixedOffsets(offsets), mixedSizes(sizes) {
51 assert(offsets.size() == sizes.size() &&
52 "expected same number of offsets and sizes");
57 mixedStrides.append(offsets.size(),
Builder(ctx).getIndexAttr(1));
67 if (
auto val = llvm::dyn_cast_if_present<Value>(ofr)) {
70 return intVal.getSExtValue();
74 Attribute attr = llvm::dyn_cast_if_present<Attribute>(ofr);
75 if (
auto intAttr = dyn_cast_or_null<IntegerAttr>(attr))
76 return intAttr.getValue().getSExtValue();
90 std::optional<int64_t> dim) {
93 assert(!dim &&
"expected no dim for index-typed values");
95 b.getAffineConstantExpr(*constInt));
98 Value value = cast<Value>(ofr);
101 assert(isa<ShapedType>(value.
getType()) &&
"expected shaped type");
107 b.getAffineSymbolExpr(0));
108 mapOperands.emplace_back(value, dim);
113 assert(map.getNumResults() == 1 &&
"expected single result");
120 for (
int64_t i = 0, e = map.getNumDims(); i < e; ++i)
121 dimReplacements.push_back(
b.getAffineSymbolExpr(i));
122 for (
int64_t i = 0, e = map.getNumSymbols(); i < e; ++i)
123 symReplacements.push_back(
b.getAffineSymbolExpr(i + map.getNumDims()));
125 dimReplacements, symReplacements, 0,
126 map.getNumSymbols() + map.getNumDims());
130 for (
auto [
index, var] : llvm::enumerate(mapOperands)) {
131 assert(var.map.getNumResults() == 1 &&
"expected single result");
132 assert(var.map.getNumDims() == 0 &&
"expected only symbols");
134 for (
auto valueDim : var.mapOperands) {
135 auto *it = llvm::find(this->mapOperands, valueDim);
136 if (it != this->mapOperands.end()) {
138 symReplacements.push_back(
b.getAffineSymbolExpr(
139 std::distance(this->mapOperands.begin(), it)));
142 symReplacements.push_back(
143 b.getAffineSymbolExpr(this->mapOperands.size()));
144 this->mapOperands.push_back(valueDim);
147 replacements[
b.getAffineSymbolExpr(
index)] =
148 var.map.getResult(0).replaceSymbols(symReplacements);
150 this->map = tmpMap.
replace(replacements, 0,
151 this->mapOperands.size());
172 assert(!dim.has_value() &&
"invalid dim value");
173 }
else if (
auto shapedType = dyn_cast<ShapedType>(value.
getType())) {
174 assert(*dim >= 0 &&
"invalid dim value");
175 if (shapedType.hasRank())
176 assert(*dim < shapedType.getRank() &&
"invalid dim value");
178 llvm_unreachable(
"unsupported type");
190 LogicalResult status =
cstr.addBound(
196 if (failed(status)) {
202 LDBG() <<
"Failed to add bound: " << expr <<
"\n";
207 std::optional<int64_t> dim) {
216 std::optional<int64_t> constSize = std::nullopt;
217 auto shapedType = dyn_cast<ShapedType>(value.
getType());
219 if (shapedType.hasRank() && !shapedType.isDynamicDim(*dim))
220 constSize = shapedType.getDimSize(*dim);
222 constSize = *constInt;
233 return builder.getAffineConstantExpr(*constSize);
243 bound(value)[*dim] == *constSize;
245 bound(value) == *constSize;
246 return builder.getAffineConstantExpr(*constSize);
256 if (
Value value = llvm::dyn_cast_if_present<Value>(ofr))
257 return getExpr(value, std::nullopt);
259 assert(constInt.has_value() &&
"expected Integer constant");
260 return builder.getAffineConstantExpr(*constInt);
264 return builder.getAffineConstantExpr(constant);
268 std::optional<int64_t> dim,
269 bool isSymbol,
bool addToWorklist) {
276 int64_t pos = isSymbol ?
cstr.appendVar(VarKind::Symbol)
277 :
cstr.appendVar(VarKind::SetDim);
278 LDBG() <<
"Inserting constraint set column " << pos <<
" for: " << value
295 (!isa<BlockArgument>(value) ||
296 cast<BlockArgument>(value).getOwner()->isEntryBlock())) {
297 LDBG() <<
"Push to worklist: " << value
306 int64_t pos = isSymbol ?
cstr.appendVar(VarKind::Symbol)
307 :
cstr.appendVar(VarKind::SetDim);
308 LDBG() <<
"Inserting anonymous constraint set column " << pos;
320 assert(map.
getNumResults() == 1 &&
"expected affine map with one result");
326 auto mapper = [&](std::pair<Value, std::optional<int64_t>> v) {
327 return getExpr(v.first, v.second);
341 return insert(var.map, var.mapOperands, isSymbol);
345 std::optional<int64_t> dim)
const {
349 LDBG() <<
"Getting pos for: " << value
359 assert(pos >= 0 && pos <
cstr.getNumDimAndSymbolVars() &&
"invalid position");
360 return pos <
cstr.getNumDimVars()
361 ?
builder.getAffineDimExpr(pos)
362 :
builder.getAffineSymbolExpr(pos -
cstr.getNumDimVars());
366 std::optional<int64_t> dim)
const {
373 LDBG() <<
"Processing value bounds worklist...";
378 "did not expect std::nullopt on worklist");
380 Value value = valueDim.first;
385 auto shapedType = cast<ShapedType>(value.
getType());
386 if (shapedType.hasRank() && !shapedType.isDynamicDim(dim)) {
387 bound(value)[dim] ==
getExpr(shapedType.getDimSize(dim));
393 auto maybeDim = dim ==
kIndexValue ? std::nullopt : std::make_optional(dim);
395 LDBG() <<
"Stop condition met for: " << value <<
" (dim: " << maybeDim
404 LDBG() <<
"Query value bounds for: " << value
408 valueBoundsOp.populateBoundsForIndexValue(value, *
this);
410 valueBoundsOp.populateBoundsForShapedValueDim(value, dim, *
this);
414 LDBG() <<
"--> ValueBoundsOpInterface not implemented";
419 auto dstOp = value.
getDefiningOp<DestinationStyleOpInterface>();
422 Value tiedOperand = dstOp.getTiedOpOperand(cast<OpResult>(value))->get();
430 cstr.projectOut(pos);
434 assert(erased &&
"inconsistent reverse mapping");
459 std::optional<int64_t> except) {
476 int64_t ubAdjustment = closedUB ? 0 : 1;
484 assert(pos == 0 &&
"expected first column");
485 cstr.processWorklist();
491 p.second ==
kIndexValue ? std::nullopt : std::make_optional(p.second);
494 cstr.projectOutAnonymous(pos);
498 cstr.cstr.getSliceBounds(pos, 1, ctx, &lb, &
ub,
504 if ((type != BoundType::LB) &&
505 (
ub.empty() || !
ub[0] ||
ub[0].getNumResults() == 0))
508 if ((type != BoundType::UB) &&
509 (lb.empty() || !lb[0] || lb[0].getNumResults() == 0))
513 if (type != BoundType::LB)
514 assert(
ub.size() == 1 &&
ub[0].getNumResults() == 1 &&
515 "multiple bounds not supported");
516 if (type != BoundType::UB)
517 assert(lb.size() == 1 && lb[0].getNumResults() == 1 &&
518 "multiple bounds not supported");
521 if (type == BoundType::EQ &&
ub[0] != lb[0])
525 if (type == BoundType::EQ || type == BoundType::LB) {
530 ub[0].getResult(0) + ubAdjustment);
534 assert(
cstr.cstr.getNumDimAndSymbolVars() ==
cstr.positionToValueDim.size() &&
535 "inconsistent mapping state");
537 int64_t numDims = 0, numSymbols = 0;
538 for (
int64_t i = 0; i <
cstr.cstr.getNumDimAndSymbolVars(); ++i) {
545 bool isDim = i <
cstr.cstr.getNumDimVars();
547 if (
bound.isFunctionOfDim(i))
550 if (
bound.isFunctionOfSymbol(i -
cstr.cstr.getNumDimVars()))
557 replacementDims.push_back(
b.getAffineConstantExpr(0));
559 replacementSymbols.push_back(
b.getAffineConstantExpr(0));
565 replacementDims.push_back(
b.getAffineDimExpr(numDims++));
567 replacementSymbols.push_back(
b.getAffineSymbolExpr(numSymbols++));
570 assert(
cstr.positionToValueDim[i].has_value() &&
571 "cannot build affine map in terms of anonymous column");
573 Value value = valueDim.first;
579 mapOperands.push_back(std::make_pair(value, std::nullopt));
583 assert(cast<ShapedType>(value.
getType()).isDynamicDim(dim) &&
584 "expected dynamic dim");
585 mapOperands.push_back(std::make_pair(value, dim));
588 resultMap =
bound.replaceDimsAndSymbols(replacementDims, replacementSymbols,
589 numDims, numSymbols);
597 resultMap, mapOperands, type, var,
599 return llvm::is_contained(dependencies, std::make_pair(v, d));
610 auto isIndependent = [&](
Value v) {
616 if (!visited.insert(next).second)
618 if (llvm::is_contained(independencies, next))
631 resultMap, mapOperands, type, var,
633 return isIndependent(v);
644 auto defaultStopCondition = [&](
Value v, std::optional<int64_t> dim,
646 return cstr.cstr.getConstantBound64(type, pos).has_value();
651 pos =
cstr.populateConstraints(var.map, var.mapOperands);
652 assert(pos == 0 &&
"expected `map` is the first column");
655 int64_t ubAdjustment = closedUB ? 0 : 1;
656 if (
auto bound =
cstr.cstr.getConstantBound64(type, pos))
657 return type == BoundType::UB ? *
bound + ubAdjustment : *
bound;
662 std::optional<int64_t> dim) {
686 std::optional<int64_t> dim1,
687 std::optional<int64_t> dim2) {
695 b.getAffineDimExpr(0) -
b.getAffineDimExpr(1));
697 Variable(map, {{value1, dim1}, {value2, dim2}}));
712 if (
cstr.isEmpty()) {
713 LDBG() <<
"cannot compare value/dims: constraint system is already empty";
726 if (cmp ==
LT || cmp ==
LE) {
729 }
else if (cmp ==
GT || cmp ==
GE) {
733 llvm_unreachable(
"unsupported comparison operator");
735 if (cmp ==
LE || cmp ==
GE)
736 eq[
cstr.getNumCols() - 1] -= 1;
741 cstr.addInequality(eq);
742 bool isEmpty =
cstr.isEmpty();
743 cstr.removeInequality(ineqPos);
767 std::optional<bool> le =
773 std::optional<bool> ge =
782 llvm_unreachable(
"invalid comparison operator");
796 int64_t lhsPos = -1, rhsPos = -1;
800 if (
size_t(lhsPos) >=
cstr.positionToValueDim.size() ||
801 size_t(rhsPos) >=
cstr.positionToValueDim.size())
804 return cstr.comparePos(lhsPos, cmp, rhsPos);
807 lhsPos =
cstr.populateConstraints(
lhs.map,
lhs.mapOperands);
808 rhsPos =
cstr.populateConstraints(
rhs.map,
rhs.mapOperands);
809 return cstr.comparePos(lhsPos, cmp, rhsPos);
815 int64_t lhsPos = -1, rhsPos = -1;
819 if (
size_t(lhsPos) >=
cstr.positionToValueDim.size() ||
820 size_t(rhsPos) >=
cstr.positionToValueDim.size())
823 FailureOr<bool> ordered =
cstr.strongComparePos(lhsPos, cmp, rhsPos);
824 return failed(ordered);
827 lhsPos =
cstr.populateConstraints(
lhs.map,
lhs.mapOperands);
828 rhsPos =
cstr.populateConstraints(
rhs.map,
rhs.mapOperands);
829 return cstr.strongComparePos(lhsPos, cmp, rhsPos);
841 "expected slices of same rank");
843 "expected slices of same rank");
845 "expected slices of same rank");
848 bool foundUnknownBound =
false;
852 b.getAffineSymbolExpr(0) +
853 b.getAffineSymbolExpr(1) *
b.getAffineSymbolExpr(2) -
854 b.getAffineSymbolExpr(3));
868 foundUnknownBound |= failed(constBound);
869 if (succeeded(constBound) && *constBound <= 0)
885 foundUnknownBound |= failed(constBound);
886 if (succeeded(constBound) && *constBound <= 0)
893 if (foundUnknownBound)
905 "expected slices of same rank");
907 "expected slices of same rank");
909 "expected slices of same rank");
915 for (
auto [offset1, offset2] :
917 FailureOr<bool> equal =
areEqual(offset1, offset2);
923 for (
auto [size1, size2] :
925 FailureOr<bool> equal =
areEqual(size1, size2);
931 for (
auto [stride1, stride2] :
933 FailureOr<bool> equal =
areEqual(stride1, stride2);
943 llvm::errs() <<
"==========\nColumns:\n";
944 llvm::errs() <<
"(column\tdim\tvalue)\n";
946 llvm::errs() <<
" " <<
index <<
"\t";
949 llvm::errs() <<
"n/a\t";
951 llvm::errs() << valueDim->second <<
"\t";
955 llvm::errs() <<
"(result " <<
result.getResultNumber() <<
")";
957 llvm::errs() <<
"(bbarg "
958 << cast<BlockArgument>(valueDim->first).getArgNumber()
961 llvm::errs() <<
"\n";
963 llvm::errs() <<
"n/a\tn/a\n";
966 llvm::errs() <<
"\nConstraint set:\n";
968 llvm::errs() <<
"==========\n";
973 assert(!this->dim.has_value() &&
"dim was already set");
985 cstr.addBound(BoundType::UB, cstr.getPos(value, this->dim), expr);
1000 cstr.addBound(BoundType::LB, cstr.getPos(value, this->dim), expr);
1007 cstr.addBound(BoundType::EQ, cstr.getPos(value, this->dim), expr);
static Operation * getOwnerOfValue(Value value)
static void assertValidValueDim(Value value, std::optional< int64_t > dim)
Base type for affine expression.
AffineExpr replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements) const
This method substitutes any uses of dimensions and symbols (e.g.
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 getNumDims() const
unsigned getNumResults() const
AffineMap replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements, unsigned numResultDims, unsigned numResultSyms) const
This method substitutes any uses of dimensions and symbols (e.g.
AffineExpr getResult(unsigned idx) const
AffineMap replace(AffineExpr expr, AffineExpr replacement, unsigned numResultDims, unsigned numResultSyms) const
Sparse replace method.
Attributes are known-constant values of operations.
This class is a general helper class for creating context-global objects like types,...
A hyperrectangular slice, represented as a list of offsets, sizes and strides.
HyperrectangularSlice(ArrayRef< OpFoldResult > offsets, ArrayRef< OpFoldResult > sizes, ArrayRef< OpFoldResult > strides)
ArrayRef< OpFoldResult > getMixedStrides() const
ArrayRef< OpFoldResult > getMixedSizes() const
ArrayRef< OpFoldResult > getMixedOffsets() const
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a single result from folding an operation.
MLIRContext * getContext() const
This is a value defined by a result of an operation.
Operation is the basic unit of execution within MLIR.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
OperationName getName()
The name of an operation is the key identifier for it.
operand_range getOperands()
Returns an iterator on the underlying Value's.
Helper class that builds a bound for a shaped value dimension or index-typed value.
void operator>(AffineExpr expr)
BoundBuilder & operator[](int64_t dim)
Specify a dimension, assuming that the underlying value is a shaped value.
void operator<=(AffineExpr expr)
void operator==(AffineExpr expr)
void operator<(AffineExpr expr)
void operator>=(AffineExpr expr)
A variable that can be added to the constraint set as a "column".
Variable(OpFoldResult ofr)
Construct a variable for an index-typed attribute or SSA value.
MLIRContext * getContext() const
static bool compare(const Variable &lhs, ComparisonOperator cmp, const Variable &rhs)
Return "true" if "lhs cmp rhs" was proven to hold.
static FailureOr< bool > areEqual(const Variable &var1, const Variable &var2)
Compute whether the given variables are equal.
DenseMap< ValueDim, int64_t > valueDimToPosition
Reverse mapping of values/shape dimensions to columns.
void processWorklist()
Iteratively process all elements on the worklist until an index-typed value or shaped value meets sto...
bool addConservativeSemiAffineBounds
Should conservative bounds be added for semi-affine expressions.
AffineExpr getExpr(Value value, std::optional< int64_t > dim=std::nullopt)
Return an expression that represents the given index-typed value or shaped value dimension.
static LogicalResult computeIndependentBound(AffineMap &resultMap, ValueDimList &mapOperands, presburger::BoundType type, const Variable &var, ValueRange independencies, bool closedUB=false)
Compute a bound in that is independent of all values in independencies.
static FailureOr< bool > areEquivalentSlices(MLIRContext *ctx, const HyperrectangularSlice &slice1, const HyperrectangularSlice &slice2)
Return "true" if the given slices are guaranteed to be equivalent.
void projectOut(int64_t pos)
Project out the given column in the constraint set.
std::function< bool( Value, std::optional< int64_t >, ValueBoundsConstraintSet &cstr)> StopConditionFn
The stop condition when traversing the backward slice of a shaped value/ index-type value.
ValueBoundsConstraintSet(MLIRContext *ctx, const StopConditionFn &stopCondition, bool addConservativeSemiAffineBounds=false)
static FailureOr< int64_t > computeConstantDelta(Value value1, Value value2, std::optional< int64_t > dim1=std::nullopt, std::optional< int64_t > dim2=std::nullopt)
Compute a constant delta between the given two values.
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 !...
void addBound(presburger::BoundType type, int64_t pos, AffineExpr expr)
Bound the given column in the underlying constraint set by the given expression.
StopConditionFn stopCondition
The current stop condition function.
ComparisonOperator
Comparison operator for ValueBoundsConstraintSet::compare.
BoundBuilder bound(Value value)
Add a bound for the given index-typed value or shaped value.
static LogicalResult computeBound(AffineMap &resultMap, ValueDimList &mapOperands, presburger::BoundType type, const Variable &var, StopConditionFn stopCondition, bool closedUB=false)
Compute a bound for the given variable.
int64_t getPos(Value value, std::optional< int64_t > dim=std::nullopt) const
Return the column position of the given value/dimension.
int64_t insert(Value value, std::optional< int64_t > dim, bool isSymbol=true, bool addToWorklist=true)
Insert a value/dimension into the constraint set.
bool comparePos(int64_t lhsPos, ComparisonOperator cmp, int64_t rhsPos)
Return "true" if, based on the current state of the constraint system, "lhs cmp rhs" was proven to ho...
SmallVector< std::optional< ValueDim >, 4 > positionToValueDim
Mapping of columns to values/shape dimensions.
void dump() const
Debugging only: Dump the constraint set and the column-to-value/dim mapping to llvm::errs.
std::queue< int64_t > worklist
Worklist of values/shape dimensions that have not been processed yet.
FlatLinearConstraints cstr
Constraint system of equalities and inequalities.
bool isMapped(Value value, std::optional< int64_t > dim=std::nullopt) const
Return "true" if the given value/dim is mapped (i.e., has a corresponding column in the constraint sy...
llvm::FailureOr< bool > strongComparePos(int64_t lhsPos, ComparisonOperator cmp, int64_t rhsPos)
Return "true" if, based on the current state of the constraint system, "lhs cmp rhs" was proven to ho...
AffineExpr getPosExpr(int64_t pos)
Return an affine expression that represents column pos in the constraint set.
void projectOutAnonymous(std::optional< int64_t > except=std::nullopt)
static FailureOr< bool > areOverlappingSlices(MLIRContext *ctx, const HyperrectangularSlice &slice1, const HyperrectangularSlice &slice2)
Return "true" if the given slices are guaranteed to be overlapping.
std::pair< Value, int64_t > ValueDim
An index-typed value or the dimension of a shaped-type value.
void populateConstraints(Value value, std::optional< int64_t > dim)
Traverse the IR starting from the given value/dim and populate constraints as long as the stop condit...
Builder builder
Builder for constructing affine expressions.
bool populateAndCompare(const Variable &lhs, ComparisonOperator cmp, const Variable &rhs)
Populate constraints for lhs/rhs (until the stop condition is met).
static FailureOr< int64_t > computeConstantBound(presburger::BoundType type, const Variable &var, const StopConditionFn &stopCondition=nullptr, bool closedUB=false)
Compute a constant bound for the given variable.
static constexpr int64_t kIndexValue
Dimension identifier to indicate a value is index-typed.
static LogicalResult computeDependentBound(AffineMap &resultMap, ValueDimList &mapOperands, presburger::BoundType type, const Variable &var, ValueDimList dependencies, bool closedUB=false)
Compute a bound in terms of the values/dimensions in dependencies.
This class provides an abstraction over the different types of ranges over Values.
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.
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
BoundType
The type of bound: equal, lower bound or upper bound.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...
SmallVector< std::pair< Value, std::optional< int64_t > >, 2 > ValueDimList
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
AffineMap foldAttributesIntoMap(Builder &b, AffineMap map, ArrayRef< OpFoldResult > operands, SmallVector< Value > &remainingValues)
Fold all attributes among the given operands into the affine map.
llvm::function_ref< Fn > function_ref