Skip to content

Incorrect debug info generated at clang-17 -O3 (clang-16 -O3 is correct) #70951

@hemangdash

Description

@hemangdash
$ clang-16 --version
clang version 16.0.4
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin

$ clang-17 --version
clang version 17.0.3
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
$ cat abc.c
int a, b, c, e, l, n, p, q;
char d, g, h, i, j, k, r;
static char f = 5;
char m[6][1];
short o;
int s() {
  a++;
  for (;;) {
    short t[1];
    n = 0;
    t[n];
    k = p = f;
    for (; e < 6; e = 8)
      g ^= c = 1 & g;
    i--;
    r = b ^= r;
    char l_1566 = 1;
    ++j;
    q = l_1566 = h = l = i;
    if (i)
      return 0;
    f = 0;
    o = t[0];
    if (o)
      (c = 1) ^ m[5][a] || (d = 1);
    if (c)
      for (;;)
        ;
  }
}
void main() { s(); }
$ clang-16 abc.c -g -O3
$ lldb ./a.out -s cmds
(lldb) target create "./a.out"
(lldb) command source -s 0 'cmds'
(lldb) b 18
Breakpoint 1: where = a.out`s + 105 at abc.c:18:5, address = 0x0000000000001199
(lldb) r
Process 2808175 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555199 a.out`s at abc.c:18:5
   15       i--;
   16       r = b ^= r;
   17       char l_1566 = 1;
-> 18       ++j;
   19       q = l_1566 = h = l = i;
   20       if (i)
   21         return 0;

Process 2808175 launched: '<datapath>/a.out' (x86_64)
(lldb) p l_1566
(char) $0 = '\x01'
(lldb) kill
Process 2808175 exited with status = 9 (0x00000009) 
(lldb) q
$ clang-17 abc.c -g -O3
$ lldb ./a.out -s cmds
(lldb) target create "./a.out"
Current executable set to <datapath>/a.out' (x86_64).
(lldb) command source -s 0 'cmds'
Executing commands in '<datapath>/cmds'.
(lldb) b 18
Breakpoint 1: 2 locations.
(lldb) r
Process 2886988 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.2
    frame #0: 0x000055555555532b a.out`main at abc.c:18:5
   15       i--;
   16       r = b ^= r;
   17       char l_1566 = 1;
-> 18       ++j;
   19       q = l_1566 = h = l = i;
   20       if (i)
   21         return 0;

Process 2886988 launched: '<datapath>/a.out' (x86_64)
(lldb) p l_1566
(char) $0 = '\xff'
(lldb) kill
Process 2886988 exited with status = 9 (0x00000009) 
(lldb) q
$ cat cmds
b 18
r
p l_1566
kill
q

The value printed using lldb in clang-16 -O3 ('\x01') and clang-17 -O3 ('\xff') are different.

By using git bisection, we find that this issue was introduced in the following commit:

commit 4343699
Author: Nikita Popov npopov@redhat.com @nikic
Date: Tue Apr 25 10:04:49 2023 +0200

[LICM] Don't try to constant fold instructions

This was introduced in 030f02021b6359ec5641622cf1aa63d873ecf55a as
an alleged compile-time optimization. In reality, trying to constant
fold instructions is more expensive than just hoisting them. In a
standard pipeline, LICM tends to run either after a run of
LoopInstSimplify or InstCombine, so LICM doesn't really see constant
foldable instructions in the first place, and the attempted fold
is futile.

This makes for a very minor compile-time improvement.

Differential Revision: https://reviews.llvm.org/D149134

Metadata

Metadata

Assignees

No one assigned

    Labels

    debuginfoquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions