Skip to content

Commit 1d1469a

Browse files
yonghong-songtstellar
authored andcommitted
BPF: fix a CORE optimization bug
For the test case in this patch like below struct t { int a; } __attribute__((preserve_access_index)); int foo(void *); int test(struct t *arg) { long param[1]; param[0] = (long)&arg->a; return foo(param); } The IR right before BPF SimplifyPatchable phase: %1:gpr = LD_imm64 @"llvm.t:0:0$0:0" %2:gpr = LDD killed %1:gpr, 0 %3:gpr = ADD_rr %0:gpr(tied-def 0), killed %2:gpr STD killed %3:gpr, %stack.0.param, 0 After SimplifyPatchable phase, the incorrect IR is generated: %1:gpr = LD_imm64 @"llvm.t:0:0$0:0" %3:gpr = ADD_rr %0:gpr(tied-def 0), killed %1:gpr CORE_MEM killed %3:gpr, 306, %0:gpr, @"llvm.t:0:0$0:0" Note that CORE_MEM pseudo op is introduced to encode memory operations related to CORE. In the above, we intend to check whether we have a store like *(%3:gpr + 0) = ... and if this is the case, we could replace it with *(%0:gpr + @"llvm.t:0:0$0:0"_ = ... Unfortunately, in the above, IR for the store is *(%stack.0.param + 0) = %3:gpr and transformation should not happen. Note that we won't have problem if the actual CORE dereference (arg->a) happens. This patch fixed the problem by skip CORE optimization if the use of ADD_rr result is not the base address of the store operation. Differential Revision: https://reviews.llvm.org/D78466 (cherry picked from commit 3cb7e7b)
1 parent 98f9f73 commit 1d1469a

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,22 @@ void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,
116116
else
117117
continue;
118118

119-
// It must be a form of %1 = *(type *)(%2 + 0) or *(type *)(%2 + 0) = %1.
119+
// It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.
120120
const MachineOperand &ImmOp = DefInst->getOperand(2);
121121
if (!ImmOp.isImm() || ImmOp.getImm() != 0)
122122
continue;
123123

124+
// Reject the form:
125+
// %1 = ADD_rr %2, %3
126+
// *(type *)(%2 + 0) = %1
127+
if (Opcode == BPF::STB || Opcode == BPF::STH || Opcode == BPF::STW ||
128+
Opcode == BPF::STD || Opcode == BPF::STB32 || Opcode == BPF::STH32 ||
129+
Opcode == BPF::STW32) {
130+
const MachineOperand &Opnd = DefInst->getOperand(0);
131+
if (Opnd.isReg() && Opnd.getReg() == I->getReg())
132+
continue;
133+
}
134+
124135
BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))
125136
.add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)
126137
.addGlobalAddress(GVal);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s
2+
; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s
3+
; Source code:
4+
; struct t {
5+
; int a;
6+
; } __attribute__((preserve_access_index));
7+
; int foo(void *);
8+
; int test(struct t *arg) {
9+
; long param[1];
10+
; param[0] = (long)&arg->a;
11+
; return foo(param);
12+
; }
13+
; Compiler flag to generate IR:
14+
; clang -target bpf -S -O2 -g -emit-llvm test.c
15+
16+
%struct.t = type { i32 }
17+
18+
; Function Attrs: nounwind
19+
define dso_local i32 @test(%struct.t* %arg) local_unnamed_addr #0 !dbg !14 {
20+
entry:
21+
%param = alloca [1 x i64], align 8
22+
call void @llvm.dbg.value(metadata %struct.t* %arg, metadata !22, metadata !DIExpression()), !dbg !27
23+
%0 = bitcast [1 x i64]* %param to i8*, !dbg !28
24+
call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %0) #5, !dbg !28
25+
call void @llvm.dbg.declare(metadata [1 x i64]* %param, metadata !23, metadata !DIExpression()), !dbg !29
26+
%1 = tail call i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.ts(%struct.t* %arg, i32 0, i32 0), !dbg !30, !llvm.preserve.access.index !18
27+
%2 = ptrtoint i32* %1 to i64, !dbg !31
28+
%arrayidx = getelementptr inbounds [1 x i64], [1 x i64]* %param, i64 0, i64 0, !dbg !32
29+
store i64 %2, i64* %arrayidx, align 8, !dbg !33, !tbaa !34
30+
%call = call i32 @foo(i8* nonnull %0) #5, !dbg !38
31+
call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %0) #5, !dbg !39
32+
ret i32 %call, !dbg !40
33+
}
34+
35+
; CHECK: r[[OFFSET:[0-9]+]] = 0
36+
; CHECK: r1 += r[[OFFSET]]
37+
; CHECK: *(u64 *)(r10 - 8) = r1
38+
39+
; Function Attrs: nounwind readnone speculatable
40+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
41+
42+
; Function Attrs: argmemonly nounwind
43+
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #2
44+
45+
; Function Attrs: nounwind readnone
46+
declare i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.ts(%struct.t*, i32, i32) #3
47+
48+
declare !dbg !5 dso_local i32 @foo(i8*) local_unnamed_addr #4
49+
50+
; Function Attrs: argmemonly nounwind
51+
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #2
52+
53+
; Function Attrs: nounwind readnone speculatable
54+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
55+
56+
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" }
57+
attributes #1 = { nounwind readnone speculatable }
58+
attributes #2 = { argmemonly nounwind }
59+
attributes #3 = { nounwind readnone }
60+
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" }
61+
attributes #5 = { nounwind }
62+
63+
!llvm.dbg.cu = !{!0}
64+
!llvm.module.flags = !{!10, !11, !12}
65+
!llvm.ident = !{!13}
66+
67+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 4f995959a05ae94cc4f9cc80035f7e4b3ecd2d88)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, splitDebugInlining: false, nameTableKind: None)
68+
!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/core")
69+
!2 = !{}
70+
!3 = !{!4, !5}
71+
!4 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
72+
!5 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 4, type: !6, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
73+
!6 = !DISubroutineType(types: !7)
74+
!7 = !{!8, !9}
75+
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
76+
!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
77+
!10 = !{i32 7, !"Dwarf Version", i32 4}
78+
!11 = !{i32 2, !"Debug Info Version", i32 3}
79+
!12 = !{i32 1, !"wchar_size", i32 4}
80+
!13 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git 4f995959a05ae94cc4f9cc80035f7e4b3ecd2d88)"}
81+
!14 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 5, type: !15, scopeLine: 5, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !21)
82+
!15 = !DISubroutineType(types: !16)
83+
!16 = !{!8, !17}
84+
!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64)
85+
!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t", file: !1, line: 1, size: 32, elements: !19)
86+
!19 = !{!20}
87+
!20 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !18, file: !1, line: 2, baseType: !8, size: 32)
88+
!21 = !{!22, !23}
89+
!22 = !DILocalVariable(name: "arg", arg: 1, scope: !14, file: !1, line: 5, type: !17)
90+
!23 = !DILocalVariable(name: "param", scope: !14, file: !1, line: 6, type: !24)
91+
!24 = !DICompositeType(tag: DW_TAG_array_type, baseType: !4, size: 64, elements: !25)
92+
!25 = !{!26}
93+
!26 = !DISubrange(count: 1)
94+
!27 = !DILocation(line: 0, scope: !14)
95+
!28 = !DILocation(line: 6, column: 5, scope: !14)
96+
!29 = !DILocation(line: 6, column: 10, scope: !14)
97+
!30 = !DILocation(line: 7, column: 28, scope: !14)
98+
!31 = !DILocation(line: 7, column: 16, scope: !14)
99+
!32 = !DILocation(line: 7, column: 5, scope: !14)
100+
!33 = !DILocation(line: 7, column: 14, scope: !14)
101+
!34 = !{!35, !35, i64 0}
102+
!35 = !{!"long", !36, i64 0}
103+
!36 = !{!"omnipotent char", !37, i64 0}
104+
!37 = !{!"Simple C/C++ TBAA"}
105+
!38 = !DILocation(line: 8, column: 12, scope: !14)
106+
!39 = !DILocation(line: 9, column: 1, scope: !14)
107+
!40 = !DILocation(line: 8, column: 5, scope: !14)

0 commit comments

Comments
 (0)