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 @@ -277,7 +277,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) @@ -853,10 +853,22 @@ 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 + // may carry a debuginfo metadata. + if (CInfo.Kind == BPFPreserveFieldInfoAI && CInfo.Metadata) { + DIType *Ty = stripQualifiers(cast(CInfo.Metadata), false); + assert(!Ty->getName().empty()); + // PatchImm : 1, AccessString: "0". + AccessKey = "llvm." + Ty->getName().str() + ":" + + std::to_string(CInfo.AccessIndex) + std::string(":1$0"); + TypeMeta = Ty; + } 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,7 @@ FIELD_RSHIFT_U64, BTF_TYPE_ID_LOCAL, BTF_TYPE_ID_REMOTE, + TYPE_EXISTENCE, MAX_FIELD_RELOC_KIND, }; 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,85 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum { TYPE_EXISTENCE = 8, }; +; typedef int (*func_t)(void); +; struct s2 { int a; }; +; int test() { +; return __builtin_preserve_field_info((func_t)0, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(*(struct s2 *)0, TYPE_EXISTENCE); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t.c + +%struct.s2 = type { i32 } + +; 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.p0f_i32f(i32 ()* null, i64 8), !dbg !23, !llvm.preserve.access.index !8 + %1 = tail call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* null, i64 8), !dbg !24, !llvm.preserve.access.index !14 + %add = add i32 %1, %0, !dbg !25 + ret i32 %add, !dbg !26 +} + +; CHECK: .long 16 # BTF_KIND_TYPEDEF(id = 4) +; CHECK: .long 48 # BTF_KIND_STRUCT(id = 7) + +; 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: .long 16 # FieldReloc +; CHECK-NEXT: .long 10 # Field reloc section string offset=10 +; CHECK-NEXT: .long 2 +; 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 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0f_i32f(i32 ()*, i64 immarg) #1 + +; Function Attrs: nounwind readnone +declare i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2*, 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 1cd4501b4ce0c2c67aee2291f6c4df16ae1d3fb6)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !7, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, 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: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !{!8, !13} +!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 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64) +!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 32, elements: !15) +!15 = !{!16} +!16 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !14, file: !1, line: 3, baseType: !12, size: 32) +!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 1cd4501b4ce0c2c67aee2291f6c4df16ae1d3fb6)"} +!21 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !10, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !22) +!22 = !{} +!23 = !DILocation(line: 5, column: 10, scope: !21) +!24 = !DILocation(line: 6, column: 10, scope: !21) +!25 = !DILocation(line: 5, column: 67, scope: !21) +!26 = !DILocation(line: 5, column: 3, scope: !21) 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,121 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck %s +; RUN: llc -march=bpfel -mattr=+alu32 -filetype=asm -o - %s | FileCheck %s +; +; Source: +; enum { TYPE_EXISTENCE = 8, }; +; typedef int (*func_t)(void); +; struct s2 { int a; }; +; int test() { +; const struct s2 s = {}; +; const func_t f = (void *)0; +; return __builtin_preserve_field_info(f, TYPE_EXISTENCE) + +; __builtin_preserve_field_info(s, TYPE_EXISTENCE); +; } +; Compiler flag to generate IR: +; clang -target bpf -S -O2 -g -emit-llvm t.c + +%struct.s2 = type { i32 } + +; Function Attrs: nounwind readnone +define dso_local i32 @test() local_unnamed_addr #0 !dbg !18 { +entry: + %s = alloca %struct.s2, align 4 + %f = alloca i32 ()*, align 8 + %0 = bitcast %struct.s2* %s to i8*, !dbg !26 + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0) #4, !dbg !26 + call void @llvm.dbg.declare(metadata %struct.s2* %s, metadata !20, metadata !DIExpression()), !dbg !27 + %1 = bitcast i32 ()** %f to i8*, !dbg !28 + call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %1) #4, !dbg !28 + call void @llvm.dbg.value(metadata i32 ()* null, metadata !25, metadata !DIExpression()), !dbg !29 + call void @llvm.dbg.value(metadata i32 ()** %f, metadata !25, metadata !DIExpression(DW_OP_deref)), !dbg !29 + %2 = call i32 @llvm.bpf.preserve.field.info.p0p0f_i32f(i32 ()** nonnull %f, i64 8), !dbg !30, !llvm.preserve.access.index !8 + %3 = call i32 @llvm.bpf.preserve.field.info.p0s_struct.s2s(%struct.s2* nonnull %s, i64 8), !dbg !31, !llvm.preserve.access.index !21 + %add = add i32 %3, %2, !dbg !32 + call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %1) #4, !dbg !33 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0) #4, !dbg !33 + ret i32 %add, !dbg !34 +} + +; CHECK: .long 16 # BTF_KIND_TYPEDEF(id = 4) +; CHECK: .long 48 # BTF_KIND_STRUCT(id = 7) + +; 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: .long 16 # FieldReloc +; CHECK-NEXT: .long 10 # Field reloc section string offset=10 +; CHECK-NEXT: .long 2 +; 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 + +; 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: 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 1cd4501b4ce0c2c67aee2291f6c4df16ae1d3fb6)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !7, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t.c", directory: "/tmp/home/yhs/tmp1") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, 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: "TYPE_EXISTENCE", value: 8, isUnsigned: true) +!7 = !{!8} +!8 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !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 1cd4501b4ce0c2c67aee2291f6c4df16ae1d3fb6)"} +!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, !25} +!20 = !DILocalVariable(name: "s", scope: !18, file: !1, line: 5, type: !21) +!21 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !22) +!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s2", file: !1, line: 3, size: 32, elements: !23) +!23 = !{!24} +!24 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !22, file: !1, line: 3, baseType: !13, size: 32) +!25 = !DILocalVariable(name: "f", scope: !18, file: !1, line: 6, type: !8) +!26 = !DILocation(line: 5, column: 3, scope: !18) +!27 = !DILocation(line: 5, column: 19, scope: !18) +!28 = !DILocation(line: 6, column: 3, scope: !18) +!29 = !DILocation(line: 0, scope: !18) +!30 = !DILocation(line: 7, column: 10, scope: !18) +!31 = !DILocation(line: 8, column: 10, scope: !18) +!32 = !DILocation(line: 7, column: 59, scope: !18) +!33 = !DILocation(line: 9, column: 1, scope: !18) +!34 = !DILocation(line: 7, column: 3, scope: !18)