Skip to content

[LLVM[NFC] Refactor to allow debug_names entries to conatain DIE offset #69399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions llvm/include/llvm/CodeGen/AccelTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,44 +252,46 @@ class DWARF5AccelTableData : public AccelTableData {
public:
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }

DWARF5AccelTableData(const DIE &Die) : Die(Die) {}
DWARF5AccelTableData(const DIE &Die, const DwarfCompileUnit &CU);
DWARF5AccelTableData(uint64_t DieOffset, unsigned DieTag, unsigned CUIndex)
: OffsetVal(DieOffset), DieTag(DieTag), UnitID(CUIndex) {}

#ifndef NDEBUG
void print(raw_ostream &OS) const override;
#endif

const DIE &getDie() const { return Die; }
uint64_t getDieOffset() const { return Die.getOffset(); }
unsigned getDieTag() const { return Die.getTag(); }
uint64_t getDieOffset() const {
assert(std::holds_alternative<uint64_t>(OffsetVal) &&
"Accessing DIE Offset before normalizing.");
return std::get<uint64_t>(OffsetVal);
}
unsigned getDieTag() const { return DieTag; }
unsigned getUnitID() const { return UnitID; }
void normalizeDIEToOffset() {
assert(std::holds_alternative<const DIE *>(OffsetVal) &&
"Accessing offset after normalizing.");
OffsetVal = std::get<const DIE *>(OffsetVal)->getOffset();
}

protected:
const DIE &Die;
std::variant<const DIE *, uint64_t> OffsetVal;
unsigned DieTag;
unsigned UnitID;

uint64_t order() const override { return Die.getOffset(); }
uint64_t order() const override { return getDieOffset(); }
};

class DWARF5AccelTableStaticData : public AccelTableData {
class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
public:
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }

DWARF5AccelTableStaticData(uint64_t DieOffset, unsigned DieTag,
unsigned CUIndex)
: DieOffset(DieOffset), DieTag(DieTag), CUIndex(CUIndex) {}

#ifndef NDEBUG
void print(raw_ostream &OS) const override;
#endif

uint64_t getDieOffset() const { return DieOffset; }
unsigned getDieTag() const { return DieTag; }
unsigned getCUIndex() const { return CUIndex; }

protected:
uint64_t DieOffset;
unsigned DieTag;
unsigned CUIndex;

uint64_t order() const override { return DieOffset; }
/// Convert DIE entries to explicit offset.
/// Needs to be called after DIE offsets are computed.
void convertDieToOffset() {
for (auto &Entry : Entries) {
for (AccelTableData *Value : Entry.second.Values) {
static_cast<DWARF5AccelTableData *>(Value)->normalizeDIEToOffset();
}
}
}
};

void emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
Expand All @@ -306,8 +308,7 @@ void emitAppleAccelTable(AsmPrinter *Asm, AccelTable<DataT> &Contents,
emitAppleAccelTableImpl(Asm, Contents, Prefix, SecBegin, DataT::Atoms);
}

void emitDWARF5AccelTable(AsmPrinter *Asm,
AccelTable<DWARF5AccelTableData> &Contents,
void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents,
const DwarfDebug &DD,
ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs);

Expand All @@ -316,9 +317,9 @@ void emitDWARF5AccelTable(AsmPrinter *Asm,
/// start of compilation unit, either offsets to the start of compilation
/// unit themselves.
void emitDWARF5AccelTable(
AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
AsmPrinter *Asm, DWARF5AccelTable &Contents,
ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
getCUIndexForEntry);

/// Accelerator table data implementation for simple Apple accelerator tables
Expand Down
5 changes: 2 additions & 3 deletions llvm/include/llvm/DWARFLinker/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class DwarfEmitter {
virtual void emitLineStrings(const NonRelocatableStringpool &Pool) = 0;

/// Emit DWARF debug names.
virtual void
emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;
virtual void emitDebugNames(DWARF5AccelTable &Table) = 0;

/// Emit Apple namespaces accelerator table.
virtual void
Expand Down Expand Up @@ -880,7 +879,7 @@ class DWARFLinker {
uint32_t LastCIEOffset = 0;

/// Apple accelerator tables.
AccelTable<DWARF5AccelTableStaticData> DebugNames;
DWARF5AccelTable DebugNames;
AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/DWARFLinker/DWARFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class DwarfStreamer : public DwarfEmitter {
StringRef Bytes) override;

/// Emit DWARF debug names.
void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) override;
void emitDebugNames(DWARF5AccelTable &Table) override;

/// Emit Apple namespaces accelerator table.
void emitAppleNamespaces(
Expand Down
24 changes: 11 additions & 13 deletions llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ void AppleAccelTableWriter::emit() const {
emitData();
}

DWARF5AccelTableData::DWARF5AccelTableData(const DIE &Die,
const DwarfCompileUnit &CU)
: OffsetVal(&Die), DieTag(Die.getTag()), UnitID(CU.getUniqueID()) {}

template <typename DataT>
void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) {
assert(CompUnitCount > 0 && "Index must have at least one CU.");
Expand Down Expand Up @@ -545,8 +549,8 @@ void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
}

void llvm::emitDWARF5AccelTable(
AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents,
const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD,
ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits;
SmallVector<unsigned, 1> CUIndex(CUs.size());
int Count = 0;
Expand Down Expand Up @@ -575,20 +579,19 @@ void llvm::emitDWARF5AccelTable(
Dwarf5AccelTableWriter<DWARF5AccelTableData>(
Asm, Contents, CompUnits,
[&](const DWARF5AccelTableData &Entry) {
const DIE *CUDie = Entry.getDie().getUnitDie();
return CUIndex[DD.lookupCU(CUDie)->getUniqueID()];
return CUIndex[Entry.getUnitID()];
})
.emit();
}

void llvm::emitDWARF5AccelTable(
AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
AsmPrinter *Asm, DWARF5AccelTable &Contents,
ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
getCUIndexForEntry) {
Contents.finalize(Asm, "names");
Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
getCUIndexForEntry)
Dwarf5AccelTableWriter<DWARF5AccelTableData>(Asm, Contents, CUs,
getCUIndexForEntry)
.emit();
}

Expand Down Expand Up @@ -687,11 +690,6 @@ void DWARF5AccelTableData::print(raw_ostream &OS) const {
OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
}

void DWARF5AccelTableStaticData::print(raw_ostream &OS) const {
OS << " Offset: " << getDieOffset() << "\n";
OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
}

void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
OS << " Offset: " << Die.getOffset() << "\n";
}
Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,10 @@ void DwarfDebug::finalizeModuleInfo() {
InfoHolder.computeSizeAndOffsets();
if (useSplitDwarf())
SkeletonHolder.computeSizeAndOffsets();

// Now that offsets are computed, can replace DIEs in debug_names Entry with
// an actual offset.
AccelDebugNames.convertDieToOffset();
}

// Emit all Dwarf sections that should come after the content.
Expand Down Expand Up @@ -3547,9 +3551,11 @@ void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU,
case AccelTableKind::Apple:
AppleAccel.addName(Ref, Die);
break;
case AccelTableKind::Dwarf:
AccelDebugNames.addName(Ref, Die);
case AccelTableKind::Dwarf: {
DwarfCompileUnit *DCU = CUMap.lookup(&CU);
AccelDebugNames.addName(Ref, Die, *DCU);
break;
}
case AccelTableKind::Default:
llvm_unreachable("Default should have already been resolved.");
case AccelTableKind::None:
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ class DwarfDebug : public DebugHandlerBase {
AddressPool AddrPool;

/// Accelerator tables.
AccelTable<DWARF5AccelTableData> AccelDebugNames;
DWARF5AccelTable AccelDebugNames;
AccelTable<AppleAccelTableOffsetData> AccelNames;
AccelTable<AppleAccelTableOffsetData> AccelObjC;
AccelTable<AppleAccelTableOffsetData> AccelNamespace;
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/DWARFLinker/DWARFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ void DwarfStreamer::emitLineStrings(const NonRelocatableStringpool &Pool) {
}
}

void DwarfStreamer::emitDebugNames(
AccelTable<DWARF5AccelTableStaticData> &Table) {
void DwarfStreamer::emitDebugNames(DWARF5AccelTable &Table) {
if (EmittedUnits.empty())
return;

Expand All @@ -307,11 +306,10 @@ void DwarfStreamer::emitDebugNames(
}

Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
emitDWARF5AccelTable(
Asm.get(), Table, CompUnits,
[&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
return UniqueIdToCuMap[Entry.getCUIndex()];
});
emitDWARF5AccelTable(Asm.get(), Table, CompUnits,
[&UniqueIdToCuMap](const DWARF5AccelTableData &Entry) {
return UniqueIdToCuMap[Entry.getUnitID()];
});
}

void DwarfStreamer::emitAppleNamespaces(
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ void DwarfEmitterImpl::emitDIE(DIE &Die) {
DebugInfoSectionSize += Die.getSize();
}

void DwarfEmitterImpl::emitDebugNames(
AccelTable<DWARF5AccelTableStaticData> &Table,
DebugNamesUnitsOffsets &CUOffsets, CompUnitIDToIdx &CUidToIdx) {
void DwarfEmitterImpl::emitDebugNames(DWARF5AccelTable &Table,
DebugNamesUnitsOffsets &CUOffsets,
CompUnitIDToIdx &CUidToIdx) {
if (CUOffsets.empty())
return;

Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
emitDWARF5AccelTable(Asm.get(), Table, CUOffsets,
[&CUidToIdx](const DWARF5AccelTableStaticData &Entry) {
return CUidToIdx[Entry.getCUIndex()];
[&CUidToIdx](const DWARF5AccelTableData &Entry) {
return CUidToIdx[Entry.getUnitID()];
});
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class DwarfEmitterImpl : public ExtraDwarfEmitter {
uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize; }

/// Emits .debug_names section according to the specified \p Table.
void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table,
void emitDebugNames(DWARF5AccelTable &Table,
DebugNamesUnitsOffsets &CUOffsets,
CompUnitIDToIdx &UnitIDToIdxMap);

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ void DWARFLinkerImpl::emitAppleAcceleratorSections(const Triple &TargetTriple) {
}

void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) {
std::unique_ptr<AccelTable<DWARF5AccelTableStaticData>> DebugNames;
std::unique_ptr<DWARF5AccelTable> DebugNames;

DebugNamesUnitsOffsets CompUnits;
CompUnitIDToIdx CUidToIdx;
Expand All @@ -1144,7 +1144,7 @@ void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) {

CU->AcceleratorRecords.forEach([&](const DwarfUnit::AccelInfo &Info) {
if (DebugNames.get() == nullptr)
DebugNames = std::make_unique<AccelTable<DWARF5AccelTableStaticData>>();
DebugNames = std::make_unique<DWARF5AccelTable>();

switch (Info.Type) {
case DwarfUnit::AccelType::Name:
Expand Down