Skip to content

Commit 6193f98

Browse files
committed
changed abbrev handling to be inline with llvm
1 parent 6a1d310 commit 6193f98

File tree

2 files changed

+42
-90
lines changed

2 files changed

+42
-90
lines changed

bolt/include/bolt/Core/DebugNames.h

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace llvm {
2222
namespace bolt {
23-
class BOLTDWARF5AccelTableData : DWARF5AccelTableData {
23+
class BOLTDWARF5AccelTableData : public DWARF5AccelTableData {
2424
public:
2525
BOLTDWARF5AccelTableData(const uint64_t DieOffset,
2626
const std::optional<uint64_t> DefiningParentOffset,
@@ -45,6 +45,10 @@ class DWARF5AcceleratorTable {
4545
public:
4646
DWARF5AcceleratorTable(const bool CreateDebugNames, BinaryContext &BC,
4747
DebugStrWriter &MainBinaryStrWriter);
48+
~DWARF5AcceleratorTable() {
49+
for (DebugNamesAbbrev *Abbrev : AbbreviationsVector)
50+
Abbrev->~DebugNamesAbbrev();
51+
}
4852
/// Add DWARF5 Accelerator table entry.
4953
/// Input is DWARFUnit being processed, DIE that belongs to it, and potential
5054
/// SkeletonCU if the Unit comes from a DWO section.
@@ -62,26 +66,6 @@ class DWARF5AcceleratorTable {
6266
}
6367

6468
private:
65-
union AbbrevDescriptor {
66-
struct {
67-
uint32_t CompUnit : 1;
68-
uint32_t TypeUnit : 1;
69-
uint32_t DieOffset : 1;
70-
uint32_t Parent : 2;
71-
uint32_t TypeHash : 1;
72-
uint32_t Tag : 26;
73-
} Bits;
74-
uint32_t Value = 0;
75-
};
76-
struct TagIndex {
77-
uint32_t DieTag;
78-
uint32_t Index;
79-
};
80-
struct cmpByTagIndex {
81-
bool operator()(const TagIndex &LHS, const TagIndex &RHS) const {
82-
return LHS.Index < RHS.Index;
83-
}
84-
};
8569
bool NeedToCreate = false;
8670
BumpPtrAllocator Allocator;
8771
DebugStrWriter &MainBinaryStrWriter;
@@ -109,9 +93,13 @@ class DWARF5AcceleratorTable {
10993
using StringEntries =
11094
MapVector<std::string, HashData, llvm::StringMap<unsigned>>;
11195
StringEntries Entries;
112-
std::map<TagIndex, SmallVector<DWARF5AccelTableData::AttributeEncoding, 2>,
113-
cmpByTagIndex>
114-
Abbreviations;
96+
/// FoldingSet that uniques the abbreviations.
97+
FoldingSet<DebugNamesAbbrev> AbbreviationsSet;
98+
/// Vector containing DebugNames abbreviations for iteration in order.
99+
SmallVector<DebugNamesAbbrev *, 5> AbbreviationsVector;
100+
/// The bump allocator to use when creating DIEAbbrev objects in the uniqued
101+
/// storage container.
102+
BumpPtrAllocator Alloc;
115103
uint32_t BucketCount = 0;
116104
uint32_t UniqueHashCount = 0;
117105
uint32_t AbbrevTableSize = 0;
@@ -140,13 +128,6 @@ class DWARF5AcceleratorTable {
140128
void addUnit(DWARFUnit &Unit, const std::optional<uint64_t> &DWOID);
141129
/// Returns number of buckets in .debug_name table.
142130
ArrayRef<HashList> getBuckets() const { return Buckets; }
143-
/// Constructs and returns a unique AbbrevTag that captures what a DIE
144-
/// accesses.
145-
TagIndex getAbbrevIndex(
146-
const unsigned DieTag,
147-
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> &EntryRet,
148-
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding>
149-
&SecondEntryRe);
150131
/// Get encoding for a given attribute.
151132
std::optional<DWARF5AccelTable::UnitIndexAndEncoding>
152133
getIndexForEntry(const BOLTDWARF5AccelTableData &Value) const;

bolt/lib/Core/DebugNames.cpp

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -322,43 +322,6 @@ void DWARF5AcceleratorTable::finalize() {
322322
TUIndexEncodingSize = *dwarf::getFixedFormByteSize(TUIndexForm, FormParams);
323323
}
324324

325-
DWARF5AcceleratorTable::TagIndex DWARF5AcceleratorTable::getAbbrevIndex(
326-
const unsigned DieTag,
327-
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> &EntryRet,
328-
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding>
329-
&SecondEntryRet) {
330-
AbbrevDescriptor AbbrvDesc;
331-
auto setFields =
332-
[&](const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> &Entry)
333-
-> void {
334-
if (!Entry)
335-
return;
336-
switch (Entry->Encoding.Index) {
337-
case dwarf::DW_IDX_compile_unit:
338-
AbbrvDesc.Bits.CompUnit = true;
339-
break;
340-
case dwarf::DW_IDX_type_unit:
341-
AbbrvDesc.Bits.TypeUnit = true;
342-
break;
343-
case dwarf::DW_IDX_parent:
344-
AbbrvDesc.Bits.Parent = 0;
345-
break;
346-
case dwarf::DW_IDX_type_hash:
347-
AbbrvDesc.Bits.TypeHash = true;
348-
break;
349-
default:
350-
return;
351-
}
352-
};
353-
setFields(EntryRet);
354-
setFields(SecondEntryRet);
355-
AbbrvDesc.Bits.DieOffset = true;
356-
AbbrvDesc.Bits.Tag = DieTag;
357-
auto Iter = AbbrevTagToIndexMap.insert(
358-
{AbbrvDesc.Value, static_cast<uint32_t>(AbbrevTagToIndexMap.size() + 1)});
359-
return {DieTag, Iter.first->second};
360-
}
361-
362325
std::optional<DWARF5AccelTable::UnitIndexAndEncoding>
363326
DWARF5AcceleratorTable::getIndexForEntry(
364327
const BOLTDWARF5AccelTableData &Value) const {
@@ -388,17 +351,26 @@ void DWARF5AcceleratorTable::populateAbbrevsMap() {
388351
// the CU.
389352
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding>
390353
SecondEntryRet = getSecondIndexForEntry(*Value);
391-
const unsigned Tag = Value->getDieTag();
392-
const TagIndex AbbrvTag = getAbbrevIndex(Tag, EntryRet, SecondEntryRet);
393-
if (Abbreviations.count(AbbrvTag) == 0) {
394-
SmallVector<DWARF5AccelTableData::AttributeEncoding, 2> UA;
395-
if (EntryRet)
396-
UA.push_back(EntryRet->Encoding);
397-
if (SecondEntryRet)
398-
UA.push_back(SecondEntryRet->Encoding);
399-
UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4});
400-
Abbreviations.try_emplace(AbbrvTag, UA);
354+
DebugNamesAbbrev Abbrev(Value->getDieTag());
355+
if (EntryRet)
356+
Abbrev.addAttribute(EntryRet->Encoding);
357+
if (SecondEntryRet)
358+
Abbrev.addAttribute(SecondEntryRet->Encoding);
359+
Abbrev.addAttribute({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4});
360+
FoldingSetNodeID ID;
361+
Abbrev.Profile(ID);
362+
void *InsertPos;
363+
if (DebugNamesAbbrev *Existing =
364+
AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
365+
Value->setAbbrevNumber(Existing->getNumber());
366+
continue;
401367
}
368+
DebugNamesAbbrev *NewAbbrev =
369+
new (Alloc) DebugNamesAbbrev(std::move(Abbrev));
370+
AbbreviationsVector.push_back(NewAbbrev);
371+
NewAbbrev->setNumber(AbbreviationsVector.size());
372+
AbbreviationsSet.InsertNode(NewAbbrev, InsertPos);
373+
Value->setAbbrevNumber(NewAbbrev->getNumber());
402374
}
403375
}
404376
}
@@ -410,12 +382,11 @@ void DWARF5AcceleratorTable::writeEntry(const BOLTDWARF5AccelTableData &Entry) {
410382
// For forgeign type (FTU) units that need to refer to the FTU and to the CU.
411383
const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> SecondEntryRet =
412384
getSecondIndexForEntry(Entry);
413-
const TagIndex TagIndexVal =
414-
getAbbrevIndex(Entry.getDieTag(), EntryRet, SecondEntryRet);
415-
auto AbbrevIt = Abbreviations.find(TagIndexVal);
416-
assert(AbbrevIt != Abbreviations.end() &&
417-
"Abbrev tag was not found in the abbreviation map!");
418-
encodeULEB128(TagIndexVal.Index, *Entriestream);
385+
const unsigned AbbrevIndex = Entry.getAbbrevNumber() - 1;
386+
assert(AbbrevIndex < AbbreviationsVector.size() &&
387+
"Entry abbrev index is outside of abbreviations vector range.");
388+
const DebugNamesAbbrev *Abbrev = AbbreviationsVector[AbbrevIndex];
389+
encodeULEB128(Entry.getAbbrevNumber(), *Entriestream);
419390
auto writeIndex = [&](uint32_t Index, uint32_t IndexSize) -> void {
420391
switch (IndexSize) {
421392
default:
@@ -436,8 +407,8 @@ void DWARF5AcceleratorTable::writeEntry(const BOLTDWARF5AccelTableData &Entry) {
436407
};
437408
};
438409

439-
for (const DWARF5AccelTableData::AttributeEncoding &AttrEnc :
440-
AbbrevIt->second) {
410+
for (const DebugNamesAbbrev::AttributeEncoding &AttrEnc :
411+
Abbrev->getAttributes()) {
441412
switch (AttrEnc.Index) {
442413
default: {
443414
llvm_unreachable("Unexpected index attribute!");
@@ -602,10 +573,10 @@ void DWARF5AcceleratorTable::emitOffsets() const {
602573
}
603574
void DWARF5AcceleratorTable::emitAbbrevs() {
604575
const uint32_t AbbrevTableStart = StrBuffer->size();
605-
for (const auto &Abbrev : Abbreviations) {
606-
encodeULEB128(Abbrev.first.Index, *StrStream);
607-
encodeULEB128(Abbrev.first.DieTag, *StrStream);
608-
for (const auto &AttrEnc : Abbrev.second) {
576+
for (const auto *Abbrev : AbbreviationsVector) {
577+
encodeULEB128(Abbrev->getNumber(), *StrStream);
578+
encodeULEB128(Abbrev->getDieTag(), *StrStream);
579+
for (const auto &AttrEnc : Abbrev->getAttributes()) {
609580
encodeULEB128(AttrEnc.Index, *StrStream);
610581
encodeULEB128(AttrEnc.Form, *StrStream);
611582
}

0 commit comments

Comments
 (0)