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

Clang (front-end) wrong code on incremental operator in a loop

Open llvmbot opened this issue 8 years ago • 5 comments

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

llvmbot avatar Dec 08 '17 12:12 llvmbot

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

mahtohappy avatar Jan 05 '24 05:01 mahtohappy

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

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

shafik avatar Jan 05 '24 18:01 shafik

@llvm/issue-subscribers-c

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [35577](https://llvm.org/bz35577) | | 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 = &amp;c.a;
int e = 0;
long *f = &amp;c.b;
int main() {
  for (; e &lt;= 1; e++) {
    *f = 0;
    ++*d;
  }
  printf("%d\n", c.a);
}
$ clang -O0 small.c; ./a.out
1
$ clang -Ofast small.c; ./a.out
2

llvmbot avatar Jan 05 '24 18:01 llvmbot

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.

zygoloid avatar Jan 05 '24 19:01 zygoloid

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

AaronBallman avatar Jan 12 '24 16:01 AaronBallman