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

Shan Huang via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 18:52:52 PDT 2024


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

>From 15e8951d30c3cfcd908df765fabac11a3421ad72 Mon Sep 17 00:00:00 2001
From: Apochens <52285902006 at stu.ecnu.edu.cn>
Date: Fri, 22 Mar 2024 11:08:49 +0000
Subject: [PATCH 1/4] [Debuginfo][TailCallElim] Fix #86262: drop the debug
 location of entry branch

---
 .../Scalar/TailRecursionElimination.cpp       |  6 +-
 llvm/test/Transforms/TailCallElim/debugloc.ll |  4 +-
 .../entry-branch-drop-debugloc.ll             | 60 +++++++++++++++++++
 3 files changed, 67 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll

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)

>From 63b07e625d6d46aca4546457307a0c87e144be08 Mon Sep 17 00:00:00 2001
From: Apochens <52285902006 at stu.ecnu.edu.cn>
Date: Fri, 22 Mar 2024 13:58:10 +0000
Subject: [PATCH 2/4] remove redundant attrs

---
 .../TailCallElim/entry-branch-drop-debugloc.ll           | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll b/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
index 5e105537908df1..caadd4933ef5dd 100644
--- a/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
+++ b/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
@@ -1,7 +1,6 @@
 ; RUN: opt -S -passes=tailcallelim < %s | FileCheck %s
 
-; Function Attrs: noinline nounwind uwtable
-define dso_local i32 @func(i32 noundef %a) #0 !dbg !10 {
+define dso_local i32 @func(i32 noundef %a) !dbg !10 {
 ; CHECK: entry
 ; CHECK: br label %tailrecurse{{$}}
 entry:
@@ -23,11 +22,9 @@ return:                                           ; preds = %if.end, %if.then
   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
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
 
-; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
-declare void @llvm.dbg.value(metadata, metadata, metadata) #1
+declare void @llvm.dbg.value(metadata, metadata, metadata)
 
 !llvm.dbg.cu = !{!0}
 !llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}

>From bf3ccfbc010c032495a9ae37adba722a9d275a05 Mon Sep 17 00:00:00 2001
From: Shan Huang <52285902006 at stu.ecnu.edu.cn>
Date: Fri, 22 Mar 2024 22:48:31 +0800
Subject: [PATCH 3/4] Update
 llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp

Simplify the fix

Co-authored-by: Stephen Tozer <Melamoto at gmail.com>
---
 llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
index e4373fbf431ebd..34d39f3fe6dcac 100644
--- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -512,9 +512,7 @@ void TailRecursionEliminator::createTailRecurseLoopHeader(CallInst *CI) {
   BranchInst *BI = BranchInst::Create(HeaderBB, NewEntry);
   // 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();
+  // So, here we don't give any debug location to BI.
 
   // Move all fixed sized allocas from HeaderBB to NewEntry.
   for (BasicBlock::iterator OEBI = HeaderBB->begin(), E = HeaderBB->end(),

>From cbc44b67803b1da47a57619853ad0af29dfdbac2 Mon Sep 17 00:00:00 2001
From: Apochens <52285902006 at stu.ecnu.edu.cn>
Date: Sat, 23 Mar 2024 01:52:39 +0000
Subject: [PATCH 4/4] remove a redundant test

---
 .../entry-branch-drop-debugloc.ll             | 57 -------------------
 1 file changed, 57 deletions(-)
 delete mode 100644 llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll

diff --git a/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll b/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
deleted file mode 100644
index caadd4933ef5dd..00000000000000
--- a/llvm/test/Transforms/TailCallElim/entry-branch-drop-debugloc.ll
+++ /dev/null
@@ -1,57 +0,0 @@
-; RUN: opt -S -passes=tailcallelim < %s | FileCheck %s
-
-define dso_local i32 @func(i32 noundef %a) !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
-}
-
-declare void @llvm.dbg.declare(metadata, metadata, metadata)
-
-declare void @llvm.dbg.value(metadata, metadata, metadata)
-
-!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)



More information about the llvm-commits mailing list