llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

possible union bug

Open regehr opened this issue 14 years ago • 10 comments

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; }

regehr avatar Feb 10 '12 06:02 regehr

mentioned in issue llvm/llvm-bugzilla-archive#28241

llvmbot avatar Nov 26 '21 21:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#21725

dobbelaj-snps avatar Nov 26 '21 19:11 dobbelaj-snps

Sure, reopening it now, sorry for the noise.

llvmbot avatar Oct 23 '17 09:10 llvmbot

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.

kosarev avatar Oct 23 '17 07:10 kosarev

*** Bug llvm/llvm-bugzilla-archive#28241 has been marked as a duplicate of this bug. ***

llvmbot avatar Oct 22 '17 21:10 llvmbot

Bug 21725 seems to be somewhat different and it doesn't involve type punning.

Anyway, this bug is finally fixed in clang 5.

llvmbot avatar Oct 22 '17 21:10 llvmbot

*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#21725 ***

dobbelaj-snps avatar Feb 17 '15 12:02 dobbelaj-snps

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.

lattner avatar Feb 10 '12 06:02 lattner

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.

regehr avatar Feb 10 '12 06:02 regehr

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.

lattner avatar Feb 10 '12 06:02 lattner