Clang (front-end) wrong code on incremental operator in a loop
| Bugzilla Link | 35577 |
| Version | trunk |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
| CC | @zygoloid |
Extended Description
$ clang -v clang version 6.0.0-svn319838-1~exp1 (trunk) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0 Candidate multilib: .;@m64 Selected multilib: .;@m64
$ cat small.c
int printf(const char *, ...);
union {
short a;
long b;
} c;
short *d = &c.a;
int e = 0;
long *f = &c.b;
int main() {
for (; e <= 1; e++) {
*f = 0;
++*d;
}
printf("%d\n", c.a);
}
$ clang -O0 small.c; ./a.out
1
$ clang -Ofast small.c; ./a.out
2
https://godbolt.org/z/TaPG3bnrb LLVM optimizer tries to optimize it with the trip count figured out and increments the value of 'd' in one go, but forgets that 'f' shares the same memory space. -fno-strict-aliasing fixes it
https://godbolt.org/z/TaPG3bnrb LLVM optimizer tries to optimize it with the trip count figured out and increments the value of 'd' in one go, but forgets that 'f' shares the same memory space.
-fno-strict-aliasingfixes it
No, this is undefined behavior. If using -fno-strict-aliasing "fixes" your expected outcome then you are almost surely violating strict aliasing rules. In this case I don't believe we can change the active member of a union through a pointer to one of it's members like the code attempts to do with *f = 0;.
CC @AaronBallman @zygoloid for a second look since this is C and I may be missing something
@llvm/issue-subscribers-c
Author: None (llvmbot)
Extended Description
$ clang -v clang version 6.0.0-svn319838-1~exp1 (trunk) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0 Candidate multilib: .;@m64 Selected multilib: .;@m64
$ cat small.c
int printf(const char *, ...);
union {
short a;
long b;
} c;
short *d = &c.a;
int e = 0;
long *f = &c.b;
int main() {
for (; e <= 1; e++) {
*f = 0;
++*d;
}
printf("%d\n", c.a);
}
$ clang -O0 small.c; ./a.out
1
$ clang -Ofast small.c; ./a.out
2
This is definitely UB in C++, and I think it's UB in C too because it does not satisfy the effective type rule, but the C rules in this area are a lot more murky.
I think this is UB per DR236 and the related discussions. I believe the status quo in C is that the union type must be what's used to change the effective type, not a pointer into the union.
It's worth noting that Clang, GCC, and ICC all treat this as undefined behavior in practice (at least from my testing): https://godbolt.org/z/ddMPP6c43