Skip to content

Emitted binary code changes when -g is enabled at -O0 #50525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
theo-lw opened this issue Jul 23, 2021 · 6 comments
Open

Emitted binary code changes when -g is enabled at -O0 #50525

theo-lw opened this issue Jul 23, 2021 · 6 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party debuginfo llvm:codegen

Comments

@theo-lw
Copy link
Contributor

theo-lw commented Jul 23, 2021

Bugzilla Link 51181
Version trunk
OS Linux
Blocks #37076
CC @chengniansun,@jmorse,@pogo59

Extended Description

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

$ cat transformed_program0.c
int main() {
  goto a;
b:
  return 0;
a:;
}
$
$ clang-trunk -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git ab5ac659c81a453a3f58df94f39fe8ba3cd35918)
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
$
$ clang-trunk -O0 transformed_program0.c ; objdump --disassemble --section=.text a.out > no_g.txt
$ clang-trunk -g -O0 transformed_program0.c ; objdump --disassemble --section=.text a.out > g.txt
$ diff no_g.txt g.txt
16,17c16,17
<   401033:	49 c7 c0 90 11 40 00 	mov    $0x401190,%r8
<   40103a:	48 c7 c1 20 11 40 00 	mov    $0x401120,%rcx
---
>   401033:	49 c7 c0 a0 11 40 00 	mov    $0x4011a0,%r8
>   40103a:	48 c7 c1 30 11 40 00 	mov    $0x401130,%rcx
90,133c90,131
<   40111b:	8b 45 fc             	mov    -0x4(%rbp),%eax
<   40111e:	5d                   	pop    %rbp
<   40111f:	c3                   	retq
<
< 0000000000401120 <__libc_csu_init>:
<   401120:	f3 0f 1e fa          	endbr64
<   401124:	41 57                	push   %r15
<   401126:	4c 8d 3d 23 2d 00 00 	lea    0x2d23(%rip),%r15        # 403e50 <__frame_dummy_init_array_entry>
<   40112d:	41 56                	push   %r14
<   40112f:	49 89 d6             	mov    %rdx,%r14
<   401132:	41 55                	push   %r13
<   401134:	49 89 f5             	mov    %rsi,%r13
<   401137:	41 54                	push   %r12
<   401139:	41 89 fc             	mov    %edi,%r12d
<   40113c:	55                   	push   %rbp
<   40113d:	48 8d 2d 14 2d 00 00 	lea    0x2d14(%rip),%rbp        # 403e58 <__do_global_dtors_aux_fini_array_entry>
<   401144:	53                   	push   %rbx
<   401145:	4c 29 fd             	sub    %r15,%rbp
<   401148:	48 83 ec 08          	sub    $0x8,%rsp
<   40114c:	e8 af fe ff ff       	callq  401000 <_init>
<   401151:	48 c1 fd 03          	sar    $0x3,%rbp
<   401155:	74 1f                	je     401176 <__libc_csu_init+0x56>
<   401157:	31 db                	xor    %ebx,%ebx
<   401159:	0f 1f 80 00 00 00 00 	nopl   0x0(%rax)
<   401160:	4c 89 f2             	mov    %r14,%rdx
<   401163:	4c 89 ee             	mov    %r13,%rsi
<   401166:	44 89 e7             	mov    %r12d,%edi
<   401169:	41 ff 14 df          	callq  *(%r15,%rbx,8)
<   40116d:	48 83 c3 01          	add    $0x1,%rbx
<   401171:	48 39 dd             	cmp    %rbx,%rbp
<   401174:	75 ea                	jne    401160 <__libc_csu_init+0x40>
<   401176:	48 83 c4 08          	add    $0x8,%rsp
<   40117a:	5b                   	pop    %rbx
<   40117b:	5d                   	pop    %rbp
<   40117c:	41 5c                	pop    %r12
<   40117e:	41 5d                	pop    %r13
<   401180:	41 5e                	pop    %r14
<   401182:	41 5f                	pop    %r15
<   401184:	c3                   	retq
<   401185:	66 66 2e 0f 1f 84 00 	data16 nopw %cs:0x0(%rax,%rax,1)
<   40118c:	00 00 00 00
<
< 0000000000401190 <__libc_csu_fini>:
<   401190:	f3 0f 1e fa          	endbr64
---
>   40111b:	e9 00 00 00 00       	jmpq   401120 <main+0x10>
>   401120:	8b 45 fc             	mov    -0x4(%rbp),%eax
>   401123:	5d                   	pop    %rbp
>   401124:	c3                   	retq
>   401125:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
>   40112c:	00 00 00
>   40112f:	90                   	nop
>
> 0000000000401130 <__libc_csu_init>:
>   401130:	f3 0f 1e fa          	endbr64
>   401134:	41 57                	push   %r15
>   401136:	4c 8d 3d 13 2d 00 00 	lea    0x2d13(%rip),%r15        # 403e50 <__frame_dummy_init_array_entry>
>   40113d:	41 56                	push   %r14
>   40113f:	49 89 d6             	mov    %rdx,%r14
>   401142:	41 55                	push   %r13
>   401144:	49 89 f5             	mov    %rsi,%r13
>   401147:	41 54                	push   %r12
>   401149:	41 89 fc             	mov    %edi,%r12d
>   40114c:	55                   	push   %rbp
>   40114d:	48 8d 2d 04 2d 00 00 	lea    0x2d04(%rip),%rbp        # 403e58 <__do_global_dtors_aux_fini_array_entry>
>   401154:	53                   	push   %rbx
>   401155:	4c 29 fd             	sub    %r15,%rbp
>   401158:	48 83 ec 08          	sub    $0x8,%rsp
>   40115c:	e8 9f fe ff ff       	callq  401000 <_init>
>   401161:	48 c1 fd 03          	sar    $0x3,%rbp
>   401165:	74 1f                	je     401186 <__libc_csu_init+0x56>
>   401167:	31 db                	xor    %ebx,%ebx
>   401169:	0f 1f 80 00 00 00 00 	nopl   0x0(%rax)
>   401170:	4c 89 f2             	mov    %r14,%rdx
>   401173:	4c 89 ee             	mov    %r13,%rsi
>   401176:	44 89 e7             	mov    %r12d,%edi
>   401179:	41 ff 14 df          	callq  *(%r15,%rbx,8)
>   40117d:	48 83 c3 01          	add    $0x1,%rbx
>   401181:	48 39 dd             	cmp    %rbx,%rbp
>   401184:	75 ea                	jne    401170 <__libc_csu_init+0x40>
>   401186:	48 83 c4 08          	add    $0x8,%rsp
>   40118a:	5b                   	pop    %rbx
>   40118b:	5d                   	pop    %rbp
>   40118c:	41 5c                	pop    %r12
>   40118e:	41 5d                	pop    %r13
>   401190:	41 5e                	pop    %r14
>   401192:	41 5f                	pop    %r15
134a133,138
>   401195:	66 66 2e 0f 1f 84 00 	data16 nopw %cs:0x0(%rax,%rax,1)
>   40119c:	00 00 00 00
>
> 00000000004011a0 <__libc_csu_fini>:
>   4011a0:	f3 0f 1e fa          	endbr64
>   4011a4:	c3                   	retq
@jmorse
Copy link
Member

jmorse commented Jul 23, 2021

It looks to me like unreachableblockelim declines to delete a block with a dbg.label intrinsic in it, which leads to an extraneous empty block.

@pogo59
Copy link
Collaborator

pogo59 commented Jul 23, 2021

It looks to me like unreachableblockelim declines to delete a block with a
dbg.label intrinsic in it, which leads to an extraneous empty block.

Right, the net difference is this extra jmp:

40111b: e9 00 00 00 00 jmpq 401120 <main+0x10>
and everything else is a consequence of the extra instruction
causing addresses of things to change.

Knowing when to ignore debug-info instructions is an ongoing pain point.

@theo-lw
Copy link
Contributor Author

theo-lw commented Aug 19, 2021

Just for reference, here's the diff of the object files.

$ clang-trunk -v
clang version 14.0.0 (https://github.com/llvm/llvm-project.git f5d5f17d3ad455de2fbb9448acea66cbc09561c5)
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
$
$ clang-trunk -O0 -c program.c ; objdump --disassemble --section=.text program.o > no_g.txt
$ clang-trunk -O0 -c -g program.c ; objdump --disassemble --section=.text program.o > g.txt
$ diff no_g.txt g.txt
11,13c11,14
<    b:	8b 45 fc             	mov    -0x4(%rbp),%eax
<    e:	5d                   	pop    %rbp
<    f:	c3                   	retq
---
>    b:	e9 00 00 00 00       	jmpq   10 <main+0x10>
>   10:	8b 45 fc             	mov    -0x4(%rbp),%eax
>   13:	5d                   	pop    %rbp
>   14:	c3                   	retq

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
@jmorse jmorse self-assigned this Apr 25, 2022
@jmorse
Copy link
Member

jmorse commented Apr 25, 2022

Patch: https://reviews.llvm.org/D124398

@jmorse
Copy link
Member

jmorse commented May 3, 2022

Hhhmmmm, actually that patch doesn't fix it. If you run clang test.cpp -o - -c -g -Xclang -disable-llvm-passes -S emit-llvm with and without -g, observe that the blocks created by the clang frontend are different. We can't hope to make the codegen the same if the inputs to LLVM are different -- this is something wrong in the frontend IR generator.

@llvmbot
Copy link
Member

llvmbot commented May 11, 2022

@llvm/issue-subscribers-debuginfo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party debuginfo llvm:codegen
Projects
None yet
Development

No branches or pull requests

5 participants