MLIR 23.0.0git
ValueBoundsOpInterfaceImpl.cpp
Go to the documentation of this file.
1//===- ValueBoundsOpInterfaceImpl.cpp - Impl. of ValueBoundsOpInterface ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
13#include "llvm/ADT/SmallVectorExtras.h"
14
15using namespace mlir;
16using namespace mlir::affine;
17
18namespace mlir {
19namespace {
20
21struct AffineApplyOpInterface
22 : public ValueBoundsOpInterface::ExternalModel<AffineApplyOpInterface,
23 AffineApplyOp> {
24 void populateBoundsForIndexValue(Operation *op, Value value,
25 ValueBoundsConstraintSet &cstr) const {
26 auto applyOp = cast<AffineApplyOp>(op);
27 assert(value == applyOp.getResult() && "invalid value");
28 assert(applyOp.getAffineMap().getNumResults() == 1 &&
29 "expected single result");
30
31 // Fully compose this affine.apply with other ops because the folding logic
32 // can see opportunities for simplifying the affine map that
33 // `FlatLinearConstraints` can currently not see.
34 AffineMap map = applyOp.getAffineMap();
35 SmallVector<Value> operands = llvm::to_vector(applyOp.getOperands());
36 fullyComposeAffineMapAndOperands(&map, &operands);
37
38 // Align affine map result with dims/symbols in the constraint set.
39 AffineExpr expr = map.getResult(0);
40 SmallVector<AffineExpr> dimReplacements, symReplacements;
41 for (int64_t i = 0, e = map.getNumDims(); i < e; ++i)
42 dimReplacements.push_back(cstr.getExpr(operands[i]));
43 for (int64_t i = map.getNumDims(),
44 e = map.getNumDims() + map.getNumSymbols();
45 i < e; ++i)
46 symReplacements.push_back(cstr.getExpr(operands[i]));
47 AffineExpr bound =
48 expr.replaceDimsAndSymbols(dimReplacements, symReplacements);
49 cstr.bound(value) == bound;
50 }
51};
52
53struct AffineMinOpInterface
54 : public ValueBoundsOpInterface::ExternalModel<AffineMinOpInterface,
55 AffineMinOp> {
56 void populateBoundsForIndexValue(Operation *op, Value value,
57 ValueBoundsConstraintSet &cstr) const {
58 auto minOp = cast<AffineMinOp>(op);
59 assert(value == minOp.getResult() && "invalid value");
60
61 // Align affine map results with dims/symbols in the constraint set.
62 for (AffineExpr expr : minOp.getAffineMap().getResults()) {
63 SmallVector<AffineExpr> dimReplacements = llvm::map_to_vector(
64 minOp.getDimOperands(), [&](Value v) { return cstr.getExpr(v); });
65 SmallVector<AffineExpr> symReplacements = llvm::map_to_vector(
66 minOp.getSymbolOperands(), [&](Value v) { return cstr.getExpr(v); });
67 AffineExpr bound =
68 expr.replaceDimsAndSymbols(dimReplacements, symReplacements);
69 cstr.bound(value) <= bound;
70 }
71 };
72};
73
74struct AffineMaxOpInterface
75 : public ValueBoundsOpInterface::ExternalModel<AffineMaxOpInterface,
76 AffineMaxOp> {
77 void populateBoundsForIndexValue(Operation *op, Value value,
78 ValueBoundsConstraintSet &cstr) const {
79 auto maxOp = cast<AffineMaxOp>(op);
80 assert(value == maxOp.getResult() && "invalid value");
81
82 // Align affine map results with dims/symbols in the constraint set.
83 for (AffineExpr expr : maxOp.getAffineMap().getResults()) {
84 SmallVector<AffineExpr> dimReplacements = llvm::map_to_vector(
85 maxOp.getDimOperands(), [&](Value v) { return cstr.getExpr(v); });
86 SmallVector<AffineExpr> symReplacements = llvm::map_to_vector(
87 maxOp.getSymbolOperands(), [&](Value v) { return cstr.getExpr(v); });
88 AffineExpr bound =
89 expr.replaceDimsAndSymbols(dimReplacements, symReplacements);
90 cstr.bound(value) >= bound;
91 }
92 };
93};
94
95struct AffineDelinearizeIndexOpInterface
96 : public ValueBoundsOpInterface::ExternalModel<
97 AffineDelinearizeIndexOpInterface, AffineDelinearizeIndexOp> {
98 void populateBoundsForIndexValue(Operation *rawOp, Value value,
99 ValueBoundsConstraintSet &cstr) const {
100 auto op = cast<AffineDelinearizeIndexOp>(rawOp);
101 auto result = cast<OpResult>(value);
102 assert(result.getOwner() == rawOp &&
103 "bounded value isn't a result of this delinearize_index");
104 unsigned resIdx = result.getResultNumber();
105
106 AffineExpr linearIdx = cstr.getExpr(op.getLinearIndex());
107
108 SmallVector<OpFoldResult> basis = op.getPaddedBasis();
109 AffineExpr divisor = cstr.getExpr(1);
110 for (OpFoldResult basisElem : llvm::drop_begin(basis, resIdx + 1))
111 divisor = divisor * cstr.getExpr(basisElem);
112
113 if (resIdx == 0) {
114 cstr.bound(value) == linearIdx.floorDiv(divisor);
115 if (!basis.front().isNull())
116 cstr.bound(value) < cstr.getExpr(basis.front());
117 return;
118 }
119 AffineExpr thisBasis = cstr.getExpr(basis[resIdx]);
120 cstr.bound(value) == (linearIdx % (thisBasis * divisor)).floorDiv(divisor);
121 }
122};
123
124struct AffineLinearizeIndexOpInterface
125 : public ValueBoundsOpInterface::ExternalModel<
126 AffineLinearizeIndexOpInterface, AffineLinearizeIndexOp> {
127 void populateBoundsForIndexValue(Operation *rawOp, Value value,
128 ValueBoundsConstraintSet &cstr) const {
129 auto op = cast<AffineLinearizeIndexOp>(rawOp);
130 assert(value == op.getResult() &&
131 "value isn't the result of this linearize");
132
133 AffineExpr bound = cstr.getExpr(0);
134 AffineExpr stride = cstr.getExpr(1);
135 SmallVector<OpFoldResult> basis = op.getPaddedBasis();
136 OperandRange multiIndex = op.getMultiIndex();
137 unsigned numArgs = multiIndex.size();
138 for (auto [revArgNum, length] : llvm::enumerate(llvm::reverse(basis))) {
139 unsigned argNum = numArgs - (revArgNum + 1);
140 if (argNum == 0)
141 break;
142 OpFoldResult indexAsFoldRes = getAsOpFoldResult(multiIndex[argNum]);
143 bound = bound + cstr.getExpr(indexAsFoldRes) * stride;
144 stride = stride * cstr.getExpr(length);
145 }
146 bound = bound + cstr.getExpr(op.getMultiIndex().front()) * stride;
147 cstr.bound(value) == bound;
148 if (op.getDisjoint() && !basis.front().isNull()) {
149 cstr.bound(value) < stride *cstr.getExpr(basis.front());
150 }
151 }
152};
153} // namespace
154} // namespace mlir
155
157 DialectRegistry &registry) {
158 registry.addExtension(+[](MLIRContext *ctx, AffineDialect *dialect) {
159 AffineApplyOp::attachInterface<AffineApplyOpInterface>(*ctx);
160 AffineMaxOp::attachInterface<AffineMaxOpInterface>(*ctx);
161 AffineMinOp::attachInterface<AffineMinOpInterface>(*ctx);
162 AffineDelinearizeIndexOp::attachInterface<
163 AffineDelinearizeIndexOpInterface>(*ctx);
164 AffineLinearizeIndexOp::attachInterface<AffineLinearizeIndexOpInterface>(
165 *ctx);
166 });
167}
168
169FailureOr<int64_t>
171 assert(value1.getType().isIndex() && "expected index type");
172 assert(value2.getType().isIndex() && "expected index type");
173
174 // Subtract the two values/dimensions from each other. If the result is 0,
175 // both are equal.
176 Builder b(value1.getContext());
177 AffineMap map = AffineMap::get(/*dimCount=*/2, /*symbolCount=*/0,
178 b.getAffineDimExpr(0) - b.getAffineDimExpr(1));
179 // Fully compose the affine map with other ops because the folding logic
180 // can see opportunities for simplifying the affine map that
181 // `FlatLinearConstraints` can currently not see.
182 SmallVector<Value> mapOperands;
183 mapOperands.push_back(value1);
184 mapOperands.push_back(value2);
188 ValueBoundsConstraintSet::Variable(map, mapOperands));
189}
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
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.
Definition AffineMap.h:46
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,...
Definition Builders.h:51
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.
Definition MLIRContext.h:63
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:407
bool isIndex() const
Definition Types.cpp:54
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...
Definition Value.h:96
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
Definition Value.h:108
Type getType() const
Return the type of this value.
Definition Value.h:105
void registerValueBoundsOpInterfaceExternalModels(DialectRegistry &registry)
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.