Skip to content

Commit 283db5f

Browse files
committed
BPF: fix enum value 0 issue for __builtin_preserve_enum_value()
Lorenz Bauer reported that the following code will have compilation error for bpf target: enum e { TWO }; bpf_core_enum_value_exists(enum e, TWO); The clang emitted the following error message: __builtin_preserve_enum_value argument 1 invalid In SemaChecking, an expression like "*(enum NAME)1" will have cast kind CK_IntegralToPointer, but "*(enum NAME)0" will have cast kind CK_NullToPointer. Current implementation only permits CK_IntegralToPointer, missing enum value 0 case. This patch permits CK_NullToPointer cast kind and the above test case can pass now. Differential Revision: https://reviews.llvm.org/D97659
1 parent 52b8e10 commit 283db5f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,10 @@ static bool isValidBPFPreserveEnumValueArg(Expr *Arg) {
26262626
return false;
26272627

26282628
const auto *CE = dyn_cast<CStyleCastExpr>(UO->getSubExpr());
2629-
if (!CE || CE->getCastKind() != CK_IntegralToPointer)
2629+
if (!CE)
2630+
return false;
2631+
if (CE->getCastKind() != CK_IntegralToPointer &&
2632+
CE->getCastKind() != CK_NullToPointer)
26302633
return false;
26312634

26322635
// The integer must be from an EnumConstantDecl.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#define _(x, y) (__builtin_preserve_enum_value((x), (y)))
55

66
enum AA {
7+
VAL0 = 0,
78
VAL1 = 2,
89
VAL2 = 0xffffffff80000000UL,
910
};
10-
typedef enum { VAL10 = -2, VAL11 = 0xffff8000, } __BB;
11+
typedef enum { VAL00, VAL10 = -2, VAL11 = 0xffff8000, } __BB;
1112

1213
unsigned unit1() {
1314
return _(*(enum AA *)VAL1, 0) + _(*(__BB *)VAL10, 1);
@@ -17,16 +18,25 @@ unsigned unit2() {
1718
return _(*(enum AA *)VAL2, 0) + _(*(__BB *)VAL11, 1);
1819
}
1920

21+
unsigned unit3() {
22+
return _(*(enum AA *)VAL0, 0) + _(*(__BB *)VAL00, 1);
23+
}
24+
2025
// CHECK: @0 = private unnamed_addr constant [7 x i8] c"VAL1:2\00", align 1
2126
// CHECK: @1 = private unnamed_addr constant [9 x i8] c"VAL10:-2\00", align 1
2227
// CHECK: @2 = private unnamed_addr constant [17 x i8] c"VAL2:-2147483648\00", align 1
2328
// CHECK: @3 = private unnamed_addr constant [17 x i8] c"VAL11:4294934528\00", align 1
29+
// CHECK: @4 = private unnamed_addr constant [7 x i8] c"VAL0:0\00", align 1
30+
// CHECK: @5 = private unnamed_addr constant [8 x i8] c"VAL00:0\00", align 1
2431

2532
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @0, i32 0, i32 0), i64 0), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[ENUM_AA:[0-9]+]]
2633
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @1, i32 0, i32 0), i64 1), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[TYPEDEF_ENUM:[0-9]+]]
2734

2835
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @2, i32 0, i32 0), i64 0), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[ENUM_AA]]
2936
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @3, i32 0, i32 0), i64 1), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[TYPEDEF_ENUM]]
3037

38+
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @4, i32 0, i32 0), i64 0), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[ENUM_AA]]
39+
// CHECK: call i64 @llvm.bpf.preserve.enum.value(i32 5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @5, i32 0, i32 0), i64 1), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[TYPEDEF_ENUM]]
40+
3141
// CHECK: ![[ENUM_AA]] = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA"
3242
// CHECK: ![[TYPEDEF_ENUM]] = !DIDerivedType(tag: DW_TAG_typedef, name: "__BB"

0 commit comments

Comments
 (0)