This is an archive of the discontinued LLVM Phabricator instance.

[OPENMP]Fix overflow during counting the number of iterations.
ClosedPublic

Authored by ABataev on Jun 15 2020, 2:11 PM.

Details

Summary

The OpenMP loops are normalized and transformed into the loops from 0 to
max number of iterations. In some cases, original scheme may lead to
overflow during calculation of number of iterations. If it is unknown,
if we can end up with overflow or not (the bounds are not constant and

we cannot define if there is an overflow), cast original type to the
unsigned.

Diff Detail

Event Timeline

ABataev created this revision.Jun 15 2020, 2:11 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 15 2020, 2:11 PM

So the idea is to do the trip count computation with defined overflow behavior, e.g., without nsw/nuw in IR, right?

So the idea is to do the trip count computation with defined overflow behavior, e.g., without nsw/nuw in IR, right?

Not quite. If we can predict that there is no overflow, everything remains the same as before. If we cannot do this, the trip count calculations are performed on unsigned types instead of signed. It does not just drop nuw/nsw flags, but also uses udiv, zext etc. instead of sdiv, sext etc. Plus, it tries to reorganize the operations a little bit to avoid possible overflow.

jdoerfert accepted this revision.Jun 16 2020, 11:25 AM

So the idea is to do the trip count computation with defined overflow behavior, e.g., without nsw/nuw in IR, right?

Not quite. If we can predict that there is no overflow, everything remains the same as before. If we cannot do this, the trip count calculations are performed on unsigned types instead of signed. It does not just drop nuw/nsw flags, but also uses udiv, zext etc. instead of sdiv, sext etc. Plus, it tries to reorganize the operations a little bit to avoid possible overflow.

Makes sense. LGTM.

This revision is now accepted and ready to land.Jun 16 2020, 11:25 AM
This revision was automatically updated to reflect the committed changes.