Skip to content

Commit 3f4ba96

Browse files
yonghong-songzmodem
authored andcommitted
[BPF] disable ReduceLoadWidth during SelectionDag phase
The compiler may transform the following code ctx = ctx + reloc_offset ... (*(u32 *)ctx) & 0x8000 ... to ctx = ctx + reloc_offset ... (*(u8 *)(ctx + 1)) & 0x80 ... where reloc_offset will be replaced with a constant during AsmPrinter phase. The above transformed code will be rejected the kernel verifier as it does not allow *(type *)((ctx + non_zero_offset1) + non_zero_offset2) style access pattern. It is hard at SelectionDag phase to identify whether a load is related to context or not. Sometime, interprocedure analysis may be needed. So let us simply prevent such optimization from happening. Differential Revision: https://reviews.llvm.org/D73997 (cherry picked from commit d96c1bb)
1 parent 720870e commit 3f4ba96

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ class BPFTargetLowering : public TargetLowering {
110110
return true;
111111
}
112112

113+
// Prevent reducing load width during SelectionDag phase.
114+
// Otherwise, we may transform the following
115+
// ctx = ctx + reloc_offset
116+
// ... (*(u32 *)ctx) & 0x8000...
117+
// to
118+
// ctx = ctx + reloc_offset
119+
// ... (*(u8 *)(ctx + 1)) & 0x80 ...
120+
// which will be rejected by the verifier.
121+
bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy,
122+
EVT NewVT) const override {
123+
return false;
124+
}
125+
113126
unsigned EmitSubregExt(MachineInstr &MI, MachineBasicBlock *BB, unsigned Reg,
114127
bool isSigned) const;
115128

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
; Source code:
4+
; struct data_t {
5+
; int d1;
6+
; int d2;
7+
; };
8+
; struct info_t {
9+
; int pid;
10+
; int flags;
11+
; } __attribute__((preserve_access_index));
12+
;
13+
; extern void output(void *);
14+
; void test(struct info_t * args) {
15+
; int is_mask2 = args->flags & 0x10000;
16+
; struct data_t data = {};
17+
;
18+
; data.d1 = is_mask2 ? 2 : args->pid;
19+
; data.d2 = (is_mask2 || (args->flags & 0x8000)) ? 1 : 2;
20+
; output(&data);
21+
; }
22+
; Compilation flag:
23+
; clang -target bpf -O2 -g -S -emit-llvm test.c
24+
25+
%struct.info_t = type { i32, i32 }
26+
%struct.data_t = type { i32, i32 }
27+
28+
; Function Attrs: nounwind
29+
define dso_local void @test(%struct.info_t* readonly %args) local_unnamed_addr #0 !dbg !12 {
30+
entry:
31+
%data = alloca i64, align 8
32+
%tmpcast = bitcast i64* %data to %struct.data_t*
33+
call void @llvm.dbg.value(metadata %struct.info_t* %args, metadata !22, metadata !DIExpression()), !dbg !29
34+
%0 = tail call i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.info_ts(%struct.info_t* %args, i32 1, i32 1), !dbg !30, !llvm.preserve.access.index !16
35+
%1 = load i32, i32* %0, align 4, !dbg !30, !tbaa !31
36+
%and = and i32 %1, 65536, !dbg !36
37+
call void @llvm.dbg.value(metadata i32 %and, metadata !23, metadata !DIExpression()), !dbg !29
38+
%2 = bitcast i64* %data to i8*, !dbg !37
39+
call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %2) #5, !dbg !37
40+
call void @llvm.dbg.declare(metadata %struct.data_t* %tmpcast, metadata !24, metadata !DIExpression()), !dbg !38
41+
store i64 0, i64* %data, align 8, !dbg !38
42+
%tobool = icmp eq i32 %and, 0, !dbg !39
43+
br i1 %tobool, label %cond.false, label %lor.end.critedge, !dbg !39
44+
45+
cond.false: ; preds = %entry
46+
%3 = tail call i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.info_ts(%struct.info_t* %args, i32 0, i32 0), !dbg !40, !llvm.preserve.access.index !16
47+
%4 = load i32, i32* %3, align 4, !dbg !40, !tbaa !41
48+
%d1 = bitcast i64* %data to i32*, !dbg !42
49+
store i32 %4, i32* %d1, align 8, !dbg !43, !tbaa !44
50+
%5 = load i32, i32* %0, align 4, !dbg !46, !tbaa !31
51+
%and2 = and i32 %5, 32768, !dbg !47
52+
%tobool3 = icmp eq i32 %and2, 0, !dbg !48
53+
%phitmp = select i1 %tobool3, i32 2, i32 1, !dbg !48
54+
br label %lor.end, !dbg !48
55+
56+
lor.end.critedge: ; preds = %entry
57+
%d1.c = bitcast i64* %data to i32*, !dbg !42
58+
store i32 2, i32* %d1.c, align 8, !dbg !43, !tbaa !44
59+
br label %lor.end, !dbg !48
60+
61+
lor.end: ; preds = %lor.end.critedge, %cond.false
62+
%6 = phi i32 [ %phitmp, %cond.false ], [ 1, %lor.end.critedge ]
63+
%d2 = getelementptr inbounds %struct.data_t, %struct.data_t* %tmpcast, i64 0, i32 1, !dbg !49
64+
store i32 %6, i32* %d2, align 4, !dbg !50, !tbaa !51
65+
call void @output(i8* nonnull %2) #5, !dbg !52
66+
call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %2) #5, !dbg !53
67+
ret void, !dbg !53
68+
}
69+
70+
; CHECK: r[[LOAD1:[0-9]+]] = *(u32 *)(r{{[0-9]+}} + 4)
71+
; CHECK: r[[LOAD1]] &= 65536
72+
; CHECK: r[[LOAD2:[0-9]+]] = *(u32 *)(r{{[0-9]+}} + 4)
73+
; CHECK: r[[LOAD2]] &= 32768
74+
75+
; Function Attrs: nounwind readnone speculatable willreturn
76+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
77+
78+
; Function Attrs: argmemonly nounwind willreturn
79+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #2
80+
81+
; Function Attrs: nounwind readnone
82+
declare i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.info_ts(%struct.info_t*, i32 immarg, i32 immarg) #3
83+
84+
declare !dbg !4 dso_local void @output(i8*) local_unnamed_addr #4
85+
86+
; Function Attrs: argmemonly nounwind willreturn
87+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #2
88+
89+
; Function Attrs: nounwind readnone speculatable willreturn
90+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
91+
92+
attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
93+
attributes #1 = { nounwind readnone speculatable willreturn }
94+
attributes #2 = { argmemonly nounwind willreturn }
95+
attributes #3 = { nounwind readnone }
96+
attributes #4 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
97+
attributes #5 = { nounwind }
98+
99+
!llvm.dbg.cu = !{!0}
100+
!llvm.module.flags = !{!8, !9, !10}
101+
!llvm.ident = !{!11}
102+
103+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 5884aae58f56786475bbc0f13ad8bd35f7f1ce69)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, splitDebugInlining: false, nameTableKind: None)
104+
!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/core")
105+
!2 = !{}
106+
!3 = !{!4}
107+
!4 = !DISubprogram(name: "output", scope: !1, file: !1, line: 10, type: !5, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
108+
!5 = !DISubroutineType(types: !6)
109+
!6 = !{null, !7}
110+
!7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
111+
!8 = !{i32 7, !"Dwarf Version", i32 4}
112+
!9 = !{i32 2, !"Debug Info Version", i32 3}
113+
!10 = !{i32 1, !"wchar_size", i32 4}
114+
!11 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git 5884aae58f56786475bbc0f13ad8bd35f7f1ce69)"}
115+
!12 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 11, type: !13, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !21)
116+
!13 = !DISubroutineType(types: !14)
117+
!14 = !{null, !15}
118+
!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64)
119+
!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "info_t", file: !1, line: 5, size: 64, elements: !17)
120+
!17 = !{!18, !20}
121+
!18 = !DIDerivedType(tag: DW_TAG_member, name: "pid", scope: !16, file: !1, line: 6, baseType: !19, size: 32)
122+
!19 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
123+
!20 = !DIDerivedType(tag: DW_TAG_member, name: "flags", scope: !16, file: !1, line: 7, baseType: !19, size: 32, offset: 32)
124+
!21 = !{!22, !23, !24}
125+
!22 = !DILocalVariable(name: "args", arg: 1, scope: !12, file: !1, line: 11, type: !15)
126+
!23 = !DILocalVariable(name: "is_mask2", scope: !12, file: !1, line: 12, type: !19)
127+
!24 = !DILocalVariable(name: "data", scope: !12, file: !1, line: 13, type: !25)
128+
!25 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "data_t", file: !1, line: 1, size: 64, elements: !26)
129+
!26 = !{!27, !28}
130+
!27 = !DIDerivedType(tag: DW_TAG_member, name: "d1", scope: !25, file: !1, line: 2, baseType: !19, size: 32)
131+
!28 = !DIDerivedType(tag: DW_TAG_member, name: "d2", scope: !25, file: !1, line: 3, baseType: !19, size: 32, offset: 32)
132+
!29 = !DILocation(line: 0, scope: !12)
133+
!30 = !DILocation(line: 12, column: 24, scope: !12)
134+
!31 = !{!32, !33, i64 4}
135+
!32 = !{!"info_t", !33, i64 0, !33, i64 4}
136+
!33 = !{!"int", !34, i64 0}
137+
!34 = !{!"omnipotent char", !35, i64 0}
138+
!35 = !{!"Simple C/C++ TBAA"}
139+
!36 = !DILocation(line: 12, column: 30, scope: !12)
140+
!37 = !DILocation(line: 13, column: 3, scope: !12)
141+
!38 = !DILocation(line: 13, column: 17, scope: !12)
142+
!39 = !DILocation(line: 15, column: 13, scope: !12)
143+
!40 = !DILocation(line: 15, column: 34, scope: !12)
144+
!41 = !{!32, !33, i64 0}
145+
!42 = !DILocation(line: 15, column: 8, scope: !12)
146+
!43 = !DILocation(line: 15, column: 11, scope: !12)
147+
!44 = !{!45, !33, i64 0}
148+
!45 = !{!"data_t", !33, i64 0, !33, i64 4}
149+
!46 = !DILocation(line: 16, column: 33, scope: !12)
150+
!47 = !DILocation(line: 16, column: 39, scope: !12)
151+
!48 = !DILocation(line: 16, column: 23, scope: !12)
152+
!49 = !DILocation(line: 16, column: 8, scope: !12)
153+
!50 = !DILocation(line: 16, column: 11, scope: !12)
154+
!51 = !{!45, !33, i64 4}
155+
!52 = !DILocation(line: 17, column: 3, scope: !12)
156+
!53 = !DILocation(line: 18, column: 1, scope: !12)

0 commit comments

Comments
 (0)