Skip to content

Commit 5f022ad

Browse files
committed
[mlir] detect integer overflow in debug mode
Rationale: This computation failed ASAN for the following input (integer overflow during 4032000000000000000 * 100): tensor<100x200x300x400x500x600x700x800xf32> This change adds a simple overflow detection during debug mode (which we run more regularly than ASAN). Arguably this is an unrealistic tensor input, but in the context of sparse tensors, we may start to see cases like this. Bug: https://bugs.llvm.org/show_bug.cgi?id=49136 Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D96530
1 parent 8ef4b96 commit 5f022ad

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mlir/lib/IR/BuiltinTypes.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ int64_t ShapedType::getNumElements() const {
209209
assert(hasStaticShape() && "cannot get element count of dynamic shaped type");
210210
auto shape = getShape();
211211
int64_t num = 1;
212-
for (auto dim : shape)
212+
for (auto dim : shape) {
213213
num *= dim;
214+
assert(num >= 0 && "integer overflow in element count computation");
215+
}
214216
return num;
215217
}
216218

@@ -801,10 +803,12 @@ AffineExpr mlir::makeCanonicalStridedLayoutExpr(ArrayRef<int64_t> sizes,
801803
? getAffineSymbolExpr(nSymbols++, context)
802804
: getAffineConstantExpr(runningSize, context);
803805
expr = expr ? expr + dimExpr * stride : dimExpr * stride;
804-
if (size > 0)
806+
if (size > 0) {
805807
runningSize *= size;
806-
else
808+
assert(runningSize > 0 && "integer overflow in size computation");
809+
} else {
807810
dynamicPoisonBit = true;
811+
}
808812
}
809813
return simplifyAffineExpr(expr, numDims, nSymbols);
810814
}

0 commit comments

Comments
 (0)