[llvm] [DebugInfo] Propagate source loc from invoke to replacement branch (PR #137206)

Stephen Tozer via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 24 08:59:34 PDT 2025


https://github.com/SLTozer created https://github.com/llvm/llvm-project/pull/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 https://github.com/llvm/llvm-project/pull/107279.

>From c8c86f976e02ea85ddd74fc58633a1ebb6349da4 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <Stephen.Tozer at sony.com>
Date: Thu, 10 Apr 2025 11:27:32 +0100
Subject: [PATCH] [DebugInfo] Propagate source loc from invoke to replacement
 branch

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 https://github.com/llvm/llvm-project/pull/107279.
---
 llvm/lib/Transforms/Utils/Local.cpp           |  6 +-
 .../X86/debugloc-invoke-to-call-br.ll         | 61 +++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/SimplifyCFG/X86/debugloc-invoke-to-call-br.ll

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]])
+;.



More information about the llvm-commits mailing list