This is an archive of the discontinued LLVM Phabricator instance.

[mlir] detect integer overflow in debug mode
ClosedPublic

Authored by aartbik on Feb 11 2021, 11:11 AM.

Details

Summary

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

Diff Detail

Event Timeline

aartbik created this revision.Feb 11 2021, 11:11 AM
aartbik requested review of this revision.Feb 11 2021, 11:11 AM

Would we catch the issue from yesterday if you moved this assertions in ShapedType::getNumElements()?
That would be more centralized and can cover more cases ideally.

Would we catch the issue from yesterday if you moved this assertions in ShapedType::getNumElements()?
That would be more centralized and can cover more cases ideally.

That wouldn't have caught this particular issue, but looking at the logic in that method, happy to add that there too.

Please also give some thought on how to detect overflow (I present two methods in the bug as alternatives).
Testing > 0 is by far the easiest, but technically not a portable way, since the behavior is undefined (perhaps
a good reason to switch to unsigned :-)?

Would we catch the issue from yesterday if you moved this assertions in ShapedType::getNumElements()?
That would be more centralized and can cover more cases ideally.

That wouldn't have caught this particular issue, but looking at the logic in that method, happy to add that there too.

OK, if you can add it there as well that'd be great!

Please also give some thought on how to detect overflow (I present two methods in the bug as alternatives).
Testing > 0 is by far the easiest, but technically not a portable way, since the behavior is undefined

Ah I missed the comment in the bug, answered there: I'm fine with 2) since we're just talking about an assertions for a poor-man UBSAN.

(perhaps good reason to switch to unsigned :-)?

Actually UBSAN catching the bug in the first place is the perfect justification why I never use unsigned :)
(unless I really intended to wrap around intentionally).

aartbik updated this revision to Diff 323114.Feb 11 2021, 12:26 PM

similar quick overflow test in number of elements
(note that here we need >= since total size can be zero)

mehdi_amini accepted this revision.Feb 11 2021, 1:35 PM
This revision is now accepted and ready to land.Feb 11 2021, 1:35 PM
This revision was landed with ongoing or failed builds.Feb 11 2021, 6:21 PM
This revision was automatically updated to reflect the committed changes.