Skip to content

Commit 521d3e3

Browse files
[BPF][ProfCheck] Fix sink-min-max.ll
In bpf-check-and-opt-ir, llvm.umax and llvm.umin are lowered into explicit comparisons/i1 bitwise operations involving select. Make sure to mark the selects as having an explicitly unknown profile given we cannot infer what is likely/unlikely to avoid profcheck failures. Reviewers: yonghong-song, mtrofin, eddyz87 Pull Request: llvm/llvm-project#209682
1 parent c102184 commit 521d3e3

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

llvm/lib/Target/BPF/BPFCheckAndAdjustIR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/IR/IntrinsicsBPF.h"
3232
#include "llvm/IR/Module.h"
3333
#include "llvm/IR/PassManager.h"
34+
#include "llvm/IR/ProfDataUtils.h"
3435
#include "llvm/IR/Type.h"
3536
#include "llvm/IR/Value.h"
3637
#include "llvm/Pass.h"
@@ -293,6 +294,9 @@ static bool sinkMinMaxInBB(BasicBlock &BB,
293294
// x < max(a, b) -> x < a || x < b
294295
Replacement = Builder.CreateLogicalOr(LHS, RHS);
295296

297+
if (SelectInst *ReplacementInst = dyn_cast<SelectInst>(Replacement))
298+
setExplicitlyUnknownBranchWeightsIfProfiled(*ReplacementInst, DEBUG_TYPE);
299+
296300
ICmp->replaceAllUsesWith(Replacement);
297301

298302
Instruction *ToRemove[] = {ICmp, Info.ZExt, Info.SExt, MinMax};

llvm/test/CodeGen/BPF/sink-min-max.ll

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
; @test10: check that umin belonging to the same loop is not touched
1616
; @test11: check that nested loops are processed
1717

18-
define i32 @test1(i64 %a, i64 %b, i64 %x) {
18+
define i32 @test1(i64 %a, i64 %b, i64 %x) !prof !0 {
1919
; CHECK-LABEL: define i32 @test1(
20-
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i64 [[X:%.*]]) {
20+
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i64 [[X:%.*]]) !prof [[PROF0:![0-9]+]] {
2121
; CHECK-NEXT: [[ENTRY:.*:]]
2222
; CHECK-NEXT: br label %[[LOOP:.*]]
2323
; CHECK: [[LOOP]]:
2424
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[X]], [[A]]
2525
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[X]], [[B]]
26-
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP0]], i1 [[TMP1]], i1 false
26+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP0]], i1 [[TMP1]], i1 false, !prof [[PROF1:![0-9]+]]
2727
; CHECK-NEXT: br i1 [[TMP2]], label %[[LOOP]], label %[[RET:.*]]
2828
; CHECK: [[RET]]:
2929
; CHECK-NEXT: ret i32 0
@@ -37,15 +37,15 @@ loop:
3737
ret: ret i32 0
3838
}
3939

40-
define i32 @test2(i64 %a, i64 %b, i64 %x) {
40+
define i32 @test2(i64 %a, i64 %b, i64 %x) !prof !0 {
4141
; CHECK-LABEL: define i32 @test2(
42-
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i64 [[X:%.*]]) {
42+
; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i64 [[X:%.*]]) !prof [[PROF0]] {
4343
; CHECK-NEXT: [[ENTRY:.*:]]
4444
; CHECK-NEXT: br label %[[LOOP:.*]]
4545
; CHECK: [[LOOP]]:
4646
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[X]], [[A]]
4747
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[X]], [[B]]
48-
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP0]], i1 true, i1 [[TMP1]]
48+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP0]], i1 true, i1 [[TMP1]], !prof [[PROF1]]
4949
; CHECK-NEXT: br i1 [[TMP2]], label %[[LOOP]], label %[[RET:.*]]
5050
; CHECK: [[RET]]:
5151
; CHECK-NEXT: ret i32 0
@@ -279,3 +279,9 @@ declare i32 @llvm.umin.i32(i32, i32)
279279
declare i32 @llvm.smin.i32(i32, i32)
280280
declare i32 @llvm.umax.i32(i32, i32)
281281
declare i32 @llvm.smax.i32(i32, i32)
282+
283+
!0 = !{!"function_entry_count", i64 1000}
284+
;.
285+
; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000}
286+
; CHECK: [[PROF1]] = !{!"unknown", !"bpf-check-and-opt-ir"}
287+
;.

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CodeGen/ARM/sjljeh-swifterror.ll
2-
CodeGen/BPF/sink-min-max.ll
32
CodeGen/WinEH/wineh-comdat.ll
43
CodeGen/WinEH/wineh-scope-statenumbering.ll
54
CodeGen/WinEH/wineh-setjmp.ll

0 commit comments

Comments
 (0)