[llvm] [licm] clone metadata when hoisting conditional branch (PR #152232)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 11:22:34 PDT 2025


https://github.com/mtrofin updated https://github.com/llvm/llvm-project/pull/152232

>From 9bea77f941044460da4fe9b2d38fcd6c076f44dd Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Tue, 5 Aug 2025 16:24:12 -0700
Subject: [PATCH] [licm] clone metadata when hoisting conditional branch

---
 llvm/lib/IR/DebugInfoMetadata.cpp             |   4 +
 llvm/lib/IR/Instruction.cpp                   |   9 ++
 llvm/lib/Transforms/Scalar/LICM.cpp           |  20 +++-
 .../Transforms/LICM/hoist-phi-metadata.ll     | 103 ++++++++++++++++++
 4 files changed, 133 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Transforms/LICM/hoist-phi-metadata.ll

diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp
index f1d4549ba015b..b8f5049ba68b1 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -42,6 +42,10 @@ LLVM_ABI cl::opt<bool> PickMergedSourceLocations(
     cl::desc("Preserve line and column number when merging locations."));
 } // namespace llvm
 
+cl::opt<bool> DebugInfoPreferSampleProfiling(
+    "dbginfo-prefer-sample-profiling", cl::init(false), cl::Hidden,
+    cl::desc("Prefer sample profiling over debugability."));
+
 uint32_t DIType::getAlignInBits() const {
   return (getTag() == dwarf::DW_TAG_LLVM_ptrauth_type ? 0 : SubclassData32);
 }
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index b7cd12ac9691e..cc18e9c642799 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -26,9 +26,18 @@
 #include "llvm/IR/Operator.h"
 #include "llvm/IR/ProfDataUtils.h"
 #include "llvm/IR/Type.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 using namespace llvm;
 
+// FIXME: Flag used for an ablation performance test, Issue #147390. Placing it
+// here because referencing IR should be feasible from anywhere. Will be
+// removed after the ablation test.
+cl::opt<bool> ProfcheckDisableMetadataFixes(
+    "profcheck-disable-metadata-fixes", cl::Hidden, cl::init(false),
+    cl::desc(
+        "Disable metadata propagation fixes discovered through Issue #147390"));
+
 InsertPosition::InsertPosition(Instruction *InsertBefore)
     : InsertAt(InsertBefore ? InsertBefore->getIterator()
                             : InstListType::iterator()) {}
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index c3f80f901a120..fbbb7bb234ce2 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -218,6 +218,8 @@ using PointersAndHasReadsOutsideSet =
 static SmallVector<PointersAndHasReadsOutsideSet, 0>
 collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L);
 
+extern cl::opt<bool> ProfcheckDisableMetadataFixes;
+extern cl::opt<bool> DebugInfoPreferSampleProfiling;
 namespace {
 struct LoopInvariantCodeMotion {
   bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT,
@@ -857,9 +859,21 @@ class ControlFlowHoister {
     }
 
     // Now finally clone BI.
-    ReplaceInstWithInst(
-        HoistTarget->getTerminator(),
-        BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
+    auto *NewBI =
+        BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition(),
+                           HoistTarget->getTerminator()->getIterator());
+    HoistTarget->getTerminator()->eraseFromParent();
+    // Handle "performance-related" metadata. In particular:
+    // - md_prof: it should also come from the original branch - since the
+    // condition was hoisted, the branch probabilities shouldn't change.
+    // - debug info should also be the same as the original branch, **if** the
+    // user explicitly indicates that.
+    if (!ProfcheckDisableMetadataFixes) {
+      if (DebugInfoPreferSampleProfiling)
+        NewBI->copyMetadata(*BI, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
+      else
+        NewBI->copyMetadata(*BI, {LLVMContext::MD_prof});
+    }
     ++NumClonedBranches;
 
     assert(CurLoop->getLoopPreheader() &&
diff --git a/llvm/test/Transforms/LICM/hoist-phi-metadata.ll b/llvm/test/Transforms/LICM/hoist-phi-metadata.ll
new file mode 100644
index 0000000000000..73900779bd522
--- /dev/null
+++ b/llvm/test/Transforms/LICM/hoist-phi-metadata.ll
@@ -0,0 +1,103 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
+; Test that hoisting conditional branches copies the debug and profiling info
+; metadata from the branch being hoisted.
+; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 %s -o - | FileCheck %s --check-prefixes=FOR-DEBUG
+; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 -dbginfo-prefer-sample-profiling %s -o - | FileCheck %s --check-prefixes=FOR-AFDO
+
+define void @triangle_phi(i32 %x, ptr %p) {
+; FOR-DEBUG-LABEL: define void @triangle_phi
+; FOR-DEBUG-SAME: (i32 [[X:%.*]], ptr [[P:%.*]]) {
+; FOR-DEBUG-NEXT:  entry:
+; FOR-DEBUG-NEXT:    [[CMP1:%.*]] = icmp sgt i32 [[X]], 0
+; FOR-DEBUG-NEXT:    br i1 [[CMP1]], label [[IF_LICM:%.*]], label [[THEN_LICM:%.*]], !prof [[PROF2:![0-9]+]]
+; FOR-DEBUG:       if.licm:
+; FOR-DEBUG-NEXT:    [[ADD:%.*]] = add i32 [[X]], 1
+; FOR-DEBUG-NEXT:    br label [[THEN_LICM]]
+; FOR-DEBUG:       then.licm:
+; FOR-DEBUG-NEXT:    [[PHI:%.*]] = phi i32 [ [[ADD]], [[IF_LICM]] ], [ [[X]], [[ENTRY:%.*]] ]
+; FOR-DEBUG-NEXT:    store i32 [[PHI]], ptr [[P]], align 4
+; FOR-DEBUG-NEXT:    [[CMP2:%.*]] = icmp ne i32 [[PHI]], 0
+; FOR-DEBUG-NEXT:    br label [[LOOP:%.*]]
+; FOR-DEBUG:       loop:
+; FOR-DEBUG-NEXT:    br i1 [[CMP1]], label [[IF:%.*]], label [[THEN:%.*]], !dbg [[DBG3:![0-9]+]], !prof [[PROF2]]
+; FOR-DEBUG:       if:
+; FOR-DEBUG-NEXT:    br label [[THEN]]
+; FOR-DEBUG:       then:
+; FOR-DEBUG-NEXT:    br i1 [[CMP2]], label [[LOOP]], label [[END:%.*]], !dbg [[DBG7:![0-9]+]], !prof [[PROF8:![0-9]+]]
+; FOR-DEBUG:       end:
+; FOR-DEBUG-NEXT:    ret void
+;
+; FOR-AFDO-LABEL: define void @triangle_phi
+; FOR-AFDO-SAME: (i32 [[X:%.*]], ptr [[P:%.*]]) {
+; FOR-AFDO-NEXT:  entry:
+; FOR-AFDO-NEXT:    [[CMP1:%.*]] = icmp sgt i32 [[X]], 0
+; FOR-AFDO-NEXT:    br i1 [[CMP1]], label [[IF_LICM:%.*]], label [[THEN_LICM:%.*]], !dbg [[DBG2:![0-9]+]], !prof [[PROF6:![0-9]+]]
+; FOR-AFDO:       if.licm:
+; FOR-AFDO-NEXT:    [[ADD:%.*]] = add i32 [[X]], 1
+; FOR-AFDO-NEXT:    br label [[THEN_LICM]]
+; FOR-AFDO:       then.licm:
+; FOR-AFDO-NEXT:    [[PHI:%.*]] = phi i32 [ [[ADD]], [[IF_LICM]] ], [ [[X]], [[ENTRY:%.*]] ]
+; FOR-AFDO-NEXT:    store i32 [[PHI]], ptr [[P]], align 4
+; FOR-AFDO-NEXT:    [[CMP2:%.*]] = icmp ne i32 [[PHI]], 0
+; FOR-AFDO-NEXT:    br label [[LOOP:%.*]]
+; FOR-AFDO:       loop:
+; FOR-AFDO-NEXT:    br i1 [[CMP1]], label [[IF:%.*]], label [[THEN:%.*]], !dbg [[DBG2]], !prof [[PROF6]]
+; FOR-AFDO:       if:
+; FOR-AFDO-NEXT:    br label [[THEN]]
+; FOR-AFDO:       then:
+; FOR-AFDO-NEXT:    br i1 [[CMP2]], label [[LOOP]], label [[END:%.*]], !dbg [[DBG7:![0-9]+]], !prof [[PROF8:![0-9]+]]
+; FOR-AFDO:       end:
+; FOR-AFDO-NEXT:    ret void
+;
+entry:
+  br label %loop, !dbg !5
+loop:
+  %cmp1 = icmp sgt i32 %x, 0
+  br i1 %cmp1, label %if, label %then, !dbg !6, !prof !8
+if:
+  %add = add i32 %x, 1
+  br label %then
+
+then:
+  %phi = phi i32 [ %add, %if ], [ %x, %loop ]
+  store i32 %phi, ptr %p
+  %cmp2 = icmp ne i32 %phi, 0
+  br i1 %cmp2, label %loop, label %end, !dbg !7, !prof !9
+
+end:
+  ret void
+}
+
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
+!1 = !DIFile(filename: "t", directory: "/")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DISubprogram(name: "triangle_phi", linkageName: "triangle_phi", scope: !1, file: !1, line: 1, unit: !0)
+!5 = !DILocation(line: 1, column: 22, scope: !4)
+!6 = !DILocation(line: 2, column: 22, scope: !4)
+!7 = !DILocation(line: 3, column: 22, scope: !4)
+!8 = !{!"branch_weights", i32 5, i32 7}
+!9 = !{!"branch_weights", i32 13, i32 11}
+;.
+; FOR-DEBUG: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
+; FOR-DEBUG: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
+; FOR-DEBUG: [[PROF2]] = !{!"branch_weights", i32 5, i32 7}
+; FOR-DEBUG: [[DBG3]] = !DILocation(line: 2, column: 22, scope: [[META4:![0-9]+]])
+; FOR-DEBUG: [[META4]] = distinct !DISubprogram(name: "triangle_phi", linkageName: "triangle_phi", scope: [[META5:![0-9]+]], file: [[META5]], line: 1, spFlags: DISPFlagDefinition, unit: [[META6:![0-9]+]])
+; FOR-DEBUG: [[META5]] = !DIFile(filename: "{{.*}}t", directory: {{.*}})
+; FOR-DEBUG: [[META6]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META5]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; FOR-DEBUG: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META4]])
+; FOR-DEBUG: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
+;.
+; FOR-AFDO: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
+; FOR-AFDO: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
+; FOR-AFDO: [[DBG2]] = !DILocation(line: 2, column: 22, scope: [[META3:![0-9]+]])
+; FOR-AFDO: [[META3]] = distinct !DISubprogram(name: "triangle_phi", linkageName: "triangle_phi", scope: [[META4:![0-9]+]], file: [[META4]], line: 1, spFlags: DISPFlagDefinition, unit: [[META5:![0-9]+]])
+; FOR-AFDO: [[META4]] = !DIFile(filename: "{{.*}}t", directory: {{.*}})
+; FOR-AFDO: [[META5]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META4]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; FOR-AFDO: [[PROF6]] = !{!"branch_weights", i32 5, i32 7}
+; FOR-AFDO: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META3]])
+; FOR-AFDO: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
+;.



More information about the llvm-commits mailing list