This is an archive of the discontinued LLVM Phabricator instance.

Fix isShiftedInt and isShiftedUint for widths > 32.
ClosedPublic

Authored by jlebar on Jul 16 2016, 3:17 PM.

Details

Summary

Previously we were doing 1 << S. "1" is an int, so this doesn't work
when S >= 32.

This patch also adds some static_asserts to these functions to ensure
that we don't hit UB by shifting left too much.

Diff Detail

Event Timeline

jlebar updated this revision to Diff 64233.Jul 16 2016, 3:17 PM
jlebar retitled this revision from to Fix isShiftedInt and isShiftedUint for widths > 32..
jlebar updated this object.
jlebar added a reviewer: rnk.
jlebar added subscribers: dylanmckay, llvm-commits.
This revision was automatically updated to reflect the committed changes.
RKSimon reopened this revision.Jul 18 2016, 3:57 AM
RKSimon added a subscriber: RKSimon.

This has introduced a compile warning on VS2015 builds:

1> MathExtrasTest.cpp
1> .\llvm\include\llvm/Support/MathExtras.h(296): warning C4293: '<<': shift count negative or too big, undefined behavior
1> .\llvm\include\llvm/Support/MathExtras.h(322): note: see reference to function template instantiation 'bool llvm::isUInt<64>(uint64_t)' being compiled
1> .\llvm\unittests\Support\MathExtrasTest.cpp(401): note: see reference to function template instantiation 'bool llvm::isShiftedUInt<1,63>(uint64_t)' being compiled

1> .\llvm\include\llvm/Support/MathExtras.h(296): warning C4293: '<<': shift count negative or too big, undefined behavior
1> .\llvm\include\llvm/Support/MathExtras.h(322): note: see reference to function template instantiation 'bool llvm::isUInt<64>(uint64_t)' being compiled
1> .\llvm\unittests\Support\MathExtrasTest.cpp(401): note: see reference to function template instantiation 'bool llvm::isShiftedUInt<1,63>(uint64_t)' being compiled

Ugh. I think MSVC is wrong. isUInt<64> does

return N >= 64 || x < (UINT64_C(1)<<(N));

Which should be safe because of short-circuit evaluation.

I guess we can specialize the isUInt<N> template. :-/ Can we file a bug on msvc?

RKSimon, can you try D22472? I'm going to close this revision again, otherwise it's confusing to have an open revision after the patch has landed. Let's take further discussion into that thread, if that's OK.

jlebar accepted this revision.Jul 18 2016, 1:15 PM
jlebar added a reviewer: jlebar.
This revision is now accepted and ready to land.Jul 18 2016, 1:15 PM
jlebar closed this revision.Jul 18 2016, 1:15 PM

RKSimon, can you try D22472? I'm going to close this revision again, otherwise it's confusing to have an open revision after the patch has landed. Let's take further discussion into that thread, if that's OK.

Thanks its fixed the shift count warning but now has an operator precedence warning.

If you're after a buildbot to check things, llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast will help. It doesn't treat warnings as errors so the build stays green and you need to drill down into the build stdio:

http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast/builds/8507/steps/build/logs/warnings%20%283%29

RKSimon, can you try D22472? I'm going to close this revision again, otherwise it's confusing to have an open revision after the patch has landed. Let's take further discussion into that thread, if that's OK.

Thanks its fixed the shift count warning but now has an operator precedence warning.

This is fixed in rL275971