[llvm] Refine metadata handling during instruction hoisting (PR #158448)

William Moses via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 14 07:37:32 PDT 2025


https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/158448

>From 27285dfe8c473214ed2b73edd53c56995ad1bb0f Mon Sep 17 00:00:00 2001
From: William Moses <gh at wsmoses.com>
Date: Sat, 13 Sep 2025 18:13:11 -0500
Subject: [PATCH 1/4] Refine metadata handling during instruction hoisting

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5a842f9b49c1b..6d6e32d51b62e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3387,11 +3387,13 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
   // Metadata can be dependent on the condition we are hoisting above.
   // Strip all UB-implying metadata on the instruction. Drop the debug loc
   // to avoid making it appear as if the condition is a constant, which would
-  // be misleading while debugging.
+  // be misleading while debugging. However, make sure to keep debug info
+  // for calls as inlinable function calls in a function with debug info must
+  // have a !dbg location.
   // Similarly strip attributes that maybe dependent on condition we are
   // hoisting above.
   for (auto &I : make_early_inc_range(*ThenBB)) {
-    if (!SpeculatedStoreValue || &I != SpeculatedStore) {
+    if (!SpeculatedStoreValue || &I != SpeculatedStore && !isa<CallBase>(&I)) {
       I.setDebugLoc(DebugLoc::getDropped());
     }
     I.dropUBImplyingAttrsAndMetadata();

>From a79af80d7aa86b384c6030fad79135cd6dcfc79b Mon Sep 17 00:00:00 2001
From: William Moses <gh at wsmoses.com>
Date: Sat, 13 Sep 2025 18:22:58 -0500
Subject: [PATCH 2/4] Refactor condition in SimplifyCFG.cpp

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6d6e32d51b62e..5cbb6f9b2befd 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3393,7 +3393,7 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
   // Similarly strip attributes that maybe dependent on condition we are
   // hoisting above.
   for (auto &I : make_early_inc_range(*ThenBB)) {
-    if (!SpeculatedStoreValue || &I != SpeculatedStore && !isa<CallBase>(&I)) {
+    if ((!SpeculatedStoreValue || &I != SpeculatedStore) && !isa<CallBase>(&I)) {
       I.setDebugLoc(DebugLoc::getDropped());
     }
     I.dropUBImplyingAttrsAndMetadata();

>From f2e658472654fb46244bc50814102972cd6f249b Mon Sep 17 00:00:00 2001
From: "William S. Moses" <gh at wsmoses.com>
Date: Sat, 13 Sep 2025 18:31:02 -0500
Subject: [PATCH 3/4] fmt

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5cbb6f9b2befd..9fde9f68bf967 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3393,7 +3393,8 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
   // Similarly strip attributes that maybe dependent on condition we are
   // hoisting above.
   for (auto &I : make_early_inc_range(*ThenBB)) {
-    if ((!SpeculatedStoreValue || &I != SpeculatedStore) && !isa<CallBase>(&I)) {
+    if ((!SpeculatedStoreValue || &I != SpeculatedStore) &&
+        !isa<CallBase>(&I)) {
       I.setDebugLoc(DebugLoc::getDropped());
     }
     I.dropUBImplyingAttrsAndMetadata();

>From 499965ec9ba2288be527ca52b9bc9b1b229fe892 Mon Sep 17 00:00:00 2001
From: "William S. Moses" <gh at wsmoses.com>
Date: Sun, 14 Sep 2025 09:37:04 -0500
Subject: [PATCH 4/4] add test

---
 ...no-drop-debug-loc-when-speculating-call.ll | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 llvm/test/Transforms/SimplifyCFG/no-drop-debug-loc-when-speculating-call.ll

diff --git a/llvm/test/Transforms/SimplifyCFG/no-drop-debug-loc-when-speculating-call.ll b/llvm/test/Transforms/SimplifyCFG/no-drop-debug-loc-when-speculating-call.ll
new file mode 100644
index 0000000000000..1a8327b92566a
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/no-drop-debug-loc-when-speculating-call.ll
@@ -0,0 +1,44 @@
+; RUN: opt -S -o - %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 | FileCheck %s
+
+
+declare i1 @make_condition()
+
+define i1 @specfn() readnone nounwind speculatable {
+    ret i1 true
+}
+
+; CHECK-LABEL: @test1(
+; CHECK: call i1 @specfn(), !dbg
+; CHECK: select i1 [[COND]]
+define void @test1() !dbg !6 {
+start:
+  %cond = call i1 @make_condition(), !dbg !8
+  br i1 %cond, label %then, label %else, !dbg !9
+
+then:                                             ; preds = %start
+  %sres = call i1 @specfn(), !dbg !8
+  br label %else, !dbg !11
+
+else:                                             ; preds = %then, %start
+  %phi = phi i1 [ %cond, %start ], [ %sres, %then ], !dbg !12
+  ret void, !dbg !13
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!3, !4}
+!llvm.module.flags = !{!5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{}
+!3 = !{i32 6}
+!4 = !{i32 0}
+!5 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "test1", linkageName: "test1", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!7 = !DISubroutineType(types: !2)
+!8 = !DILocation(line: 1, column: 1, scope: !6)
+!9 = !DILocation(line: 2, column: 1, scope: !6)
+!10 = !DILocation(line: 3, column: 2, scope: !6)
+!11 = !DILocation(line: 4, column: 2, scope: !6)
+!12 = !DILocation(line: 5, column: 3, scope: !6)
+!13 = !DILocation(line: 6, column: 3, scope: !6)



More information about the llvm-commits mailing list