[llvm] [Debuginfo][TailCallElim] Fix #86262: drop the debug location of entry branch (PR #86269)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 04:10:12 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)

<details>
<summary>Changes</summary>

…y branch

---
Full diff: https://github.com/llvm/llvm-project/pull/86269.diff


3 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp (+5-1) 
- (modified) llvm/test/Transforms/TailCallElim/debugloc.ll (+2-2) 
- (added) llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll (+60) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
index 519ff3221a3bc3..e4373fbf431ebd 100644
--- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -510,7 +510,11 @@ void TailRecursionEliminator::createTailRecurseLoopHeader(CallInst *CI) {
   NewEntry->takeName(HeaderBB);
   HeaderBB->setName("tailrecurse");
   BranchInst *BI = BranchInst::Create(HeaderBB, NewEntry);
-  BI->setDebugLoc(CI->getDebugLoc());
+  // If the new branch preserves the debug location of CI, it could result in
+  // misleading stepping, if CI is located in a conditional branch.
+  // So, here we don't give any debug location to BI, and use dropLocation()
+  // to explicitly present this dicision.
+  BI->dropLocation();
 
   // Move all fixed sized allocas from HeaderBB to NewEntry.
   for (BasicBlock::iterator OEBI = HeaderBB->begin(), E = HeaderBB->end(),
diff --git a/llvm/test/Transforms/TailCallElim/debugloc.ll b/llvm/test/Transforms/TailCallElim/debugloc.ll
index 3abbd6552efce3..49957695a421b9 100644
--- a/llvm/test/Transforms/TailCallElim/debugloc.ll
+++ b/llvm/test/Transforms/TailCallElim/debugloc.ll
@@ -4,13 +4,13 @@
 define void @foo() {
 entry:
 ; CHECK-LABEL: entry:
-; CHECK: br label %tailrecurse, !dbg ![[DbgLoc:[0-9]+]]
+; CHECK: br label %tailrecurse{{$}}
 
   call void @foo()                            ;; line 1
   ret void
 
 ; CHECK-LABEL: tailrecurse:
-; CHECK: br label %tailrecurse, !dbg ![[DbgLoc]]
+; CHECK: br label %tailrecurse, !dbg ![[DbgLoc:[0-9]+]]
 }
 
 ;; Make sure tailrecurse has the call instruction's DL
diff --git a/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll b/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
new file mode 100644
index 00000000000000..5e105537908df1
--- /dev/null
+++ b/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
@@ -0,0 +1,60 @@
+; RUN: opt -S -passes=tailcallelim < %s | FileCheck %s
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @func(i32 noundef %a) #0 !dbg !10 {
+; CHECK: entry
+; CHECK: br label %tailrecurse{{$}}
+entry:
+  tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata !DIExpression()), !dbg !16
+  %cmp = icmp sgt i32 %a, 1, !dbg !17
+  br i1 %cmp, label %if.then, label %if.end, !dbg !19
+
+if.then:                                          ; preds = %entry
+  %sub = sub nsw i32 %a, 1, !dbg !20
+  %call = call i32 @func(i32 noundef %sub), !dbg !22
+  %mul = mul nsw i32 %a, %call, !dbg !23
+  br label %return, !dbg !24
+
+if.end:                                           ; preds = %entry
+  br label %return, !dbg !25
+
+return:                                           ; preds = %if.end, %if.then
+  %retval.0 = phi i32 [ %mul, %if.then ], [ 1, %if.end ], !dbg !16
+  ret i32 %retval.0, !dbg !26
+}
+
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
+
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare void @llvm.dbg.value(metadata, metadata, metadata) #1
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 19.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "main.c", directory: "/root/llvm-test/TailCallElim")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 8, !"PIC Level", i32 2}
+!6 = !{i32 7, !"PIE Level", i32 2}
+!7 = !{i32 7, !"uwtable", i32 2}
+!8 = !{i32 7, !"frame-pointer", i32 2}
+!10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
+!11 = !DISubroutineType(types: !12)
+!12 = !{!13, !13}
+!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!14 = !{}
+!15 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !1, line: 1, type: !13)
+!16 = !DILocation(line: 0, scope: !10)
+!17 = !DILocation(line: 2, column: 11, scope: !18)
+!18 = distinct !DILexicalBlock(scope: !10, file: !1, line: 2, column: 9)
+!19 = !DILocation(line: 2, column: 9, scope: !10)
+!20 = !DILocation(line: 3, column: 27, scope: !21)
+!21 = distinct !DILexicalBlock(scope: !18, file: !1, line: 2, column: 16)
+!22 = !DILocation(line: 3, column: 20, scope: !21)
+!23 = !DILocation(line: 3, column: 18, scope: !21)
+!24 = !DILocation(line: 3, column: 9, scope: !21)
+!25 = !DILocation(line: 5, column: 5, scope: !10)
+!26 = !DILocation(line: 6, column: 1, scope: !10)

``````````

</details>


https://github.com/llvm/llvm-project/pull/86269


More information about the llvm-commits mailing list