Skip to content

Commit ee06802

Browse files
[JTS] Correctly handle all zero profile values in VP metadata (#193402)
We can end up with cases where the VP metadata only has zero profile values, for example if all of the functions end up being external and uninstrumented. This caused fixes an assertion failure on the BOLT builder that came up last time we tried to turn the pass on by default.
1 parent dee5769 commit ee06802

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ expandToSwitch(CallBase *CB, const JumpTableTy &JT, DomTreeUpdater &DTU,
193193
return OptimizationRemark(DEBUG_TYPE, "ReplacedJumpTableWithSwitch", CB)
194194
<< "expanded indirect call into switch";
195195
});
196-
if (HadProfile && !ProfcheckDisableMetadataFixes) {
197-
// At least one of the targets must've been taken.
198-
assert(llvm::any_of(BranchWeights, not_equal_to(0)));
196+
// Only set branch weights on the switch if we have non-zero branch weights.
197+
// We can have no non-zero branch weights while having VP metadata if for
198+
// example, all of the functions are external and not instrumented.
199+
if (HadProfile && !ProfcheckDisableMetadataFixes &&
200+
llvm::any_of(BranchWeights, not_equal_to(0))) {
199201
setBranchWeights(*Switch, downscaleWeights(BranchWeights),
200202
/*IsExpected=*/false);
201203
} else

llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,45 @@ define i32 @caller_dup_guid(i32 %idx) {
8282
ret i32 %r
8383
}
8484

85+
define i32 @caller_only_zero_guids(i32 %idx) {
86+
; CHECK-LABEL: define i32 @caller_only_zero_guids(
87+
; CHECK-SAME: i32 [[IDX:%.*]]) {
88+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [2 x ptr], ptr @jt, i32 0, i32 [[IDX]]
89+
; CHECK-NEXT: [[FPTR:%.*]] = load ptr, ptr [[GEP]], align 8
90+
; CHECK-NEXT: switch i32 [[IDX]], label %[[DEFAULT_SWITCH_CASE_UNREACHABLE:.*]] [
91+
; CHECK-NEXT: i32 0, label %[[CALL_0:.*]]
92+
; CHECK-NEXT: i32 1, label %[[CALL_1:.*]]
93+
; CHECK-NEXT: ], !prof [[PROF2:![0-9]+]]
94+
; CHECK: [[DEFAULT_SWITCH_CASE_UNREACHABLE]]:
95+
; CHECK-NEXT: unreachable
96+
; CHECK: [[CALL_0]]:
97+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @jt_target_0()
98+
; CHECK-NEXT: br [[DOTTAIL:label %.*]]
99+
; CHECK: [[CALL_1]]:
100+
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @jt_target_1()
101+
; CHECK-NEXT: br [[DOTTAIL]]
102+
; CHECK: [[_TAIL:.*:]]
103+
; CHECK-NEXT: [[TMP3:%.*]] = phi i32 [ [[TMP1]], %[[CALL_0]] ], [ [[TMP2]], %[[CALL_1]] ]
104+
; CHECK-NEXT: ret i32 [[TMP3]]
105+
;
106+
%gep = getelementptr inbounds [2 x ptr], ptr @jt, i32 0, i32 %idx
107+
%fptr = load ptr, ptr %gep
108+
%r = call i32 %fptr(), !prof !2
109+
ret i32 %r
110+
}
111+
85112
;; VP metadata: GUID 11912887233601027218 = MD5("jt_target_0"), count 100
86113
;; GUID 18156790114353049777 = MD5("jt_target_1"), count 50
87114
!0 = !{!"VP", i32 0, i64 150, i64 11912887233601027218, i64 100, i64 18156790114353049777, i64 50}
88115

89116
;; VP metadata with multiple zero values.
90117
!1 = !{!"VP", i32 0, i64 150, i64 11912887233601027218, i64 60, i64 0, i64 50, i64 0, i64 40}
91118

119+
;; VP metadata with only a zero value.
120+
!2 = !{!"VP", i32 0, i64 150, i64 0, i64 40}
121+
92122
;.
93123
; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 100, i32 50}
94124
; CHECK: [[PROF1]] = !{!"branch_weights", i32 0, i32 60, i32 0}
125+
; CHECK: [[PROF2]] = !{!"unknown", !"jump-table-to-switch"}
95126
;.

0 commit comments

Comments
 (0)