Skip to content

Commit 93ad7f6

Browse files
committed
[BPF] Generate BTF info using 'btf:type_tag' annotation
This is a follow-up for BPF mailing list discussion at [1]. Previous commit in a series updated DWARF generation for the following example: int __attribute__((btf_type_tag("tag1"))) *g; To generate DWARF that looks as follows: 0x0000001e: DW_TAG_variable DW_AT_name ("g") DW_AT_type (0x00000029 "int *") 0x00000029: DW_TAG_pointer_type DW_AT_type (0x00000032 "int") 0x00000032: DW_TAG_base_type DW_AT_name ("int") 0x00000036: DW_TAG_LLVM_annotation DW_AT_name ("btf:type_tag") DW_AT_const_value ("tag1") The fresh part is attachment of `btf:type_tag` annotations to types other than pointers. This commit changes BTF generation to rely on `btf:type_tag` annotations to generate TYPE_TAG entries. This necessitates the following changes: - The logic for `BTFTypeTypeTag` chains creation is moved to `BTFDebug::addType()`; - Special logic is added to avoid duplicate BTF entries for tagged and un-tagged type variants, e.g. in the following case: #define __tag1 __attribute__((btf_type_tag("tag1"))) #define __tag2 __attribute__((btf_type_tag("tag2"))) struct foo {}; struct bar { struct foo __tag1 aa; struct foo __tag2 bb; struct foo cc; }; Debug information generated for this example contains three instances of `DICompositeType(name: "foo")` with different `annotations` fields, however single BTF definition for structure "foo" should be generated. Field `BTFDebug::DIDedupMap` and method `BTFDebug::lookupType()` are responsible for this logic; - Care is taken to avoid references to type tags in relocation entries. [1] https://lore.kernel.org/bpf/87r0w9jjoq.fsf@oracle.com/ This was previously tracked as differential revision: https://reviews.llvm.org/D145891
1 parent ba6a989 commit 93ad7f6

20 files changed

+2125
-78
lines changed

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 554 additions & 75 deletions
Large diffs are not rendered by default.

llvm/lib/Target/BPF/BTFDebug.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class BTFTypeBase {
4848
virtual ~BTFTypeBase() = default;
4949
void setId(uint32_t Id) { this->Id = Id; }
5050
uint32_t getId() { return Id; }
51+
uint32_t getKind() { return Kind; }
5152
uint32_t roundupToBytes(uint32_t NumBits) { return (NumBits + 7) >> 3; }
5253
/// Get the size of this BTF type entry.
5354
virtual uint32_t getSize() { return BTF::CommonTypeSize; }
@@ -68,10 +69,12 @@ class BTFTypeDerived : public BTFTypeBase {
6869

6970
public:
7071
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup);
71-
BTFTypeDerived(unsigned NextTypeId, unsigned Tag, StringRef Name);
72+
BTFTypeDerived(unsigned NextTypeId, enum BTF::TypeKinds Kind,
73+
StringRef Name = StringRef());
7274
void completeType(BTFDebug &BDebug) override;
7375
void emitType(MCStreamer &OS) override;
7476
void setPointeeType(uint32_t PointeeType);
77+
uint32_t getPointeeType();
7578
};
7679

7780
/// Handle struct or union forward declaration.
@@ -240,6 +243,8 @@ class BTFTypeTypeTag : public BTFTypeBase {
240243
BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag);
241244
BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag);
242245
void completeType(BTFDebug &BDebug) override;
246+
uint32_t getNextTypeId();
247+
StringRef getTag();
243248
};
244249

245250
/// String table.
@@ -285,6 +290,20 @@ struct BTFFieldReloc {
285290
uint32_t RelocKind; ///< What to patch the instruction
286291
};
287292

293+
/// Used for de-duplication for types annotated with btf_type_tag annotation,
294+
/// See comment at BTFDebug.cpp:addType() for details.
295+
struct BTFTypeDedupKey {
296+
const DIType *CanonTy;
297+
298+
BTFTypeDedupKey(const DIType *CanonTy) : CanonTy(CanonTy) {}
299+
300+
bool operator==(const BTFTypeDedupKey &Other) const;
301+
302+
struct Hash {
303+
size_t operator()(BTFTypeDedupKey const &Key) const;
304+
};
305+
};
306+
288307
/// Collect and emit BTF information.
289308
class BTFDebug : public DebugHandlerBase {
290309
MCStreamer &OS;
@@ -296,6 +315,8 @@ class BTFDebug : public DebugHandlerBase {
296315
BTFStringTable StringTable;
297316
std::vector<std::unique_ptr<BTFTypeBase>> TypeEntries;
298317
std::unordered_map<const DIType *, uint32_t> DIToIdMap;
318+
std::unordered_map<BTFTypeDedupKey, uint32_t, BTFTypeDedupKey::Hash>
319+
DIDedupMap;
299320
std::map<uint32_t, std::vector<BTFFuncInfo>> FuncInfoTable;
300321
std::map<uint32_t, std::vector<BTFLineInfo>> LineInfoTable;
301322
std::map<uint32_t, std::vector<BTFFieldReloc>> FieldRelocTable;
@@ -311,11 +332,17 @@ class BTFDebug : public DebugHandlerBase {
311332
/// Add types to TypeEntries.
312333
/// @{
313334
/// Add types to TypeEntries and DIToIdMap.
314-
uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty);
335+
uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty,
336+
uint32_t *RealId = nullptr);
315337
/// Add types to TypeEntries only and return type id.
316338
uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry);
339+
uint32_t replaceType(uint32_t Id, std::unique_ptr<BTFTypeBase> TypeEntry);
317340
/// @}
318341

342+
BTFTypeBase *getType(uint32_t Id);
343+
344+
std::optional<uint32_t> lookupType(const DIType *Ty);
345+
319346
/// IR type visiting functions.
320347
/// @{
321348
void visitTypeEntry(const DIType *Ty);
@@ -368,7 +395,10 @@ class BTFDebug : public DebugHandlerBase {
368395
/// the base type of DTy. Return the type id of the first BTF type_tag
369396
/// in the chain. If no type_tag's are generated, a negative value
370397
/// is returned.
371-
int genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId);
398+
uint32_t genBTFTypeTags(const DIType *Ty, int BaseId,
399+
const DIDerivedType *DTy, StringRef AnnotName);
400+
uint32_t genBTFTypeTagsV1(const DIDerivedType *DTy);
401+
uint32_t genBTFTypeTagsV2(const DIType *Ty, uint32_t BaseId);
372402

373403
/// Generate one field relocation record.
374404
void generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
@@ -390,6 +420,16 @@ class BTFDebug : public DebugHandlerBase {
390420
/// Emit the .BTF.ext section.
391421
void emitBTFExtSection();
392422

423+
uint32_t skipBTFTypeTags(uint32_t Id);
424+
425+
/// BTF post processing phase rewriting type chains like below:
426+
/// CONST -> TYPE_TAG '...' -> ...
427+
/// To:
428+
/// TYPE_TAG '...' -> CONST -> ...
429+
void moveTypeTagsBeforeCVR();
430+
class QualifiedTypesCache;
431+
void rebuildTypeTagsChain(uint32_t Id, QualifiedTypesCache &Cache);
432+
393433
protected:
394434
/// Gather pre-function debug information.
395435
void beginFunctionImpl(const MachineFunction *MF) override;

0 commit comments

Comments
 (0)