From 41860e602aaa94556ba996fae7b8baa268e25096 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Sun, 17 Oct 2021 21:26:15 -0700 Subject: [PATCH] BPF: Support btf_type_tag attribute A new kind BTF_KIND_TYPE_TAG is defined. The tags associated with a pointer type are emitted in their IR order as modifiers. For example, for the following declaration: int __tag1 * __tag1 __tag2 *g; The BTF type chain will look like VAR(g) -> __tag1 --> __tag2 -> pointer -> __tag1 -> pointer -> int In the above "->" means BTF CommonType.Type which indicates the point-to type. Differential Revision: https://reviews.llvm.org/D113222 --- llvm/lib/Target/BPF/BTF.def | 1 + llvm/lib/Target/BPF/BTF.h | 2 +- llvm/lib/Target/BPF/BTFDebug.cpp | 51 ++++++++++++++++-- llvm/lib/Target/BPF/BTFDebug.h | 8 +++ llvm/test/CodeGen/BPF/BTF/type-tag-var.ll | 63 +++++++++++++++++++++++ 5 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 llvm/test/CodeGen/BPF/BTF/type-tag-var.ll diff --git a/llvm/lib/Target/BPF/BTF.def b/llvm/lib/Target/BPF/BTF.def index aeb81c6fecaf..0ae4194bc512 100644 --- a/llvm/lib/Target/BPF/BTF.def +++ b/llvm/lib/Target/BPF/BTF.def @@ -32,5 +32,6 @@ HANDLE_BTF_KIND(14, VAR) HANDLE_BTF_KIND(15, DATASEC) HANDLE_BTF_KIND(16, FLOAT) HANDLE_BTF_KIND(17, DECL_TAG) +HANDLE_BTF_KIND(18, TYPE_TAG) #undef HANDLE_BTF_KIND diff --git a/llvm/lib/Target/BPF/BTF.h b/llvm/lib/Target/BPF/BTF.h index 95a64609ae38..e54b97cd49a9 100644 --- a/llvm/lib/Target/BPF/BTF.h +++ b/llvm/lib/Target/BPF/BTF.h @@ -113,7 +113,7 @@ struct CommonType { /// "Size" tells the size of the type it is describing. /// /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, - /// FUNC, FUNC_PROTO, VAR and DECL_TAG. + /// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG. /// "Type" is a type_id referring to another type. union { uint32_t Size; diff --git a/llvm/lib/Target/BPF/BTFDebug.cpp b/llvm/lib/Target/BPF/BTFDebug.cpp index bbe8eb1071ae..401bdfa47659 100644 --- a/llvm/lib/Target/BPF/BTFDebug.cpp +++ b/llvm/lib/Target/BPF/BTFDebug.cpp @@ -408,6 +408,19 @@ void BTFTypeDeclTag::emitType(MCStreamer &OS) { OS.emitInt32(Info); } +BTFTypeTypeTag::BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag) : Tag(Tag) { + Kind = BTF::BTF_KIND_TYPE_TAG; + BTFType.Info = Kind << 24; + BTFType.Type = BaseTypeId; +} + +void BTFTypeTypeTag::completeType(BTFDebug &BDebug) { + if (IsCompleted) + return; + IsCompleted = true; + BTFType.NameOff = BDebug.addString(Tag); +} + uint32_t BTFStringTable::addString(StringRef S) { // Check whether the string already exists. for (auto &OffsetM : OffsetToIdMap) { @@ -658,9 +671,41 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId, } } - if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef || - Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type || - Tag == dwarf::DW_TAG_restrict_type) { + if (Tag == dwarf::DW_TAG_pointer_type) { + SmallVector MDStrs; + DINodeArray Annots = DTy->getAnnotations(); + if (Annots) { + for (const Metadata *Annotations : Annots->operands()) { + const MDNode *MD = cast(Annotations); + const MDString *Name = cast(MD->getOperand(0)); + if (!Name->getString().equals("btf_type_tag")) + continue; + MDStrs.push_back(cast(MD->getOperand(1))); + } + } + + if (MDStrs.size() > 0) { + auto TypeEntry = std::make_unique(DTy, Tag, false); + unsigned TmpTypeId = addType(std::move(TypeEntry)); + for (unsigned I = MDStrs.size(); I > 0; I--) { + const MDString *Value = MDStrs[I - 1]; + if (I != 1) { + auto TypeEntry = + std::make_unique(TmpTypeId, Value->getString()); + TmpTypeId = addType(std::move(TypeEntry)); + } else { + auto TypeEntry = + std::make_unique(TmpTypeId, Value->getString()); + TypeId = addType(std::move(TypeEntry), DTy); + } + } + } else { + auto TypeEntry = std::make_unique(DTy, Tag, false); + TypeId = addType(std::move(TypeEntry), DTy); + } + } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type || + Tag == dwarf::DW_TAG_volatile_type || + Tag == dwarf::DW_TAG_restrict_type) { auto TypeEntry = std::make_unique(DTy, Tag, false); TypeId = addType(std::move(TypeEntry), DTy); if (Tag == dwarf::DW_TAG_typedef) diff --git a/llvm/lib/Target/BPF/BTFDebug.h b/llvm/lib/Target/BPF/BTFDebug.h index 9e0203f292b2..69b53f20ab71 100644 --- a/llvm/lib/Target/BPF/BTFDebug.h +++ b/llvm/lib/Target/BPF/BTFDebug.h @@ -216,6 +216,14 @@ class BTFTypeDeclTag : public BTFTypeBase { void emitType(MCStreamer &OS) override; }; +class BTFTypeTypeTag : public BTFTypeBase { + StringRef Tag; + +public: + BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag); + void completeType(BTFDebug &BDebug) override; +}; + /// String table. class BTFStringTable { /// String table size in bytes. diff --git a/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll b/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll new file mode 100644 index 000000000000..1acf2c6db83f --- /dev/null +++ b/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll @@ -0,0 +1,63 @@ +; 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: +; #define __tag1 __attribute__((btf_type_tag("tag1"))) +; #define __tag2 __attribute__((btf_type_tag("tag2"))) +; int __tag1 * __tag1 __tag2 *g; +; Compilation flag: +; clang -target bpf -O2 -g -S -emit-llvm test.c + +@g = dso_local local_unnamed_addr global i32** null, align 8, !dbg !0 + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!12, !13, !14, !15} +!llvm.ident = !{!16} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 3, type: !5, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 14.0.0 (https://github.com/llvm/llvm-project.git 077b2e0cf1e97c4d97ca5ceab3ec0192ed11c66e)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/llvm/btf_tag_type") +!4 = !{!0} +!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64, annotations: !10) +!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64, annotations: !8) +!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!8 = !{!9} +!9 = !{!"btf_type_tag", !"tag1"} +!10 = !{!9, !11} +!11 = !{!"btf_type_tag", !"tag2"} + +; CHECK: .long 0 # BTF_KIND_PTR(id = 1) +; CHECK-NEXT: .long 33554432 # 0x2000000 +; CHECK-NEXT: .long 5 +; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 2) +; CHECK-NEXT: .long 301989888 # 0x12000000 +; CHECK-NEXT: .long 1 +; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 3) +; CHECK-NEXT: .long 301989888 # 0x12000000 +; CHECK-NEXT: .long 2 +; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 4) +; CHECK-NEXT: .long 33554432 # 0x2000000 +; CHECK-NEXT: .long 6 +; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 5) +; CHECK-NEXT: .long 301989888 # 0x12000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 11 # BTF_KIND_INT(id = 6) +; CHECK-NEXT: .long 16777216 # 0x1000000 +; CHECK-NEXT: .long 4 +; CHECK-NEXT: .long 16777248 # 0x1000020 +; CHECK-NEXT: .long 15 # BTF_KIND_VAR(id = 7) +; CHECK-NEXT: .long 234881024 # 0xe000000 +; CHECK-NEXT: .long 3 +; CHECK-NEXT: .long 1 + +; CHECK: .ascii "tag2" # string offset=1 +; CHECK: .ascii "tag1" # string offset=6 +; CHECK: .ascii "int" # string offset=11 +; CHECK: .byte 103 # string offset=15 + +!12 = !{i32 7, !"Dwarf Version", i32 4} +!13 = !{i32 2, !"Debug Info Version", i32 3} +!14 = !{i32 1, !"wchar_size", i32 4} +!15 = !{i32 7, !"frame-pointer", i32 2} +!16 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 077b2e0cf1e97c4d97ca5ceab3ec0192ed11c66e)"}