Skip to content

Commit b0cf8c2

Browse files
committed
[MLIR][Affine] Fix addInductionVarOrTerminalSymbol to return status
Fixes: #64287 This method is failable on valid IR and we should've been returning failure instead of asserting, and checking status at its users.
1 parent f5f5286 commit b0cf8c2

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ class FlatAffineValueConstraints : public FlatLinearValueConstraints {
106106

107107
/// Add the specified values as a dim or symbol var depending on its nature,
108108
/// if it already doesn't exist in the system. `val` has to be either a
109-
/// terminal symbol or a loop IV, i.e., it cannot be the result affine.apply
110-
/// of any symbols or loop IVs. The variable is added to the end of the
111-
/// existing dims or symbols. Additional information on the variable is
112-
/// extracted from the IR and added to the constraint system.
113-
void addInductionVarOrTerminalSymbol(Value val);
109+
/// terminal symbol or a loop IV, i.e., it cannot be the result of an
110+
/// affine.apply of any symbols or loop IVs. Return failure if the addition
111+
/// wasn't possible due to the above conditions not being met. This method can
112+
/// also fail if the addition of the domain of an affine IV fails. The
113+
/// variable is added to the end of the existing dims or symbols. Additional
114+
/// information on the variable is extracted from the IR and added to the
115+
/// constraint system.
116+
LogicalResult addInductionVarOrTerminalSymbol(Value val);
114117

115118
/// Adds slice lower bounds represented by lower bounds in `lbMaps` and upper
116119
/// bounds in `ubMaps` to each variable in the constraint system which has

mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,45 @@ using namespace mlir;
3434
using namespace affine;
3535
using namespace presburger;
3636

37-
38-
void FlatAffineValueConstraints::addInductionVarOrTerminalSymbol(Value val) {
37+
LogicalResult
38+
FlatAffineValueConstraints::addInductionVarOrTerminalSymbol(Value val) {
3939
if (containsVar(val))
40-
return;
40+
return success();
4141

4242
// Caller is expected to fully compose map/operands if necessary.
43-
assert((isTopLevelValue(val) || isAffineInductionVar(val)) &&
44-
"non-terminal symbol / loop IV expected");
43+
if (val.getDefiningOp<affine::AffineApplyOp>() ||
44+
(!isValidSymbol(val) && !isAffineInductionVar(val))) {
45+
LLVM_DEBUG(llvm::dbgs()
46+
<< "only valid terminal symbols and affine IVs supported\n");
47+
return failure();
48+
}
4549
// Outer loop IVs could be used in forOp's bounds.
4650
if (auto loop = getForInductionVarOwner(val)) {
4751
appendDimVar(val);
48-
if (failed(this->addAffineForOpDomain(loop)))
52+
if (failed(this->addAffineForOpDomain(loop))) {
4953
LLVM_DEBUG(
5054
loop.emitWarning("failed to add domain info to constraint system"));
51-
return;
55+
return failure();
56+
}
57+
return success();
5258
}
59+
5360
if (auto parallel = getAffineParallelInductionVarOwner(val)) {
5461
appendDimVar(parallel.getIVs());
55-
if (failed(this->addAffineParallelOpDomain(parallel)))
62+
if (failed(this->addAffineParallelOpDomain(parallel))) {
5663
LLVM_DEBUG(parallel.emitWarning(
5764
"failed to add domain info to constraint system"));
58-
return;
65+
return failure();
66+
}
67+
return success();
5968
}
6069

6170
// Add top level symbol.
6271
appendSymbolVar(val);
6372
// Check if the symbol is a constant.
6473
if (std::optional<int64_t> constOp = getConstantIntValue(val))
6574
addBound(BoundType::EQ, val, constOp.value());
75+
return success();
6676
}
6777

6878
LogicalResult
@@ -222,8 +232,10 @@ LogicalResult FlatAffineValueConstraints::addBound(BoundType type, unsigned pos,
222232
fullyComposeAffineMapAndOperands(&map, &operands);
223233
map = simplifyAffineMap(map);
224234
canonicalizeMapAndOperands(&map, &operands);
225-
for (auto operand : operands)
226-
addInductionVarOrTerminalSymbol(operand);
235+
for (Value operand : operands) {
236+
if (failed(addInductionVarOrTerminalSymbol(operand)))
237+
return failure();
238+
}
227239
return addBound(type, pos, computeAlignedMap(map, operands));
228240
}
229241

mlir/lib/Dialect/Affine/Analysis/Utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,8 @@ LogicalResult MemRefRegion::compute(Operation *op, unsigned loopDepth,
12551255
if (sliceState != nullptr) {
12561256
// Add dim and symbol slice operands.
12571257
for (auto operand : sliceState->lbOperands[0]) {
1258-
cst.addInductionVarOrTerminalSymbol(operand);
1258+
if (failed(cst.addInductionVarOrTerminalSymbol(operand)))
1259+
return failure();
12591260
}
12601261
// Add upper/lower bounds from 'sliceState' to 'cst'.
12611262
LogicalResult ret =

0 commit comments

Comments
 (0)