Skip to content

Commit e39928e

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 22eb290 commit e39928e

File tree

1 file changed

+115
-116
lines changed

1 file changed

+115
-116
lines changed

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 115 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -632,119 +632,122 @@ 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+
} else {
659+
auto AddrW =
660+
std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
661+
AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
662+
LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
663+
if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
664+
assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
665+
"LegacyRangeLists writer for DWO unit already exists.");
666+
auto LegacyRangesSectionWriterByCU =
667+
std::make_unique<DebugRangesSectionWriter>();
668+
LegacyRangesSectionWriterByCU->initSection(CU);
669+
LegacyRangesWritersByCU[*DWOId] =
670+
std::move(LegacyRangesSectionWriterByCU);
671+
}
672+
}
673+
LocListWritersIndexByCU[CU.getOffset()] = CUIndex++;
674+
};
673675

674676
DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
675677
*StrWriter);
676678
GDBIndex GDBIndexSection(BC);
677679
DWPState State;
678680
if (opts::WriteDWP)
679681
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.
682+
auto processSplitCU = [&](DWARFUnit &Unit, DIEBuilder &DIEBlder) {
683683
std::optional<DWARFUnit *> SplitCU;
684-
std::optional<uint64_t> RangesBase;
685-
std::optional<uint64_t> DWOId = Unit->getDWOId();
684+
std::optional<uint64_t> DWOId = Unit.getDWOId();
686685
if (DWOId)
687686
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);
687+
if (!SplitCU)
688+
return;
689+
DebugAddrWriter &AddressWriter =
690+
*AddressWritersByCU[Unit.getOffset()].get();
691+
DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
692+
&Unit);
693+
DWODIEBuilder.buildDWOUnit(**SplitCU);
694+
std::string DWOName = "";
695+
std::optional<std::string> DwarfOutputPath =
696+
opts::DwarfOutputPath.empty()
697+
? std::nullopt
698+
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
699+
{
700+
std::lock_guard<std::mutex> Lock(AccessMutex);
701+
DWOName = DIEBlder.updateDWONameCompDir(
702+
*StrOffstsWriter, *StrWriter, Unit, DwarfOutputPath, std::nullopt);
734703
}
735-
736-
if (Unit->getVersion() >= 5) {
737-
RangesBase = RangesSectionWriter->getSectionOffset() +
704+
DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
705+
DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
706+
DWODIEBuilder.updateDWONameCompDirForTypes(
707+
DWOStrOffstsWriter, DWOStrWriter, **SplitCU, DwarfOutputPath, DWOName);
708+
DebugLoclistWriter DebugLocDWoWriter(Unit, Unit.getVersion(), true,
709+
AddressWriter);
710+
DebugRangesSectionWriter *TempRangesSectionWriter =
711+
Unit.getVersion() >= 5 ? RangeListsWritersByCU[*DWOId].get()
712+
: LegacyRangesWritersByCU[*DWOId].get();
713+
714+
updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
715+
*TempRangesSectionWriter, AddressWriter);
716+
DebugLocDWoWriter.finalize(DWODIEBuilder,
717+
*DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
718+
if (Unit.getVersion() >= 5)
719+
TempRangesSectionWriter->finalizeSection();
720+
721+
emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, Unit, State,
722+
DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
723+
GDBIndexSection);
724+
};
725+
auto processMainBinaryCU = [&](DWARFUnit &Unit, DIEBuilder &DIEBlder,
726+
DebugLocWriter &DebugLocWriter) {
727+
DebugAddrWriter &AddressWriter =
728+
*AddressWritersByCU[Unit.getOffset()].get();
729+
DebugRangesSectionWriter &RangesSectionWriter =
730+
Unit.getVersion() >= 5 ? *RangeListsSectionWriter.get()
731+
: *LegacyRangesSectionWriter.get();
732+
std::optional<uint64_t> RangesBase;
733+
std::optional<DWARFUnit *> SplitCU;
734+
std::optional<uint64_t> DWOId = Unit.getDWOId();
735+
if (DWOId)
736+
SplitCU = BC.getDWOCU(*DWOId);
737+
if (Unit.getVersion() >= 5) {
738+
RangesBase = RangesSectionWriter.getSectionOffset() +
738739
getDWARF5RngListLocListHeaderSize();
739-
RangesSectionWriter->initSection(*Unit);
740-
StrOffstsWriter->finalizeSection(*Unit, *DIEBlder);
740+
RangesSectionWriter.initSection(Unit);
741+
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
742+
} else if (SplitCU) {
743+
RangesBase = LegacyRangesSectionWriter.get()->getSectionOffset();
741744
}
742745

743-
updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, *RangesSectionWriter,
744-
*AddressWriter, RangesBase);
745-
DebugLocWriter->finalize(*DIEBlder, *DIEBlder->getUnitDIEbyUnit(*Unit));
746-
if (Unit->getVersion() >= 5)
747-
RangesSectionWriter->finalizeSection();
746+
updateUnitDebugInfo(Unit, DIEBlder, DebugLocWriter, RangesSectionWriter,
747+
AddressWriter, RangesBase);
748+
DebugLocWriter.finalize(DIEBlder, *DIEBlder.getUnitDIEbyUnit(Unit));
749+
if (Unit.getVersion() >= 5)
750+
RangesSectionWriter.finalizeSection();
748751
};
749752

750753
DIEBuilder DIEBlder(BC, BC.DwCtx.get(), DebugNamesTable);
@@ -759,25 +762,21 @@ void DWARFRewriter::updateDebugInfo() {
759762
CUOffsetMap OffsetMap =
760763
finalizeTypeSections(DIEBlder, *Streamer, GDBIndexSection);
761764

762-
const bool SingleThreadedMode =
763-
opts::NoThreads || opts::DeterministicDebugInfo;
764-
if (!SingleThreadedMode)
765-
DIEBlder.buildCompileUnits();
766-
if (SingleThreadedMode) {
767-
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
768-
for (std::vector<DWARFUnit *> &Vec : PartVec) {
769-
DIEBlder.buildCompileUnits(Vec);
770-
for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
771-
processUnitDIE(CU, &DIEBlder);
772-
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
773-
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
765+
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
766+
for (std::vector<DWARFUnit *> &Vec : PartVec) {
767+
DIEBlder.buildCompileUnits(Vec);
768+
llvm::DenseMap<uint64_t, uint64_t> LocListWritersIndexByCU;
769+
for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) {
770+
createRangeLocListAddressWriters(*CU, LocListWritersIndexByCU);
771+
processSplitCU(*CU, DIEBlder);
774772
}
775-
} else {
776-
// Update unit debug info in parallel
777-
ThreadPoolInterface &ThreadPool = ParallelUtilities::getThreadPool();
778-
for (std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units())
779-
ThreadPool.async(processUnitDIE, CU.get(), &DIEBlder);
780-
ThreadPool.wait();
773+
for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) {
774+
DebugLocWriter *DebugLocWriter =
775+
LocListWritersByCU[LocListWritersIndexByCU[CU->getOffset()]].get();
776+
processMainBinaryCU(*CU, DIEBlder, *DebugLocWriter);
777+
}
778+
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
779+
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
781780
}
782781

783782
DebugNamesTable.emitAccelTable();

0 commit comments

Comments
 (0)