[DebugInfo] Propagate source loc from invoke to replacement branch - #137206
Merged
Conversation
An existing transformation replaces invoke instructions with a call to the invoked function and a branch to the destination; when this happens, we propagate the invoke's source location to the call but not to the branch. This patch updates this behaviour to propagate to the branch as well. Found using llvm#107279.
Member
|
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-debuginfo Author: Stephen Tozer (SLTozer) ChangesAn existing transformation replaces invoke instructions with a call to the invoked function and a branch to the destination; when this happens, we propagate the invoke's source location to the call but not to the branch. This patch updates this behaviour to propagate to the branch as well. Found using #107279. Full diff: https://github.com/llvm/llvm-project/pull/137206.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index b3204da93049b..f914d596f0bf0 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2964,7 +2964,11 @@ CallInst *llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) {
// Follow the call by a branch to the normal destination.
BasicBlock *NormalDestBB = II->getNormalDest();
- BranchInst::Create(NormalDestBB, II->getIterator());
+ auto *BI = BranchInst::Create(NormalDestBB, II->getIterator());
+ // Although it takes place after the call itself, the new branch is still
+ // performing part of the control-flow functionality of the invoke, so we use
+ // II's DebugLoc.
+ BI->setDebugLoc(II->getDebugLoc());
// Update PHI nodes in the unwind destination
BasicBlock *BB = II->getParent();
diff --git a/llvm/test/Transforms/SimplifyCFG/X86/debugloc-invoke-to-call-br.ll b/llvm/test/Transforms/SimplifyCFG/X86/debugloc-invoke-to-call-br.ll
new file mode 100644
index 0000000000000..12aa7fe1a9f84
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/X86/debugloc-invoke-to-call-br.ll
@@ -0,0 +1,61 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -p=simplifycfg -S | FileCheck %s
+
+;; Test that when we replace the invoke of @baz with a call and branch to the
+;; invoke destination, we propagate the source location of the invoke to both
+;; the call and the branch.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @widget(i1 %arg) personality ptr null !dbg !5 {
+; CHECK-LABEL: define void @widget(
+; CHECK-SAME: i1 [[ARG:%.*]]) personality ptr null !dbg [[DBG5:![0-9]+]] {
+; CHECK-NEXT: [[BB:.*:]]
+; CHECK-NEXT: br i1 [[ARG]], label %[[BB2:.*]], label %[[BB1:.*]]
+; CHECK: [[BB1]]:
+; CHECK-NEXT: call void @baz(ptr null) #[[ATTR0:[0-9]+]], !dbg [[DBG8:![0-9]+]]
+; CHECK-NEXT: br label %[[BB2]], !dbg [[DBG8]]
+; CHECK: [[BB2]]:
+; CHECK-NEXT: ret void
+;
+bb:
+ br i1 %arg, label %bb2, label %bb1
+
+bb1: ; preds = %bb
+ invoke void @baz(ptr null)
+ to label %bb2 unwind label %bb3, !dbg !8
+
+bb2: ; preds = %bb1, %bb
+ ret void
+
+bb3: ; preds = %bb1
+ %landingpad = landingpad { ptr, i32 }
+ cleanup
+ store i1 false, ptr null, align 1
+ ret void
+}
+
+declare void @baz(ptr)
+
+!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: "debugloc-invoke-to-call-br.ll", directory: "/")
+!2 = !{i32 6}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "widget", linkageName: "widget", 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)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META1:![0-9]+]], producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+; CHECK: [[META1]] = !DIFile(filename: "debugloc-invoke-to-call-br.ll", directory: {{.*}})
+; CHECK: [[DBG5]] = distinct !DISubprogram(name: "widget", linkageName: "widget", scope: null, file: [[META1]], line: 1, type: [[META6:![0-9]+]], scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]])
+; CHECK: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
+; CHECK: [[META7]] = !{}
+; CHECK: [[DBG8]] = !DILocation(line: 1, column: 1, scope: [[DBG5]])
+;.
|
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
…lvm#137206) An existing transformation replaces invoke instructions with a call to the invoked function and a branch to the destination; when this happens, we propagate the invoke's source location to the call but not to the branch. This patch updates this behaviour to propagate to the branch as well. Found using llvm#107279.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An existing transformation replaces invoke instructions with a call to the invoked function and a branch to the destination; when this happens, we propagate the invoke's source location to the call but not to the branch. This patch updates this behaviour to propagate to the branch as well.
Found using #107279.