Skip to content

Commit 910012e

Browse files
authored
[BOLT][DWARF][NFC] Split DIEBuilder::finish (llvm#101244)
Split DIEBuilder::finish so that code updating .debug_names is in a separate function.
1 parent 30b5d4a commit 910012e

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

bolt/include/bolt/Core/DIEBuilder.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class DIEBuilder {
127127
DWARFContext *DwarfContext{nullptr};
128128
DWARFUnit *SkeletonCU{nullptr};
129129
uint64_t UnitSize{0};
130+
/// Adds separate UnitSize counter for updating DebugNames
131+
/// so there is no dependency between the functions.
132+
uint64_t DebugNamesUnitSize{0};
130133
llvm::DenseSet<uint64_t> AllProcessed;
131134
DWARF5AcceleratorTable &DebugNamesTable;
132135
// Unordered map to handle name collision if output DWO directory is
@@ -203,13 +206,16 @@ class DIEBuilder {
203206
/// Update references once the layout is finalized.
204207
void updateReferences();
205208

206-
/// Update the Offset and Size of DIE, populate DebugNames table.
209+
/// Update the Offset and Size of DIE.
207210
/// Along with current CU, and DIE being processed and the new DIE offset to
208211
/// be updated, it takes in Parents vector that can be empty if this DIE has
209212
/// no parents.
210-
uint32_t finalizeDIEs(DWARFUnit &CU, DIE &Die,
211-
std::optional<BOLTDWARF5AccelTableData *> Parent,
212-
uint32_t NumberParentsInChain, uint32_t &CurOffset);
213+
uint32_t finalizeDIEs(DWARFUnit &CU, DIE &Die, uint32_t &CurOffset);
214+
215+
/// Populates DebugNames table.
216+
void populateDebugNamesTable(DWARFUnit &CU, const DIE &Die,
217+
std::optional<BOLTDWARF5AccelTableData *> Parent,
218+
uint32_t NumberParentsInChain);
213219

214220
void registerUnit(DWARFUnit &DU, bool NeedSort);
215221

@@ -338,6 +344,9 @@ class DIEBuilder {
338344
/// Finish current DIE construction.
339345
void finish();
340346

347+
/// Update debug names table.
348+
void updateDebugNamesTable();
349+
341350
// Interface to edit DIE
342351
template <class T> T *allocateDIEValue() {
343352
return new (getState().DIEAlloc) T;

bolt/lib/Core/DIEBuilder.cpp

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,11 @@ getUnitForOffset(DIEBuilder &Builder, DWARFContext &DWCtx,
461461
return nullptr;
462462
}
463463

464-
uint32_t
465-
DIEBuilder::finalizeDIEs(DWARFUnit &CU, DIE &Die,
466-
std::optional<BOLTDWARF5AccelTableData *> Parent,
467-
uint32_t NumberParentsInChain, uint32_t &CurOffset) {
464+
uint32_t DIEBuilder::finalizeDIEs(DWARFUnit &CU, DIE &Die,
465+
uint32_t &CurOffset) {
468466
getState().DWARFDieAddressesParsed.erase(Die.getOffset());
469467
uint32_t CurSize = 0;
470468
Die.setOffset(CurOffset);
471-
std::optional<BOLTDWARF5AccelTableData *> NameEntry =
472-
DebugNamesTable.addAccelTableEntry(
473-
CU, Die, SkeletonCU ? SkeletonCU->getDWOId() : std::nullopt,
474-
NumberParentsInChain, Parent);
475469
// It is possible that an indexed debugging information entry has a parent
476470
// that is not indexed (for example, if its parent does not have a name
477471
// attribute). In such a case, a parent attribute may point to a nameless
@@ -485,18 +479,13 @@ DIEBuilder::finalizeDIEs(DWARFUnit &CU, DIE &Die,
485479
// If Parent is nullopt and NumberParentsInChain is not zero, then forward
486480
// declaration was encountered in this DF traversal. Propagating nullopt for
487481
// Parent to children.
488-
if (!Parent && NumberParentsInChain)
489-
NameEntry = std::nullopt;
490-
if (NameEntry)
491-
++NumberParentsInChain;
492482
for (DIEValue &Val : Die.values())
493483
CurSize += Val.sizeOf(CU.getFormParams());
494484
CurSize += getULEB128Size(Die.getAbbrevNumber());
495485
CurOffset += CurSize;
496486

497487
for (DIE &Child : Die.children()) {
498-
uint32_t ChildSize =
499-
finalizeDIEs(CU, Child, NameEntry, NumberParentsInChain, CurOffset);
488+
uint32_t ChildSize = finalizeDIEs(CU, Child, CurOffset);
500489
CurSize += ChildSize;
501490
}
502491
// for children end mark.
@@ -514,10 +503,9 @@ void DIEBuilder::finish() {
514503
DIE *UnitDIE = getUnitDIEbyUnit(CU);
515504
uint32_t HeaderSize = CU.getHeaderSize();
516505
uint32_t CurOffset = HeaderSize;
517-
DebugNamesTable.setCurrentUnit(CU, UnitStartOffset);
518506
std::vector<std::optional<BOLTDWARF5AccelTableData *>> Parents;
519507
Parents.push_back(std::nullopt);
520-
finalizeDIEs(CU, *UnitDIE, std::nullopt, 0, CurOffset);
508+
finalizeDIEs(CU, *UnitDIE, CurOffset);
521509

522510
DWARFUnitInfo &CurUnitInfo = getUnitInfoByDwarfUnit(CU);
523511
CurUnitInfo.UnitOffset = UnitStartOffset;
@@ -548,6 +536,48 @@ void DIEBuilder::finish() {
548536
dbgs() << Twine::utohexstr(Address) << "\n";
549537
}
550538
}
539+
}
540+
541+
void DIEBuilder::populateDebugNamesTable(
542+
DWARFUnit &CU, const DIE &Die,
543+
std::optional<BOLTDWARF5AccelTableData *> Parent,
544+
uint32_t NumberParentsInChain) {
545+
std::optional<BOLTDWARF5AccelTableData *> NameEntry =
546+
DebugNamesTable.addAccelTableEntry(
547+
CU, Die, SkeletonCU ? SkeletonCU->getDWOId() : std::nullopt,
548+
NumberParentsInChain, Parent);
549+
if (!Parent && NumberParentsInChain)
550+
NameEntry = std::nullopt;
551+
if (NameEntry)
552+
++NumberParentsInChain;
553+
554+
for (const DIE &Child : Die.children())
555+
populateDebugNamesTable(CU, Child, NameEntry, NumberParentsInChain);
556+
}
557+
558+
void DIEBuilder::updateDebugNamesTable() {
559+
auto finalizeDebugNamesTableForCU = [&](DWARFUnit &CU,
560+
uint64_t &UnitStartOffset) -> void {
561+
DIE *UnitDIE = getUnitDIEbyUnit(CU);
562+
DebugNamesTable.setCurrentUnit(CU, UnitStartOffset);
563+
populateDebugNamesTable(CU, *UnitDIE, std::nullopt, 0);
564+
565+
DWARFUnitInfo &CurUnitInfo = getUnitInfoByDwarfUnit(CU);
566+
UnitStartOffset += CurUnitInfo.UnitLength;
567+
};
568+
569+
uint64_t TypeUnitStartOffset = 0;
570+
for (DWARFUnit *CU : getState().DUList) {
571+
if (!(CU->getVersion() < 5 && CU->isTypeUnit()))
572+
break;
573+
finalizeDebugNamesTableForCU(*CU, TypeUnitStartOffset);
574+
}
575+
576+
for (DWARFUnit *CU : getState().DUList) {
577+
if (CU->getVersion() < 5 && CU->isTypeUnit())
578+
continue;
579+
finalizeDebugNamesTableForCU(*CU, DebugNamesUnitSize);
580+
}
551581
updateReferences();
552582
}
553583

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,8 @@ void DWARFRewriter::updateDebugInfo() {
650650
DebugRangesSectionWriter &TempRangesSectionWriter,
651651
DebugAddrWriter &AddressWriter,
652652
const std::string &DWOName,
653-
const std::optional<std::string> &DwarfOutputPath) {
654-
DIEBuilder DWODIEBuilder(BC, &(SplitCU).getContext(), DebugNamesTable,
655-
&Unit);
653+
const std::optional<std::string> &DwarfOutputPath,
654+
DIEBuilder &DWODIEBuilder) {
656655
DWODIEBuilder.buildDWOUnit(SplitCU);
657656
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
658657
DebugStrWriter DWOStrWriter((SplitCU).getContext(), true);
@@ -719,6 +718,7 @@ void DWARFRewriter::updateDebugInfo() {
719718
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
720719
for (std::vector<DWARFUnit *> &Vec : PartVec) {
721720
DIEBlder.buildCompileUnits(Vec);
721+
llvm::SmallVector<std::unique_ptr<DIEBuilder>, 72> DWODIEBuildersByCU;
722722
for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) {
723723
createRangeLocListAddressWriters(*CU);
724724
std::optional<DWARFUnit *> SplitCU;
@@ -738,11 +738,17 @@ void DWARFRewriter::updateDebugInfo() {
738738
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
739739
std::string DWOName = DIEBlder.updateDWONameCompDir(
740740
*StrOffstsWriter, *StrWriter, *CU, DwarfOutputPath, std::nullopt);
741+
auto DWODIEBuilderPtr = std::make_unique<DIEBuilder>(
742+
BC, &(**SplitCU).getContext(), DebugNamesTable, CU);
743+
DIEBuilder &DWODIEBuilder =
744+
*DWODIEBuildersByCU.emplace_back(std::move(DWODIEBuilderPtr)).get();
741745
if (CU->getVersion() >= 5)
742746
StrOffstsWriter->finalizeSection(*CU, DIEBlder);
743747
processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
744-
AddressWriter, DWOName, DwarfOutputPath);
748+
AddressWriter, DWOName, DwarfOutputPath, DWODIEBuilder);
745749
}
750+
for (std::unique_ptr<DIEBuilder> &DWODIEBuilderPtr : DWODIEBuildersByCU)
751+
DWODIEBuilderPtr->updateDebugNamesTable();
746752
for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
747753
processMainBinaryCU(*CU, DIEBlder);
748754
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
@@ -1442,6 +1448,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
14421448
// generate and populate abbrevs here
14431449
DIEBlder.generateAbbrevs();
14441450
DIEBlder.finish();
1451+
DIEBlder.updateDebugNamesTable();
14451452
SmallVector<char, 20> OutBuffer;
14461453
std::shared_ptr<raw_svector_ostream> ObjOS =
14471454
std::make_shared<raw_svector_ostream>(OutBuffer);
@@ -1646,6 +1653,7 @@ void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder,
16461653
}
16471654
DIEBlder.generateAbbrevs();
16481655
DIEBlder.finish();
1656+
DIEBlder.updateDebugNamesTable();
16491657
// generate debug_info and CUMap
16501658
for (DWARFUnit *CU : CUs) {
16511659
emitUnit(DIEBlder, Streamer, *CU);

0 commit comments

Comments
 (0)