possible union bug
| Bugzilla Link | 11966 |
| Version | trunk |
| OS | Linux |
| CC | @asl,@lattner,@kosarev,@dobbelaj-snps,@grimreaper,@sunfishcode |
Extended Description
I don't know that this is a bug, but wanted to get a couple people to look at it.
All non-LLVM compilers that I tested (5 or so) print "1" at all optimization levels.
Clang prints "0" at -O2.
int printf (const char *, ...);
union U0 { short f0; int f1; } b;
union U0 *c = &b;
int main () { b.f0 = 0; c->f1 = 1; printf ("%d\n", b.f0); return 0; }
mentioned in issue llvm/llvm-bugzilla-archive#28241
mentioned in issue llvm/llvm-bugzilla-archive#21725
Sure, reopening it now, sorry for the noise.
Not really fixed yet, just temporarily "pessimized". See:
https://reviews.llvm.org/D33328
Alexander, will you please re-open this bug report? Once we have a proper solution, it would be nice to check it against test cases like this. Thanks.
*** Bug llvm/llvm-bugzilla-archive#28241 has been marked as a duplicate of this bug. ***
Bug 21725 seems to be somewhat different and it doesn't involve type punning.
Anyway, this bug is finally fixed in clang 5.
*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#21725 ***
Fair points. It's sad that C has such a completely unsound type system. Forming a union of two types could be done in another translation unit and the compiler would still perform the optimization.
Yeah I just brought it up because (1) this example makes it look like you pretty much better not take the address of a union field at all and of course this could be buried deep in a large code where people won't know to disable TBAA and (2) LLVM appears to be an outlier among modern C compilers.
I believe that this is a known issue. We intentionally don't model the TBAA rules for unions. A workaround is to disable strict aliasing.