Skip to content

Commit 8d499bd

Browse files
committed
BPF: change btf_type_tag BTF output format
For the declaration like below: int __tag1 * __tag1 __tag2 *g Commit 41860e6 ("BPF: Support btf_type_tag attribute") implemented the following encoding: VAR(g) -> __tag1 --> __tag2 -> pointer -> __tag1 -> pointer -> int Some further experiments with linux btf_type_tag support, esp. with generating attributes in vmlinux.h, and also some internal discussion showed the following format is more desirable: VAR(g) -> pointer -> __tag2 -> __tag1 -> pointer -> __tag1 -> int The format makes it similar to other modifier like 'const', e.g., const int *g which has encoding VAR(g) -> PTR -> CONST -> int Differential Revision: https://reviews.llvm.org/D113496
1 parent 791baf3 commit 8d499bd

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void BTFTypeBase::emitType(MCStreamer &OS) {
4343

4444
BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
4545
bool NeedsFixup)
46-
: DTy(DTy), NeedsFixup(NeedsFixup) {
46+
: DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) {
4747
switch (Tag) {
4848
case dwarf::DW_TAG_pointer_type:
4949
Kind = BTF::BTF_KIND_PTR;
@@ -66,14 +66,23 @@ BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
6666
BTFType.Info = Kind << 24;
6767
}
6868

69+
/// Used by DW_TAG_pointer_type only.
70+
BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag,
71+
StringRef Name)
72+
: DTy(nullptr), NeedsFixup(false), Name(Name) {
73+
Kind = BTF::BTF_KIND_PTR;
74+
BTFType.Info = Kind << 24;
75+
BTFType.Type = NextTypeId;
76+
}
77+
6978
void BTFTypeDerived::completeType(BTFDebug &BDebug) {
7079
if (IsCompleted)
7180
return;
7281
IsCompleted = true;
7382

74-
BTFType.NameOff = BDebug.addString(DTy->getName());
83+
BTFType.NameOff = BDebug.addString(Name);
7584

76-
if (NeedsFixup)
85+
if (NeedsFixup || !DTy)
7786
return;
7887

7988
// The base type for PTR/CONST/VOLATILE could be void.
@@ -408,17 +417,31 @@ void BTFTypeDeclTag::emitType(MCStreamer &OS) {
408417
OS.emitInt32(Info);
409418
}
410419

411-
BTFTypeTypeTag::BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag) : Tag(Tag) {
420+
BTFTypeTypeTag::BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
421+
: DTy(nullptr), Tag(Tag) {
422+
Kind = BTF::BTF_KIND_TYPE_TAG;
423+
BTFType.Info = Kind << 24;
424+
BTFType.Type = NextTypeId;
425+
}
426+
427+
BTFTypeTypeTag::BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag)
428+
: DTy(DTy), Tag(Tag) {
412429
Kind = BTF::BTF_KIND_TYPE_TAG;
413430
BTFType.Info = Kind << 24;
414-
BTFType.Type = BaseTypeId;
415431
}
416432

417433
void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
418434
if (IsCompleted)
419435
return;
420436
IsCompleted = true;
421437
BTFType.NameOff = BDebug.addString(Tag);
438+
if (DTy) {
439+
const DIType *ResolvedType = DTy->getBaseType();
440+
if (!ResolvedType)
441+
BTFType.Type = 0;
442+
else
443+
BTFType.Type = BDebug.getTypeId(ResolvedType);
444+
}
422445
}
423446

424447
uint32_t BTFStringTable::addString(StringRef S) {
@@ -675,6 +698,8 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
675698
SmallVector<const MDString *, 4> MDStrs;
676699
DINodeArray Annots = DTy->getAnnotations();
677700
if (Annots) {
701+
// For type with "int __tag1 __tag2 *p", the MDStrs will have
702+
// content: [__tag1, __tag2].
678703
for (const Metadata *Annotations : Annots->operands()) {
679704
const MDNode *MD = cast<MDNode>(Annotations);
680705
const MDString *Name = cast<MDString>(MD->getOperand(0));
@@ -685,20 +710,22 @@ void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
685710
}
686711

687712
if (MDStrs.size() > 0) {
688-
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
713+
// With MDStrs [__tag1, __tag2], the output type chain looks like
714+
// PTR -> __tag2 -> __tag1 -> BaseType
715+
// In the below, we construct BTF types with the order of __tag1, __tag2
716+
// and PTR.
717+
auto TypeEntry =
718+
std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
689719
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-
}
720+
for (unsigned I = 1; I < MDStrs.size(); I++) {
721+
const MDString *Value = MDStrs[I];
722+
TypeEntry =
723+
std::make_unique<BTFTypeTypeTag>(TmpTypeId, Value->getString());
724+
TmpTypeId = addType(std::move(TypeEntry));
701725
}
726+
auto TypeDEntry =
727+
std::make_unique<BTFTypeDerived>(TmpTypeId, Tag, DTy->getName());
728+
TypeId = addType(std::move(TypeDEntry), DTy);
702729
} else {
703730
auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
704731
TypeId = addType(std::move(TypeEntry), DTy);

llvm/lib/Target/BPF/BTFDebug.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ class BTFTypeBase {
6464
class BTFTypeDerived : public BTFTypeBase {
6565
const DIDerivedType *DTy;
6666
bool NeedsFixup;
67+
StringRef Name;
6768

6869
public:
6970
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup);
71+
BTFTypeDerived(unsigned NextTypeId, unsigned Tag, StringRef Name);
7072
void completeType(BTFDebug &BDebug) override;
7173
void emitType(MCStreamer &OS) override;
7274
void setPointeeType(uint32_t PointeeType);
@@ -217,10 +219,12 @@ class BTFTypeDeclTag : public BTFTypeBase {
217219
};
218220

219221
class BTFTypeTypeTag : public BTFTypeBase {
222+
const DIDerivedType *DTy;
220223
StringRef Tag;
221224

222225
public:
223-
BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag);
226+
BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag);
227+
BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag);
224228
void completeType(BTFDebug &BDebug) override;
225229
};
226230

llvm/test/CodeGen/BPF/BTF/type-tag-var.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
!10 = !{!9, !11}
2828
!11 = !{!"btf_type_tag", !"tag2"}
2929

30-
; CHECK: .long 0 # BTF_KIND_PTR(id = 1)
31-
; CHECK-NEXT: .long 33554432 # 0x2000000
30+
; CHECK: .long 1 # BTF_KIND_TYPE_TAG(id = 1)
31+
; CHECK-NEXT: .long 301989888 # 0x12000000
3232
; CHECK-NEXT: .long 5
33-
; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 2)
33+
; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 2)
3434
; CHECK-NEXT: .long 301989888 # 0x12000000
3535
; 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)
36+
; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 3)
4037
; CHECK-NEXT: .long 33554432 # 0x2000000
41-
; CHECK-NEXT: .long 6
42-
; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 5)
38+
; CHECK-NEXT: .long 2
39+
; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 4)
4340
; CHECK-NEXT: .long 301989888 # 0x12000000
41+
; CHECK-NEXT: .long 6
42+
; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 5)
43+
; CHECK-NEXT: .long 33554432 # 0x2000000
4444
; CHECK-NEXT: .long 4
4545
; CHECK-NEXT: .long 11 # BTF_KIND_INT(id = 6)
4646
; CHECK-NEXT: .long 16777216 # 0x1000000
@@ -51,8 +51,8 @@
5151
; CHECK-NEXT: .long 3
5252
; CHECK-NEXT: .long 1
5353

54-
; CHECK: .ascii "tag2" # string offset=1
55-
; CHECK: .ascii "tag1" # string offset=6
54+
; CHECK: .ascii "tag1" # string offset=1
55+
; CHECK: .ascii "tag2" # string offset=6
5656
; CHECK: .ascii "int" # string offset=11
5757
; CHECK: .byte 103 # string offset=15
5858

0 commit comments

Comments
 (0)