Skip to content

Commit 0e3af54

Browse files
sayhaanyuxuanchen1997
authored andcommitted
[BOLT][DWARF][NFC] Split processUnitDIE into two lambdas (#99225)
Summary: Split processUnitDIE into two lambdas to separate the processing of DWO CUs and CUs in the main binary. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251258
1 parent 8f62264 commit 0e3af54

File tree

1 file changed

+77
-67
lines changed

1 file changed

+77
-67
lines changed

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,10 @@ void DWARFRewriter::updateDebugInfo() {
620620
uint32_t CUIndex = 0;
621621
std::mutex AccessMutex;
622622
// Needs to be invoked in the same order as CUs are processed.
623-
auto createRangeLocListAddressWriters =
624-
[&](DWARFUnit &CU) -> DebugLocWriter * {
623+
llvm::DenseMap<uint64_t, uint64_t> LocListWritersIndexByCU;
624+
auto createRangeLocListAddressWriters = [&](DWARFUnit &CU) {
625625
std::lock_guard<std::mutex> Lock(AccessMutex);
626+
626627
const uint16_t DwarfVersion = CU.getVersion();
627628
if (DwarfVersion >= 5) {
628629
auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
@@ -641,7 +642,6 @@ void DWARFRewriter::updateDebugInfo() {
641642
RangeListsWritersByCU[*DWOId] = std::move(DWORangeListsSectionWriter);
642643
}
643644
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
644-
645645
} else {
646646
auto AddrW =
647647
std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
@@ -657,7 +657,7 @@ void DWARFRewriter::updateDebugInfo() {
657657
std::move(LegacyRangesSectionWriterByCU);
658658
}
659659
}
660-
return LocListWritersByCU[CUIndex++].get();
660+
LocListWritersIndexByCU[CU.getOffset()] = CUIndex++;
661661
};
662662

663663
DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
@@ -666,74 +666,68 @@ void DWARFRewriter::updateDebugInfo() {
666666
DWPState State;
667667
if (opts::WriteDWP)
668668
initDWPState(State);
669-
auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
670-
// Check if the unit is a skeleton and we need special updates for it and
671-
// its matching split/DWO CU.
672-
std::optional<DWARFUnit *> SplitCU;
669+
auto processSplitCU = [&](DWARFUnit &Unit, DWARFUnit &SplitCU,
670+
DIEBuilder &DIEBlder,
671+
DebugRangesSectionWriter &TempRangesSectionWriter,
672+
DebugAddrWriter &AddressWriter) {
673+
DIEBuilder DWODIEBuilder(BC, &(SplitCU).getContext(), DebugNamesTable,
674+
&Unit);
675+
DWODIEBuilder.buildDWOUnit(SplitCU);
676+
std::string DWOName = "";
677+
std::optional<std::string> DwarfOutputPath =
678+
opts::DwarfOutputPath.empty()
679+
? std::nullopt
680+
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
681+
{
682+
std::lock_guard<std::mutex> Lock(AccessMutex);
683+
DWOName = DIEBlder.updateDWONameCompDir(
684+
*StrOffstsWriter, *StrWriter, Unit, DwarfOutputPath, std::nullopt);
685+
}
686+
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
687+
DebugStrWriter DWOStrWriter((SplitCU).getContext(), true);
688+
DWODIEBuilder.updateDWONameCompDirForTypes(
689+
DWOStrOffstsWriter, DWOStrWriter, SplitCU, DwarfOutputPath, DWOName);
690+
DebugLoclistWriter DebugLocDWoWriter(Unit, Unit.getVersion(), true,
691+
AddressWriter);
692+
693+
updateUnitDebugInfo(SplitCU, DWODIEBuilder, DebugLocDWoWriter,
694+
TempRangesSectionWriter, AddressWriter);
695+
DebugLocDWoWriter.finalize(DWODIEBuilder,
696+
*DWODIEBuilder.getUnitDIEbyUnit(SplitCU));
697+
if (Unit.getVersion() >= 5)
698+
TempRangesSectionWriter.finalizeSection();
699+
700+
emitDWOBuilder(DWOName, DWODIEBuilder, *this, SplitCU, Unit, State,
701+
DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
702+
GDBIndexSection);
703+
};
704+
auto processMainBinaryCU = [&](DWARFUnit &Unit, DIEBuilder &DIEBlder) {
705+
DebugAddrWriter &AddressWriter =
706+
*AddressWritersByCU[Unit.getOffset()].get();
707+
DebugRangesSectionWriter &RangesSectionWriter =
708+
Unit.getVersion() >= 5 ? *RangeListsSectionWriter.get()
709+
: *LegacyRangesSectionWriter.get();
710+
DebugLocWriter &DebugLocWriter =
711+
*LocListWritersByCU[LocListWritersIndexByCU[Unit.getOffset()]].get();
673712
std::optional<uint64_t> RangesBase;
674-
std::optional<uint64_t> DWOId = Unit->getDWOId();
713+
std::optional<DWARFUnit *> SplitCU;
714+
std::optional<uint64_t> DWOId = Unit.getDWOId();
675715
if (DWOId)
676716
SplitCU = BC.getDWOCU(*DWOId);
677-
DebugLocWriter *DebugLocWriter = createRangeLocListAddressWriters(*Unit);
678-
DebugRangesSectionWriter *RangesSectionWriter =
679-
Unit->getVersion() >= 5 ? RangeListsSectionWriter.get()
680-
: LegacyRangesSectionWriter.get();
681-
DebugAddrWriter *AddressWriter =
682-
AddressWritersByCU[Unit->getOffset()].get();
683-
// Skipping CUs that failed to load.
684-
if (SplitCU) {
685-
DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
686-
Unit);
687-
DWODIEBuilder.buildDWOUnit(**SplitCU);
688-
std::string DWOName = "";
689-
std::optional<std::string> DwarfOutputPath =
690-
opts::DwarfOutputPath.empty()
691-
? std::nullopt
692-
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
693-
{
694-
std::lock_guard<std::mutex> Lock(AccessMutex);
695-
DWOName = DIEBlder->updateDWONameCompDir(
696-
*StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt);
697-
}
698-
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
699-
DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
700-
DWODIEBuilder.updateDWONameCompDirForTypes(DWOStrOffstsWriter,
701-
DWOStrWriter, **SplitCU,
702-
DwarfOutputPath, DWOName);
703-
DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true,
704-
*AddressWriter);
705-
DebugRangesSectionWriter *TempRangesSectionWriter = RangesSectionWriter;
706-
if (Unit->getVersion() >= 5) {
707-
TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get();
708-
} else {
709-
TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get();
710-
RangesBase = RangesSectionWriter->getSectionOffset();
711-
}
712-
713-
updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
714-
*TempRangesSectionWriter, *AddressWriter);
715-
DebugLocDWoWriter.finalize(DWODIEBuilder,
716-
*DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
717-
if (Unit->getVersion() >= 5)
718-
TempRangesSectionWriter->finalizeSection();
719-
720-
emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
721-
DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
722-
GDBIndexSection);
723-
}
724-
725-
if (Unit->getVersion() >= 5) {
726-
RangesBase = RangesSectionWriter->getSectionOffset() +
717+
if (Unit.getVersion() >= 5) {
718+
RangesBase = RangesSectionWriter.getSectionOffset() +
727719
getDWARF5RngListLocListHeaderSize();
728-
RangesSectionWriter->initSection(*Unit);
729-
StrOffstsWriter->finalizeSection(*Unit, *DIEBlder);
720+
RangesSectionWriter.initSection(Unit);
721+
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
722+
} else if (SplitCU) {
723+
RangesBase = LegacyRangesSectionWriter.get()->getSectionOffset();
730724
}
731725

732-
updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, *RangesSectionWriter,
733-
*AddressWriter, RangesBase);
734-
DebugLocWriter->finalize(*DIEBlder, *DIEBlder->getUnitDIEbyUnit(*Unit));
735-
if (Unit->getVersion() >= 5)
736-
RangesSectionWriter->finalizeSection();
726+
updateUnitDebugInfo(Unit, DIEBlder, DebugLocWriter, RangesSectionWriter,
727+
AddressWriter, RangesBase);
728+
DebugLocWriter.finalize(DIEBlder, *DIEBlder.getUnitDIEbyUnit(Unit));
729+
if (Unit.getVersion() >= 5)
730+
RangesSectionWriter.finalizeSection();
737731
};
738732

739733
DIEBuilder DIEBlder(BC, BC.DwCtx.get(), DebugNamesTable);
@@ -751,8 +745,24 @@ void DWARFRewriter::updateDebugInfo() {
751745
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
752746
for (std::vector<DWARFUnit *> &Vec : PartVec) {
753747
DIEBlder.buildCompileUnits(Vec);
748+
for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) {
749+
createRangeLocListAddressWriters(*CU);
750+
std::optional<DWARFUnit *> SplitCU;
751+
std::optional<uint64_t> DWOId = CU->getDWOId();
752+
if (DWOId)
753+
SplitCU = BC.getDWOCU(*DWOId);
754+
if (!SplitCU)
755+
continue;
756+
DebugAddrWriter &AddressWriter =
757+
*AddressWritersByCU[CU->getOffset()].get();
758+
DebugRangesSectionWriter *TempRangesSectionWriter =
759+
CU->getVersion() >= 5 ? RangeListsWritersByCU[*DWOId].get()
760+
: LegacyRangesWritersByCU[*DWOId].get();
761+
processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
762+
AddressWriter);
763+
}
754764
for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
755-
processUnitDIE(CU, &DIEBlder);
765+
processMainBinaryCU(*CU, DIEBlder);
756766
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
757767
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
758768
}

0 commit comments

Comments
 (0)