llvm / llvm-project

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

[debuginfo] [optimizations] "opt -passes='function(sroa<modify-cfg>),ipsccp' " leads to debug information missing #86215

Open edumoot opened 8 months ago

edumoot commented 8 months ago

LLVM 17.0.6 (commit, 6009fe18)

Reproduce with:

clang -g -O0 case.c -Xclang -disable-O0-optnone -emit-llvm -c -o case.bc
opt -passes='function(sroa<modify-cfg>),ipsccp' case.bc  -o case.opt.bc
llc case.opt.bc -o case.s
clang case.s -o case.out

Result 1: variable b is optimized out (set a breakpoint on line 22 on case.c)

(lldb) b case.c:21
Breakpoint 1: where = case.out`func_c + 4 at case.c:21:9, address = 0x0000000000001184
(lldb) r
Process 125277 launched: '/home/ad/Downloads/lldb/reproduce_bugs/case.out' (x86_64)
Process 125277 stopped
* thread #1, name = 'case.out', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555184 case.out`func_c at case.c:21:9
   18       int a = 55;
   19       int b = 8888;
   20       int c = 1111;
-> 21       b = func_a() && func_b(a, c);
   22       return b;
   23   }
   24   
(lldb) fr v
(int) a = 55
(int) b = 8888
(int) c = 1111
(lldb) n
Process 125277 stopped
* thread #1, name = 'case.out', stop reason = step over
    frame #0: 0x000055555555519f case.out`func_c at case.c:22:5
   19       int b = 8888;
   20       int c = 1111;
   21       b = func_a() && func_b(a, c);
-> 22       return b;
   23   }
   24   
   25   int main (void)
(lldb) fr v
(int) a = 55
(int) b = <variable not available>

(int) c = 1111

Result 2: a[4]=0 (set a breakpoint on line 8 on case.c)

(lldb) b case.c:8
Breakpoint 2: where = case.out`func_a + 26 at case.c:8:5, address = 0x000055555555515a
(lldb) r
There is a running process, kill it and restart?: [Y/n] y
Process 125277 exited with status = 9 (0x00000009) killed
Process 125858 launched: '/home/ad/Downloads/lldb/reproduce_bugs/case.out' (x86_64)
Process 125858 stopped
* thread #1, name = 'case.out', stop reason = breakpoint 2.1
    frame #0: 0x000055555555515a case.out`func_a at case.c:8:5
   5    int func_a()
   6    { 
   7        int a[9] = {2, 2, 2, 2, 2, 3, 3, 3, 3};
-> 8        return a[4];
   9    }
   10   
   11   int  func_b(int  p1, int  p2)
(lldb) fr v
(int[9]) a = ([0] = 2, [1] = 2, [2] = 2, [3] = 2, [4] = 0, [5] = 3, [6] = 3, [7] = 3, [8] = 3)

cat case.c

#include <stdio.h>
static unsigned g1 = 15;
static unsigned g[3] = {3, 3, 3};

int func_a()
{ 
    int a[9] = {2, 2, 2, 2, 2, 3, 3, 3, 3};
    return a[4];
}

int  func_b(int  p1, int  p2)
{   
    return g[1]+p1+p2;
}

int  func_c()
{ 
    int a = 55;
    int b = 8888;
    int c = 1111;
    b = func_a() && func_b(a, c);
    return b;
}

int main (void)
{   int i = 0;
    printf("%d",func_c());
    return 0;
}
llvmbot commented 8 months ago

@llvm/issue-subscribers-debuginfo

Author: Yachao Zhu (edumoot)

LLVM commit (17.0.6), [6009fe18](https://github.com/llvm/llvm-project/commit/6009708b4367171ccdbf4b5905cb6a803753fe18) Reproduce with: ```compiling pipeline clang -g -O0 case.c -Xclang -disable-O0-optnone -emit-llvm -c -o case.bc opt -passes='function(sroa<modify-cfg>),ipsccp' case.bc -o case.opt.bc llc case.opt.bc -o case.s clang case.s -o case.out ``` Result 1: variable b is optimized out (set a breakpoint on line 22 on case.c) ```lldb (lldb) b case.c:21 Breakpoint 1: where = case.out`func_c + 4 at case.c:21:9, address = 0x0000000000001184 (lldb) r Process 125277 launched: '/home/ad/Downloads/lldb/reproduce_bugs/case.out' (x86_64) Process 125277 stopped * thread #1, name = 'case.out', stop reason = breakpoint 1.1 frame #0: 0x0000555555555184 case.out`func_c at case.c:21:9 18 int a = 55; 19 int b = 8888; 20 int c = 1111; -> 21 b = func_a() && func_b(a, c); 22 return b; 23 } 24 (lldb) fr v (int) a = 55 (int) b = 8888 (int) c = 1111 (lldb) n Process 125277 stopped * thread #1, name = 'case.out', stop reason = step over frame #0: 0x000055555555519f case.out`func_c at case.c:22:5 19 int b = 8888; 20 int c = 1111; 21 b = func_a() && func_b(a, c); -> 22 return b; 23 } 24 25 int main (void) (lldb) fr v (int) a = 55 (int) b = <variable not available> (int) c = 1111 ``` Result 2: a[4]=0 (set a breakpoint on line 8 on case.c) ```lldb (lldb) b case.c:8 Breakpoint 2: where = case.out`func_a + 26 at case.c:8:5, address = 0x000055555555515a (lldb) r There is a running process, kill it and restart?: [Y/n] y Process 125277 exited with status = 9 (0x00000009) killed Process 125858 launched: '/home/ad/Downloads/lldb/reproduce_bugs/case.out' (x86_64) Process 125858 stopped * thread #1, name = 'case.out', stop reason = breakpoint 2.1 frame #0: 0x000055555555515a case.out`func_a at case.c:8:5 5 int func_a() 6 { 7 int a[9] = {2, 2, 2, 2, 2, 3, 3, 3, 3}; -> 8 return a[4]; 9 } 10 11 int func_b(int p1, int p2) (lldb) fr v (int[9]) a = ([0] = 2, [1] = 2, [2] = 2, [3] = 2, [4] = 0, [5] = 3, [6] = 3, [7] = 3, [8] = 3) ``` `cat case.c` ```source code #include <stdio.h> static unsigned g1 = 15; static unsigned g[3] = {3, 3, 3}; int func_a() { int a[9] = {2, 2, 2, 2, 2, 3, 3, 3, 3}; return a[4]; } int func_b(int p1, int p2) { return g[1]+p1+p2; } int func_c() { int a = 55; int b = 8888; int c = 1111; b = func_a() && func_b(a, c); return b; } int main (void) { int i = 0; printf("%d",func_c()); return 0; } ```
EugeneZelenko commented 8 months ago

Could you please try 18 or main branch?

edumoot commented 8 months ago

These two results also can be reproduced by opt -passes='function(sroa),ipsccp'

For result 1, we can get the following information through parsing further details:

0x000000be:     DW_TAG_variable
                  DW_AT_location    (indexed (0x1) loclist = 0x00000022: 
                     [0x0000000000001184, 0x000000000000119f): DW_OP_consts +8888, DW_OP_stack_value)
                  DW_AT_name    ("b")
                  DW_AT_decl_file   ("/home/ad/Downloads/lldb/reproduce_bugs/case.c")
                  DW_AT_decl_line   (19)
                  DW_AT_type    (0x000000eb "int")

comparing with generated debug information from one of binaries by applying a different sequence of optimization passes

0x000000ae:     DW_TAG_variable
                  DW_AT_location    (indexed (0x4) loclist = 0x00000058: 
                     [0x0000000000001174, 0x000000000000119c): DW_OP_consts +8888, DW_OP_stack_value
                     [0x000000000000119c, 0x000000000000119e): DW_OP_consts +1, DW_OP_stack_value)

We might infer that some location information is missing, due to this sequence of optimization passes.

edumoot commented 8 months ago

Could you please try 18 or main branch?

The problems exist: LLVM 18.1.2 (commit 261aae0c) LLVM 16.0.3 (commit da3cb08c)