<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/73361>73361</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang does not automatically link `atomic` builtins
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          huixie90
      </td>
    </tr>
</table>

<pre>
    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
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVNtu4zgPfhrmhkggy44PF75o3Lr_DwxQYIG9DmSJidVRpEKSc9inX_jQmbToDHYXCBzLpL4DJVKEoI-WqIbtDraPKzHE3vm6H_RVU8VWnVO3-sXiN22HKwIvhVXoYk8-QNoCr4A3GHtCyNl-L6I7abkP0XmCnH3IxzkIvMJu0CZqi8pRsMCLiBfnv6MbIrrDhNa56waBPQJ7-DOQx4s2BgMRCjTafieP5L3zcwbkbPlNy8EqOmhLCj0dyJOVhNF9IZAXX-6fn_-fhYRbiHTCXgQ8SonahiiMIfVuexjVSTcYhUKpkWRtFqM52-D_3IXO5MdsZxewgBcd-9HsrxEvE2IvzpP0g7ZqiknnPcmI0p3etCG_9hGN7rzwN4y9iGiJVBi3dDQXaoK99Fr2qANaF2f8g9dklbltUBphjxj6ifB1CHE5J2HMbYIY0Wbun5yXnux8TstJfsF7V8s_6M07NUjykD7cBz5VXoqIkDaQNk8vLULRzOqA74DvcH2V729zeoh-kBG_CX8khGI3f0VE1DbiWZiBYLtLGIPtI6RLGIq793uY56b5CDLjzjC_3n12Wo0HCbx8bhrgOYqx5NPmcXUWZlyPik50cv62d16NN6K6Y_t4LUvgudjMxLxB4PkC8gngg6r7qt7bapLkjmj_sDRhuQis_plBmSTAy2Z8_ieDiLjfyyTZ_9bpv7M5MQptJ5iJDH9kPL20v-ntZ6c6ZyKkD9jH-BbGW8lb4O1xDmycPwJv_wLeHrJX2T-dr-eVqlNVpZVYUZ0UjJXbPGHJqq_TrMwOeVpueZozlWdFWW27Q9mJrpRZkSUrXXPG0yThGWcsyZINO_BUVduMs7KS20JBxugktNkYcz6N3CsdwkB1kaZ5sjKiIxOm-cz50g98HNW-HvPX3XAMkDGjQww_EaKOhuq5uccxO7W-GKI7iXjf3JCzH-PqvZvDavCm_lQZHfuh20h3At6OLMvf-s27V5IReDuJDsDbSfffAQAA__9CNe3p">