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

Unexpected code generation for volatile atomic RMW operations #100840

Open
keinflue opened this issue Jul 26, 2024 · 1 comment
Open

Unexpected code generation for volatile atomic RMW operations #100840

keinflue opened this issue Jul 26, 2024 · 1 comment

Comments

@keinflue
Copy link

The C++ test case

#include<atomic>

void f(volatile std::atomic<int>& x) {
    x.fetch_add(0, std::memory_order_relaxed);
}

on Clang 18.1.0 x86-64 with -O1 produces

f(std::atomic<int> volatile&):
        mfence
        mov     eax, dword ptr [rdi]
        ret

as does the equivalent C program.

This is surprising, because only a load instead of a RMW operation is emitted. fetch_add is always a RMW operation on the abstract machine, even if the stored value is the same as the one loaded, and the expectation would be that volatile preserves this semantic.

This may or may not be a conformance issue. The standards seem to be unclear about the interpretation of "access" with regards to volatile-qualified RMW operations.

See discussion at https://stackoverflow.com/questions/78776735/semantics-of-volatile-stdatomict and https://stackoverflow.com/questions/78777076/semantics-of-volatile-atomic.

@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2024

@llvm/issue-subscribers-backend-x86

Author: None (keinflue)

The C++ test case
#include&lt;atomic&gt;

void f(volatile std::atomic&lt;int&gt;&amp; x) {
    x.fetch_add(0, std::memory_order_relaxed);
}

on Clang 18.1.0 x86-64 with -O1 produces

f(std::atomic&lt;int&gt; volatile&amp;):
        mfence
        mov     eax, dword ptr [rdi]
        ret

as does the equivalent C program.

This is surprising, because only a load instead of a RMW operation is emitted. fetch_add is always a RMW operation on the abstract machine, even if the stored value is the same as the one loaded, and the expectation would be that volatile preserves this semantic.

This may or may not be a conformance issue. The standards seem to be unclear about the interpretation of "access" with regards to volatile-qualified RMW operations.

See discussion at https://stackoverflow.com/questions/78776735/semantics-of-volatile-stdatomict and https://stackoverflow.com/questions/78777076/semantics-of-volatile-atomic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants