Skip to content

Commit

Permalink
BPF: Support btf_type_tag attribute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yonghong-song committed Nov 5, 2021
1 parent 07a029c commit 41860e6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
1 change: 1 addition & 0 deletions llvm/lib/Target/BPF/BTF.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion llvm/lib/Target/BPF/BTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 48 additions & 3 deletions llvm/lib/Target/BPF/BTFDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<const MDString *, 4> MDStrs;
DINodeArray Annots = DTy->getAnnotations();
if (Annots) {
for (const Metadata *Annotations : Annots->operands()) {
const MDNode *MD = cast<MDNode>(Annotations);
const MDString *Name = cast<MDString>(MD->getOperand(0));
if (!Name->getString().equals("btf_type_tag"))
continue;
MDStrs.push_back(cast<MDString>(MD->getOperand(1)));
}
}

if (MDStrs.size() > 0) {
auto TypeEntry = std::make_unique<BTFTypeDerived>(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<BTFTypeTypeTag>(TmpTypeId, Value->getString());
TmpTypeId = addType(std::move(TypeEntry));
} else {
auto TypeEntry =
std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
TypeId = addType(std::move(TypeEntry), DTy);
}
}
} else {
auto TypeEntry = std::make_unique<BTFTypeDerived>(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<BTFTypeDerived>(DTy, Tag, false);
TypeId = addType(std::move(TypeEntry), DTy);
if (Tag == dwarf::DW_TAG_typedef)
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Target/BPF/BTFDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions llvm/test/CodeGen/BPF/BTF/type-tag-var.ll
Original file line number Diff line number Diff line change
@@ -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)"}

0 comments on commit 41860e6

Please sign in to comment.