Skip to content

Commit 446d927

Browse files
committed
Split processUnitDIE into two lambdas
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D59822423
1 parent d06b55e commit 446d927

File tree

1 file changed

+119
-97
lines changed

1 file changed

+119
-97
lines changed

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 119 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -632,119 +632,123 @@ void DWARFRewriter::updateDebugInfo() {
632632
std::mutex AccessMutex;
633633
// Needs to be invoked in the same order as CUs are processed.
634634
auto createRangeLocListAddressWriters =
635-
[&](DWARFUnit &CU) -> DebugLocWriter * {
636-
std::lock_guard<std::mutex> Lock(AccessMutex);
637-
const uint16_t DwarfVersion = CU.getVersion();
638-
if (DwarfVersion >= 5) {
639-
auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
640-
&BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
641-
RangeListsSectionWriter->setAddressWriter(AddrW.get());
642-
LocListWritersByCU[CUIndex] =
643-
std::make_unique<DebugLoclistWriter>(CU, DwarfVersion, false, *AddrW);
644-
645-
if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
646-
assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
647-
"RangeLists writer for DWO unit already exists.");
648-
auto DWORangeListsSectionWriter =
649-
std::make_unique<DebugRangeListsSectionWriter>();
650-
DWORangeListsSectionWriter->initSection(CU);
651-
DWORangeListsSectionWriter->setAddressWriter(AddrW.get());
652-
RangeListsWritersByCU[*DWOId] = std::move(DWORangeListsSectionWriter);
653-
}
654-
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
635+
[&](DWARFUnit &CU,
636+
llvm::DenseMap<uint64_t, uint64_t> &LocListWritersIndexByCU) {
637+
std::lock_guard<std::mutex> Lock(AccessMutex);
655638

656-
} else {
657-
auto AddrW =
658-
std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
659-
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
660-
LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
661-
if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
662-
assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
663-
"LegacyRangeLists writer for DWO unit already exists.");
664-
auto LegacyRangesSectionWriterByCU =
665-
std::make_unique<DebugRangesSectionWriter>();
666-
LegacyRangesSectionWriterByCU->initSection(CU);
667-
LegacyRangesWritersByCU[*DWOId] =
668-
std::move(LegacyRangesSectionWriterByCU);
669-
}
670-
}
671-
return LocListWritersByCU[CUIndex++].get();
672-
};
639+
const uint16_t DwarfVersion = CU.getVersion();
640+
if (DwarfVersion >= 5) {
641+
auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
642+
&BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
643+
RangeListsSectionWriter->setAddressWriter(AddrW.get());
644+
LocListWritersByCU[CUIndex] = std::make_unique<DebugLoclistWriter>(
645+
CU, DwarfVersion, false, *AddrW);
646+
647+
if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
648+
assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
649+
"RangeLists writer for DWO unit already exists.");
650+
auto DWORangeListsSectionWriter =
651+
std::make_unique<DebugRangeListsSectionWriter>();
652+
DWORangeListsSectionWriter->initSection(CU);
653+
DWORangeListsSectionWriter->setAddressWriter(AddrW.get());
654+
RangeListsWritersByCU[*DWOId] =
655+
std::move(DWORangeListsSectionWriter);
656+
}
657+
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
658+
659+
} else {
660+
auto AddrW =
661+
std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
662+
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
663+
LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
664+
if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
665+
assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
666+
"LegacyRangeLists writer for DWO unit already exists.");
667+
auto LegacyRangesSectionWriterByCU =
668+
std::make_unique<DebugRangesSectionWriter>();
669+
LegacyRangesSectionWriterByCU->initSection(CU);
670+
LegacyRangesWritersByCU[*DWOId] =
671+
std::move(LegacyRangesSectionWriterByCU);
672+
}
673+
}
674+
LocListWritersIndexByCU[CU.getOffset()] = CUIndex++;
675+
};
673676

674677
DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
675678
*StrWriter);
676679
GDBIndex GDBIndexSection(BC);
677680
DWPState State;
678681
if (opts::WriteDWP)
679682
initDWPState(State);
680-
auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
681-
// Check if the unit is a skeleton and we need special updates for it and
682-
// its matching split/DWO CU.
683+
auto processSplitCU = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder,
684+
std::optional<uint64_t> &RangesBase) {
683685
std::optional<DWARFUnit *> SplitCU;
684-
std::optional<uint64_t> RangesBase;
685686
std::optional<uint64_t> DWOId = Unit->getDWOId();
686687
if (DWOId)
687688
SplitCU = BC.getDWOCU(*DWOId);
688-
DebugLocWriter *DebugLocWriter = createRangeLocListAddressWriters(*Unit);
689-
DebugRangesSectionWriter *RangesSectionWriter =
690-
Unit->getVersion() >= 5 ? RangeListsSectionWriter.get()
691-
: LegacyRangesSectionWriter.get();
692-
DebugAddrWriter *AddressWriter =
693-
AddressWritersByCU[Unit->getOffset()].get();
694-
// Skipping CUs that failed to load.
695-
if (SplitCU) {
696-
DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
697-
Unit);
698-
DWODIEBuilder.buildDWOUnit(**SplitCU);
699-
std::string DWOName = "";
700-
std::optional<std::string> DwarfOutputPath =
701-
opts::DwarfOutputPath.empty()
702-
? std::nullopt
703-
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
704-
{
705-
std::lock_guard<std::mutex> Lock(AccessMutex);
706-
DWOName = DIEBlder->updateDWONameCompDir(
707-
*StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt);
708-
}
709-
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
710-
DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
711-
DWODIEBuilder.updateDWONameCompDirForTypes(DWOStrOffstsWriter,
712-
DWOStrWriter, **SplitCU,
713-
DwarfOutputPath, DWOName);
714-
DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true,
715-
*AddressWriter);
716-
DebugRangesSectionWriter *TempRangesSectionWriter = RangesSectionWriter;
717-
if (Unit->getVersion() >= 5) {
718-
TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get();
719-
} else {
720-
TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get();
721-
RangesBase = RangesSectionWriter->getSectionOffset();
722-
}
723-
724-
updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
725-
*TempRangesSectionWriter, *AddressWriter);
726-
DebugLocDWoWriter.finalize(DWODIEBuilder,
727-
*DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
728-
if (Unit->getVersion() >= 5)
729-
TempRangesSectionWriter->finalizeSection();
730-
731-
emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
732-
DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
733-
GDBIndexSection);
689+
if (!SplitCU)
690+
return;
691+
DebugAddrWriter &AddressWriter =
692+
*AddressWritersByCU[Unit->getOffset()].get();
693+
DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
694+
Unit);
695+
DWODIEBuilder.buildDWOUnit(**SplitCU);
696+
std::string DWOName = "";
697+
std::optional<std::string> DwarfOutputPath =
698+
opts::DwarfOutputPath.empty()
699+
? std::nullopt
700+
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
701+
{
702+
DWOName = DIEBlder->updateDWONameCompDir(
703+
*StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt);
734704
}
705+
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
706+
DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
707+
DWODIEBuilder.updateDWONameCompDirForTypes(
708+
DWOStrOffstsWriter, DWOStrWriter, **SplitCU, DwarfOutputPath, DWOName);
709+
DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true,
710+
AddressWriter);
711+
DebugRangesSectionWriter *TempRangesSectionWriter;
712+
if (Unit->getVersion() >= 5) {
713+
TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get();
714+
} else {
715+
TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get();
716+
RangesBase = Unit->getVersion() >= 5
717+
? RangeListsSectionWriter.get()->getSectionOffset()
718+
: LegacyRangesSectionWriter.get()->getSectionOffset();
719+
}
720+
721+
updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
722+
*TempRangesSectionWriter, AddressWriter);
723+
DebugLocDWoWriter.finalize(DWODIEBuilder,
724+
*DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
725+
if (Unit->getVersion() >= 5)
726+
TempRangesSectionWriter->finalizeSection();
735727

728+
emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
729+
DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
730+
GDBIndexSection);
731+
};
732+
auto processMainBinaryCU = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder,
733+
DebugLocWriter *DebugLocWriter,
734+
std::optional<uint64_t> &RangesBase) {
735+
DebugAddrWriter &AddressWriter =
736+
*AddressWritersByCU[Unit->getOffset()].get();
737+
DebugRangesSectionWriter &RangesSectionWriter =
738+
Unit->getVersion() >= 5 ? *RangeListsSectionWriter.get()
739+
: *LegacyRangesSectionWriter.get();
736740
if (Unit->getVersion() >= 5) {
737-
RangesBase = RangesSectionWriter->getSectionOffset() +
741+
RangesBase = RangesSectionWriter.getSectionOffset() +
738742
getDWARF5RngListLocListHeaderSize();
739-
RangesSectionWriter->initSection(*Unit);
743+
RangesSectionWriter.initSection(*Unit);
740744
StrOffstsWriter->finalizeSection(*Unit, *DIEBlder);
741745
}
742746

743-
updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, *RangesSectionWriter,
744-
*AddressWriter, RangesBase);
747+
updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, RangesSectionWriter,
748+
AddressWriter, RangesBase);
745749
DebugLocWriter->finalize(*DIEBlder, *DIEBlder->getUnitDIEbyUnit(*Unit));
746750
if (Unit->getVersion() >= 5)
747-
RangesSectionWriter->finalizeSection();
751+
RangesSectionWriter.finalizeSection();
748752
};
749753

750754
DIEBuilder DIEBlder(BC, BC.DwCtx.get(), DebugNamesTable);
@@ -767,17 +771,35 @@ void DWARFRewriter::updateDebugInfo() {
767771
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
768772
for (std::vector<DWARFUnit *> &Vec : PartVec) {
769773
DIEBlder.buildCompileUnits(Vec);
770-
for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
771-
processUnitDIE(CU, &DIEBlder);
774+
llvm::SmallVector<DWARFUnit *> CompileUnits(
775+
std::begin(DIEBlder.getProcessedCUs()),
776+
std::end(DIEBlder.getProcessedCUs()));
777+
llvm::DenseMap<uint64_t, uint64_t> RangesBaseByCU;
778+
llvm::DenseMap<uint64_t, uint64_t> LocListWritersIndexByCU;
779+
for (DWARFUnit *CU : CompileUnits) {
780+
createRangeLocListAddressWriters(*CU, LocListWritersIndexByCU);
781+
std::optional<uint64_t> RangesBase;
782+
processSplitCU(CU, &DIEBlder, RangesBase);
783+
if (RangesBase)
784+
RangesBaseByCU[CU->getOffset()] = *RangesBase;
785+
}
786+
for (DWARFUnit *CU : CompileUnits) {
787+
std::optional<uint64_t> RangesBase;
788+
if (RangesBaseByCU.count(CU->getOffset()))
789+
RangesBase = RangesBaseByCU[CU->getOffset()];
790+
DebugLocWriter *DebugLocWriter =
791+
LocListWritersByCU[LocListWritersIndexByCU[CU->getOffset()]].get();
792+
processMainBinaryCU(CU, &DIEBlder, DebugLocWriter, RangesBase);
793+
}
772794
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
773-
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
795+
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
774796
}
775797
} else {
776798
// Update unit debug info in parallel
777799
ThreadPoolInterface &ThreadPool = ParallelUtilities::getThreadPool();
778800
for (std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units())
779-
ThreadPool.async(processUnitDIE, CU.get(), &DIEBlder);
780-
ThreadPool.wait();
801+
// ThreadPool.async(processUnitDIE, CU.get(), &DIEBlder);
802+
ThreadPool.wait();
781803
}
782804

783805
DebugNamesTable.emitAccelTable();

0 commit comments

Comments
 (0)