Skip to content

clang does not automatically link atomic builtins #73361

New issue

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

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

Already on GitHub? # to your account

Open
huixie90 opened this issue Nov 24, 2023 · 1 comment
Open

clang does not automatically link atomic builtins #73361

huixie90 opened this issue Nov 24, 2023 · 1 comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl'

Comments

@huixie90
Copy link
Contributor

huixie90 commented Nov 24, 2023

On Linux (and others?), the __atomic_store (and other atomic) builtin doesn't work out of the box.
User will see a linker error

undefined reference to `__atomic_store'

If the system has gcc installed, the user could add -latomic. However, on systems without gcc installed, the user would have to find the correct compiler-rt library that needs to be linked, which is not user friendly. clang should just atomically link to the compiler-rt when the builtin needs to be linked

Reproducer:

cat <<EOF | clang++ -xc++ -
struct Large {
    int value[100];
};

struct GCC {
    Large value;
};

void gcc(GCC& a, Large& val, int memory_order) {
    __atomic_store(&a.value, &val, memory_order);
}


struct C11 {
    _Atomic(Large) value;
};

void c11(C11& a, Large& val, int memory_order) {
    __c11_atomic_store(&a.value, val, memory_order);
}

int main() { }
EOF

Godbolt: https://godbolt.org/z/f4jchEvxv

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 24, 2023
@EugeneZelenko EugeneZelenko added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' and removed clang Clang issues not falling into any other category labels Nov 25, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2023

@llvm/issue-subscribers-clang-driver

Author: Hui (huixie90)

On Linux (and others?), the `__atomic_store` (and other atomic) builtin doesn't work out of the box. User will see a linker error ``` undefined reference to `__atomic_store' ```

If the system has gcc installed, the user could add -latomic. However, on systems without gcc installed, the user would have to find the correct compiler-rt library that needs to be linked, which is not user friendly. clang should just atomically link to the compiler-rt when the builtin needs to be linked

Reproducer:

cat &lt;&lt;EOF | clang++ -xc++ -
struct Large {
    int value[100];
};

struct GCC {
    Large value;
};

void gcc(GCC&amp; a, Large&amp; val, int memory_order) {
    __atomic_store(&amp;a.value, &amp;val, memory_order);
}


struct C11 {
    _Atomic(Large) value;
};

void c11(C11&amp; a, Large&amp; val, int memory_order) {
    __c11_atomic_store(&amp;a.value, val, memory_order);
}

int main() { }
EOF

Godbolt: https://godbolt.org/z/f4jchEvxv

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl'
Projects
None yet
Development

No branches or pull requests

3 participants