llvm / llvm-project

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

Debug location information dropped unnecessarily in div-rem-pairs optimization pass. #55515

Open cbossut7 opened 2 years ago

cbossut7 commented 2 years ago

Overview: In the attached example, the div-rem-pairs pass will drop debug location unnecessarily.

Steps to reproduce (files attached at the end):

Here is the version of llvm:

$ ../llvm-build/bin/clang++ --version
clang version 15.0.0 (https://github.com/llvm/llvm-project.git f422c5d87142cc1dc65cf7fb15444c2afc810611)
Target: x86_64-unknown-linux-gnu
Thread model: posix

$ opt -div-rem-pairs debugified_out.ll > debugified_opt.bc
$ llvm-dis debugified_opt.bc

$ diff debugified_out.ll debugified_opt.ll 
1c1
< ; ModuleID = 'bugpoint-reduced-simplified.ll'
---
> ; ModuleID = 'debugified_out.ll'
3c3
< target triple = "x86_64-unknown-linux-gnu"
---
> target triple = "x86_64-unknown-unknown"
11,12c11,12
<   %rem = sub i16 %a, %t0, !dbg !15
<   call void @llvm.dbg.value(metadata i16 %rem, metadata !12, metadata !DIExpression()), !dbg !15
---
>   %rem.recomposed = srem i16 %a, %b
>   call void @llvm.dbg.value(metadata i16 %rem.recomposed, metadata !12, metadata !DIExpression()), !dbg !15

The variable %rem seems to have a one-to-one relationship with %rem.recomposed, so it seems the debug location !15 is unnecessarily dropped, according to the principles in: https://llvm.org/docs/HowToUpdateDebugInfo.html

There's a one-to-one relationship between the new and old instruction, and it's placed in the same location, so the DebugLoc should be preserved in the reproducer.

Relevant files: File 1—------------------------------------------------------------------------------

$ cat debugified_out.ll
; ModuleID = 'bugpoint-reduced-simplified.ll'
source_filename = "div-expanded-rem-pair.ll"
target triple = "x86_64-unknown-linux-gnu"

define void @hoist_srem(i16 %a, i16 %b) !dbg !5 {
entry:
  %div = sdiv i16 %a, %b, !dbg !13
  call void @llvm.dbg.value(metadata i16 %div, metadata !9, metadata !DIExpression()), !dbg !13
  %t0 = mul i16 %div, %b, !dbg !14
  call void @llvm.dbg.value(metadata i16 %t0, metadata !11, metadata !DIExpression()), !dbg !14
  %rem = sub i16 %a, %t0, !dbg !15
  call void @llvm.dbg.value(metadata i16 %rem, metadata !12, metadata !DIExpression()), !dbg !15
  unreachable, !dbg !16
}

; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
declare void @llvm.dbg.value(metadata, metadata, metadata) #0

attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!0}
!llvm.debugify = !{!2, !3}
!llvm.module.flags = !{!4}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "bugpoint-reduced-simplified.ll", directory: "/")
!2 = !{i32 4}
!3 = !{i32 3}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "hoist_srem", linkageName: "hoist_srem", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!6 = !DISubroutineType(types: !7)
!7 = !{}
!8 = !{!9, !11, !12}
!9 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !10)
!12 = !DILocalVariable(name: "3", scope: !5, file: !1, line: 3, type: !10)
!13 = !DILocation(line: 1, column: 1, scope: !5)
!14 = !DILocation(line: 2, column: 1, scope: !5)
!15 = !DILocation(line: 3, column: 1, scope: !5)
!16 = !DILocation(line: 4, column: 1, scope: !5)

File 2-----------------------------------------------------------------------

$ cat debugified_opt.ll
; ModuleID = 'debugified_out.ll'
source_filename = "div-expanded-rem-pair.ll"
target triple = "x86_64-unknown-unknown"

define void @hoist_srem(i16 %a, i16 %b) !dbg !5 {
entry:
  %div = sdiv i16 %a, %b, !dbg !13
  call void @llvm.dbg.value(metadata i16 %div, metadata !9, metadata !DIExpression()), !dbg !13
  %t0 = mul i16 %div, %b, !dbg !14
  call void @llvm.dbg.value(metadata i16 %t0, metadata !11, metadata !DIExpression()), !dbg !14
  %rem.recomposed = srem i16 %a, %b
  call void @llvm.dbg.value(metadata i16 %rem.recomposed, metadata !12, metadata !DIExpression()), !dbg !15
  unreachable, !dbg !16
}

; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
declare void @llvm.dbg.value(metadata, metadata, metadata) #0

attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!0}
!llvm.debugify = !{!2, !3}
!llvm.module.flags = !{!4}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "bugpoint-reduced-simplified.ll", directory: "/")
!2 = !{i32 4}
!3 = !{i32 3}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "hoist_srem", linkageName: "hoist_srem", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!6 = !DISubroutineType(types: !7)
!7 = !{}
!8 = !{!9, !11, !12}
!9 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !10)
!12 = !DILocalVariable(name: "3", scope: !5, file: !1, line: 3, type: !10)
!13 = !DILocation(line: 1, column: 1, scope: !5)
!14 = !DILocation(line: 2, column: 1, scope: !5)
!15 = !DILocation(line: 3, column: 1, scope: !5)
!16 = !DILocation(line: 4, column: 1, scope: !5)
llvmbot commented 2 years ago

@llvm/issue-subscribers-debuginfo

jmorse commented 2 years ago

Good find, thanks for the comprehensive bug report -- it looks like the site that creates the instruction is here:

https://github.com/llvm/llvm-project/blob/f00f894d5d828c3ea7757f62268eaa4cd9a28c88/llvm/lib/Transforms/Scalar/DivRemPairs.cpp#L209

and it indeed doesn't call setDebugLoc on the instruction it creates. The fix would be to use setDebugLoc to preserve the line number, and write a test to ensure that behaviour is preserved (probably using your reproducer). As you're already using debugify to find bugs, do you feel up to submitting a patch for that?