diff --git a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp --- a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp +++ b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp @@ -165,6 +165,8 @@ Value *computeBaseAndAccessKey(CallInst *Call, CallInfo &CInfo, std::string &AccessKey, MDNode *&BaseMeta); + MDNode *computeAccessKey(CallInst *Call, CallInfo &CInfo, + std::string &AccessKey); uint64_t getConstant(const Value *IndexValue); bool transformGEPChain(Module &M, CallInst *Call, CallInfo &CInfo); }; @@ -285,6 +287,34 @@ CInfo.AccessIndex = InfoKind; return true; } + if (GV->getName().startswith("llvm.bpf.preserve.type.info")) { + CInfo.Kind = BPFPreserveFieldInfoAI; + CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); + if (!CInfo.Metadata) + report_fatal_error("Missing metadata for llvm.preserve.type.info intrinsic"); + uint64_t Flag = getConstant(Call->getArgOperand(2)); + if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_TYPE_INFO_FLAG) + report_fatal_error("Incorrect flag for llvm.bpf.preserve.type.info intrinsic"); + if (Flag == BPFCoreSharedInfo::PRESERVE_TYPE_INFO_EXISTENCE) + CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_EXISTENCE; + else + CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_SIZE; + return true; + } + if (GV->getName().startswith("llvm.bpf.preserve.enum.value")) { + CInfo.Kind = BPFPreserveFieldInfoAI; + CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); + if (!CInfo.Metadata) + report_fatal_error("Missing metadata for llvm.preserve.enum.value intrinsic"); + uint64_t Flag = getConstant(Call->getArgOperand(2)); + if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_ENUM_VALUE_FLAG) + report_fatal_error("Incorrect flag for llvm.bpf.preserve.enum.value intrinsic"); + if (Flag == BPFCoreSharedInfo::PRESERVE_ENUM_VALUE_EXISTENCE) + CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE; + else + CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE; + return true; + } return false; } @@ -847,16 +877,57 @@ return Base; } +MDNode *BPFAbstractMemberAccess::computeAccessKey(CallInst *Call, + CallInfo &CInfo, + std::string &AccessKey) { + DIType *Ty = stripQualifiers(cast(CInfo.Metadata), false); + assert(!Ty->getName().empty()); + + uint32_t PatchImm, AccessStrVal = 0; + if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_EXISTENCE) { + PatchImm = 1; + } else if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_SIZE) { + // typedef debuginfo type has size 0, get the eventual base type. + DIType *BaseTy = stripQualifiers(Ty, true); + PatchImm = BaseTy->getSizeInBits() / 8; + } else { + // ENUM_VALUE_EXISTENCE and ENUM_VALUE + const auto *ValExpr = dyn_cast(Call->getArgOperand(0)); + assert(ValExpr); + assert(ValExpr->getOpcode() == Instruction::IntToPtr); + Value *IntVal = ValExpr->getOperand(0); + assert(IntVal); + if (CInfo.AccessIndex == BPFCoreSharedInfo::ENUM_VALUE) { + PatchImm = getConstant(IntVal); + } else { + PatchImm = 1; + AccessStrVal = getConstant(IntVal); + } + } + + AccessKey = "llvm." + Ty->getName().str() + ":" + + std::to_string(CInfo.AccessIndex) + std::string(":") + + std::to_string(PatchImm) + std::string("$") + + std::to_string(AccessStrVal); + + return Ty; +} + /// Call/Kind is the base preserve_*_access_index() call. Attempts to do /// transformation to a chain of relocable GEPs. bool BPFAbstractMemberAccess::transformGEPChain(Module &M, CallInst *Call, CallInfo &CInfo) { std::string AccessKey; MDNode *TypeMeta; - Value *Base = - computeBaseAndAccessKey(Call, CInfo, AccessKey, TypeMeta); - if (!Base) - return false; + Value *Base = nullptr; + + if (CInfo.Kind == BPFPreserveFieldInfoAI && CInfo.Metadata) { + TypeMeta = computeAccessKey(Call, CInfo, AccessKey); + } else { + Base = computeBaseAndAccessKey(Call, CInfo, AccessKey, TypeMeta); + if (!Base) + return false; + } BasicBlock *BB = Call->getParent(); GlobalVariable *GV; diff --git a/llvm/lib/Target/BPF/BPFCORE.h b/llvm/lib/Target/BPF/BPFCORE.h --- a/llvm/lib/Target/BPF/BPFCORE.h +++ b/llvm/lib/Target/BPF/BPFCORE.h @@ -24,6 +24,10 @@ FIELD_RSHIFT_U64, BTF_TYPE_ID_LOCAL, BTF_TYPE_ID_REMOTE, + TYPE_EXISTENCE, + TYPE_SIZE, + ENUM_VALUE_EXISTENCE, + ENUM_VALUE, MAX_FIELD_RELOC_KIND, }; @@ -35,6 +39,20 @@ MAX_BTF_TYPE_ID_FLAG, }; + enum PreserveTypeInfo : uint32_t { + PRESERVE_TYPE_INFO_EXISTENCE = 0, + PRESERVE_TYPE_INFO_SIZE, + + MAX_PRESERVE_TYPE_INFO_FLAG, + }; + + enum PreserveEnumValue : uint32_t { + PRESERVE_ENUM_VALUE_EXISTENCE = 0, + PRESERVE_ENUM_VALUE, + + MAX_PRESERVE_ENUM_VALUE_FLAG, + }; + /// The attribute attached to globals representing a field access static constexpr StringRef AmaAttr = "btf_ama"; /// The attribute attached to globals representing a type id diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-enum-value.ll @@ -0,0 +1,84 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum AA { VAL = 100 }; +; typedef enum { VAL2 = 200 } __BB; +; int test() { +; return __builtin_preserve_enum_value(*(enum AA *)100, 0) + +; __builtin_preserve_enum_value(*(__BB *)VAL2, 1); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t1.c + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !18 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.enum.value.p0i32.i32(i32* nonnull inttoptr (i64 100 to i32*), i32 1, i64 0), !dbg !23, !llvm.preserve.access.index !3 + %1 = tail call i32 @llvm.bpf.preserve.enum.value.p0i32.i32(i32* nonnull inttoptr (i64 200 to i32*), i32 1, i64 1), !dbg !24, !llvm.preserve.access.index !13 + %add = add i32 %1, %0, !dbg !25 + ret i32 %add, !dbg !26 +} + +; CHECK: r{{[0-9]+}} = 1 +; CHECK: r{{[0-9]+}} = 200 +; CHECK: exit + +; CHECK: .long 16 # BTF_KIND_ENUM(id = 4) +; CHECK: .long 51 # BTF_KIND_TYPEDEF(id = 5) + +; CHECK: .ascii ".text" # string offset=10 +; CHECK: .ascii "AA" # string offset=16 +; CHECK: .ascii "100" # string offset=23 +; CHECK: .ascii "__BB" # string offset=51 +; CHECK: .byte 48 # string offset=61 + +; CHECK: .long 16 # FieldReloc +; CHECK: .long 10 # Field reloc section string offset=10 +; CHECK: .long 2 +; CHECK: .long .Ltmp{{[0-9]+}} +; CHECK: .long 4 +; CHECK: .long 23 +; CHECK: .long 10 +; CHECK: .long .Ltmp{{[0-9]+}} +; CHECK: .long 5 +; CHECK: .long 61 +; CHECK: .long 11 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.enum.value.p0i32.i32(i32*, i32, i64) #1 + +attributes #0 = { nounwind readnone "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"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!14, !15, !16} +!llvm.ident = !{!17} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !10, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t1.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3, !7} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA", file: !1, line: 1, baseType: !4, size: 32, elements: !5) +!4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!5 = !{!6} +!6 = !DIEnumerator(name: "VAL", value: 100, isUnsigned: true) +!7 = !DICompositeType(tag: DW_TAG_enumeration_type, file: !1, line: 2, baseType: !4, size: 32, elements: !8) +!8 = !{!9} +!9 = !DIEnumerator(name: "VAL2", value: 200, isUnsigned: true) +!10 = !{!11, !12, !13} +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64) +!13 = !DIDerivedType(tag: DW_TAG_typedef, name: "__BB", file: !1, line: 2, baseType: !7) +!14 = !{i32 7, !"Dwarf Version", i32 4} +!15 = !{i32 2, !"Debug Info Version", i32 3} +!16 = !{i32 1, !"wchar_size", i32 4} +!17 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)"} +!18 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 3, type: !19, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !22) +!19 = !DISubroutineType(types: !20) +!20 = !{!21} +!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!22 = !{} +!23 = !DILocation(line: 4, column: 10, scope: !18) +!24 = !DILocation(line: 5, column: 10, scope: !18) +!25 = !DILocation(line: 4, column: 60, scope: !18) +!26 = !DILocation(line: 4, column: 3, scope: !18) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-exist.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-exist.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-exist.ll @@ -0,0 +1,109 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum AA { VAL = 100 }; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; return __builtin_preserve_type_info(*(func_t *)0, 0) + +; __builtin_preserve_type_info(*(struct s2 *)0, 0) + +; __builtin_preserve_type_info(*(enum AA *)0, 0); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t1.c + +%struct.s2 = type { [10 x i32] } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !26 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()** null, i32 1, i64 0), !dbg !28, !llvm.preserve.access.index !9 + %1 = tail call i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2* null, i32 1, i64 0), !dbg !29, !llvm.preserve.access.index !15 + %add = add i32 %1, %0, !dbg !30 + %2 = tail call i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32* null, i32 1, i64 0), !dbg !31, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %2, !dbg !32 + ret i32 %add1, !dbg !33 +} + +; CHECK: r{{[0-9]+}} = 1 +; CHECK: r{{[0-9]+}} = 1 +; CHECK: r{{[0-9]+}} = 1 +; CHECK: exit + +; CHECK: .long 16 # BTF_KIND_TYPEDEF(id = 4) +; CHECK: .long 49 # BTF_KIND_STRUCT(id = 7) +; CHECK: .long 74 # BTF_KIND_ENUM(id = 10) + +; CHECK: .ascii ".text" # string offset=10 +; CHECK: .ascii "func_t" # string offset=16 +; CHECK: .byte 48 # string offset=23 +; CHECK: .ascii "s2" # string offset=49 +; CHECK: .ascii "AA" # string offset=74 + +; 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 23 +; CHECK-NEXT: .long 8 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 8 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 8 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()**, i32, i64) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2*, i32, i64) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32*, i32, i64) #1 + +attributes #0 = { nounwind readnone "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"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!22, !23, !24} +!llvm.ident = !{!25} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !7, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t1.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA", file: !1, line: 1, baseType: !4, size: 32, elements: !5) +!4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!5 = !{!6} +!6 = !DIEnumerator(name: "VAL", value: 100, isUnsigned: true) +!7 = !{!8, !14, !21, !9} +!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64) +!9 = !DIDerivedType(tag: DW_TAG_typedef, name: "func_t", file: !1, line: 2, baseType: !10) +!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64) +!11 = !DISubroutineType(types: !12) +!12 = !{!13} +!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!14 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !15, size: 64) +!15 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !16) +!16 = !{!17} +!17 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !15, file: !1, line: 3, baseType: !18, size: 320) +!18 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, size: 320, elements: !19) +!19 = !{!20} +!20 = !DISubrange(count: 10) +!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!22 = !{i32 7, !"Dwarf Version", i32 4} +!23 = !{i32 2, !"Debug Info Version", i32 3} +!24 = !{i32 1, !"wchar_size", i32 4} +!25 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)"} +!26 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !27) +!27 = !{} +!28 = !DILocation(line: 5, column: 10, scope: !26) +!29 = !DILocation(line: 6, column: 10, scope: !26) +!30 = !DILocation(line: 5, column: 56, scope: !26) +!31 = !DILocation(line: 7, column: 10, scope: !26) +!32 = !DILocation(line: 6, column: 59, scope: !26) +!33 = !DILocation(line: 5, column: 3, scope: !26) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-1.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-1.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-1.ll @@ -0,0 +1,109 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum AA { VAL = 100 }; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; return __builtin_preserve_type_info(*(func_t *)0, 1) + +; __builtin_preserve_type_info(*(struct s2 *)0, 1) + +; __builtin_preserve_type_info(*(enum AA *)0, 1); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t1.c + +%struct.s2 = type { [10 x i32] } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !26 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()** null, i32 1, i64 1), !dbg !28, !llvm.preserve.access.index !9 + %1 = tail call i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2* null, i32 1, i64 1), !dbg !29, !llvm.preserve.access.index !15 + %add = add i32 %1, %0, !dbg !30 + %2 = tail call i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32* null, i32 1, i64 1), !dbg !31, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %2, !dbg !32 + ret i32 %add1, !dbg !33 +} + +; CHECK: r{{[0-9]+}} = 8 +; CHECK: r{{[0-9]+}} = 40 +; CHECK: r{{[0-9]+}} = 4 +; CHECK: exit + +; CHECK: .long 16 # BTF_KIND_TYPEDEF(id = 4) +; CHECK: .long 49 # BTF_KIND_STRUCT(id = 7) +; CHECK: .long 74 # BTF_KIND_ENUM(id = 10) + +; CHECK: .ascii ".text" # string offset=10 +; CHECK: .ascii "func_t" # string offset=16 +; CHECK: .byte 48 # string offset=23 +; CHECK: .ascii "s2" # string offset=49 +; CHECK: .ascii "AA" # string offset=74 + +; 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 23 +; CHECK-NEXT: .long 9 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 9 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 9 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()**, i32, i64) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2*, i32, i64) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32*, i32, i64) #1 + +attributes #0 = { nounwind readnone "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"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!22, !23, !24} +!llvm.ident = !{!25} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !7, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t1.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA", file: !1, line: 1, baseType: !4, size: 32, elements: !5) +!4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!5 = !{!6} +!6 = !DIEnumerator(name: "VAL", value: 100, isUnsigned: true) +!7 = !{!8, !14, !21, !9} +!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64) +!9 = !DIDerivedType(tag: DW_TAG_typedef, name: "func_t", file: !1, line: 2, baseType: !10) +!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64) +!11 = !DISubroutineType(types: !12) +!12 = !{!13} +!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!14 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !15, size: 64) +!15 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !16) +!16 = !{!17} +!17 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !15, file: !1, line: 3, baseType: !18, size: 320) +!18 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, size: 320, elements: !19) +!19 = !{!20} +!20 = !DISubrange(count: 10) +!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!22 = !{i32 7, !"Dwarf Version", i32 4} +!23 = !{i32 2, !"Debug Info Version", i32 3} +!24 = !{i32 1, !"wchar_size", i32 4} +!25 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)"} +!26 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !27) +!27 = !{} +!28 = !DILocation(line: 5, column: 10, scope: !26) +!29 = !DILocation(line: 6, column: 10, scope: !26) +!30 = !DILocation(line: 5, column: 56, scope: !26) +!31 = !DILocation(line: 7, column: 10, scope: !26) +!32 = !DILocation(line: 6, column: 59, scope: !26) +!33 = !DILocation(line: 5, column: 3, scope: !26) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-2.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-typeinfo-type-size-2.ll @@ -0,0 +1,148 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum AA { VAL = 100 }; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; func_t f; +; struct s2 s; +; enum AA a; +; return __builtin_preserve_type_info(f, 1) + +; __builtin_preserve_type_info(s, 1) + +; __builtin_preserve_type_info(a, 1); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t1.c + +%struct.s2 = type { [10 x i32] } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !17 { +entry: + %f = alloca i32 ()*, align 8 + %s = alloca %struct.s2, align 4 + %a = alloca i32, align 4 + %0 = bitcast i32 ()** %f to i8*, !dbg !28 + call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %0) #4, !dbg !28 + %1 = bitcast %struct.s2* %s to i8*, !dbg !29 + call void @llvm.lifetime.start.p0i8(i64 40, i8* nonnull %1) #4, !dbg !29 + call void @llvm.dbg.declare(metadata %struct.s2* %s, metadata !20, metadata !DIExpression()), !dbg !30 + %2 = bitcast i32* %a to i8*, !dbg !31 + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2) #4, !dbg !31 + call void @llvm.dbg.value(metadata i32 ()** %f, metadata !19, metadata !DIExpression(DW_OP_deref)), !dbg !32 + %3 = call i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()** nonnull %f, i32 1, i64 1), !dbg !33, !llvm.preserve.access.index !8 + %4 = call i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2* nonnull %s, i32 1, i64 1), !dbg !34, !llvm.preserve.access.index !21 + %add = add i32 %4, %3, !dbg !35 + call void @llvm.dbg.value(metadata i32* %a, metadata !27, metadata !DIExpression(DW_OP_deref)), !dbg !32 + %5 = call i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32* nonnull %a, i32 1, i64 1), !dbg !36, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %5, !dbg !37 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2) #4, !dbg !38 + call void @llvm.lifetime.end.p0i8(i64 40, i8* nonnull %1) #4, !dbg !38 + call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %0) #4, !dbg !38 + ret i32 %add1, !dbg !39 +} + +; CHECK: r{{[0-9]+}} = 8 +; CHECK: r{{[0-9]+}} = 40 +; CHECK: r{{[0-9]+}} = 4 +; CHECK: exit + +; CHECK: .long 16 # BTF_KIND_TYPEDEF(id = 4) +; CHECK: .long 49 # BTF_KIND_STRUCT(id = 7) +; CHECK: .long 74 # BTF_KIND_ENUM(id = 10) + +; CHECK: .ascii ".text" # string offset=10 +; CHECK: .ascii "func_t" # string offset=16 +; CHECK: .byte 48 # string offset=23 +; CHECK: .ascii "s2" # string offset=49 +; CHECK: .ascii "VAL" # string offset=77 + +; 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 23 +; CHECK-NEXT: .long 9 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 9 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long 23 +; CHECK-NEXT: .long 9 + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0p0f_i32f.i32(i32 ()**, i32, i64) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0s_struct.s2s.i32(%struct.s2*, i32, i64) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.type.info.p0i32.i32(i32*, i32, i64) #3 + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + +attributes #0 = { nounwind readnone "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"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { argmemonly nounwind willreturn } +attributes #2 = { nounwind readnone speculatable willreturn } +attributes #3 = { nounwind readnone } +attributes #4 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!13, !14, !15} +!llvm.ident = !{!16} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !7, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t1.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "AA", file: !1, line: 1, baseType: !4, size: 32, elements: !5) +!4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!5 = !{!6} +!6 = !DIEnumerator(name: "VAL", value: 100, isUnsigned: true) +!7 = !{!8} +!8 = !DIDerivedType(tag: DW_TAG_typedef, name: "func_t", file: !1, line: 2, baseType: !9) +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!10 = !DISubroutineType(types: !11) +!11 = !{!12} +!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!13 = !{i32 7, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{i32 1, !"wchar_size", i32 4} +!16 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce93f0e840fd56c2af045a5184ecdbd61d91e676)"} +!17 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !10, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !18) +!18 = !{!19, !20, !27} +!19 = !DILocalVariable(name: "f", scope: !17, file: !1, line: 5, type: !8) +!20 = !DILocalVariable(name: "s", scope: !17, file: !1, line: 6, type: !21) +!21 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !22) +!22 = !{!23} +!23 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !21, file: !1, line: 3, baseType: !24, size: 320) +!24 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, size: 320, elements: !25) +!25 = !{!26} +!26 = !DISubrange(count: 10) +!27 = !DILocalVariable(name: "a", scope: !17, file: !1, line: 7, type: !3) +!28 = !DILocation(line: 5, column: 3, scope: !17) +!29 = !DILocation(line: 6, column: 3, scope: !17) +!30 = !DILocation(line: 6, column: 13, scope: !17) +!31 = !DILocation(line: 7, column: 3, scope: !17) +!32 = !DILocation(line: 0, scope: !17) +!33 = !DILocation(line: 8, column: 10, scope: !17) +!34 = !DILocation(line: 9, column: 10, scope: !17) +!35 = !DILocation(line: 8, column: 45, scope: !17) +!36 = !DILocation(line: 10, column: 10, scope: !17) +!37 = !DILocation(line: 9, column: 45, scope: !17) +!38 = !DILocation(line: 11, column: 1, scope: !17) +!39 = !DILocation(line: 8, column: 3, scope: !17)