Skip to content

Commit 41860e6

Browse files
committed
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
1 parent 07a029c commit 41860e6

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

llvm/lib/Target/BPF/BTF.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ HANDLE_BTF_KIND(14, VAR)
3232
HANDLE_BTF_KIND(15, DATASEC)
3333
HANDLE_BTF_KIND(16, FLOAT)
3434
HANDLE_BTF_KIND(17, DECL_TAG)
35+
HANDLE_BTF_KIND(18, TYPE_TAG)
3536

3637
#undef HANDLE_BTF_KIND

llvm/lib/Target/BPF/BTF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct CommonType {
113113
/// "Size" tells the size of the type it is describing.
114114
///
115115
/// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
116-
/// FUNC, FUNC_PROTO, VAR and DECL_TAG.
116+
/// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
117117
/// "Type" is a type_id referring to another type.
118118
union {
119119
uint32_t Size;

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ void BTFTypeDeclTag::emitType(MCStreamer &OS) {
408408
OS.emitInt32(Info);
409409
}
410410

411+
BTFTypeTypeTag::BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag) : Tag(Tag) {
412+
Kind = BTF::BTF_KIND_TYPE_TAG;
413+
BTFType.Info = Kind << 24;
414+
BTFType.Type = BaseTypeId;
415+
}
416+
417+
void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
418+
if (IsCompleted)
419+
return;
420+
IsCompleted = true;
421+
BTFType.NameOff = BDebug.addString(Tag);
422+
}
423+
411424
uint32_t BTFStringTable::addString(StringRef S) {
412425
// Check whether the string already exists.
413426
for (auto &OffsetM : OffsetToIdMap) {
@@ -658,9 +671,41 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
658671
}
659672
}
660673

661-
if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
662-
Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
663-
Tag == dwarf::DW_TAG_restrict_type) {
674+
if (Tag == dwarf::DW_TAG_pointer_type) {
675+
SmallVector<const MDString *, 4> MDStrs;
676+
DINodeArray Annots = DTy->getAnnotations();
677+
if (Annots) {
678+
for (const Metadata *Annotations : Annots->operands()) {
679+
const MDNode *MD = cast<MDNode>(Annotations);
680+
const MDString *Name = cast<MDString>(MD->getOperand(0));
681+
if (!Name->getString().equals("btf_type_tag"))
682+
continue;
683+
MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
684+
}
685+
}
686+
687+
if (MDStrs.size() > 0) {
688+
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
689+
unsigned TmpTypeId = addType(std::move(TypeEntry));
690+
for (unsigned I = MDStrs.size(); I > 0; I--) {
691+
const MDString *Value = MDStrs[I - 1];
692+
if (I != 1) {
693+
auto TypeEntry =
694+
std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
695+
TmpTypeId = addType(std::move(TypeEntry));
696+
} else {
697+
auto TypeEntry =
698+
std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
699+
TypeId = addType(std::move(TypeEntry), DTy);
700+
}
701+
}
702+
} else {
703+
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
704+
TypeId = addType(std::move(TypeEntry), DTy);
705+
}
706+
} else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
707+
Tag == dwarf::DW_TAG_volatile_type ||
708+
Tag == dwarf::DW_TAG_restrict_type) {
664709
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
665710
TypeId = addType(std::move(TypeEntry), DTy);
666711
if (Tag == dwarf::DW_TAG_typedef)

llvm/lib/Target/BPF/BTFDebug.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ class BTFTypeDeclTag : public BTFTypeBase {
216216
void emitType(MCStreamer &OS) override;
217217
};
218218

219+
class BTFTypeTypeTag : public BTFTypeBase {
220+
StringRef Tag;
221+
222+
public:
223+
BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag);
224+
void completeType(BTFDebug &BDebug) override;
225+
};
226+
219227
/// String table.
220228
class BTFStringTable {
221229
/// String table size in bytes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
;
4+
; Source:
5+
; #define __tag1 __attribute__((btf_type_tag("tag1")))
6+
; #define __tag2 __attribute__((btf_type_tag("tag2")))
7+
; int __tag1 * __tag1 __tag2 *g;
8+
; Compilation flag:
9+
; clang -target bpf -O2 -g -S -emit-llvm test.c
10+
11+
@g = dso_local local_unnamed_addr global i32** null, align 8, !dbg !0
12+
13+
!llvm.dbg.cu = !{!2}
14+
!llvm.module.flags = !{!12, !13, !14, !15}
15+
!llvm.ident = !{!16}
16+
17+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
18+
!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 3, type: !5, isLocal: false, isDefinition: true)
19+
!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)
20+
!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/llvm/btf_tag_type")
21+
!4 = !{!0}
22+
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64, annotations: !10)
23+
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64, annotations: !8)
24+
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
25+
!8 = !{!9}
26+
!9 = !{!"btf_type_tag", !"tag1"}
27+
!10 = !{!9, !11}
28+
!11 = !{!"btf_type_tag", !"tag2"}
29+
30+
; CHECK: .long 0 # BTF_KIND_PTR(id = 1)
31+
; CHECK-NEXT: .long 33554432 # 0x2000000
32+
; CHECK-NEXT: .long 5
33+
; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 2)
34+
; CHECK-NEXT: .long 301989888 # 0x12000000
35+
; CHECK-NEXT: .long 1
36+
; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 3)
37+
; CHECK-NEXT: .long 301989888 # 0x12000000
38+
; CHECK-NEXT: .long 2
39+
; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 4)
40+
; CHECK-NEXT: .long 33554432 # 0x2000000
41+
; CHECK-NEXT: .long 6
42+
; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 5)
43+
; CHECK-NEXT: .long 301989888 # 0x12000000
44+
; CHECK-NEXT: .long 4
45+
; CHECK-NEXT: .long 11 # BTF_KIND_INT(id = 6)
46+
; CHECK-NEXT: .long 16777216 # 0x1000000
47+
; CHECK-NEXT: .long 4
48+
; CHECK-NEXT: .long 16777248 # 0x1000020
49+
; CHECK-NEXT: .long 15 # BTF_KIND_VAR(id = 7)
50+
; CHECK-NEXT: .long 234881024 # 0xe000000
51+
; CHECK-NEXT: .long 3
52+
; CHECK-NEXT: .long 1
53+
54+
; CHECK: .ascii "tag2" # string offset=1
55+
; CHECK: .ascii "tag1" # string offset=6
56+
; CHECK: .ascii "int" # string offset=11
57+
; CHECK: .byte 103 # string offset=15
58+
59+
!12 = !{i32 7, !"Dwarf Version", i32 4}
60+
!13 = !{i32 2, !"Debug Info Version", i32 3}
61+
!14 = !{i32 1, !"wchar_size", i32 4}
62+
!15 = !{i32 7, !"frame-pointer", i32 2}
63+
!16 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 077b2e0cf1e97c4d97ca5ceab3ec0192ed11c66e)"}

0 commit comments

Comments
 (0)