Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment in removing ptr->int->ptr casts #7664

Closed
wants to merge 3 commits into from

Conversation

steven-johnson
Copy link
Contributor

While looking over clang-tidy checks that we had disabled, I came across performance-no-int-to-ptr and ended up in a fascinating rabbit hole. TL;DR: doing ptr->int->ptr casting reduces opportunities for compiler optimizations (and that's what this check attempts to warn you about). I had no idea this was even a thing, but https://www.ralfj.de/blog/2020/12/14/provenance.html and https://www.ralfj.de/blog/2022/04/11/provenance-exposed.html provided interesting backstory.

Running clang-tidy with this enabled revealed a lot of places in our runtime code that might run afoul of this... but it's not clear whether any of them are on a critical path that makes this inefficiency critical (except, perhaps, for the bit-tagging we do in synchronization_common).

Anyway, I experimentally converted a few places to follow what I think are the right guidelines (and they no longer trigger this check). Not sure if this is worth pursuing any further, but I thought it worthwhile to post here as a Draft for people to take a look at.

@steven-johnson steven-johnson added the buildbot_test_everything Buildbots should run all available tests on this PR (unless build_test_nothing is set). label Jun 27, 2023
#if defined(__has_builtin) && __has_builtin(__builtin_align_up)
return __builtin_align_up(value, alignment);
#else
if constexpr (std::is_pointer<SomeType>::value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this implementation for align_up:

ptr += ((-(uintptr_t)(ptr)) & (alignment - 1));

Curious if that passes the clang-tidy checker. It just adds a computed offset to a pointer, where the offset depends on casting the pointer to an int. It never casts an int to a ptr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buildbot_test_everything Buildbots should run all available tests on this PR (unless build_test_nothing is set).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants