[llvm] [DebugInfo][LowerConstantIntrinsics] Fix the missing debug location of new branch instruction (PR #97145)

Shan Huang via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 29 00:28:40 PDT 2024


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

>From df13e0f332615bb9f634a1c961d5b44613c9a6ee Mon Sep 17 00:00:00 2001
From: Apochens <52285902006 at stu.ecnu.edu.cn>
Date: Sat, 29 Jun 2024 07:17:42 +0000
Subject: [PATCH 1/2] fix the missing debug location

---
 .../Scalar/LowerConstantIntrinsics.cpp        |  5 +-
 .../preserving-debugloc-br.ll                 | 46 +++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll

diff --git a/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp b/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
index 939c36164f781..709150fb48b4d 100644
--- a/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
@@ -85,8 +85,11 @@ static bool replaceConditionalBranchesOnConstant(Instruction *II,
     if (Target && Target != Other) {
       BasicBlock *Source = BI->getParent();
       Other->removePredecessor(Source);
+
+      Instruction * NewBI = BranchInst::Create(Target, Source);
+      NewBI->setDebugLoc(BI->getDebugLoc());
       BI->eraseFromParent();
-      BranchInst::Create(Target, Source);
+      
       if (DTU)
         DTU->applyUpdates({{DominatorTree::Delete, Source, Other}});
       if (pred_empty(Other))
diff --git a/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll b/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll
new file mode 100644
index 0000000000000..e73b30f38d4b8
--- /dev/null
+++ b/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll
@@ -0,0 +1,46 @@
+; RUN: opt -S -passes=lower-constant-intrinsics < %s | FileCheck %s
+
+; Function Attrs: nounwind
+define i32 @test_branch(i32 %in) !dbg !5 {
+; CHECK-LABEL: define i32 @test_branch(
+; CHECK:           br label %[[FALSE:.*]], !dbg [[DBG8:![0-9]+]]
+; CHECK:       [[FALSE]]:
+;
+  %v = call i1 @llvm.is.constant.i32(i32 %in), !dbg !8
+  br i1 %v, label %True, label %False, !dbg !9
+
+True:                                             ; preds = %0
+  %call1 = tail call i32 @subfun_1(), !dbg !10
+  ret i32 %call1, !dbg !11
+
+False:                                            ; preds = %0
+  %call2 = tail call i32 @subfun_2(), !dbg !12
+  ret i32 %call2, !dbg !13
+}
+
+declare i32 @subfun_1()
+declare i32 @subfun_2()
+
+declare i1 @llvm.is.constant.i32(i32)
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+; CHECK: [[DBG8]] = !DILocation(line: 2,
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "main.ll", directory: "/")
+!2 = !{i32 6}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test_branch", linkageName: "test_branch", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocation(line: 1, column: 1, scope: !5)
+!9 = !DILocation(line: 2, column: 1, scope: !5)
+!10 = !DILocation(line: 3, column: 1, scope: !5)
+!11 = !DILocation(line: 4, column: 1, scope: !5)
+!12 = !DILocation(line: 5, column: 1, scope: !5)
+!13 = !DILocation(line: 6, column: 1, scope: !5)
+

>From f7cc5f0772b7a8f13630ad726c5d2862e756113f Mon Sep 17 00:00:00 2001
From: Apochens <52285902006 at stu.ecnu.edu.cn>
Date: Sat, 29 Jun 2024 07:28:30 +0000
Subject: [PATCH 2/2] nit

---
 llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp        | 4 ++--
 .../LowerConstantIntrinsics/preserving-debugloc-br.ll         | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp b/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
index 709150fb48b4d..bd7895feb64a7 100644
--- a/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
@@ -86,10 +86,10 @@ static bool replaceConditionalBranchesOnConstant(Instruction *II,
       BasicBlock *Source = BI->getParent();
       Other->removePredecessor(Source);
 
-      Instruction * NewBI = BranchInst::Create(Target, Source);
+      Instruction *NewBI = BranchInst::Create(Target, Source);
       NewBI->setDebugLoc(BI->getDebugLoc());
       BI->eraseFromParent();
-      
+
       if (DTU)
         DTU->applyUpdates({{DominatorTree::Delete, Source, Other}});
       if (pred_empty(Other))
diff --git a/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll b/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll
index e73b30f38d4b8..0302c9d6435ca 100644
--- a/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll
+++ b/llvm/test/Transforms/LowerConstantIntrinsics/preserving-debugloc-br.ll
@@ -1,5 +1,8 @@
 ; RUN: opt -S -passes=lower-constant-intrinsics < %s | FileCheck %s
 
+; Check that LowerConstantIntrinsics's replaceConditionalBranchesOnConstant() correctly
+; propagates the debug location from the old br instruction to the new one.
+
 ; Function Attrs: nounwind
 define i32 @test_branch(i32 %in) !dbg !5 {
 ; CHECK-LABEL: define i32 @test_branch(
@@ -43,4 +46,3 @@ declare i1 @llvm.is.constant.i32(i32)
 !11 = !DILocation(line: 4, column: 1, scope: !5)
 !12 = !DILocation(line: 5, column: 1, scope: !5)
 !13 = !DILocation(line: 6, column: 1, scope: !5)
-



More information about the llvm-commits mailing list