Skip to content

Commit 99a2d94

Browse files
committed
[ELF] Speed up/simplify removeUnusedSyntheticSections. NFC
Make one change: when the OutputSection is nullptr (due to /DISCARD/ or garbage collected BssSection (replaceCommonSymbols)), discard the SyntheticSection as well.
1 parent 4aac00a commit 99a2d94

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

lld/ELF/Writer.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,33 +1836,26 @@ static void removeUnusedSyntheticSections() {
18361836
})
18371837
.base();
18381838

1839-
DenseSet<InputSectionDescription *> isdSet;
18401839
// Mark unused synthetic sections for deletion
1841-
auto end = std::stable_partition(
1842-
start, inputSections.end(), [&](InputSectionBase *s) {
1843-
SyntheticSection *ss = dyn_cast<SyntheticSection>(s);
1844-
OutputSection *os = ss->getParent();
1845-
if (!os || ss->isNeeded())
1846-
return true;
1847-
1848-
// If we reach here, then ss is an unused synthetic section and we want
1849-
// to remove it from the corresponding input section description, and
1850-
// orphanSections.
1851-
for (SectionCommand *b : os->commands)
1852-
if (auto *isd = dyn_cast<InputSectionDescription>(b))
1853-
isdSet.insert(isd);
1854-
1855-
llvm::erase_if(
1856-
script->orphanSections,
1857-
[=](const InputSectionBase *isec) { return isec == ss; });
1858-
1859-
return false;
1860-
});
1861-
1840+
auto end = std::stable_partition(start, inputSections.end(),
1841+
[&](InputSectionBase *s) {
1842+
auto *sec = cast<SyntheticSection>(s);
1843+
return sec->getParent() && sec->isNeeded();
1844+
});
1845+
1846+
// Remove unused synthetic sections from the corresponding input section
1847+
// description and orphanSections.
18621848
DenseSet<InputSectionBase *> unused(end, inputSections.end());
1863-
for (auto *isd : isdSet)
1864-
llvm::erase_if(isd->sections,
1865-
[=](InputSection *isec) { return unused.count(isec); });
1849+
for (auto it = end; it != inputSections.end(); ++it)
1850+
if (OutputSection *osec = cast<SyntheticSection>(*it)->getParent())
1851+
for (SectionCommand *cmd : osec->commands)
1852+
if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
1853+
llvm::erase_if(isd->sections, [&](InputSection *isec) {
1854+
return unused.count(isec);
1855+
});
1856+
llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
1857+
return unused.count(sec);
1858+
});
18661859

18671860
// Erase unused synthetic sections.
18681861
inputSections.erase(end, inputSections.end());

0 commit comments

Comments
 (0)