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 @@ -64,9 +64,11 @@ class BTFTypeDerived : public BTFTypeBase { const DIDerivedType *DTy; bool NeedsFixup; + StringRef Name; public: BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup); + BTFTypeDerived(unsigned NextTypeId, unsigned Tag, StringRef Name); void completeType(BTFDebug &BDebug) override; void emitType(MCStreamer &OS) override; void setPointeeType(uint32_t PointeeType); @@ -217,10 +219,12 @@ }; class BTFTypeTypeTag : public BTFTypeBase { + const DIDerivedType *DTy; StringRef Tag; public: - BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag); + BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag); + BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag); void completeType(BTFDebug &BDebug) override; }; 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 @@ -43,7 +43,7 @@ BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag, bool NeedsFixup) - : DTy(DTy), NeedsFixup(NeedsFixup) { + : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) { switch (Tag) { case dwarf::DW_TAG_pointer_type: Kind = BTF::BTF_KIND_PTR; @@ -66,14 +66,23 @@ BTFType.Info = Kind << 24; } +/// Used by DW_TAG_pointer_type only. +BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag, + StringRef Name) + : DTy(nullptr), NeedsFixup(false), Name(Name) { + Kind = BTF::BTF_KIND_PTR; + BTFType.Info = Kind << 24; + BTFType.Type = NextTypeId; +} + void BTFTypeDerived::completeType(BTFDebug &BDebug) { if (IsCompleted) return; IsCompleted = true; - BTFType.NameOff = BDebug.addString(DTy->getName()); + BTFType.NameOff = BDebug.addString(Name); - if (NeedsFixup) + if (NeedsFixup || !DTy) return; // The base type for PTR/CONST/VOLATILE could be void. @@ -408,10 +417,17 @@ OS.emitInt32(Info); } -BTFTypeTypeTag::BTFTypeTypeTag(uint32_t BaseTypeId, StringRef Tag) : Tag(Tag) { +BTFTypeTypeTag::BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag) + : DTy(nullptr), Tag(Tag) { + Kind = BTF::BTF_KIND_TYPE_TAG; + BTFType.Info = Kind << 24; + BTFType.Type = NextTypeId; +} + +BTFTypeTypeTag::BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag) + : DTy(DTy), Tag(Tag) { Kind = BTF::BTF_KIND_TYPE_TAG; BTFType.Info = Kind << 24; - BTFType.Type = BaseTypeId; } void BTFTypeTypeTag::completeType(BTFDebug &BDebug) { @@ -419,6 +435,13 @@ return; IsCompleted = true; BTFType.NameOff = BDebug.addString(Tag); + if (DTy) { + const DIType *ResolvedType = DTy->getBaseType(); + if (!ResolvedType) + BTFType.Type = 0; + else + BTFType.Type = BDebug.getTypeId(ResolvedType); + } } uint32_t BTFStringTable::addString(StringRef S) { @@ -675,6 +698,8 @@ SmallVector MDStrs; DINodeArray Annots = DTy->getAnnotations(); if (Annots) { + // For type with "int __tag1 __tag2 *p", the MDStrs will have + // content: [__tag1, __tag2]. for (const Metadata *Annotations : Annots->operands()) { const MDNode *MD = cast(Annotations); const MDString *Name = cast(MD->getOperand(0)); @@ -685,20 +710,22 @@ } if (MDStrs.size() > 0) { - auto TypeEntry = std::make_unique(DTy, Tag, false); + // With MDStrs [__tag1, __tag2], the output type chain looks like + // PTR -> __tag2 -> __tag1 -> BaseType + // In the below, we construct BTF types with the order of __tag1, __tag2 + // and PTR. + auto TypeEntry = + std::make_unique(DTy, MDStrs[0]->getString()); 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); - } + for (unsigned I = 1; I < MDStrs.size(); I++) { + const MDString *Value = MDStrs[I]; + TypeEntry = + std::make_unique(TmpTypeId, Value->getString()); + TmpTypeId = addType(std::move(TypeEntry)); } + auto TypeDEntry = + std::make_unique(TmpTypeId, Tag, DTy->getName()); + TypeId = addType(std::move(TypeDEntry), DTy); } else { auto TypeEntry = std::make_unique(DTy, Tag, false); TypeId = addType(std::move(TypeEntry), DTy); diff --git a/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll b/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll --- a/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll +++ b/llvm/test/CodeGen/BPF/BTF/type-tag-var.ll @@ -27,20 +27,20 @@ !10 = !{!9, !11} !11 = !{!"btf_type_tag", !"tag2"} -; CHECK: .long 0 # BTF_KIND_PTR(id = 1) -; CHECK-NEXT: .long 33554432 # 0x2000000 +; CHECK: .long 1 # BTF_KIND_TYPE_TAG(id = 1) +; CHECK-NEXT: .long 301989888 # 0x12000000 ; CHECK-NEXT: .long 5 -; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 2) +; CHECK-NEXT: .long 6 # 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 0 # BTF_KIND_PTR(id = 3) ; CHECK-NEXT: .long 33554432 # 0x2000000 -; CHECK-NEXT: .long 6 -; CHECK-NEXT: .long 6 # BTF_KIND_TYPE_TAG(id = 5) +; CHECK-NEXT: .long 2 +; CHECK-NEXT: .long 1 # BTF_KIND_TYPE_TAG(id = 4) ; CHECK-NEXT: .long 301989888 # 0x12000000 +; CHECK-NEXT: .long 6 +; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 5) +; CHECK-NEXT: .long 33554432 # 0x2000000 ; CHECK-NEXT: .long 4 ; CHECK-NEXT: .long 11 # BTF_KIND_INT(id = 6) ; CHECK-NEXT: .long 16777216 # 0x1000000 @@ -51,8 +51,8 @@ ; CHECK-NEXT: .long 3 ; CHECK-NEXT: .long 1 -; CHECK: .ascii "tag2" # string offset=1 -; CHECK: .ascii "tag1" # string offset=6 +; CHECK: .ascii "tag1" # string offset=1 +; CHECK: .ascii "tag2" # string offset=6 ; CHECK: .ascii "int" # string offset=11 ; CHECK: .byte 103 # string offset=15