Skip to content

clang treats asm volatile block as a memory barrier, no-op on gcc #76369

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

Closed
crosdahl opened this issue Dec 25, 2023 · 2 comments
Closed

clang treats asm volatile block as a memory barrier, no-op on gcc #76369

crosdahl opened this issue Dec 25, 2023 · 2 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc.

Comments

@crosdahl
Copy link

int a = 0;

int main(void)
{
        a = 1;
        asm volatile ("");
        a = 2;
        return a;
}

gcc only generates one store, a = 2;, while clang keeps both. clang seems to treat asm volatile(""); as a memory barrier, gcc doesn't. afaik, asm volatile("" ::: "memory"); is the only thing that works on gcc.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Dec 25, 2023
@EugeneZelenko EugeneZelenko added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang Clang issues not falling into any other category labels Dec 26, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 26, 2023

@llvm/issue-subscribers-clang-codegen

Author: None (crosdahl)

```c int a = 0;

int main(void)
{
a = 1;
asm volatile ("");
a = 2;
return a;
}

gcc only generates one store, `a = 2;`, while clang keeps both. clang seems to treat `asm volatile("");` as a memory barrier, gcc doesn't. afaik, `asm volatile("" ::: "memory");` is the only thing that works on gcc.
</details>

@pinskia
Copy link

pinskia commented Dec 26, 2023

Note from the GCC manual (https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Basic-Asm.html) towards the end:

For basic asm with non-empty assembler string GCC assumes the assembler block does not change any general purpose registers, but it may read or write any globally accessible variable.

This means asm(""); is not a compiler memory barrier but asm(";"); will be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc.
Projects
None yet
Development

No branches or pull requests

5 participants