Skip to content

[Clang][BPF] Add __BPF_CPU_VERSION__ macro #71856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions clang/lib/Basic/Targets/BPF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ void BPFTargetInfo::getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
Builder.defineMacro("__bpf__");
Builder.defineMacro("__BPF__");

std::string CPU = getTargetOpts().CPU;
if (CPU == "probe") {
Builder.defineMacro("__BPF_CPU_VERSION__", "0");
return;
}
if (CPU.empty() || CPU == "generic" || CPU == "v1") {
Builder.defineMacro("__BPF_CPU_VERSION__", "1");
return;
}

std::string CpuVerNumStr = CPU.substr(1);
Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr);

int CpuVerNum = std::stoi(CpuVerNumStr);
if (CpuVerNum >= 2)
Builder.defineMacro("__BPF_FEATURE_JMP_EXT");

if (CpuVerNum >= 3) {
Builder.defineMacro("__BPF_FEATURE_JMP32");
Builder.defineMacro("__BPF_FEATURE_ALU32");
}

if (CpuVerNum >= 4) {
Builder.defineMacro("__BPF_FEATURE_LDSX");
Builder.defineMacro("__BPF_FEATURE_MOVSX");
Builder.defineMacro("__BPF_FEATURE_BSWAP");
Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD");
Builder.defineMacro("__BPF_FEATURE_GOTOL");
Builder.defineMacro("__BPF_FEATURE_ST");
}
}

static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2",
Expand Down
83 changes: 81 additions & 2 deletions clang/test/Preprocessor/bpf-predefined-macros.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// RUN: %clang -E -target bpfel -x c -o - %s | FileCheck %s
// RUN: %clang -E -target bpfeb -x c -o - %s | FileCheck %s
// RUN: %clang -E -target bpfel -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_NO %s
// RUN: %clang -E -target bpfeb -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_NO %s
// RUN: %clang -E -target bpfel -mcpu=v1 -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_V1 %s
// RUN: %clang -E -target bpfel -mcpu=v2 -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_V2 %s
// RUN: %clang -E -target bpfel -mcpu=v3 -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_V3 %s
// RUN: %clang -E -target bpfel -mcpu=v4 -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_V4 %s
// RUN: %clang -E -target bpfel -mcpu=generic -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_GENERIC %s
// RUN: %clang -E -target bpfel -mcpu=probe -x c -o - %s | FileCheck -check-prefix=CHECK -check-prefix=CPU_PROBE %s

#ifdef __bpf__
int b;
Expand All @@ -10,7 +16,80 @@ int c;
#ifdef bpf
int d;
#endif
#ifdef __BPF_CPU_VERSION__
int e;
#endif
#if __BPF_CPU_VERSION__ == 0
int f;
#endif
#if __BPF_CPU_VERSION__ == 1
int g;
#endif
#if __BPF_CPU_VERSION__ == 2
int h;
#endif
#if __BPF_CPU_VERSION__ == 3
int i;
#endif
#if __BPF_CPU_VERSION__ == 4
int j;
#endif
#ifdef __BPF_FEATURE_JMP_EXT
int k;
#endif
#ifdef __BPF_FEATURE_JMP32
int l;
#endif
#ifdef __BPF_FEATURE_ALU32
int m;
#endif
#ifdef __BPF_FEATURE_LDSX
int n;
#endif
#ifdef __BPF_FEATURE_MOVSX
int o;
#endif
#ifdef __BPF_FEATURE_BSWAP
int p;
#endif
#ifdef __BPF_FEATURE_SDIV_SMOD
int q;
#endif
#ifdef __BPF_FEATURE_GOTOL
int r;
#endif
#ifdef __BPF_FEATURE_ST
int s;
#endif

// CHECK: int b;
// CHECK: int c;
// CHECK-NOT: int d;
// CHECK: int e;

// CPU_NO: int g;

// CPU_V1: int g;

// CPU_V2: int h;
// CPU_V2: int k;

// CPU_V3: int i;
// CPU_V3: int k;
// CPU_V3: int l;
// CPU_V3: int m;

// CPU_V4: int j;
// CPU_V4: int k;
// CPU_V4: int l;
// CPU_V4: int m;
// CPU_V4: int n;
// CPU_V4: int o;
// CPU_V4: int p;
// CPU_V4: int q;
// CPU_V4: int r;
// CPU_V4: int s;

// CPU_GENERIC: int g;

// CPU_PROBE: int f;