Skip to content

Commit ebd7155

Browse files
ayermolozahiraam
authored andcommitted
[LLVM[NFC] Refactor to allow debug_names entries to conatain DIE offset (llvm#69399)
This is pre-cursor patch to enabling type units with DWARF5 acceleration tables. With this change it allows for entries to contain offsets directly, this way type units do not need to be preserved until .debug_names is written out.
1 parent a48d12c commit ebd7155

File tree

10 files changed

+68
-66
lines changed

10 files changed

+68
-66
lines changed

llvm/include/llvm/CodeGen/AccelTable.h

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -252,44 +252,46 @@ class DWARF5AccelTableData : public AccelTableData {
252252
public:
253253
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }
254254

255-
DWARF5AccelTableData(const DIE &Die) : Die(Die) {}
255+
DWARF5AccelTableData(const DIE &Die, const DwarfCompileUnit &CU);
256+
DWARF5AccelTableData(uint64_t DieOffset, unsigned DieTag, unsigned CUIndex)
257+
: OffsetVal(DieOffset), DieTag(DieTag), UnitID(CUIndex) {}
256258

257259
#ifndef NDEBUG
258260
void print(raw_ostream &OS) const override;
259261
#endif
260262

261-
const DIE &getDie() const { return Die; }
262-
uint64_t getDieOffset() const { return Die.getOffset(); }
263-
unsigned getDieTag() const { return Die.getTag(); }
263+
uint64_t getDieOffset() const {
264+
assert(std::holds_alternative<uint64_t>(OffsetVal) &&
265+
"Accessing DIE Offset before normalizing.");
266+
return std::get<uint64_t>(OffsetVal);
267+
}
268+
unsigned getDieTag() const { return DieTag; }
269+
unsigned getUnitID() const { return UnitID; }
270+
void normalizeDIEToOffset() {
271+
assert(std::holds_alternative<const DIE *>(OffsetVal) &&
272+
"Accessing offset after normalizing.");
273+
OffsetVal = std::get<const DIE *>(OffsetVal)->getOffset();
274+
}
264275

265276
protected:
266-
const DIE &Die;
277+
std::variant<const DIE *, uint64_t> OffsetVal;
278+
unsigned DieTag;
279+
unsigned UnitID;
267280

268-
uint64_t order() const override { return Die.getOffset(); }
281+
uint64_t order() const override { return getDieOffset(); }
269282
};
270283

271-
class DWARF5AccelTableStaticData : public AccelTableData {
284+
class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
272285
public:
273-
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }
274-
275-
DWARF5AccelTableStaticData(uint64_t DieOffset, unsigned DieTag,
276-
unsigned CUIndex)
277-
: DieOffset(DieOffset), DieTag(DieTag), CUIndex(CUIndex) {}
278-
279-
#ifndef NDEBUG
280-
void print(raw_ostream &OS) const override;
281-
#endif
282-
283-
uint64_t getDieOffset() const { return DieOffset; }
284-
unsigned getDieTag() const { return DieTag; }
285-
unsigned getCUIndex() const { return CUIndex; }
286-
287-
protected:
288-
uint64_t DieOffset;
289-
unsigned DieTag;
290-
unsigned CUIndex;
291-
292-
uint64_t order() const override { return DieOffset; }
286+
/// Convert DIE entries to explicit offset.
287+
/// Needs to be called after DIE offsets are computed.
288+
void convertDieToOffset() {
289+
for (auto &Entry : Entries) {
290+
for (AccelTableData *Value : Entry.second.Values) {
291+
static_cast<DWARF5AccelTableData *>(Value)->normalizeDIEToOffset();
292+
}
293+
}
294+
}
293295
};
294296

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

309-
void emitDWARF5AccelTable(AsmPrinter *Asm,
310-
AccelTable<DWARF5AccelTableData> &Contents,
311+
void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents,
311312
const DwarfDebug &DD,
312313
ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs);
313314

@@ -316,9 +317,9 @@ void emitDWARF5AccelTable(AsmPrinter *Asm,
316317
/// start of compilation unit, either offsets to the start of compilation
317318
/// unit themselves.
318319
void emitDWARF5AccelTable(
319-
AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
320+
AsmPrinter *Asm, DWARF5AccelTable &Contents,
320321
ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
321-
llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
322+
llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
322323
getCUIndexForEntry);
323324

324325
/// Accelerator table data implementation for simple Apple accelerator tables

llvm/include/llvm/DWARFLinker/DWARFLinker.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ class DwarfEmitter {
119119
virtual void emitLineStrings(const NonRelocatableStringpool &Pool) = 0;
120120

121121
/// Emit DWARF debug names.
122-
virtual void
123-
emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;
122+
virtual void emitDebugNames(DWARF5AccelTable &Table) = 0;
124123

125124
/// Emit Apple namespaces accelerator table.
126125
virtual void
@@ -880,7 +879,7 @@ class DWARFLinker {
880879
uint32_t LastCIEOffset = 0;
881880

882881
/// Apple accelerator tables.
883-
AccelTable<DWARF5AccelTableStaticData> DebugNames;
882+
DWARF5AccelTable DebugNames;
884883
AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
885884
AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
886885
AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;

llvm/include/llvm/DWARFLinker/DWARFStreamer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class DwarfStreamer : public DwarfEmitter {
163163
StringRef Bytes) override;
164164

165165
/// Emit DWARF debug names.
166-
void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) override;
166+
void emitDebugNames(DWARF5AccelTable &Table) override;
167167

168168
/// Emit Apple namespaces accelerator table.
169169
void emitAppleNamespaces(

llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ void AppleAccelTableWriter::emit() const {
357357
emitData();
358358
}
359359

360+
DWARF5AccelTableData::DWARF5AccelTableData(const DIE &Die,
361+
const DwarfCompileUnit &CU)
362+
: OffsetVal(&Die), DieTag(Die.getTag()), UnitID(CU.getUniqueID()) {}
363+
360364
template <typename DataT>
361365
void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) {
362366
assert(CompUnitCount > 0 && "Index must have at least one CU.");
@@ -545,8 +549,8 @@ void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
545549
}
546550

547551
void llvm::emitDWARF5AccelTable(
548-
AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents,
549-
const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
552+
AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD,
553+
ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
550554
std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits;
551555
SmallVector<unsigned, 1> CUIndex(CUs.size());
552556
int Count = 0;
@@ -575,20 +579,19 @@ void llvm::emitDWARF5AccelTable(
575579
Dwarf5AccelTableWriter<DWARF5AccelTableData>(
576580
Asm, Contents, CompUnits,
577581
[&](const DWARF5AccelTableData &Entry) {
578-
const DIE *CUDie = Entry.getDie().getUnitDie();
579-
return CUIndex[DD.lookupCU(CUDie)->getUniqueID()];
582+
return CUIndex[Entry.getUnitID()];
580583
})
581584
.emit();
582585
}
583586

584587
void llvm::emitDWARF5AccelTable(
585-
AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
588+
AsmPrinter *Asm, DWARF5AccelTable &Contents,
586589
ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
587-
llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
590+
llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
588591
getCUIndexForEntry) {
589592
Contents.finalize(Asm, "names");
590-
Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
591-
getCUIndexForEntry)
593+
Dwarf5AccelTableWriter<DWARF5AccelTableData>(Asm, Contents, CUs,
594+
getCUIndexForEntry)
592595
.emit();
593596
}
594597

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

690-
void DWARF5AccelTableStaticData::print(raw_ostream &OS) const {
691-
OS << " Offset: " << getDieOffset() << "\n";
692-
OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
693-
}
694-
695693
void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
696694
OS << " Offset: " << Die.getOffset() << "\n";
697695
}

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,10 @@ void DwarfDebug::finalizeModuleInfo() {
13891389
InfoHolder.computeSizeAndOffsets();
13901390
if (useSplitDwarf())
13911391
SkeletonHolder.computeSizeAndOffsets();
1392+
1393+
// Now that offsets are computed, can replace DIEs in debug_names Entry with
1394+
// an actual offset.
1395+
AccelDebugNames.convertDieToOffset();
13921396
}
13931397

13941398
// Emit all Dwarf sections that should come after the content.
@@ -3547,9 +3551,11 @@ void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU,
35473551
case AccelTableKind::Apple:
35483552
AppleAccel.addName(Ref, Die);
35493553
break;
3550-
case AccelTableKind::Dwarf:
3551-
AccelDebugNames.addName(Ref, Die);
3554+
case AccelTableKind::Dwarf: {
3555+
DwarfCompileUnit *DCU = CUMap.lookup(&CU);
3556+
AccelDebugNames.addName(Ref, Die, *DCU);
35523557
break;
3558+
}
35533559
case AccelTableKind::Default:
35543560
llvm_unreachable("Default should have already been resolved.");
35553561
case AccelTableKind::None:

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ class DwarfDebug : public DebugHandlerBase {
496496
AddressPool AddrPool;
497497

498498
/// Accelerator tables.
499-
AccelTable<DWARF5AccelTableData> AccelDebugNames;
499+
DWARF5AccelTable AccelDebugNames;
500500
AccelTable<AppleAccelTableOffsetData> AccelNames;
501501
AccelTable<AppleAccelTableOffsetData> AccelObjC;
502502
AccelTable<AppleAccelTableOffsetData> AccelNamespace;

llvm/lib/DWARFLinker/DWARFStreamer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ void DwarfStreamer::emitLineStrings(const NonRelocatableStringpool &Pool) {
291291
}
292292
}
293293

294-
void DwarfStreamer::emitDebugNames(
295-
AccelTable<DWARF5AccelTableStaticData> &Table) {
294+
void DwarfStreamer::emitDebugNames(DWARF5AccelTable &Table) {
296295
if (EmittedUnits.empty())
297296
return;
298297

@@ -307,11 +306,10 @@ void DwarfStreamer::emitDebugNames(
307306
}
308307

309308
Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
310-
emitDWARF5AccelTable(
311-
Asm.get(), Table, CompUnits,
312-
[&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
313-
return UniqueIdToCuMap[Entry.getCUIndex()];
314-
});
309+
emitDWARF5AccelTable(Asm.get(), Table, CompUnits,
310+
[&UniqueIdToCuMap](const DWARF5AccelTableData &Entry) {
311+
return UniqueIdToCuMap[Entry.getUnitID()];
312+
});
315313
}
316314

317315
void DwarfStreamer::emitAppleNamespaces(

llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,16 @@ void DwarfEmitterImpl::emitDIE(DIE &Die) {
223223
DebugInfoSectionSize += Die.getSize();
224224
}
225225

226-
void DwarfEmitterImpl::emitDebugNames(
227-
AccelTable<DWARF5AccelTableStaticData> &Table,
228-
DebugNamesUnitsOffsets &CUOffsets, CompUnitIDToIdx &CUidToIdx) {
226+
void DwarfEmitterImpl::emitDebugNames(DWARF5AccelTable &Table,
227+
DebugNamesUnitsOffsets &CUOffsets,
228+
CompUnitIDToIdx &CUidToIdx) {
229229
if (CUOffsets.empty())
230230
return;
231231

232232
Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
233233
emitDWARF5AccelTable(Asm.get(), Table, CUOffsets,
234-
[&CUidToIdx](const DWARF5AccelTableStaticData &Entry) {
235-
return CUidToIdx[Entry.getCUIndex()];
234+
[&CUidToIdx](const DWARF5AccelTableData &Entry) {
235+
return CUidToIdx[Entry.getUnitID()];
236236
});
237237
}
238238

llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class DwarfEmitterImpl : public ExtraDwarfEmitter {
8787
uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize; }
8888

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

llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ void DWARFLinkerImpl::emitAppleAcceleratorSections(const Triple &TargetTriple) {
11291129
}
11301130

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

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

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

11491149
switch (Info.Type) {
11501150
case DwarfUnit::AccelType::Name:

0 commit comments

Comments
 (0)