llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.76k stars 11.44k forks source link

Emitted binary code changes when -g is enabled at -O2 with -flto #51881

Open theo-lw opened 2 years ago

theo-lw commented 2 years ago
Bugzilla Link 52539
Version trunk
OS All
Blocks llvm/llvm-project#37076
CC @chengniansun

Extended Description

The .text section for the following program (program0_preprocessed.c ) changes after toggling the -g flag.

$ clang-trunk -v
clang version 14.0.0 (https://github.com/llvm/llvm-project.git 82fc4cc60bec6d0eacad7e8575f404dcdf5144c5)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cnsun/usr/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
$ cat program0_preprocessed.c
int a, e;
int volatile b;
int *c;
short d;
void main() {
  int f;
  for (;;) {
    int g, h;
    int i[6];
    c = i;
    d ^= 4;
    f = d > i[5] ? 0 : d + i[5];
    g = h = f - a;
    int j = 2;
    e = 2 - h;
    b = e;
  }
}
$ clang-trunk -w -flto -O2 program0_preprocessed.c -o a.out; objdump --disassemble --section=.text a.out > no_g.txt
$ clang-trunk -w -flto -O2 -g program0_preprocessed.c -o a.out; objdump --disassemble --section=.text a.out > g.txt
$ diff no_g.txt g.txt
98,106c98,105
<   401144: ba 02 00 00 00          mov    $0x2,%edx
<   401149: 29 f2                   sub    %esi,%edx
<   40114b: 29 fa                   sub    %edi,%edx
<   40114d: 39 fe                   cmp    %edi,%esi
<   40114f: 41 0f 4c d1             cmovl  %r9d,%edx
<   401153: 89 15 e3 2e 00 00       mov    %edx,0x2ee3(%rip)        # 40403c <b>
<   401159: 8b 74 24 fc             mov    -0x4(%rsp),%esi
<   40115d: eb d1                   jmp    401130 <main+0x20>
<   40115f: 90                      nop
---
>   401144: 8d 0c 3e                lea    (%rsi,%rdi,1),%ecx
>   401147: ba 02 00 00 00          mov    $0x2,%edx
>   40114c: 29 ca                   sub    %ecx,%edx
>   40114e: 39 fe                   cmp    %edi,%esi
>   401150: 41 0f 4c d1             cmovl  %r9d,%edx
>   401154: 89 15 e2 2e 00 00       mov    %edx,0x2ee2(%rip)        # 40403c <b>
>   40115a: 8b 74 24 fc             mov    -0x4(%rsp),%esi
>   40115e: eb d0                   jmp    401130 <main+0x20>
$
theo-lw commented 2 years ago

For what it's worth, I tried reproducing this on previous versions of clang. I could reproduce it on clang-13 but not on clang-12. Still seems reproducible on clang-trunk.

$ clang-13 -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git ec1a49170129ddb62f268ff0b3f12b3d09987a7e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /scratch/software/clang-trunk/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
$ clang-12 -v
clang version 12.0.0 (https://github.com/llvm/llvm-project.git ad7aaa475e5e632242b07380ec47d2f35d077209)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /scratch/software/clang-trunk/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64