[llvm] 6ee71e5 - [BPF] handle opaque-pointer for __builtin_preserve_enum_value

Yonghong Song via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 14 11:35:00 PDT 2022


Author: Yonghong Song
Date: 2022-04-14T11:34:32-07:00
New Revision: 6ee71e53e56b382d3f3a38751f84b02feb0befda

URL: https://github.com/llvm/llvm-project/commit/6ee71e53e56b382d3f3a38751f84b02feb0befda
DIFF: https://github.com/llvm/llvm-project/commit/6ee71e53e56b382d3f3a38751f84b02feb0befda.diff

LOG: [BPF] handle opaque-pointer for __builtin_preserve_enum_value

Opaque pointer [1] is enabled as the default with commit [2].
Andrii found that current __builtin_preserve_enum_value() can only handle
non opaque pointer code pattern and will segfault with latest
llvm main branch where opaque-pointer is enabled by default.

This patch added the opaque pointer support.
Besides llvm selftests, also verified with bpf-next bpf selftests.

  [1] https://llvm.org/docs/OpaquePointers.html
  [2] https://reviews.llvm.org/D123122

Differential Revision: https://reviews.llvm.org/D123800

Added: 
    llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value-opaque-pointer.ll

Modified: 
    llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
index 64cdb51bdf819..de1b66b3c01be 100644
--- a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
+++ b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
@@ -943,8 +943,11 @@ MDNode *BPFAbstractMemberAccess::computeAccessKey(CallInst *Call,
     // ENUM_VALUE_EXISTENCE and ENUM_VALUE
     IsInt32Ret = false;
 
-    const auto *CE = cast<ConstantExpr>(Call->getArgOperand(1));
-    const GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
+    // The argument could be a global variable or a getelementptr with base to
+    // a global variable depending on whether the clang option `opaque-options`
+    // is set or not.
+    const GlobalVariable *GV =
+        cast<GlobalVariable>(Call->getArgOperand(1)->stripPointerCasts());
     assert(GV->hasInitializer());
     const ConstantDataArray *DA = cast<ConstantDataArray>(GV->getInitializer());
     assert(DA->isString());

diff  --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value-opaque-pointer.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value-opaque-pointer.ll
new file mode 100644
index 0000000000000..2acf942d3503e
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value-opaque-pointer.ll
@@ -0,0 +1,103 @@
+; RUN: opt -O2 %s | llvm-dis > %t1
+; RUN: llc -filetype=asm -o - %t1 | FileCheck %s
+; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck %s
+;
+; Source:
+;   enum AA { VAL1 = -100, VAL2 = 0xffff8000 };
+;   typedef enum { VAL10 = 0xffffFFFF80000000 } __BB;
+;   int test() {
+;     return __builtin_preserve_enum_value(*(enum AA *)VAL1, 0) +
+;            __builtin_preserve_enum_value(*(enum AA *)VAL2, 1) +
+;            __builtin_preserve_enum_value(*(__BB *)VAL10, 1);
+;   }
+; Compiler flag to generate IR:
+;   clang -target bpf -S -O2 -g -emit-llvm -Xclang -disable-llvm-passes t1.c
+
+target triple = "bpf"
+
+ at 0 = private unnamed_addr constant [10 x i8] c"VAL1:-100\00", align 1
+ at 1 = private unnamed_addr constant [16 x i8] c"VAL2:4294934528\00", align 1
+ at 2 = private unnamed_addr constant [18 x i8] c"VAL10:-2147483648\00", align 1
+
+; Function Attrs: nounwind
+define dso_local i32 @test() #0 !dbg !19 {
+entry:
+  %0 = call i64 @llvm.bpf.preserve.enum.value(i32 0, ptr @0, i64 0), !dbg !24, !llvm.preserve.access.index !3
+  %1 = call i64 @llvm.bpf.preserve.enum.value(i32 1, ptr @1, i64 1), !dbg !25, !llvm.preserve.access.index !3
+  %add = add i64 %0, %1, !dbg !26
+  %2 = call i64 @llvm.bpf.preserve.enum.value(i32 2, ptr @2, i64 1), !dbg !27, !llvm.preserve.access.index !13
+  %add1 = add i64 %add, %2, !dbg !28
+  %conv = trunc i64 %add1 to i32, !dbg !24
+  ret i32 %conv, !dbg !29
+}
+
+; CHECK:             r{{[0-9]+}} = 1 ll
+; CHECK:             r{{[0-9]+}} = 4294934528 ll
+; CHECK:             r{{[0-9]+}} = -2147483648 ll
+; CHECK:             exit
+
+; CHECK:             .long   16                              # BTF_KIND_ENUM(id = 4)
+; CHECK:             .long   57                              # BTF_KIND_TYPEDEF(id = 5)
+
+; CHECK:             .ascii  ".text"                         # string offset=10
+; CHECK:             .ascii  "AA"                            # string offset=16
+; CHECK:             .byte   48                              # string offset=29
+; CHECK:             .byte   49                              # string offset=55
+; CHECK:             .ascii  "__BB"                          # string offset=57
+
+; CHECK:             .long   16                              # FieldReloc
+; CHECK-NEXT:        .long   10                              # Field reloc section string offset=10
+; CHECK-NEXT:        .long   3
+; CHECK-NEXT:        .long   .Ltmp{{[0-9]+}}
+; CHECK-NEXT:        .long   4
+; CHECK-NEXT:        .long   29
+; CHECK-NEXT:        .long   10
+; CHECK-NEXT:        .long   .Ltmp{{[0-9]+}}
+; CHECK-NEXT:        .long   4
+; CHECK-NEXT:        .long   55
+; CHECK-NEXT:        .long   11
+; CHECK-NEXT:        .long   .Ltmp{{[0-9]+}}
+; CHECK-NEXT:        .long   5
+; CHECK-NEXT:        .long   29
+; CHECK-NEXT:        .long   11
+
+; Function Attrs: nounwind readnone
+declare i64 @llvm.bpf.preserve.enum.value(i32, ptr, i64) #1
+
+attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #1 = { nounwind readnone }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!14, !15, !16, !17}
+!llvm.ident = !{!18}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 15.0.0 (https://github.com/llvm/llvm-project.git 1218d7e1cf1284666cd7403ea021e40b3b40e92b)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !12, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "t1.c", directory: "/tmp/home/yhs/tmp1", checksumkind: CSK_MD5, checksum: "e1a546573a450dae0abedfbf6bebcba9")
+!2 = !{!3, !8}
+!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA", file: !1, line: 1, baseType: !4, size: 64, elements: !5)
+!4 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
+!5 = !{!6, !7}
+!6 = !DIEnumerator(name: "VAL1", value: -100)
+!7 = !DIEnumerator(name: "VAL2", value: 4294934528)
+!8 = !DICompositeType(tag: DW_TAG_enumeration_type, file: !1, line: 2, baseType: !9, size: 64, elements: !10)
+!9 = !DIBasicType(name: "unsigned long", size: 64, encoding: DW_ATE_unsigned)
+!10 = !{!11}
+!11 = !DIEnumerator(name: "VAL10", value: 18446744071562067968, isUnsigned: true)
+!12 = !{!13}
+!13 = !DIDerivedType(tag: DW_TAG_typedef, name: "__BB", file: !1, line: 2, baseType: !8)
+!14 = !{i32 7, !"Dwarf Version", i32 5}
+!15 = !{i32 2, !"Debug Info Version", i32 3}
+!16 = !{i32 1, !"wchar_size", i32 4}
+!17 = !{i32 7, !"frame-pointer", i32 2}
+!18 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 1218d7e1cf1284666cd7403ea021e40b3b40e92b)"}
+!19 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 3, type: !20, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !23)
+!20 = !DISubroutineType(types: !21)
+!21 = !{!22}
+!22 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!23 = !{}
+!24 = !DILocation(line: 4, column: 10, scope: !19)
+!25 = !DILocation(line: 5, column: 10, scope: !19)
+!26 = !DILocation(line: 4, column: 61, scope: !19)
+!27 = !DILocation(line: 6, column: 10, scope: !19)
+!28 = !DILocation(line: 5, column: 61, scope: !19)
+!29 = !DILocation(line: 4, column: 3, scope: !19)


        


More information about the llvm-commits mailing list