[llvm-bugs] [Bug 35569] New: [Clang-optimization] wrong code when link-time-optimization with assign a union to a global static variable

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 7 19:05:47 PST 2017


https://bugs.llvm.org/show_bug.cgi?id=35569

            Bug ID: 35569
           Summary: [Clang-optimization] wrong code when
                    link-time-optimization with assign a union to a global
                    static variable
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: yangyibiao at nju.edu.cn
                CC: llvm-bugs at lists.llvm.org

$ clang -v
clang version 6.0.0-svn320088-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 {
  int a;
  short b;
} c;
int *d = &c.a;
static short *e = &c.b;
int f;
char g;
int main() {
  f = c.a;
  *d = 1;
  *e |= g;
  printf("%d\n", c.b);
}

$ clang -O0 small.c; ./a.out
1
$ clang -O0 -flto small.c; ./a.out
1
$ clang -O1 -flto small.c; ./a.out
1
$ clang -O2 -flto small.c; ./a.out
0
$ clang -O3 -flto small.c; ./a.out
0
$ clang -Ofast -flto small.c; ./a.out
0

As we can find, the when optimization is O2/O3/Ofast along with -flto, the
output is different. When remove static attribute, the result is correct.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171208/adbec4cf/attachment.html>


More information about the llvm-bugs mailing list