This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Avoid relying on non-portable behaviour in std::align
ClosedPublic

Authored by arichardson on Sep 21 2022, 7:40 AM.

Details

Summary

Round-tripping pointers via size_t is not portable, the C/C++ standards
only require this to be valid when using (u)intptr_t. While touching
these lines also make use of the Clang 10+ __builtin_align_up to avoid
the need for reinterpret_cast.

Originally committed to the CHERI fork of LLVM as
https://github.com/CTSRD-CHERI/llvm-project/commit/dd01245185ab9e71b70b418bee8f11ea0199e1a3,
but I forgot to upstream the change. I rediscovered this issue due to a
compiler warning when building libc++ on a Arm Morello system.

Diff Detail

Event Timeline

arichardson created this revision.Sep 21 2022, 7:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 21 2022, 7:40 AM
arichardson requested review of this revision.Sep 21 2022, 7:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 21 2022, 7:40 AM
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald Transcript
ldionne accepted this revision.Sep 21 2022, 10:32 AM
ldionne added a subscriber: ldionne.

LGTM but please re-upload to get a CI run.

This revision is now accepted and ready to land.Sep 21 2022, 10:32 AM

Is there any benefit of the builtin other than removing reinterpret_cast? If not I'd say just drop it, since we have to keep the reinterpret_casts anyways.

Drop __builtin_align_up

arichardson added a comment.EditedSep 22 2022, 2:26 AM

Is there any benefit of the builtin other than removing reinterpret_cast? If not I'd say just drop it, since we have to keep the reinterpret_casts anyways.

Not really - in theory it could improve codegen, but it just emits the same thing as the hand-written code anyway. I think the main reason I originally used it is that CHERI Clang emitted a (false-positive) warning on the arithmetic expression back in 2019 that has since been fixed. I've dropped the builtin from this version.

rebase to try and get a working CI run

ldionne accepted this revision.Sep 23 2022, 1:51 PM

LGTM once CI is green. You should try rebasing now, the CI is back online.

rebase for CI

philnik accepted this revision.Sep 26 2022, 5:56 AM