Skip to content

Commit 54d9f74

Browse files
committed
BPF: move AbstractMemberAccess and PreserveDIType passes to EP_EarlyAsPossible
Move abstractMemberAccess and PreserveDIType passes as early as possible, right after clang code generation. Currently, compiler may transform the above code p1 = llvm.bpf.builtin.preserve.struct.access(base, 0, 0); p2 = llvm.bpf.builtin.preserve.struct.access(p1, 1, 2); a = llvm.bpf.builtin.preserve_field_info(p2, EXIST); if (a) { p1 = llvm.bpf.builtin.preserve.struct.access(base, 0, 0); p2 = llvm.bpf.builtin.preserve.struct.access(p1, 1, 2); bpf_probe_read(buf, buf_size, p2); } to p1 = llvm.bpf.builtin.preserve.struct.access(base, 0, 0); p2 = llvm.bpf.builtin.preserve.struct.access(p1, 1, 2); a = llvm.bpf.builtin.preserve_field_info(p2, EXIST); if (a) { bpf_probe_read(buf, buf_size, p2); } and eventually assembly code looks like reloc_exist = 1; reloc_member_offset = 10; //calculate member offset from base p2 = base + reloc_member_offset; if (reloc_exist) { bpf_probe_read(bpf, buf_size, p2); } if during libbpf relocation resolution, reloc_exist is actually resolved to 0 (not exist), reloc_member_offset relocation cannot be resolved and will be patched with illegal instruction. This will cause verifier failure. This patch attempts to address this issue by do chaining analysis and replace chains with special globals right after clang code gen. This will remove the cse possibility described in the above. The IR typically looks like %6 = load @llvm.sk_buff:0:50$0:0:0:2:0 %7 = bitcast %struct.sk_buff* %2 to i8* %8 = getelementptr i8, i8* %7, %6 for a particular address computation relocation. But this transformation has another consequence, code sinking may happen like below: PHI = <possibly different @preserve_*_access_globals> %7 = bitcast %struct.sk_buff* %2 to i8* %8 = getelementptr i8, i8* %7, %6 For such cases, we will not able to generate relocations since multiple relocations are merged into one. This patch introduced a passthrough builtin to prevent such optimization. Looks like inline assembly has more impact for optimizaiton, e.g., inlining. Using passthrough has less impact on optimizations. A new IR pass is introduced at the beginning of target-dependent IR optimization, which does: - report fatal error if any reloc global in PHI nodes - remove all bpf passthrough builtin functions Changes for existing CORE tests: - for clang tests, add "-Xclang -disable-llvm-passes" flags to avoid builtin->reloc_global transformation so the test is still able to check correctness for clang generated IR. - for llvm CodeGen/BPF tests, add "opt -O2 <ir_file> | llvm-dis" command before "llc" command since "opt" is needed to call newly-placed builtin->reloc_global transformation. Add target triple in the IR file since "opt" requires it. - Since target triple is added in IR file, if a test may produce different results for different endianness, two tests will be created, one for bpfeb and another for bpfel, e.g., some tests for relocation of lshift/rshift of bitfields. - field-reloc-bitfield-1.ll has different relocations compared to old codes. This is because for the structure in the test, new code returns struct layout alignment 4 while old code is 8. Align 8 is more precise and permits double load. With align 4, the new mechanism uses 4-byte load, so generating different relocations. - test intrinsic-transforms.ll is removed. This is used to test cse on intrinsics so we do not lose metadata. Now metadata is attached to global and not instruction, it won't get lost with cse. Differential Revision: https://reviews.llvm.org/D87153
1 parent ee80615 commit 54d9f74

File tree

90 files changed

+1367
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1367
-517
lines changed

clang/test/CodeGen/bpf-attr-preserve-access-index-1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-5.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-7.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-attr-preserve-access-index-8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define __reloc__ __attribute__((preserve_access_index))
55

clang/test/CodeGen/bpf-preserve-access-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang %s -target bpfeb -x c -emit-llvm -S -g -O2 -o - | FileCheck --check-prefix=CHECK %s
2-
// RUN: %clang %s -target bpfel -x c -emit-llvm -S -g -O2 -o - | FileCheck --check-prefix=CHECK %s
1+
// RUN: %clang %s -target bpfeb -x c -emit-llvm -S -g -O2 -Xclang -disable-llvm-passes -o - | FileCheck --check-prefix=CHECK %s
2+
// RUN: %clang %s -target bpfel -x c -emit-llvm -S -g -O2 -Xclang -disable-llvm-passes -o - | FileCheck --check-prefix=CHECK %s
33

44
struct t {
55
int i:1;

clang/test/CodeGen/builtin-bpf-btf-type-id.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
unsigned test1(int a) { return __builtin_btf_type_id(a, 0); }
55
unsigned test2(int a) { return __builtin_btf_type_id(&a, 0); }

clang/test/CodeGen/builtin-preserve-access-index-typedef.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
55
typedef struct {

clang/test/CodeGen/builtins-bpf-preserve-field-info-1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define _(x, y) (__builtin_preserve_field_info((x), (y)))
55

clang/test/CodeGen/builtins-bpf-preserve-field-info-2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define _(x, y) (__builtin_preserve_field_info((x), (y)))
55

clang/test/CodeGen/builtins-bpf-preserve-field-info-3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define _(x, y) (__builtin_preserve_type_info((x), (y)))
55

clang/test/CodeGen/builtins-bpf-preserve-field-info-4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: bpf-registered-target
2-
// RUN: %clang -target bpf -emit-llvm -S -g %s -o - | FileCheck %s
2+
// RUN: %clang -target bpf -emit-llvm -S -g -Xclang -disable-llvm-passes %s -o - | FileCheck %s
33

44
#define _(x, y) (__builtin_preserve_enum_value((x), (y)))
55

llvm/include/llvm/IR/IntrinsicsBPF.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ let TargetPrefix = "bpf" in { // All intrinsics start with "llvm.bpf."
3232
def int_bpf_preserve_enum_value : GCCBuiltin<"__builtin_bpf_preserve_enum_value">,
3333
Intrinsic<[llvm_i64_ty], [llvm_i32_ty, llvm_ptr_ty, llvm_i64_ty],
3434
[IntrNoMem]>;
35+
def int_bpf_passthrough : GCCBuiltin<"__builtin_bpf_passthrough">,
36+
Intrinsic<[llvm_any_ty], [llvm_i32_ty, llvm_any_ty], [IntrNoMem]>;
3537
}

llvm/lib/Target/BPF/BPF.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
namespace llvm {
1616
class BPFTargetMachine;
1717

18-
ModulePass *createBPFAbstractMemberAccess(BPFTargetMachine *TM);
19-
ModulePass *createBPFPreserveDIType();
18+
ModulePass *createBPFCheckAndAdjustIR();
2019

20+
FunctionPass *createBPFAbstractMemberAccess(BPFTargetMachine *TM);
21+
FunctionPass *createBPFPreserveDIType();
2122
FunctionPass *createBPFISelDag(BPFTargetMachine &TM);
2223
FunctionPass *createBPFMISimplifyPatchablePass();
2324
FunctionPass *createBPFMIPeepholePass();
2425
FunctionPass *createBPFMIPeepholeTruncElimPass();
2526
FunctionPass *createBPFMIPreEmitPeepholePass();
2627
FunctionPass *createBPFMIPreEmitCheckingPass();
2728

29+
void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
30+
2831
void initializeBPFAbstractMemberAccessPass(PassRegistry&);
2932
void initializeBPFPreserveDITypePass(PassRegistry&);
3033
void initializeBPFMISimplifyPatchablePass(PassRegistry&);

0 commit comments

Comments
 (0)