Skip to content

Commit 6bbcba6

Browse files
[mlir][Interfaces][NFC] ValueBoundsConstraintSet: Add columns for constant values/dims
`ValueBoundsConstraintSet` maintains an internal constraint set (`IntegerRelation`), where every analyzed index-typed SSA value or dimension of a shaped type is represented with a dimension/symbol. Prior to this change, index-typed values with a statically known constant value and static shaped type dimensions were not added to the constraint set. Instead, `getExpr` directly returned an affine constrant expression. With this commit, dynamic and static values/dimension sizes are treated in the same way: in either case, a dimension/symbol is added to the constraint set. This is needed for a subsequent commit that adds support for branches.
1 parent 6f44bb7 commit 6bbcba6

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

mlir/include/mlir/Interfaces/ValueBoundsOpInterface.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,13 @@ class ValueBoundsConstraintSet
294294

295295
/// Insert a value/dimension into the constraint set. If `isSymbol` is set to
296296
/// "false", a dimension is added. The value/dimension is added to the
297-
/// worklist.
297+
/// worklist if `addToWorklist` is set.
298298
///
299299
/// Note: There are certain affine restrictions wrt. dimensions. E.g., they
300300
/// cannot be multiplied. Furthermore, bounds can only be queried for
301301
/// dimensions but not for symbols.
302-
int64_t insert(Value value, std::optional<int64_t> dim, bool isSymbol = true);
302+
int64_t insert(Value value, std::optional<int64_t> dim, bool isSymbol = true,
303+
bool addToWorklist = true);
303304

304305
/// Insert an anonymous column into the constraint set. The column is not
305306
/// bound to any value/dimension. If `isSymbol` is set to "false", a dimension

mlir/lib/Interfaces/ValueBoundsOpInterface.cpp

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,57 @@ AffineExpr ValueBoundsConstraintSet::getExpr(Value value,
107107
assertValidValueDim(value, dim);
108108
#endif // NDEBUG
109109

110+
// Helper function that returns an affine expression that represents column
111+
// `pos` in the constraint set.
112+
auto getPosExpr = [&](int64_t pos) {
113+
assert(pos >= 0 && pos < cstr.getNumDimAndSymbolVars() &&
114+
"invalid position");
115+
return pos < cstr.getNumDimVars()
116+
? builder.getAffineDimExpr(pos)
117+
: builder.getAffineSymbolExpr(pos - cstr.getNumDimVars());
118+
};
119+
120+
// Check if the value/dim is statically known. In that case, an affine
121+
// constant expression should be returned. This allows us to support
122+
// multiplications with constants. (Multiplications of two columns in the
123+
// constraint set is not supported.)
124+
std::optional<int64_t> constSize = std::nullopt;
110125
auto shapedType = dyn_cast<ShapedType>(value.getType());
111126
if (shapedType) {
112-
// Static dimension: return constant directly.
113127
if (shapedType.hasRank() && !shapedType.isDynamicDim(*dim))
114-
return builder.getAffineConstantExpr(shapedType.getDimSize(*dim));
115-
} else {
116-
// Constant index value: return directly.
117-
if (auto constInt = ::getConstantIntValue(value))
118-
return builder.getAffineConstantExpr(*constInt);
128+
constSize = shapedType.getDimSize(*dim);
129+
} else if (auto constInt = ::getConstantIntValue(value)) {
130+
constSize = *constInt;
119131
}
120132

121-
// Dynamic value: add to constraint set.
133+
// If the value/dim is already mapped, return the corresponding expression
134+
// directly.
122135
ValueDim valueDim = std::make_pair(value, dim.value_or(kIndexValue));
123-
if (!valueDimToPosition.contains(valueDim))
124-
(void)insert(value, dim);
125-
int64_t pos = getPos(value, dim);
126-
return pos < cstr.getNumDimVars()
127-
? builder.getAffineDimExpr(pos)
128-
: builder.getAffineSymbolExpr(pos - cstr.getNumDimVars());
136+
if (valueDimToPosition.contains(valueDim)) {
137+
// If it is a constant, return an affine constant expression. Otherwise,
138+
// return an affine expression that represents the respective column in the
139+
// constraint set.
140+
if (constSize)
141+
return builder.getAffineConstantExpr(*constSize);
142+
return getPosExpr(getPos(value, dim));
143+
}
144+
145+
if (constSize) {
146+
// Constant index value/dim: add column to the constraint set, add EQ bound
147+
// and return an affine constant expression without pushing the newly added
148+
// column to the worklist.
149+
(void)insert(value, dim, /*isSymbol=*/true, /*addToWorklist=*/false);
150+
if (shapedType)
151+
bound(value)[*dim] == *constSize;
152+
else
153+
bound(value) == *constSize;
154+
return builder.getAffineConstantExpr(*constSize);
155+
}
156+
157+
// Dynamic value/dim: insert column to the constraint set and put it on the
158+
// worklist. Return an affine expression that represents the newly inserted
159+
// column in the constraint set.
160+
return getPosExpr(insert(value, dim, /*isSymbol=*/true));
129161
}
130162

131163
AffineExpr ValueBoundsConstraintSet::getExpr(OpFoldResult ofr) {
@@ -142,7 +174,7 @@ AffineExpr ValueBoundsConstraintSet::getExpr(int64_t constant) {
142174

143175
int64_t ValueBoundsConstraintSet::insert(Value value,
144176
std::optional<int64_t> dim,
145-
bool isSymbol) {
177+
bool isSymbol, bool addToWorklist) {
146178
#ifndef NDEBUG
147179
assertValidValueDim(value, dim);
148180
#endif // NDEBUG
@@ -157,7 +189,12 @@ int64_t ValueBoundsConstraintSet::insert(Value value,
157189
if (positionToValueDim[i].has_value())
158190
valueDimToPosition[*positionToValueDim[i]] = i;
159191

160-
worklist.push(pos);
192+
if (addToWorklist) {
193+
LLVM_DEBUG(llvm::dbgs() << "Push to worklist: " << value
194+
<< " (dim: " << dim.value_or(kIndexValue) << ")\n");
195+
worklist.push(pos);
196+
}
197+
161198
return pos;
162199
}
163200

0 commit comments

Comments
 (0)