Skip to content

Commit 6cdf6e5

Browse files
yonghong-songtstellar
authored andcommitted
BPF: avoid NE/EQ loop exit condition
Kuniyuki Iwashima reported in [1] that llvm compiler may convert a loop exit condition with "i < bound" to "i != bound", where "i" is the loop index variable and "bound" is the upper bound. In case that "bound" is not a constant, verifier will always have "i != bound" true, which will cause verifier failure since to verifier this is an infinite loop. The fix is to avoid transforming "i < bound" to "i != bound". In llvm, the transformation is done by IndVarSimplify pass. The compiler checks loop condition cost (i = i + 1) and if the cost is lower, it may transform "i < bound" to "i != bound". This patch implemented getArithmeticInstrCost() in BPF TargetTransformInfo class to return a higher cost for such an operation, which will prevent the transformation for the test case added in this patch. [1] https://lore.kernel.org/netdev/[email protected]/ Differential Revision: https://reviews.llvm.org/D107483 (cherry picked from commit e52946b)
1 parent 8fbd0e2 commit 6cdf6e5

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

llvm/lib/Target/BPF/BPFTargetTransformInfo.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ class BPFTTIImpl : public BasicTTIImplBase<BPFTTIImpl> {
5454
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
5555
I);
5656
}
57+
58+
InstructionCost getArithmeticInstrCost(
59+
unsigned Opcode, Type *Ty,
60+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput,
61+
TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
62+
TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
63+
TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
64+
TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None,
65+
ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
66+
const Instruction *CxtI = nullptr) {
67+
int ISD = TLI->InstructionOpcodeToISD(Opcode);
68+
if (ISD == ISD::ADD && CostKind == TTI::TCK_RecipThroughput)
69+
return SCEVCheapExpansionBudget.getValue() + 1;
70+
71+
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
72+
Opd2Info, Opd1PropInfo,
73+
Opd2PropInfo);
74+
}
5775
};
5876

5977
} // end namespace llvm
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
; RUN: opt -O2 -S -o %t1 < %s
2+
; RUN: llc -march=bpf -mcpu=v3 %t1 -o - | FileCheck %s
3+
;
4+
; Source code:
5+
; typedef unsigned long u64;
6+
; void foo(char *data, int idx, u64 *);
7+
; int test(int len, char *data) {
8+
; if (len < 100) {
9+
; for (int i = 1; i < len; i++) {
10+
; u64 d[1];
11+
; d[0] = data[0] ?: '0';
12+
; foo("%c", i, d);
13+
; }
14+
; }
15+
; return 0;
16+
; }
17+
; Compilation flag:
18+
; clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes test.c
19+
20+
; ModuleID = 'test.c'
21+
source_filename = "test.c"
22+
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
23+
target triple = "bpf"
24+
25+
@.str = private unnamed_addr constant [3 x i8] c"%c\00", align 1
26+
27+
; Function Attrs: nounwind
28+
define dso_local i32 @test(i32 %len, i8* %data) #0 {
29+
entry:
30+
%len.addr = alloca i32, align 4
31+
%data.addr = alloca i8*, align 8
32+
%i = alloca i32, align 4
33+
%d = alloca [1 x i64], align 8
34+
store i32 %len, i32* %len.addr, align 4, !tbaa !3
35+
store i8* %data, i8** %data.addr, align 8, !tbaa !7
36+
%0 = load i32, i32* %len.addr, align 4, !tbaa !3
37+
%cmp = icmp slt i32 %0, 100
38+
br i1 %cmp, label %if.then, label %if.end
39+
40+
if.then: ; preds = %entry
41+
%1 = bitcast i32* %i to i8*
42+
call void @llvm.lifetime.start.p0i8(i64 4, i8* %1) #3
43+
store i32 1, i32* %i, align 4, !tbaa !3
44+
br label %for.cond
45+
46+
for.cond: ; preds = %for.inc, %if.then
47+
%2 = load i32, i32* %i, align 4, !tbaa !3
48+
%3 = load i32, i32* %len.addr, align 4, !tbaa !3
49+
%cmp1 = icmp slt i32 %2, %3
50+
br i1 %cmp1, label %for.body, label %for.cond.cleanup
51+
52+
; CHECK: w[[LEN:[0-9]+]] = w1
53+
; CHECK: w[[IDX:[0-9]+]] += 1
54+
; CHECK-NEXT: w[[IDX]] s< w[[LEN]] goto
55+
56+
for.cond.cleanup: ; preds = %for.cond
57+
%4 = bitcast i32* %i to i8*
58+
call void @llvm.lifetime.end.p0i8(i64 4, i8* %4) #3
59+
br label %for.end
60+
61+
for.body: ; preds = %for.cond
62+
%5 = bitcast [1 x i64]* %d to i8*
63+
call void @llvm.lifetime.start.p0i8(i64 8, i8* %5) #3
64+
%6 = load i8*, i8** %data.addr, align 8, !tbaa !7
65+
%arrayidx = getelementptr inbounds i8, i8* %6, i64 0
66+
%7 = load i8, i8* %arrayidx, align 1, !tbaa !9
67+
%conv = sext i8 %7 to i32
68+
%tobool = icmp ne i32 %conv, 0
69+
br i1 %tobool, label %cond.true, label %cond.false
70+
71+
cond.true: ; preds = %for.body
72+
br label %cond.end
73+
74+
cond.false: ; preds = %for.body
75+
br label %cond.end
76+
77+
cond.end: ; preds = %cond.false, %cond.true
78+
%cond = phi i32 [ %conv, %cond.true ], [ 48, %cond.false ]
79+
%conv2 = sext i32 %cond to i64
80+
%arrayidx3 = getelementptr inbounds [1 x i64], [1 x i64]* %d, i64 0, i64 0
81+
store i64 %conv2, i64* %arrayidx3, align 8, !tbaa !10
82+
%8 = load i32, i32* %i, align 4, !tbaa !3
83+
%arraydecay = getelementptr inbounds [1 x i64], [1 x i64]* %d, i64 0, i64 0
84+
call void @foo(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8, i64* %arraydecay)
85+
%9 = bitcast [1 x i64]* %d to i8*
86+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %9) #3
87+
br label %for.inc
88+
89+
for.inc: ; preds = %cond.end
90+
%10 = load i32, i32* %i, align 4, !tbaa !3
91+
%inc = add nsw i32 %10, 1
92+
store i32 %inc, i32* %i, align 4, !tbaa !3
93+
br label %for.cond, !llvm.loop !12
94+
95+
for.end: ; preds = %for.cond.cleanup
96+
br label %if.end
97+
98+
if.end: ; preds = %for.end, %entry
99+
ret i32 0
100+
}
101+
102+
; Function Attrs: argmemonly nofree nosync nounwind willreturn
103+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
104+
105+
declare dso_local void @foo(i8*, i32, i64*) #2
106+
107+
; Function Attrs: argmemonly nofree nosync nounwind willreturn
108+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1
109+
110+
attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
111+
attributes #1 = { argmemonly nofree nosync nounwind willreturn }
112+
attributes #2 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
113+
attributes #3 = { nounwind }
114+
115+
!llvm.module.flags = !{!0, !1}
116+
!llvm.ident = !{!2}
117+
118+
!0 = !{i32 1, !"wchar_size", i32 4}
119+
!1 = !{i32 7, !"frame-pointer", i32 2}
120+
!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 8385de118443144518c9fba8b3d831d9076e746b)"}
121+
!3 = !{!4, !4, i64 0}
122+
!4 = !{!"int", !5, i64 0}
123+
!5 = !{!"omnipotent char", !6, i64 0}
124+
!6 = !{!"Simple C/C++ TBAA"}
125+
!7 = !{!8, !8, i64 0}
126+
!8 = !{!"any pointer", !5, i64 0}
127+
!9 = !{!5, !5, i64 0}
128+
!10 = !{!11, !11, i64 0}
129+
!11 = !{!"long", !5, i64 0}
130+
!12 = distinct !{!12, !13}
131+
!13 = !{!"llvm.loop.mustprogress"}

0 commit comments

Comments
 (0)