Skip to content

Commit 9bea77f

Browse files
committed
[licm] clone metadata when hoisting conditional branch
1 parent f675483 commit 9bea77f

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ LLVM_ABI cl::opt<bool> PickMergedSourceLocations(
4242
cl::desc("Preserve line and column number when merging locations."));
4343
} // namespace llvm
4444

45+
cl::opt<bool> DebugInfoPreferSampleProfiling(
46+
"dbginfo-prefer-sample-profiling", cl::init(false), cl::Hidden,
47+
cl::desc("Prefer sample profiling over debugability."));
48+
4549
uint32_t DIType::getAlignInBits() const {
4650
return (getTag() == dwarf::DW_TAG_LLVM_ptrauth_type ? 0 : SubclassData32);
4751
}

llvm/lib/IR/Instruction.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@
2626
#include "llvm/IR/Operator.h"
2727
#include "llvm/IR/ProfDataUtils.h"
2828
#include "llvm/IR/Type.h"
29+
#include "llvm/Support/CommandLine.h"
2930
#include "llvm/Support/Compiler.h"
3031
using namespace llvm;
3132

33+
// FIXME: Flag used for an ablation performance test, Issue #147390. Placing it
34+
// here because referencing IR should be feasible from anywhere. Will be
35+
// removed after the ablation test.
36+
cl::opt<bool> ProfcheckDisableMetadataFixes(
37+
"profcheck-disable-metadata-fixes", cl::Hidden, cl::init(false),
38+
cl::desc(
39+
"Disable metadata propagation fixes discovered through Issue #147390"));
40+
3241
InsertPosition::InsertPosition(Instruction *InsertBefore)
3342
: InsertAt(InsertBefore ? InsertBefore->getIterator()
3443
: InstListType::iterator()) {}

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ using PointersAndHasReadsOutsideSet =
218218
static SmallVector<PointersAndHasReadsOutsideSet, 0>
219219
collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L);
220220

221+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
222+
extern cl::opt<bool> DebugInfoPreferSampleProfiling;
221223
namespace {
222224
struct LoopInvariantCodeMotion {
223225
bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT,
@@ -857,9 +859,21 @@ class ControlFlowHoister {
857859
}
858860

859861
// Now finally clone BI.
860-
ReplaceInstWithInst(
861-
HoistTarget->getTerminator(),
862-
BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
862+
auto *NewBI =
863+
BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition(),
864+
HoistTarget->getTerminator()->getIterator());
865+
HoistTarget->getTerminator()->eraseFromParent();
866+
// Handle "performance-related" metadata. In particular:
867+
// - md_prof: it should also come from the original branch - since the
868+
// condition was hoisted, the branch probabilities shouldn't change.
869+
// - debug info should also be the same as the original branch, **if** the
870+
// user explicitly indicates that.
871+
if (!ProfcheckDisableMetadataFixes) {
872+
if (DebugInfoPreferSampleProfiling)
873+
NewBI->copyMetadata(*BI, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
874+
else
875+
NewBI->copyMetadata(*BI, {LLVMContext::MD_prof});
876+
}
863877
++NumClonedBranches;
864878

865879
assert(CurLoop->getLoopPreheader() &&
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
2+
; Test that hoisting conditional branches copies the debug and profiling info
3+
; metadata from the branch being hoisted.
4+
; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 %s -o - | FileCheck %s --check-prefixes=FOR-DEBUG
5+
; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 -dbginfo-prefer-sample-profiling %s -o - | FileCheck %s --check-prefixes=FOR-AFDO
6+
7+
define void @triangle_phi(i32 %x, ptr %p) {
8+
; FOR-DEBUG-LABEL: define void @triangle_phi
9+
; FOR-DEBUG-SAME: (i32 [[X:%.*]], ptr [[P:%.*]]) {
10+
; FOR-DEBUG-NEXT: entry:
11+
; FOR-DEBUG-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[X]], 0
12+
; FOR-DEBUG-NEXT: br i1 [[CMP1]], label [[IF_LICM:%.*]], label [[THEN_LICM:%.*]], !prof [[PROF2:![0-9]+]]
13+
; FOR-DEBUG: if.licm:
14+
; FOR-DEBUG-NEXT: [[ADD:%.*]] = add i32 [[X]], 1
15+
; FOR-DEBUG-NEXT: br label [[THEN_LICM]]
16+
; FOR-DEBUG: then.licm:
17+
; FOR-DEBUG-NEXT: [[PHI:%.*]] = phi i32 [ [[ADD]], [[IF_LICM]] ], [ [[X]], [[ENTRY:%.*]] ]
18+
; FOR-DEBUG-NEXT: store i32 [[PHI]], ptr [[P]], align 4
19+
; FOR-DEBUG-NEXT: [[CMP2:%.*]] = icmp ne i32 [[PHI]], 0
20+
; FOR-DEBUG-NEXT: br label [[LOOP:%.*]]
21+
; FOR-DEBUG: loop:
22+
; FOR-DEBUG-NEXT: br i1 [[CMP1]], label [[IF:%.*]], label [[THEN:%.*]], !dbg [[DBG3:![0-9]+]], !prof [[PROF2]]
23+
; FOR-DEBUG: if:
24+
; FOR-DEBUG-NEXT: br label [[THEN]]
25+
; FOR-DEBUG: then:
26+
; FOR-DEBUG-NEXT: br i1 [[CMP2]], label [[LOOP]], label [[END:%.*]], !dbg [[DBG7:![0-9]+]], !prof [[PROF8:![0-9]+]]
27+
; FOR-DEBUG: end:
28+
; FOR-DEBUG-NEXT: ret void
29+
;
30+
; FOR-AFDO-LABEL: define void @triangle_phi
31+
; FOR-AFDO-SAME: (i32 [[X:%.*]], ptr [[P:%.*]]) {
32+
; FOR-AFDO-NEXT: entry:
33+
; FOR-AFDO-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[X]], 0
34+
; FOR-AFDO-NEXT: br i1 [[CMP1]], label [[IF_LICM:%.*]], label [[THEN_LICM:%.*]], !dbg [[DBG2:![0-9]+]], !prof [[PROF6:![0-9]+]]
35+
; FOR-AFDO: if.licm:
36+
; FOR-AFDO-NEXT: [[ADD:%.*]] = add i32 [[X]], 1
37+
; FOR-AFDO-NEXT: br label [[THEN_LICM]]
38+
; FOR-AFDO: then.licm:
39+
; FOR-AFDO-NEXT: [[PHI:%.*]] = phi i32 [ [[ADD]], [[IF_LICM]] ], [ [[X]], [[ENTRY:%.*]] ]
40+
; FOR-AFDO-NEXT: store i32 [[PHI]], ptr [[P]], align 4
41+
; FOR-AFDO-NEXT: [[CMP2:%.*]] = icmp ne i32 [[PHI]], 0
42+
; FOR-AFDO-NEXT: br label [[LOOP:%.*]]
43+
; FOR-AFDO: loop:
44+
; FOR-AFDO-NEXT: br i1 [[CMP1]], label [[IF:%.*]], label [[THEN:%.*]], !dbg [[DBG2]], !prof [[PROF6]]
45+
; FOR-AFDO: if:
46+
; FOR-AFDO-NEXT: br label [[THEN]]
47+
; FOR-AFDO: then:
48+
; FOR-AFDO-NEXT: br i1 [[CMP2]], label [[LOOP]], label [[END:%.*]], !dbg [[DBG7:![0-9]+]], !prof [[PROF8:![0-9]+]]
49+
; FOR-AFDO: end:
50+
; FOR-AFDO-NEXT: ret void
51+
;
52+
entry:
53+
br label %loop, !dbg !5
54+
loop:
55+
%cmp1 = icmp sgt i32 %x, 0
56+
br i1 %cmp1, label %if, label %then, !dbg !6, !prof !8
57+
if:
58+
%add = add i32 %x, 1
59+
br label %then
60+
61+
then:
62+
%phi = phi i32 [ %add, %if ], [ %x, %loop ]
63+
store i32 %phi, ptr %p
64+
%cmp2 = icmp ne i32 %phi, 0
65+
br i1 %cmp2, label %loop, label %end, !dbg !7, !prof !9
66+
67+
end:
68+
ret void
69+
}
70+
71+
!llvm.module.flags = !{!2, !3}
72+
73+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
74+
!1 = !DIFile(filename: "t", directory: "/")
75+
!2 = !{i32 7, !"Dwarf Version", i32 5}
76+
!3 = !{i32 2, !"Debug Info Version", i32 3}
77+
!4 = distinct !DISubprogram(name: "triangle_phi", linkageName: "triangle_phi", scope: !1, file: !1, line: 1, unit: !0)
78+
!5 = !DILocation(line: 1, column: 22, scope: !4)
79+
!6 = !DILocation(line: 2, column: 22, scope: !4)
80+
!7 = !DILocation(line: 3, column: 22, scope: !4)
81+
!8 = !{!"branch_weights", i32 5, i32 7}
82+
!9 = !{!"branch_weights", i32 13, i32 11}
83+
;.
84+
; FOR-DEBUG: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
85+
; FOR-DEBUG: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
86+
; FOR-DEBUG: [[PROF2]] = !{!"branch_weights", i32 5, i32 7}
87+
; FOR-DEBUG: [[DBG3]] = !DILocation(line: 2, column: 22, scope: [[META4:![0-9]+]])
88+
; 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]+]])
89+
; FOR-DEBUG: [[META5]] = !DIFile(filename: "{{.*}}t", directory: {{.*}})
90+
; FOR-DEBUG: [[META6]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META5]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
91+
; FOR-DEBUG: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META4]])
92+
; FOR-DEBUG: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
93+
;.
94+
; FOR-AFDO: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
95+
; FOR-AFDO: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
96+
; FOR-AFDO: [[DBG2]] = !DILocation(line: 2, column: 22, scope: [[META3:![0-9]+]])
97+
; 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]+]])
98+
; FOR-AFDO: [[META4]] = !DIFile(filename: "{{.*}}t", directory: {{.*}})
99+
; FOR-AFDO: [[META5]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META4]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
100+
; FOR-AFDO: [[PROF6]] = !{!"branch_weights", i32 5, i32 7}
101+
; FOR-AFDO: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META3]])
102+
; FOR-AFDO: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
103+
;.

0 commit comments

Comments
 (0)