Skip to content

[DebugInfo][JumpThreading] Fix missing debug location updates for br instructions #96889

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

Merged

Conversation

Apochens
Copy link
Contributor

Fix #96885 .

@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)

Changes

Fix #96885 .


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+4-2)
  • (added) llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll (+114)
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 1aef2800e9846..bffe1f4c58518 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1038,7 +1038,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
 
     LLVM_DEBUG(dbgs() << "  In block '" << BB->getName()
                       << "' folding undef terminator: " << *BBTerm << '\n');
-    BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm->getIterator());
+    Instruction *NewBI = BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm->getIterator());
+    NewBI->setDebugLoc(BBTerm->getDebugLoc());
     ++NumFolds;
     BBTerm->eraseFromParent();
     DTU->applyUpdatesPermissive(Updates);
@@ -1657,7 +1658,8 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB,
 
       // Finally update the terminator.
       Instruction *Term = BB->getTerminator();
-      BranchInst::Create(OnlyDest, Term->getIterator());
+      Instruction *NewBI = BranchInst::Create(OnlyDest, Term->getIterator());
+      NewBI->setDebugLoc(Term->getDebugLoc());
       ++NumFolds;
       Term->eraseFromParent();
       DTU->applyUpdatesPermissive(Updates);
diff --git a/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll b/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll
new file mode 100644
index 0000000000000..ca67f0dec31ba
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll
@@ -0,0 +1,114 @@
+; RUN: opt -S -passes=jump-threading < %s | FileCheck %s
+
+; @process_block_branch checks that JumpThreading's processBlock() propagates 
+; the debug location to the new branch instruction.
+
+; @process_threadable_edges_branch checks that JumpThreading's processThreadableEdges()
+; propagates the debug location to the new branch instruction.
+
+define i32 @process_block_branch(i32 %action) #0 !dbg !5 {
+; CHECK-LABEL: define i32 @process_block_branch(
+; CHECK:       for.cond:
+; CHECK-NEXT:    br label %for.cond, !dbg [[DBG10:![0-9]+]]
+;
+entry:
+  switch i32 %action, label %lor.rhs [
+  i32 1, label %if.then
+  i32 0, label %lor.end
+  ], !dbg !8
+
+if.then:                                          ; preds = %for.cond, %lor.end, %entry
+  ret i32 undef, !dbg !9
+
+lor.rhs:                                          ; preds = %entry
+  br label %lor.end, !dbg !10
+
+lor.end:                                          ; preds = %lor.rhs, %entry
+  %cmp103 = xor i1 undef, undef, !dbg !11
+  br i1 %cmp103, label %for.cond, label %if.then, !dbg !12
+
+for.cond:                                         ; preds = %for.body, %lor.end
+  br i1 undef, label %if.then, label %for.body, !dbg !13
+
+for.body:                                         ; preds = %for.cond
+  br label %for.cond, !dbg !14
+}
+
+define void @process_threadable_edges_branch(i32 %value) #0 !dbg !15 {
+; CHECK-LABEL: define void @process_threadable_edges_branch(
+; CHECK:    L0:
+; CHECK:      br label %L2, !dbg [[DBG17:![0-9]+]]
+;
+entry:
+  %cmp = icmp eq i32 %value, 32, !dbg !16
+  %add = add i32 %value, 64, !dbg !17
+  br i1 %cmp, label %L0, label %L2, !dbg !18
+
+L0:                                               ; preds = %entry
+  %0 = call i32 @f2(), !dbg !19
+  %1 = call i32 @f2(), !dbg !20
+  switch i32 %add, label %L3 [
+  i32 32, label %L1
+  i32 96, label %L2
+  ], !dbg !21
+
+L1:                                               ; preds = %L0
+  call void @f3(), !dbg !22
+  ret void, !dbg !23
+
+L2:                                               ; preds = %L0, %entry
+  call void @f4(i32 %add), !dbg !24
+  ret void, !dbg !25
+
+L3:                                               ; preds = %L0
+  call void @f3(), !dbg !26
+  ret void, !dbg !27
+}
+
+declare i32 @f1()
+
+declare i32 @f2()
+
+declare void @f3()
+
+declare void @f4(i32)
+
+attributes #0 = { nounwind }
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+;.
+; CHECK: [[DBG10]] = !DILocation(line: 6,
+; CHECK: [[DBG17]] = !DILocation(line: 13,
+;.
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "temp.ll", directory: "/")
+!2 = !{i32 30}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "process_block_branch", linkageName: "process_block_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)
+!14 = !DILocation(line: 7, column: 1, scope: !5)
+!15 = distinct !DISubprogram(name: "process_threadable_edges_branch", linkageName: "process_threadable_edges_branch", scope: null, file: !1, line: 8, type: !6, scopeLine: 8, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!16 = !DILocation(line: 8, column: 1, scope: !15)
+!17 = !DILocation(line: 9, column: 1, scope: !15)
+!18 = !DILocation(line: 10, column: 1, scope: !15)
+!19 = !DILocation(line: 11, column: 1, scope: !15)
+!20 = !DILocation(line: 12, column: 1, scope: !15)
+!21 = !DILocation(line: 13, column: 1, scope: !15)
+!22 = !DILocation(line: 14, column: 1, scope: !15)
+!23 = !DILocation(line: 15, column: 1, scope: !15)
+!24 = !DILocation(line: 16, column: 1, scope: !15)
+!25 = !DILocation(line: 17, column: 1, scope: !15)
+!26 = !DILocation(line: 18, column: 1, scope: !15)
+!27 = !DILocation(line: 19, column: 1, scope: !15)

@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-debuginfo

Author: Shan Huang (Apochens)

Changes

Fix #96885 .


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+4-2)
  • (added) llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll (+114)
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 1aef2800e9846..bffe1f4c58518 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1038,7 +1038,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
 
     LLVM_DEBUG(dbgs() << "  In block '" << BB->getName()
                       << "' folding undef terminator: " << *BBTerm << '\n');
-    BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm->getIterator());
+    Instruction *NewBI = BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm->getIterator());
+    NewBI->setDebugLoc(BBTerm->getDebugLoc());
     ++NumFolds;
     BBTerm->eraseFromParent();
     DTU->applyUpdatesPermissive(Updates);
@@ -1657,7 +1658,8 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB,
 
       // Finally update the terminator.
       Instruction *Term = BB->getTerminator();
-      BranchInst::Create(OnlyDest, Term->getIterator());
+      Instruction *NewBI = BranchInst::Create(OnlyDest, Term->getIterator());
+      NewBI->setDebugLoc(Term->getDebugLoc());
       ++NumFolds;
       Term->eraseFromParent();
       DTU->applyUpdatesPermissive(Updates);
diff --git a/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll b/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll
new file mode 100644
index 0000000000000..ca67f0dec31ba
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/preserving-debugloc-br.ll
@@ -0,0 +1,114 @@
+; RUN: opt -S -passes=jump-threading < %s | FileCheck %s
+
+; @process_block_branch checks that JumpThreading's processBlock() propagates 
+; the debug location to the new branch instruction.
+
+; @process_threadable_edges_branch checks that JumpThreading's processThreadableEdges()
+; propagates the debug location to the new branch instruction.
+
+define i32 @process_block_branch(i32 %action) #0 !dbg !5 {
+; CHECK-LABEL: define i32 @process_block_branch(
+; CHECK:       for.cond:
+; CHECK-NEXT:    br label %for.cond, !dbg [[DBG10:![0-9]+]]
+;
+entry:
+  switch i32 %action, label %lor.rhs [
+  i32 1, label %if.then
+  i32 0, label %lor.end
+  ], !dbg !8
+
+if.then:                                          ; preds = %for.cond, %lor.end, %entry
+  ret i32 undef, !dbg !9
+
+lor.rhs:                                          ; preds = %entry
+  br label %lor.end, !dbg !10
+
+lor.end:                                          ; preds = %lor.rhs, %entry
+  %cmp103 = xor i1 undef, undef, !dbg !11
+  br i1 %cmp103, label %for.cond, label %if.then, !dbg !12
+
+for.cond:                                         ; preds = %for.body, %lor.end
+  br i1 undef, label %if.then, label %for.body, !dbg !13
+
+for.body:                                         ; preds = %for.cond
+  br label %for.cond, !dbg !14
+}
+
+define void @process_threadable_edges_branch(i32 %value) #0 !dbg !15 {
+; CHECK-LABEL: define void @process_threadable_edges_branch(
+; CHECK:    L0:
+; CHECK:      br label %L2, !dbg [[DBG17:![0-9]+]]
+;
+entry:
+  %cmp = icmp eq i32 %value, 32, !dbg !16
+  %add = add i32 %value, 64, !dbg !17
+  br i1 %cmp, label %L0, label %L2, !dbg !18
+
+L0:                                               ; preds = %entry
+  %0 = call i32 @f2(), !dbg !19
+  %1 = call i32 @f2(), !dbg !20
+  switch i32 %add, label %L3 [
+  i32 32, label %L1
+  i32 96, label %L2
+  ], !dbg !21
+
+L1:                                               ; preds = %L0
+  call void @f3(), !dbg !22
+  ret void, !dbg !23
+
+L2:                                               ; preds = %L0, %entry
+  call void @f4(i32 %add), !dbg !24
+  ret void, !dbg !25
+
+L3:                                               ; preds = %L0
+  call void @f3(), !dbg !26
+  ret void, !dbg !27
+}
+
+declare i32 @f1()
+
+declare i32 @f2()
+
+declare void @f3()
+
+declare void @f4(i32)
+
+attributes #0 = { nounwind }
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+;.
+; CHECK: [[DBG10]] = !DILocation(line: 6,
+; CHECK: [[DBG17]] = !DILocation(line: 13,
+;.
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "temp.ll", directory: "/")
+!2 = !{i32 30}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "process_block_branch", linkageName: "process_block_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)
+!14 = !DILocation(line: 7, column: 1, scope: !5)
+!15 = distinct !DISubprogram(name: "process_threadable_edges_branch", linkageName: "process_threadable_edges_branch", scope: null, file: !1, line: 8, type: !6, scopeLine: 8, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!16 = !DILocation(line: 8, column: 1, scope: !15)
+!17 = !DILocation(line: 9, column: 1, scope: !15)
+!18 = !DILocation(line: 10, column: 1, scope: !15)
+!19 = !DILocation(line: 11, column: 1, scope: !15)
+!20 = !DILocation(line: 12, column: 1, scope: !15)
+!21 = !DILocation(line: 13, column: 1, scope: !15)
+!22 = !DILocation(line: 14, column: 1, scope: !15)
+!23 = !DILocation(line: 15, column: 1, scope: !15)
+!24 = !DILocation(line: 16, column: 1, scope: !15)
+!25 = !DILocation(line: 17, column: 1, scope: !15)
+!26 = !DILocation(line: 18, column: 1, scope: !15)
+!27 = !DILocation(line: 19, column: 1, scope: !15)

Copy link
Member

@jryans jryans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks! 😄

@Apochens Apochens merged commit 37661a1 into llvm:main Jul 1, 2024
10 checks passed
@Apochens Apochens deleted the #96885_jumpthreading_preserving_debugloc_branch branch July 1, 2024 06:06
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DebugInfo][JumpThreading] Missing debug location updates for branch instructions
3 participants