Skip to content

Commit 39f292f

Browse files
authored
[profcheck] Add unknown branch weight for inlined memchr calls. (#160964)
The memchr inliner creates new switch branches but was failling to add profile metada. This patch fixes the issue by explicitly adding unknown branch weights to these branches. Issue [#147390](#147390)
1 parent 1fcf481 commit 39f292f

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,10 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
13501350
BB->getTerminator()->eraseFromParent();
13511351
SwitchInst *SI = IRB.CreateSwitch(
13521352
IRB.CreateTrunc(Call->getArgOperand(1), ByteTy), BBNext, N);
1353+
// We can't know the precise weights here, as they would depend on the value
1354+
// distribution of Call->getArgOperand(1). So we just mark it as "unknown".
1355+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, *Call->getFunction(),
1356+
DEBUG_TYPE);
13531357
Type *IndexTy = DL.getIndexType(Call->getType());
13541358
SmallVector<DominatorTree::UpdateType, 8> Updates;
13551359

llvm/test/Transforms/AggressiveInstCombine/memchr.ll

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
declare ptr @memchr(ptr, i32, i64)
88

9-
define i1 @test_memchr_null(i32 %x) {
9+
define i1 @test_memchr_null(i32 %x) !prof !0 {
1010
; CHECK-LABEL: define i1 @test_memchr_null(
11-
; CHECK-SAME: i32 [[X:%.*]]) {
11+
; CHECK-SAME: i32 [[X:%.*]])
12+
; CHECK: !prof [[PROF_0:![0-9]+]] {
1213
; CHECK-NEXT: [[ENTRY:.*]]:
1314
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8
1415
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [
@@ -40,9 +41,10 @@ entry:
4041
ret i1 %isnull
4142
}
4243

43-
define ptr @test_memchr(i32 %x) {
44+
define ptr @test_memchr(i32 %x) !prof !0 {
4445
; CHECK-LABEL: define ptr @test_memchr(
45-
; CHECK-SAME: i32 [[X:%.*]]) {
46+
; CHECK-SAME: i32 [[X:%.*]])
47+
; CHECK: !prof [[PROF_0]] {
4648
; CHECK-NEXT: [[ENTRY:.*]]:
4749
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8
4850
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [
@@ -72,16 +74,17 @@ entry:
7274
ret ptr %memchr
7375
}
7476

75-
define ptr @test_memchr_smaller_n(i32 %x) {
77+
define ptr @test_memchr_smaller_n(i32 %x) !prof !0 {
7678
; CHECK-LABEL: define ptr @test_memchr_smaller_n(
77-
; CHECK-SAME: i32 [[X:%.*]]) {
79+
; CHECK-SAME: i32 [[X:%.*]])
80+
; CHECK: !prof [[PROF_0]] {
7881
; CHECK-NEXT: [[ENTRY:.*]]:
7982
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8
8083
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [
8184
; CHECK-NEXT: i8 48, label %[[MEMCHR_CASE:.*]]
8285
; CHECK-NEXT: i8 49, label %[[MEMCHR_CASE1:.*]]
8386
; CHECK-NEXT: i8 0, label %[[MEMCHR_CASE2:.*]]
84-
; CHECK-NEXT: ]
87+
; CHECK-NEXT: ], !prof [[PROF_1:![0-9]+]]
8588
; CHECK: [[MEMCHR_CASE]]:
8689
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS:.*]]
8790
; CHECK: [[MEMCHR_CASE1]]:
@@ -103,9 +106,10 @@ entry:
103106

104107
; negative tests
105108

106-
define ptr @test_memchr_larger_n(i32 %x) {
109+
define ptr @test_memchr_larger_n(i32 %x) !prof !0 {
107110
; CHECK-LABEL: define ptr @test_memchr_larger_n(
108-
; CHECK-SAME: i32 [[X:%.*]]) {
111+
; CHECK-SAME: i32 [[X:%.*]])
112+
; CHECK: !prof [[PROF_0]] {
109113
; CHECK-NEXT: [[ENTRY:.*:]]
110114
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 [[X]], i64 6)
111115
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -115,9 +119,10 @@ entry:
115119
ret ptr %memchr
116120
}
117121

118-
define ptr @test_memchr_non_constant(i32 %x, ptr %str) {
122+
define ptr @test_memchr_non_constant(i32 %x, ptr %str) !prof !0 {
119123
; CHECK-LABEL: define ptr @test_memchr_non_constant(
120-
; CHECK-SAME: i32 [[X:%.*]], ptr [[STR:%.*]]) {
124+
; CHECK-SAME: i32 [[X:%.*]], ptr [[STR:%.*]])
125+
; CHECK: !prof [[PROF_0]] {
121126
; CHECK-NEXT: [[ENTRY:.*:]]
122127
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr [[STR]], i32 [[X]], i64 5)
123128
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -127,8 +132,9 @@ entry:
127132
ret ptr %memchr
128133
}
129134

130-
define ptr @test_memchr_constant_ch() {
131-
; CHECK-LABEL: define ptr @test_memchr_constant_ch() {
135+
define ptr @test_memchr_constant_ch() !prof !0 {
136+
; CHECK-LABEL: define ptr @test_memchr_constant_ch()
137+
; CHECK: !prof [[PROF_0]] {
132138
; CHECK-NEXT: [[ENTRY:.*:]]
133139
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 49, i64 5)
134140
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -138,9 +144,10 @@ entry:
138144
ret ptr %memchr
139145
}
140146

141-
define ptr @test_memchr_dynamic_n(i32 %x, i32 %y) {
147+
define ptr @test_memchr_dynamic_n(i32 %x, i32 %y) !prof !0 {
142148
; CHECK-LABEL: define ptr @test_memchr_dynamic_n(
143-
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
149+
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]])
150+
; CHECK: !prof [[PROF_0]] {
144151
; CHECK-NEXT: [[ENTRY:.*:]]
145152
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 [[X]], i32 [[Y]])
146153
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -150,9 +157,10 @@ entry:
150157
ret ptr %memchr
151158
}
152159

153-
define ptr @test_memchr_long(i32 %x) {
160+
define ptr @test_memchr_long(i32 %x) !prof !0 {
154161
; CHECK-LABEL: define ptr @test_memchr_long(
155-
; CHECK-SAME: i32 [[X:%.*]]) {
162+
; CHECK-SAME: i32 [[X:%.*]])
163+
; CHECK: !prof [[PROF_0]] {
156164
; CHECK-NEXT: [[ENTRY:.*:]]
157165
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str_long, i32 [[X]], i64 8)
158166
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -163,9 +171,10 @@ entry:
163171
}
164172

165173
; We want to check that the compiler still calls memchr if the length is non-constant:
166-
define ptr @test_memchr_non_constant_length2(i32 %x, i64 %len) {
174+
define ptr @test_memchr_non_constant_length2(i32 %x, i64 %len) !prof !0 {
167175
; CHECK-LABEL: define ptr @test_memchr_non_constant_length2(
168-
; CHECK-SAME: i32 [[X:%.*]], i64 [[LEN:%.*]]) {
176+
; CHECK-SAME: i32 [[X:%.*]], i64 [[LEN:%.*]])
177+
; CHECK: !prof [[PROF_0]] {
169178
; CHECK-NEXT: [[ENTRY:.*:]]
170179
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 [[X]], i64 [[LEN]])
171180
; CHECK-NEXT: ret ptr [[MEMCHR]]
@@ -174,3 +183,7 @@ entry:
174183
%memchr = call ptr @memchr(ptr @str, i32 %x, i64 %len)
175184
ret ptr %memchr
176185
}
186+
187+
!0 = !{!"function_entry_count", i64 1000}
188+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
189+
; CHECK: [[PROF_1]] = !{!"unknown", !"aggressive-instcombine"}

0 commit comments

Comments
 (0)