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

C++20's P0528R3/P1123R0 not implemented (atomic ignoring padding bits) #64829

Closed
comex opened this issue Aug 19, 2023 · 6 comments
Closed

C++20's P0528R3/P1123R0 not implemented (atomic ignoring padding bits) #64829

comex opened this issue Aug 19, 2023 · 6 comments
Assignees
Labels
clang:codegen duplicate Resolved as duplicate libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@comex
Copy link
Contributor

comex commented Aug 19, 2023

C++20 includes P0528R3 and P1123R0, which together require std::atomic<T>::compare_exchange_strong and std::atomic_ref<T>::compare_exchange_strong to ignore any padding bits that exist in T.

However, this is not implemented in Clang or libc++ as of b41e75c. Also, neither paper is mentioned on the cxx_status page.

As a result of the missing implementation, the following prints 0 when it should print 1:

#include <atomic>
#include <stdio.h>
int main() {
    struct HasPadding { char a; short b; };
    HasPadding expected{};
    std::atomic<HasPadding> atomic(expected);
    ((char *)&expected)[1] = 0x42; // update padding byte
    printf("%d\n", atomic.compare_exchange_strong(
        expected, expected));
}

See also:

@RalfJung
Copy link
Contributor

Relatedly, it would be good to get clarification on the behavior of LLVM's cmpxchg when the value being compared is partially undef / uninitialized / poison (which is what happens with the above program). Naively, the answer would be "it's UB" (that's usually what happens when you memcp memory that contains undef / uninitialized / poison), but that would make it impossible to perform cmpxchg on data with padding which would be unfortunate.

@llvmbot
Copy link
Member

llvmbot commented Aug 19, 2023

@llvm/issue-subscribers-clang-codegen

@huixie90 huixie90 self-assigned this Nov 22, 2023
@huixie90 huixie90 added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 22, 2023
@huixie90
Copy link
Contributor

On the libc++ side, we need clang equivalent of gcc __builtin_clear_padding to make it implementable. Also notice that for long double in the x87_fp80 format, gcc __builtin_clear_padding also clears the last 58 bits as only first 80bits of the long double is the value representation

@gonzalobg
Copy link
Contributor

Is there an issue tracking __builtin_clear_padding ?

@pkasting
Copy link
Member

Is there an issue tracking __builtin_clear_padding ?

Yes, #64830.

@ldionne ldionne closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
@ldionne ldionne added the duplicate Resolved as duplicate label Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen duplicate Resolved as duplicate libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

8 participants