20struct AffineApplyOpInterface
21 :
public ValueBoundsOpInterface::ExternalModel<AffineApplyOpInterface,
23 void populateBoundsForIndexValue(Operation *op, Value value,
24 ValueBoundsConstraintSet &cstr)
const {
25 auto applyOp = cast<AffineApplyOp>(op);
26 assert(value == applyOp.getResult() &&
"invalid value");
27 assert(applyOp.getAffineMap().getNumResults() == 1 &&
28 "expected single result");
33 AffineMap map = applyOp.getAffineMap();
34 SmallVector<Value> operands = llvm::to_vector(applyOp.getOperands());
39 SmallVector<AffineExpr> dimReplacements, symReplacements;
40 for (int64_t i = 0, e = map.
getNumDims(); i < e; ++i)
41 dimReplacements.push_back(cstr.
getExpr(operands[i]));
45 symReplacements.push_back(cstr.
getExpr(operands[i]));
48 cstr.
bound(value) == bound;
52struct AffineMinOpInterface
53 :
public ValueBoundsOpInterface::ExternalModel<AffineMinOpInterface,
55 void populateBoundsForIndexValue(Operation *op, Value value,
56 ValueBoundsConstraintSet &cstr)
const {
57 auto minOp = cast<AffineMinOp>(op);
58 assert(value == minOp.getResult() &&
"invalid value");
61 for (AffineExpr expr : minOp.getAffineMap().getResults()) {
62 SmallVector<AffineExpr> dimReplacements = llvm::to_vector(llvm::map_range(
63 minOp.getDimOperands(), [&](Value v) { return cstr.getExpr(v); }));
64 SmallVector<AffineExpr> symReplacements = llvm::to_vector(llvm::map_range(
65 minOp.getSymbolOperands(), [&](Value v) { return cstr.getExpr(v); }));
68 cstr.
bound(value) <= bound;
73struct AffineMaxOpInterface
74 :
public ValueBoundsOpInterface::ExternalModel<AffineMaxOpInterface,
76 void populateBoundsForIndexValue(Operation *op, Value value,
77 ValueBoundsConstraintSet &cstr)
const {
78 auto maxOp = cast<AffineMaxOp>(op);
79 assert(value == maxOp.getResult() &&
"invalid value");
82 for (AffineExpr expr : maxOp.getAffineMap().getResults()) {
83 SmallVector<AffineExpr> dimReplacements = llvm::to_vector(llvm::map_range(
84 maxOp.getDimOperands(), [&](Value v) { return cstr.getExpr(v); }));
85 SmallVector<AffineExpr> symReplacements = llvm::to_vector(llvm::map_range(
86 maxOp.getSymbolOperands(), [&](Value v) { return cstr.getExpr(v); }));
89 cstr.
bound(value) >= bound;
94struct AffineDelinearizeIndexOpInterface
95 :
public ValueBoundsOpInterface::ExternalModel<
96 AffineDelinearizeIndexOpInterface, AffineDelinearizeIndexOp> {
97 void populateBoundsForIndexValue(Operation *rawOp, Value value,
98 ValueBoundsConstraintSet &cstr)
const {
99 auto op = cast<AffineDelinearizeIndexOp>(rawOp);
100 auto result = cast<OpResult>(value);
101 assert(
result.getOwner() == rawOp &&
102 "bounded value isn't a result of this delinearize_index");
103 unsigned resIdx =
result.getResultNumber();
105 AffineExpr linearIdx = cstr.
getExpr(op.getLinearIndex());
107 SmallVector<OpFoldResult> basis = op.getPaddedBasis();
108 AffineExpr divisor = cstr.
getExpr(1);
109 for (OpFoldResult basisElem : llvm::drop_begin(basis, resIdx + 1))
110 divisor = divisor * cstr.
getExpr(basisElem);
114 if (!basis.front().isNull())
118 AffineExpr thisBasis = cstr.
getExpr(basis[resIdx]);
119 cstr.
bound(value) == (linearIdx % (thisBasis * divisor)).floorDiv(divisor);
123struct AffineLinearizeIndexOpInterface
124 :
public ValueBoundsOpInterface::ExternalModel<
125 AffineLinearizeIndexOpInterface, AffineLinearizeIndexOp> {
126 void populateBoundsForIndexValue(Operation *rawOp, Value value,
127 ValueBoundsConstraintSet &cstr)
const {
128 auto op = cast<AffineLinearizeIndexOp>(rawOp);
130 "value isn't the result of this linearize");
132 AffineExpr bound = cstr.
getExpr(0);
133 AffineExpr stride = cstr.
getExpr(1);
134 SmallVector<OpFoldResult> basis = op.getPaddedBasis();
135 OperandRange multiIndex = op.getMultiIndex();
136 unsigned numArgs = multiIndex.size();
137 for (
auto [revArgNum, length] : llvm::enumerate(llvm::reverse(basis))) {
138 unsigned argNum = numArgs - (revArgNum + 1);
142 bound = bound + cstr.
getExpr(indexAsFoldRes) * stride;
143 stride = stride * cstr.
getExpr(length);
145 bound = bound + cstr.
getExpr(op.getMultiIndex().front()) * stride;
146 cstr.
bound(value) == bound;
147 if (op.getDisjoint() && !basis.front().isNull()) {
148 cstr.
bound(value) < stride *cstr.
getExpr(basis.front());
158 AffineApplyOp::attachInterface<AffineApplyOpInterface>(*ctx);
159 AffineMaxOp::attachInterface<AffineMaxOpInterface>(*ctx);
160 AffineMinOp::attachInterface<AffineMinOpInterface>(*ctx);
161 AffineDelinearizeIndexOp::attachInterface<
162 AffineDelinearizeIndexOpInterface>(*ctx);
163 AffineLinearizeIndexOp::attachInterface<AffineLinearizeIndexOpInterface>(
177 b.getAffineDimExpr(0) -
b.getAffineDimExpr(1));
182 mapOperands.push_back(value1);
183 mapOperands.push_back(value2);
AffineExpr replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements) const
This method substitutes any uses of dimensions and symbols (e.g.
AffineExpr floorDiv(uint64_t v) const
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
AffineExpr getResult(unsigned idx) const
This class is a general helper class for creating context-global objects like types,...
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
A variable that can be added to the constraint set as a "column".
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.
BoundBuilder bound(Value value)
Add a bound for the given index-typed value or shaped value.
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.
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.
void registerValueBoundsOpInterfaceExternalModels(DialectRegistry ®istry)
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...
FailureOr< int64_t > fullyComposeAndComputeConstantDelta(Value value1, Value value2)
Compute a constant delta of the given two values.
Include the generated interface declarations.
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.