Skip to content

Commit 7f58904

Browse files
committed
[BOLT][DWARF][NFC] Split processUnitDIE into two lambdas
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60073712
1 parent e59a619 commit 7f58904

File tree

3 files changed

+108
-69
lines changed

3 files changed

+108
-69
lines changed

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 79 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,70 @@ 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+
if (Unit.getVersion() >= 5)
708+
RangeListsSectionWriter->setAddressWriter(&AddressWriter);
709+
DebugRangesSectionWriter &RangesSectionWriter =
710+
Unit.getVersion() >= 5 ? *RangeListsSectionWriter.get()
711+
: *LegacyRangesSectionWriter.get();
712+
DebugLocWriter &DebugLocWriter =
713+
*LocListWritersByCU[LocListWritersIndexByCU[Unit.getOffset()]].get();
673714
std::optional<uint64_t> RangesBase;
674-
std::optional<uint64_t> DWOId = Unit->getDWOId();
715+
std::optional<DWARFUnit *> SplitCU;
716+
std::optional<uint64_t> DWOId = Unit.getDWOId();
675717
if (DWOId)
676718
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() +
719+
if (Unit.getVersion() >= 5) {
720+
RangesBase = RangesSectionWriter.getSectionOffset() +
727721
getDWARF5RngListLocListHeaderSize();
728-
RangesSectionWriter->initSection(*Unit);
729-
StrOffstsWriter->finalizeSection(*Unit, *DIEBlder);
722+
RangesSectionWriter.initSection(Unit);
723+
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
724+
} else if (SplitCU) {
725+
RangesBase = LegacyRangesSectionWriter.get()->getSectionOffset();
730726
}
731727

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

739735
DIEBuilder DIEBlder(BC, BC.DwCtx.get(), DebugNamesTable);
@@ -751,8 +747,24 @@ void DWARFRewriter::updateDebugInfo() {
751747
CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
752748
for (std::vector<DWARFUnit *> &Vec : PartVec) {
753749
DIEBlder.buildCompileUnits(Vec);
750+
for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) {
751+
createRangeLocListAddressWriters(*CU);
752+
std::optional<DWARFUnit *> SplitCU;
753+
std::optional<uint64_t> DWOId = CU->getDWOId();
754+
if (DWOId)
755+
SplitCU = BC.getDWOCU(*DWOId);
756+
if (!SplitCU)
757+
continue;
758+
DebugAddrWriter &AddressWriter =
759+
*AddressWritersByCU[CU->getOffset()].get();
760+
DebugRangesSectionWriter *TempRangesSectionWriter =
761+
CU->getVersion() >= 5 ? RangeListsWritersByCU[*DWOId].get()
762+
: LegacyRangesWritersByCU[*DWOId].get();
763+
processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
764+
AddressWriter);
765+
}
754766
for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
755-
processUnitDIE(CU, &DIEBlder);
767+
processMainBinaryCU(*CU, DIEBlder);
756768
finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
757769
DIEBlder.getProcessedCUs(), *FinalAddrWriter);
758770
}

bolt/test/X86/dwarf5-dwarf4-types-backward-forward-cross-reference.test

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
# RUN: %clang %cflags %tmain.o %thelper.o -o %t.exe
66
# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections
77
# RUN: llvm-dwarfdump --show-form --verbose --debug-info %t.bolt | FileCheck --check-prefix=POSTCHECK %s
8+
# RUN: llvm-dwarfdump --show-form --verbose --debug-addr %t.bolt | FileCheck --check-prefix=POSTCHECKADDR %s
89
# RUN: llvm-dwarfdump --show-form --verbose --debug-types %t.bolt | FileCheck --check-prefix=POSTCHECKTU %s
910

1011
## This test checks that BOLT handles correctly backward and forward cross CU references
11-
## for DWARF5 and DWARF4 with -fdebug-types-section
12+
## for DWARF5 and DWARF4 with -fdebug-types-section and checks the address table is correct.
1213

1314
# POSTCHECK: version = 0x0005
1415
# POSTCHECK: DW_TAG_type_unit
@@ -29,6 +30,15 @@
2930
# POSTCHECK: DW_TAG_variable [20]
3031
# POSTCHECK: DW_AT_type [DW_FORM_ref_addr] (0x{{[0-9a-f]+}} "Foo3a")
3132

33+
# POSTCHECKADDR: Addrs: [
34+
# POSTCHECKADDR-NEXT: 0x0000000000001360
35+
# POSTCHECKADDR-NEXT: 0x0000000000000000
36+
# POSTCHECKADDR-NEXT: ]
37+
# POSTCHECKADDR: Addrs: [
38+
# POSTCHECKADDR-NEXT: 0x00000000000013e0
39+
# POSTCHECKADDR-NEXT: 0x0000000000000000
40+
# POSTCHECKADDR-NEXT: ]
41+
3242
# POSTCHECKTU: version = 0x0004
3343
# POSTCHECKTU: DW_TAG_type_unit
3444
# POSTCHECKTU: DW_TAG_structure_type

bolt/test/X86/dwarf5-locexpr-referrence.test

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
# RUN: %clang %cflags -dwarf-5 %tmain.o %thelper.o -o %t.exe -Wl,-q
66
# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections
77
# RUN: llvm-dwarfdump --show-form --verbose --debug-info %t.bolt | FileCheck --check-prefix=CHECK %s
8+
# RUN: llvm-dwarfdump --show-form --verbose --debug-addr %t.bolt | FileCheck --check-prefix=CHECKADDR %s
89

9-
## This test checks that we update relative DIE references with DW_OP_convert that are in locexpr.
10+
## This test checks that we update relative DIE references with DW_OP_convert that are in locexpr
11+
## and checks the address table is correct.
1012

1113
# CHECK: version = 0x0005
1214
# CHECK: DW_TAG_variable
@@ -19,3 +21,18 @@
1921
# CHECK-SAME: DW_OP_convert (0x00000028 -> 0x00000092)
2022
# CHECK-SAME: DW_OP_convert (0x0000002c -> 0x00000096)
2123
# CHECK: version = 0x0005
24+
25+
# CHECKADDR: Addrs: [
26+
# CHECKADDR-NEXT: 0x0000000000001330
27+
# CHECKADDR-NEXT: 0x0000000000000000
28+
# CHECKADDR-NEXT: 0x0000000000001333
29+
# CHECKADDR-NEXT: ]
30+
# CHECKADDR: Addrs: [
31+
# CHECKADDR-NEXT: 0x0000000000001340
32+
# CHECKADDR-NEXT: 0x0000000000000000
33+
# CHECKADDR-NEXT: 0x0000000000001343
34+
# CHECKADDR-NEXT: ]
35+
# CHECKADDR: Addrs: [
36+
# CHECKADDR-NEXT: 0x0000000000001320
37+
# CHECKADDR-NEXT: 0x0000000000000000
38+
# CHECKADDR-NEXT: ]

0 commit comments

Comments
 (0)