diff --git a/llvm/lib/Target/BPF/BTF.h b/llvm/lib/Target/BPF/BTF.h --- a/llvm/lib/Target/BPF/BTF.h +++ b/llvm/lib/Target/BPF/BTF.h @@ -106,14 +106,14 @@ /// Bits 24-27: kind (e.g. int, ptr, array...etc) /// Bits 28-30: unused /// Bit 31: kind_flag, currently used by - /// struct, union and fwd + /// struct, union, fwd and attr uint32_t Info; /// "Size" is used by INT, ENUM, STRUCT and UNION. /// "Size" tells the size of the type it is describing. /// /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, - /// FUNC, FUNC_PROTO and VAR. + /// FUNC, FUNC_PROTO, VAR and ATTR. /// "Type" is a type_id referring to another type. union { uint32_t Size; diff --git a/llvm/lib/Target/BPF/BTF.def b/llvm/lib/Target/BPF/BTF.def --- a/llvm/lib/Target/BPF/BTF.def +++ b/llvm/lib/Target/BPF/BTF.def @@ -31,5 +31,6 @@ HANDLE_BTF_KIND(14, VAR) HANDLE_BTF_KIND(15, DATASEC) HANDLE_BTF_KIND(16, FLOAT) +HANDLE_BTF_KIND(17, TAG) #undef HANDLE_BTF_KIND diff --git a/llvm/lib/Target/BPF/BTFDebug.h b/llvm/lib/Target/BPF/BTFDebug.h --- a/llvm/lib/Target/BPF/BTFDebug.h +++ b/llvm/lib/Target/BPF/BTFDebug.h @@ -204,6 +204,18 @@ void completeType(BTFDebug &BDebug) override; }; +/// Handle attributes. +class BTFTypeTag : public BTFTypeBase { + uint32_t Info; + StringRef Tag; + +public: + BTFTypeTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag); + uint32_t getSize() override { return BTFTypeBase::getSize() + 4; } + void completeType(BTFDebug &BDebug) override; + void emitType(MCStreamer &OS) override; +}; + /// String table. class BTFStringTable { /// String table size in bytes. @@ -313,6 +325,10 @@ /// Generate types for function prototypes. void processFuncPrototypes(const Function *); + /// Generate types for annotations. + void processAnnotations(DINodeArray Annotations, uint32_t BaseTypeId, + int ComponentId); + /// Generate one field relocation record. void generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId, const GlobalVariable *, bool IsAma); diff --git a/llvm/lib/Target/BPF/BTFDebug.cpp b/llvm/lib/Target/BPF/BTFDebug.cpp --- a/llvm/lib/Target/BPF/BTFDebug.cpp +++ b/llvm/lib/Target/BPF/BTFDebug.cpp @@ -386,6 +386,27 @@ BTFType.NameOff = BDebug.addString(Name); } +BTFTypeTag::BTFTypeTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag) + : Tag(Tag) { + Kind = BTF::BTF_KIND_TAG; + BTFType.Info = ((ComponentId < 0) << 31) | (Kind << 24); + BTFType.Type = BaseTypeId; + Info = ComponentId < 0 ? 0 : ComponentId; +} + +void BTFTypeTag::completeType(BTFDebug &BDebug) { + if (IsCompleted) + return; + IsCompleted = true; + + BTFType.NameOff = BDebug.addString(Tag); +} + +void BTFTypeTag::emitType(MCStreamer &OS) { + BTFTypeBase::emitType(OS); + OS.emitInt32(Info); +} + uint32_t BTFStringTable::addString(StringRef S) { // Check whether the string already exists. for (auto &OffsetM : OffsetToIdMap) { @@ -475,6 +496,24 @@ } } +void BTFDebug::processAnnotations(DINodeArray Annotations, uint32_t BaseTypeId, + int ComponentId) { + if (!Annotations) + return; + + for (const Metadata *Annotation: Annotations->operands()) { + const MDNode *MD = cast(Annotation); + const MDString *Name = cast(MD->getOperand(0)); + if (!Name->getString().equals("btf_tag")) + continue; + + const MDString *Value = cast(MD->getOperand(1)); + auto TypeEntry = std::make_unique(BaseTypeId, ComponentId, + Value->getString()); + addType(std::move(TypeEntry)); + } +} + /// Handle structure/union types. void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct, uint32_t &TypeId) { @@ -498,9 +537,17 @@ StructTypes.push_back(TypeEntry.get()); TypeId = addType(std::move(TypeEntry), CTy); + // Check struct/union annotations + processAnnotations(CTy->getAnnotations(), TypeId, -1); + // Visit all struct members. - for (const auto *Element : Elements) - visitTypeEntry(cast(Element)); + int FieldNo = 0; + for (const auto *Element : Elements) { + const auto Elem = cast(Element); + visitTypeEntry(Elem); + processAnnotations(Elem->getAnnotations(), TypeId, FieldNo); + FieldNo++; + } } void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) { @@ -964,6 +1011,17 @@ std::make_unique(SP->getName(), ProtoTypeId, Scope); uint32_t FuncTypeId = addType(std::move(FuncTypeEntry)); + // Process argument annotations. + for (const DINode *DN : SP->getRetainedNodes()) { + if (const auto *DV = dyn_cast(DN)) { + uint32_t Arg = DV->getArg(); + if (Arg) + processAnnotations(DV->getAnnotations(), FuncTypeId, Arg - 1); + } + } + + processAnnotations(SP->getAnnotations(), FuncTypeId, -1); + for (const auto &TypeEntry : TypeEntries) TypeEntry->completeType(*this); @@ -1176,11 +1234,13 @@ continue; uint32_t GVTypeId = 0; + DIGlobalVariable *DIGlobal = nullptr; for (auto *GVE : GVs) { + DIGlobal = GVE->getVariable(); if (SecName.startswith(".maps")) - visitMapDefType(GVE->getVariable()->getType(), GVTypeId); + visitMapDefType(DIGlobal->getType(), GVTypeId); else - visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false); + visitTypeEntry(DIGlobal->getType(), GVTypeId, false, false); break; } @@ -1212,6 +1272,8 @@ std::make_unique(Global.getName(), GVTypeId, GVarInfo); uint32_t VarId = addType(std::move(VarEntry)); + processAnnotations(DIGlobal->getAnnotations(), VarId, -1); + // An empty SecName means an extern variable without section attribute. if (SecName.empty()) continue; diff --git a/llvm/test/CodeGen/BPF/BTF/tag-1.ll b/llvm/test/CodeGen/BPF/BTF/tag-1.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/BTF/tag-1.ll @@ -0,0 +1,91 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s +; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s + +; Source code: +; #define __tag1 __attribute__((btf_tag("tag1"))) +; #define __tag2 __attribute__((btf_tag("tag2"))) +; struct t1 { +; int a1; +; int a2 __tag1 __tag2; +; } __tag1 __tag2; +; struct t1 g1 __tag1 __tag2; +; Compilation flag: +; clang -target bpf -O2 -g -S -emit-llvm t.c + +%struct.t1 = type { i32, i32 } + +@g1 = dso_local local_unnamed_addr global %struct.t1 zeroinitializer, align 4, !dbg !0 + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!14, !15, !16, !17} +!llvm.ident = !{!18} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "g1", scope: !2, file: !3, line: 7, type: !6, isLocal: false, isDefinition: true, annotations: !11) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 825661b8e31d0b29d78178df1e518949dfec9f9a)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "t.c", directory: "/tmp/home/yhs/work/tests/llvm/btf_tag") +!4 = !{} +!5 = !{!0} +!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t1", file: !3, line: 3, size: 64, elements: !7, annotations: !11) +!7 = !{!8, !10} +!8 = !DIDerivedType(tag: DW_TAG_member, name: "a1", scope: !6, file: !3, line: 4, baseType: !9, size: 32) +!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!10 = !DIDerivedType(tag: DW_TAG_member, name: "a2", scope: !6, file: !3, line: 5, baseType: !9, size: 32, offset: 32, annotations: !11) +!11 = !{!12, !13} +!12 = !{!"btf_tag", !"tag1"} +!13 = !{!"btf_tag", !"tag2"} +!14 = !{i32 7, !"Dwarf Version", i32 4} +!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 13.0.0 (https://github.com/llvm/llvm-project.git 825661b8e31d0b29d78178df1e518949dfec9f9a)"} + +; CHECK: .long 1 # BTF_KIND_STRUCT(id = 1) +; CHECK-NEXT: .long 67108866 # 0x4000002 +; CHECK-NEXT: .long 8 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 0 # 0x0 +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 32 # 0x20 +; CHECK-NEXT: .long 10 # BTF_KIND_TAG(id = 2) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 0 +; CHECK-NEXT: .long 15 # BTF_KIND_TAG(id = 3) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 0 +; CHECK-NEXT: .long 20 # BTF_KIND_INT(id = 4) +; CHECK-NEXT: .long 16777216 # 0x1000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 16777248 # 0x1000020 +; CHECK-NEXT: .long 10 # BTF_KIND_TAG(id = 5) +; CHECK-NEXT: .long 285212672 # 0x11000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 15 # BTF_KIND_TAG(id = 6) +; CHECK-NEXT: .long 285212672 # 0x11000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 24 # BTF_KIND_VAR(id = 7) +; CHECK-NEXT: .long 234881024 # 0xe000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 10 # BTF_KIND_TAG(id = 8) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 0 +; CHECK-NEXT: .long 15 # BTF_KIND_TAG(id = 9) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 7 +; CHECK-NEXT: .long 0 + +; CHECK: .ascii "t1" # string offset=1 +; CHECK: .ascii "a1" # string offset=4 +; CHECK: .ascii "a2" # string offset=7 +; CHECK: .ascii "tag1" # string offset=10 +; CHECK: .ascii "tag2" # string offset=15 +; CHECK: .ascii "int" # string offset=20 +; CHECK: .ascii "g1" # string offset=24 diff --git a/llvm/test/CodeGen/BPF/BTF/tag-2.ll b/llvm/test/CodeGen/BPF/BTF/tag-2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/BPF/BTF/tag-2.ll @@ -0,0 +1,101 @@ +; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s +; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s + +; Source code: +; #define __tag1 __attribute__((btf_tag("tag1"))) +; #define __tag2 __attribute__((btf_tag("tag2"))) +; int __tag1 __tag2 foo(int arg1, int __tag1 __tag2 *arg2) { +; return arg1 + *arg2; +; } +; Compilation flag: +; clang -target bpf -O2 -g -S -emit-llvm t.c + +; Function Attrs: mustprogress nofree norecurse nosync nounwind readonly willreturn +define dso_local i32 @foo(i32 %arg1, i32* nocapture readonly %arg2) local_unnamed_addr #0 !dbg !8 { +entry: + call void @llvm.dbg.value(metadata i32 %arg1, metadata !14, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32* %arg2, metadata !15, metadata !DIExpression()), !dbg !19 + %0 = load i32, i32* %arg2, align 4, !dbg !20, !tbaa !21 + %add = add nsw i32 %0, %arg1, !dbg !25 + ret i32 %add, !dbg !26 +} + +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { mustprogress nofree norecurse nosync nounwind readonly willreturn "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } +attributes #1 = { nofree nosync nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5, !6} +!llvm.ident = !{!7} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 825661b8e31d0b29d78178df1e518949dfec9f9a)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "t.c", directory: "/tmp/home/yhs/work/tests/llvm/btf_tag") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{i32 7, !"frame-pointer", i32 2} +!7 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 825661b8e31d0b29d78178df1e518949dfec9f9a)"} +!8 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !9, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13, annotations: !16) +!9 = !DISubroutineType(types: !10) +!10 = !{!11, !11, !12} +!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64) +!13 = !{!14, !15} +!14 = !DILocalVariable(name: "arg1", arg: 1, scope: !8, file: !1, line: 3, type: !11) +!15 = !DILocalVariable(name: "arg2", arg: 2, scope: !8, file: !1, line: 3, type: !12, annotations: !16) +!16 = !{!17, !18} +!17 = !{!"btf_tag", !"tag1"} +!18 = !{!"btf_tag", !"tag2"} +!19 = !DILocation(line: 0, scope: !8) +!20 = !DILocation(line: 4, column: 17, scope: !8) +!21 = !{!22, !22, i64 0} +!22 = !{!"int", !23, i64 0} +!23 = !{!"omnipotent char", !24, i64 0} +!24 = !{!"Simple C/C++ TBAA"} +!25 = !DILocation(line: 4, column: 15, scope: !8) +!26 = !DILocation(line: 4, column: 3, scope: !8) + +; CHECK: .long 1 # BTF_KIND_INT(id = 1) +; CHECK-NEXT: .long 16777216 # 0x1000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 16777248 # 0x1000020 +; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 2) +; CHECK-NEXT: .long 33554432 # 0x2000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 3) +; CHECK-NEXT: .long 218103810 # 0xd000002 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 5 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 10 +; CHECK-NEXT: .long 2 +; CHECK-NEXT: .long 15 # BTF_KIND_FUNC(id = 4) +; CHECK-NEXT: .long 201326593 # 0xc000001 +; CHECK-NEXT: .long 3 +; CHECK-NEXT: .long 19 # BTF_KIND_TAG(id = 5) +; CHECK-NEXT: .long 285212672 # 0x11000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 24 # BTF_KIND_TAG(id = 6) +; CHECK-NEXT: .long 285212672 # 0x11000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 19 # BTF_KIND_TAG(id = 7) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 0 +; CHECK-NEXT: .long 24 # BTF_KIND_TAG(id = 8) +; CHECK-NEXT: .long 2432696320 # 0x91000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 0 + +; CHECK: .ascii "int" # string offset=1 +; CHECK: .ascii "arg1" # string offset=5 +; CHECK: .ascii "arg2" # string offset=10 +; CHECK: .ascii "foo" # string offset=15 +; CHECK: .ascii "tag1" # string offset=19 +; CHECK: .ascii "tag2" # string offset=24 +