Issue 240165
Summary Union members accessed through `*u->m` instead of `u->m[0]` are miscompiled
Labels new issue
Assignees
Reporter blat-blatnik
    The following C code miscompiles with `clang -O2`:
```c
union FU {
    float f[1];
    unsigned u[1];
};
float foo(union FU* fu, unsigned* p) {
    fu->f[0] = 1;
    *p = 2;
    return fu->f[0]; // CORRECT, value is reloaded
}
float bar(union FU* fu, unsigned* p) {
    *fu->f = 1;
    *p = 2;        // p could alias fu
    return *fu->f; // WRONG, value is not reloaded, always returns 1.0
}
```
[Godbolt](https://godbolt.org/z/3E67YvYTv) shows this x64 assembly (also note that GCC correctly reloads the value):
```asm
foo:
 mov     dword ptr [rdi], 1065353216
        mov     dword ptr [rsi], 2
 movss   xmm0, dword ptr [rdi]
        ret

.LCPI1_0:
        .long 0x3f800000 ; 1.0
bar:
        mov     dword ptr [rdi], 1065353216
 mov     dword ptr [rsi], 2
        movss   xmm0, dword ptr [rip + .LCPI1_0] ; load 1.0 into return register
        ret
```
So clearly LLVM treats `*fu->f` differently than `fu->f[0]` somehow. But the C standard says they should be treated exactly the same.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to