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); }; @@ -277,7 +279,7 @@ } if (GV->getName().startswith("llvm.bpf.preserve.field.info")) { CInfo.Kind = BPFPreserveFieldInfoAI; - CInfo.Metadata = nullptr; + CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); // Check validity of info_kind as clang did not check this. uint64_t InfoKind = getConstant(Call->getArgOperand(1)); if (InfoKind >= BPFCoreSharedInfo::MAX_FIELD_RELOC_KIND) @@ -847,16 +849,54 @@ 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; + if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_EXISTENCE) { + PatchImm = 1; + } else if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_SIZEOF) { + // typedef debuginfo type has size 0, get the eventual base type. + DIType *BaseTy = stripQualifiers(Ty, true); + PatchImm = BaseTy->getSizeInBits() / 8; + } else { + // ENUM_VALUE + const auto *ValExpr = dyn_cast(Call->getArgOperand(0)); + assert(ValExpr); + assert(ValExpr->getOpcode() == Instruction::IntToPtr); + Value *IntVal = ValExpr->getOperand(0); + assert(IntVal); + PatchImm = getConstant(IntVal); + } + + // AccessString: "0" + AccessKey = "llvm." + Ty->getName().str() + ":" + + std::to_string(CInfo.AccessIndex) + std::string(":") + + std::to_string(PatchImm) + std::string("$0"); + + 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; + + // For BPFPreserveFieldInfoAI intrinsic, only TYPE_EXISTENCE, + // TYPE_SIZEOF or ENUM_VALUE may carry a debuginfo metadata. + 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,9 @@ FIELD_RSHIFT_U64, BTF_TYPE_ID_LOCAL, BTF_TYPE_ID_REMOTE, + TYPE_EXISTENCE, + TYPE_SIZEOF, + ENUM_VALUE, MAX_FIELD_RELOC_KIND, }; diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-enum-value.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-enum-value.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-enum-value.ll @@ -0,0 +1,106 @@ +; 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 { TYPE_EXISTENCE = 8, TYPE_SIZEOF = 9, ENUM_VALUE = 10 }; +; typedef enum { VAL1 = 4, VAL2 = 5 } __BB; +; int test() { +; return __builtin_preserve_field_info(*(enum AA *)8, ENUM_VALUE) + +; __builtin_preserve_field_info(*(enum AA *)TYPE_SIZEOF, ENUM_VALUE) + +; __builtin_preserve_field_info(*(__BB *)4, ENUM_VALUE) + +; __builtin_preserve_field_info(*(__BB *)VAL2, ENUM_VALUE); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t2.c + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !21 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull inttoptr (i64 8 to i32*), i64 10), !dbg !26, !llvm.preserve.access.index !3 + %1 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull inttoptr (i64 9 to i32*), i64 10), !dbg !27, !llvm.preserve.access.index !3 + %add = add i32 %1, %0, !dbg !28 + %2 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull inttoptr (i64 4 to i32*), i64 10), !dbg !29, !llvm.preserve.access.index !16 + %add1 = add i32 %add, %2, !dbg !30 + %3 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull inttoptr (i64 5 to i32*), i64 10), !dbg !31, !llvm.preserve.access.index !16 + %add2 = add i32 %add1, %3, !dbg !32 + ret i32 %add2, !dbg !33 +} + +; CHECK: r{{[0-9]+}} = 8 +; CHECK: r{{[0-9]+}} = 9 +; CHECK: r{{[0-9]+}} = 4 +; CHECK: r{{[0-9]+}} = 5 +; CHECK: exit + +; CHECK: .long 16 # BTF_KIND_ENUM(id = 4) +; CHECK: .long 83 # BTF_KIND_TYPEDEF(id = 5) + +; CHECK: .ascii ".text" # string offset=10 +; CHECK: .ascii "AA" # string offset=16 +; CHECK: .byte 48 # string offset=57 +; CHECK: .ascii "__BB" # string offset=83 + +; CHECK: .long 16 # FieldReloc +; CHECK-NEXT: .long 10 # Field reloc section string offset=10 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 57 +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 57 +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 5 +; CHECK-NEXT: .long 57 +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long .Ltmp{{[0-9]+}} +; CHECK-NEXT: .long 5 +; CHECK-NEXT: .long 57 +; CHECK-NEXT: .long 10 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0i32(i32*, i64 immarg) #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 = !{!17, !18, !19} +!llvm.ident = !{!20} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !13, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t2.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3, !9} +!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, !7, !8} +!6 = !DIEnumerator(name: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !DIEnumerator(name: "TYPE_SIZEOF", value: 9, isUnsigned: true) +!8 = !DIEnumerator(name: "ENUM_VALUE", value: 10, isUnsigned: true) +!9 = !DICompositeType(tag: DW_TAG_enumeration_type, file: !1, line: 2, baseType: !4, size: 32, elements: !10) +!10 = !{!11, !12} +!11 = !DIEnumerator(name: "VAL1", value: 4, isUnsigned: true) +!12 = !DIEnumerator(name: "VAL2", value: 5, isUnsigned: true) +!13 = !{!14, !15, !16} +!14 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64) +!16 = !DIDerivedType(tag: DW_TAG_typedef, name: "__BB", file: !1, line: 2, baseType: !9) +!17 = !{i32 7, !"Dwarf Version", i32 4} +!18 = !{i32 2, !"Debug Info Version", i32 3} +!19 = !{i32 1, !"wchar_size", i32 4} +!20 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)"} +!21 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 3, type: !22, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !25) +!22 = !DISubroutineType(types: !23) +!23 = !{!24} +!24 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!25 = !{} +!26 = !DILocation(line: 4, column: 10, scope: !21) +!27 = !DILocation(line: 5, column: 10, scope: !21) +!28 = !DILocation(line: 4, column: 67, scope: !21) +!29 = !DILocation(line: 6, column: 10, scope: !21) +!30 = !DILocation(line: 5, column: 77, scope: !21) +!31 = !DILocation(line: 7, column: 10, scope: !21) +!32 = !DILocation(line: 6, column: 64, scope: !21) +!33 = !DILocation(line: 4, column: 3, scope: !21) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-1.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-1.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-1.ll @@ -0,0 +1,110 @@ +; 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 { TYPE_EXISTENCE = 8, TYPE_SIZEOF = 9}; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; return __builtin_preserve_field_info(*(func_t *)0, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(*(struct s2 *)0, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(*(enum AA *)0, TYPE_EXISTENCE); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t.c + +%struct.s2 = type { [10 x i32] } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !27 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.field.info.p0p0f_i32f(i32 ()** null, i64 8), !dbg !29, !llvm.preserve.access.index !10 + %1 = tail call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* null, i64 8), !dbg !30, !llvm.preserve.access.index !16 + %add = add i32 %1, %0, !dbg !31 + %2 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* null, i64 8), !dbg !32, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %2, !dbg !33 + ret i32 %add1, !dbg !34 +} + +; 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 48 # BTF_KIND_STRUCT(id = 7) +; CHECK: .long 73 # 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=48 +; CHECK: .ascii "AA" # string offset=73 + +; 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.field.info.p0p0f_i32f(i32 ()**, i64 immarg) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2*, i64 immarg) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0i32(i32*, i64 immarg) #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 = !{!23, !24, !25} +!llvm.ident = !{!26} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !8, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t.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, !7} +!6 = !DIEnumerator(name: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !DIEnumerator(name: "TYPE_SIZEOF", value: 9, isUnsigned: true) +!8 = !{!9, !15, !22, !10} +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "func_t", file: !1, line: 2, baseType: !11) +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64) +!12 = !DISubroutineType(types: !13) +!13 = !{!14} +!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64) +!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !17) +!17 = !{!18} +!18 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !16, file: !1, line: 3, baseType: !19, size: 320) +!19 = !DICompositeType(tag: DW_TAG_array_type, baseType: !14, size: 320, elements: !20) +!20 = !{!21} +!21 = !DISubrange(count: 10) +!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!23 = !{i32 7, !"Dwarf Version", i32 4} +!24 = !{i32 2, !"Debug Info Version", i32 3} +!25 = !{i32 1, !"wchar_size", i32 4} +!26 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)"} +!27 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !12, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !28) +!28 = !{} +!29 = !DILocation(line: 5, column: 10, scope: !27) +!30 = !DILocation(line: 6, column: 10, scope: !27) +!31 = !DILocation(line: 5, column: 70, scope: !27) +!32 = !DILocation(line: 7, column: 10, scope: !27) +!33 = !DILocation(line: 6, column: 73, scope: !27) +!34 = !DILocation(line: 5, column: 3, scope: !27) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-2.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-exist-2.ll @@ -0,0 +1,149 @@ +; 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 { TYPE_EXISTENCE = 8, TYPE_SIZEOF = 9}; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; func_t f; +; struct s2 s; +; enum AA a; +; return __builtin_preserve_field_info(f, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(s, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(a, TYPE_EXISTENCE); +; } +; 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 !18 { +entry: + %f = alloca i32 ()*, align 8 + %s = alloca %struct.s2, align 4 + %a = alloca i32, align 4 + %0 = bitcast i32 ()** %f to i8*, !dbg !29 + call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %0) #4, !dbg !29 + %1 = bitcast %struct.s2* %s to i8*, !dbg !30 + call void @llvm.lifetime.start.p0i8(i64 40, i8* nonnull %1) #4, !dbg !30 + call void @llvm.dbg.declare(metadata %struct.s2* %s, metadata !21, metadata !DIExpression()), !dbg !31 + %2 = bitcast i32* %a to i8*, !dbg !32 + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2) #4, !dbg !32 + call void @llvm.dbg.value(metadata i32 ()** %f, metadata !20, metadata !DIExpression(DW_OP_deref)), !dbg !33 + %3 = call i32 @llvm.bpf.preserve.field.info.p0p0f_i32f(i32 ()** nonnull %f, i64 8), !dbg !34, !llvm.preserve.access.index !9 + %4 = call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* nonnull %s, i64 8), !dbg !35, !llvm.preserve.access.index !22 + %add = add i32 %4, %3, !dbg !36 + call void @llvm.dbg.value(metadata i32* %a, metadata !28, metadata !DIExpression(DW_OP_deref)), !dbg !33 + %5 = call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull %a, i64 8), !dbg !37, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %5, !dbg !38 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2) #4, !dbg !39 + call void @llvm.lifetime.end.p0i8(i64 40, i8* nonnull %1) #4, !dbg !39 + call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %0) #4, !dbg !39 + ret i32 %add1, !dbg !40 +} + +; 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: 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.field.info.p0p0f_i32f(i32 ()**, i64 immarg) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2*, i64 immarg) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0i32(i32*, i64 immarg) #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 = !{!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 ce6811317f5e16c0ed449e11c41cb97dc81f9679)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !8, 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, !7} +!6 = !DIEnumerator(name: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !DIEnumerator(name: "TYPE_SIZEOF", value: 9, isUnsigned: true) +!8 = !{!9} +!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 = !{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 ce6811317f5e16c0ed449e11c41cb97dc81f9679)"} +!18 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !19) +!19 = !{!20, !21, !28} +!20 = !DILocalVariable(name: "f", scope: !18, file: !1, line: 5, type: !9) +!21 = !DILocalVariable(name: "s", scope: !18, file: !1, line: 6, type: !22) +!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !23) +!23 = !{!24} +!24 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !22, file: !1, line: 3, baseType: !25, size: 320) +!25 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, size: 320, elements: !26) +!26 = !{!27} +!27 = !DISubrange(count: 10) +!28 = !DILocalVariable(name: "a", scope: !18, file: !1, line: 7, type: !3) +!29 = !DILocation(line: 5, column: 3, scope: !18) +!30 = !DILocation(line: 6, column: 3, scope: !18) +!31 = !DILocation(line: 6, column: 13, scope: !18) +!32 = !DILocation(line: 7, column: 3, scope: !18) +!33 = !DILocation(line: 0, scope: !18) +!34 = !DILocation(line: 8, column: 10, scope: !18) +!35 = !DILocation(line: 9, column: 10, scope: !18) +!36 = !DILocation(line: 8, column: 59, scope: !18) +!37 = !DILocation(line: 10, column: 10, scope: !18) +!38 = !DILocation(line: 9, column: 59, scope: !18) +!39 = !DILocation(line: 11, column: 1, scope: !18) +!40 = !DILocation(line: 8, column: 3, scope: !18) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-1.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-1.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-1.ll @@ -0,0 +1,110 @@ +; 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 { TYPE_EXISTENCE = 8, TYPE_SIZEOF = 9}; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; return __builtin_preserve_field_info(*(func_t *)0, TYPE_SIZEOF) + +; __builtin_preserve_field_info(*(struct s2 *)0, TYPE_SIZEOF) + +; __builtin_preserve_field_info(*(enum AA *)0, TYPE_SIZEOF); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t.c + +%struct.s2 = type { [10 x i32] } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !27 { +entry: + %0 = tail call i32 @llvm.bpf.preserve.field.info.p0p0f_i32f(i32 ()** null, i64 9), !dbg !29, !llvm.preserve.access.index !10 + %1 = tail call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* null, i64 9), !dbg !30, !llvm.preserve.access.index !16 + %add = add i32 %1, %0, !dbg !31 + %2 = tail call i32 @llvm.bpf.preserve.field.info.p0i32(i32* null, i64 9), !dbg !32, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %2, !dbg !33 + ret i32 %add1, !dbg !34 +} + +; 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 48 # BTF_KIND_STRUCT(id = 7) +; CHECK: .long 73 # 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=48 +; CHECK: .ascii "AA" # string offset=73 + +; 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.field.info.p0p0f_i32f(i32 ()**, i64 immarg) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2*, i64 immarg) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0i32(i32*, i64 immarg) #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 = !{!23, !24, !25} +!llvm.ident = !{!26} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !8, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t.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, !7} +!6 = !DIEnumerator(name: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !DIEnumerator(name: "TYPE_SIZEOF", value: 9, isUnsigned: true) +!8 = !{!9, !15, !22, !10} +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "func_t", file: !1, line: 2, baseType: !11) +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64) +!12 = !DISubroutineType(types: !13) +!13 = !{!14} +!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64) +!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !17) +!17 = !{!18} +!18 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !16, file: !1, line: 3, baseType: !19, size: 320) +!19 = !DICompositeType(tag: DW_TAG_array_type, baseType: !14, size: 320, elements: !20) +!20 = !{!21} +!21 = !DISubrange(count: 10) +!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 64) +!23 = !{i32 7, !"Dwarf Version", i32 4} +!24 = !{i32 2, !"Debug Info Version", i32 3} +!25 = !{i32 1, !"wchar_size", i32 4} +!26 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git ce6811317f5e16c0ed449e11c41cb97dc81f9679)"} +!27 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !12, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !28) +!28 = !{} +!29 = !DILocation(line: 5, column: 10, scope: !27) +!30 = !DILocation(line: 6, column: 10, scope: !27) +!31 = !DILocation(line: 5, column: 67, scope: !27) +!32 = !DILocation(line: 7, column: 10, scope: !27) +!33 = !DILocation(line: 6, column: 70, scope: !27) +!34 = !DILocation(line: 5, column: 3, scope: !27) diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-2.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-fieldinfo-type-sizeof-2.ll @@ -0,0 +1,149 @@ +; 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 { TYPE_EXISTENCE = 8, TYPE_SIZEOF = 9}; +; typedef int (*func_t)(void); +; struct s2 { int a[10]; }; +; int test() { +; func_t f; +; struct s2 s; +; enum AA a; +; return __builtin_preserve_field_info(f, TYPE_SIZEOF) + +; __builtin_preserve_field_info(s, TYPE_SIZEOF) + +; __builtin_preserve_field_info(a, TYPE_SIZEOF); +; } +; 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 !18 { +entry: + %f = alloca i32 ()*, align 8 + %s = alloca %struct.s2, align 4 + %a = alloca i32, align 4 + %0 = bitcast i32 ()** %f to i8*, !dbg !29 + call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %0) #4, !dbg !29 + %1 = bitcast %struct.s2* %s to i8*, !dbg !30 + call void @llvm.lifetime.start.p0i8(i64 40, i8* nonnull %1) #4, !dbg !30 + call void @llvm.dbg.declare(metadata %struct.s2* %s, metadata !21, metadata !DIExpression()), !dbg !31 + %2 = bitcast i32* %a to i8*, !dbg !32 + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2) #4, !dbg !32 + call void @llvm.dbg.value(metadata i32 ()** %f, metadata !20, metadata !DIExpression(DW_OP_deref)), !dbg !33 + %3 = call i32 @llvm.bpf.preserve.field.info.p0p0f_i32f(i32 ()** nonnull %f, i64 9), !dbg !34, !llvm.preserve.access.index !9 + %4 = call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* nonnull %s, i64 9), !dbg !35, !llvm.preserve.access.index !22 + %add = add i32 %4, %3, !dbg !36 + call void @llvm.dbg.value(metadata i32* %a, metadata !28, metadata !DIExpression(DW_OP_deref)), !dbg !33 + %5 = call i32 @llvm.bpf.preserve.field.info.p0i32(i32* nonnull %a, i64 9), !dbg !37, !llvm.preserve.access.index !3 + %add1 = add i32 %add, %5, !dbg !38 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2) #4, !dbg !39 + call void @llvm.lifetime.end.p0i8(i64 40, i8* nonnull %1) #4, !dbg !39 + call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %0) #4, !dbg !39 + ret i32 %add1, !dbg !40 +} + +; 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: 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.field.info.p0p0f_i32f(i32 ()**, i64 immarg) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2*, i64 immarg) #3 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0i32(i32*, i64 immarg) #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 = !{!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 ce6811317f5e16c0ed449e11c41cb97dc81f9679)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !8, 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, !7} +!6 = !DIEnumerator(name: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !DIEnumerator(name: "TYPE_SIZEOF", value: 9, isUnsigned: true) +!8 = !{!9} +!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 = !{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 ce6811317f5e16c0ed449e11c41cb97dc81f9679)"} +!18 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !19) +!19 = !{!20, !21, !28} +!20 = !DILocalVariable(name: "f", scope: !18, file: !1, line: 5, type: !9) +!21 = !DILocalVariable(name: "s", scope: !18, file: !1, line: 6, type: !22) +!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 320, elements: !23) +!23 = !{!24} +!24 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !22, file: !1, line: 3, baseType: !25, size: 320) +!25 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, size: 320, elements: !26) +!26 = !{!27} +!27 = !DISubrange(count: 10) +!28 = !DILocalVariable(name: "a", scope: !18, file: !1, line: 7, type: !3) +!29 = !DILocation(line: 5, column: 3, scope: !18) +!30 = !DILocation(line: 6, column: 3, scope: !18) +!31 = !DILocation(line: 6, column: 13, scope: !18) +!32 = !DILocation(line: 7, column: 3, scope: !18) +!33 = !DILocation(line: 0, scope: !18) +!34 = !DILocation(line: 8, column: 10, scope: !18) +!35 = !DILocation(line: 9, column: 10, scope: !18) +!36 = !DILocation(line: 8, column: 56, scope: !18) +!37 = !DILocation(line: 10, column: 10, scope: !18) +!38 = !DILocation(line: 9, column: 56, scope: !18) +!39 = !DILocation(line: 11, column: 1, scope: !18) +!40 = !DILocation(line: 8, column: 3, scope: !18)