[llvm-branch-commits] [lld] [lld][MachO] Split ObjFile::parse() and parse extracted archive rounds in parallel (PR #240983)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 10 12:04:50 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld
Author: Liza Burakova (liza371)
<details>
<summary>Changes</summary>
This is part of the ld64.lld performance improvements tracked by #<!-- -->222068 and #<!-- -->238938.
Currently, parsePendingExtracts() parses each extracted archive member serially from start to finish, even though section creation, subsection splitting, local symbol creation, and hashing external symbol names only touch per-file state.
This change splits ObjFile::parse() into parsePrepare() and parseFinish(), using makeThreadLocal() for per-file allocations. parsePendingExtracts() now processes extracted members in rounds, running parsePrepare() in parallel across each round and parseFinish() serially in file order.
Locally, this shows a ~4% link-time performance improvement over the baseline.
AI use: this change is based off a prototype developed by an LLM.
This commit is output-preserving.
---
Patch is 25.43 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/240983.diff
2 Files Affected:
- (modified) lld/MachO/InputFiles.cpp (+184-82)
- (modified) lld/MachO/InputFiles.h (+33-4)
``````````diff
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 84b382d51dffa..c1356d06920a2 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -354,7 +354,8 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
StringRef segname =
StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
- sections.push_back(make<Section>(this, segname, name, sec.flags, sec.addr));
+ sections.push_back(
+ makeThreadLocal<Section>(this, segname, name, sec.flags, sec.addr));
if (sec.align >= 32) {
error("alignment " + std::to_string(sec.align) + " of section " + name +
" is too large");
@@ -372,7 +373,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
Subsections &subsections = section.subsections;
subsections.reserve(data.size() / recordSize);
for (uint64_t off = 0; off < data.size(); off += recordSize) {
- auto *isec = make<ConcatInputSection>(
+ auto *isec = makeThreadLocal<ConcatInputSection>(
section, data.slice(off, std::min(data.size(), recordSize)), align);
subsections.push_back({off, isec});
}
@@ -385,8 +386,8 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
" contains relocations, which is unsupported");
bool dedupLiterals =
name == section_names::objcMethname || config->dedupStrings;
- InputSection *isec =
- make<CStringInputSection>(section, data, align, dedupLiterals);
+ InputSection *isec = makeThreadLocal<CStringInputSection>(
+ section, data, align, dedupLiterals);
// FIXME: parallelize this?
cast<CStringInputSection>(isec)->splitIntoPieces();
section.subsections.push_back({0, isec});
@@ -394,7 +395,8 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
if (sec.nreloc)
fatal(toString(this) + ": " + sec.segname + "," + sec.sectname +
" contains relocations, which is unsupported");
- InputSection *isec = make<WordLiteralInputSection>(section, data, align);
+ InputSection *isec =
+ makeThreadLocal<WordLiteralInputSection>(section, data, align);
section.subsections.push_back({0, isec});
} else if (auto recordSize = getRecordSize(segname, name)) {
splitRecords(*recordSize);
@@ -417,7 +419,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
if (name == section_names::addrSig)
addrSigSection = sections.back();
- auto *isec = make<ConcatInputSection>(section, data, align);
+ auto *isec = makeThreadLocal<ConcatInputSection>(section, data, align);
if (isDebugSection(isec->getFlags()) &&
isec->getSegName() == segment_names::dwarf) {
// Instead of emitting DWARF sections, we emit STABS symbols to the
@@ -450,9 +452,9 @@ void ObjFile::splitEhFrames(ArrayRef<uint8_t> data, Section &ehFrameSection) {
// Note that we still want to preserve the alignment of the overall section,
// just not of the individual EH frames.
ehFrameSection.subsections.push_back(
- {frameOff, make<ConcatInputSection>(ehFrameSection,
- data.slice(frameOff, fullLength),
- /*align=*/1)});
+ {frameOff, makeThreadLocal<ConcatInputSection>(
+ ehFrameSection, data.slice(frameOff, fullLength),
+ /*align=*/1)});
}
ehFrameSection.doneSplitting = true;
}
@@ -709,10 +711,21 @@ static bool shouldIgnoreLabel(const InputSection *isec, StringRef name) {
return false;
}
+// Whether a section symbol goes in the symbol table. See createDefined().
+template <class NList>
+static bool isExternalDefined(const NList &sym, const InputSection *isec,
+ StringRef name) {
+ return (sym.n_type & N_EXT) && !shouldIgnoreLabel(isec, name);
+}
+
+// Creates the symbol for a section symbol. External symbols (see
+// isExternalDefined()) go through the symbol table and need their name hashed;
+// the far more numerous local ones do not, so they take the plain name.
template <class NList>
static macho::Symbol *createDefined(const NList &sym, StringRef name,
InputSection *isec, uint64_t value,
uint64_t size, bool forceHidden,
+ const CachedHashStringRef *hashedName,
macho::Symbol *known = nullptr) {
// Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
// N_EXT: Global symbols. These go in the symbol table during the link,
@@ -735,7 +748,8 @@ static macho::Symbol *createDefined(const NList &sym, StringRef name,
bool isCold = sym.n_desc & N_COLD_FUNC;
- if ((sym.n_type & N_EXT) && !shouldIgnoreLabel(isec, name)) {
+ if (isExternalDefined(sym, isec, name)) {
+ assert(hashedName && "external symbols need their hashed name");
// -load_hidden makes us treat global symbols as linkage unit scoped.
// Duplicates are reported but the symbol does not go in the export trie.
bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
@@ -776,12 +790,13 @@ static macho::Symbol *createDefined(const NList &sym, StringRef name,
else if (isWeakDefCanBeHidden)
isPrivateExtern = true;
return symtab->addDefined(
- name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
- isPrivateExtern, sym.n_desc & REFERENCED_DYNAMICALLY,
- sym.n_desc & N_NO_DEAD_STRIP, isWeakDefCanBeHidden, isCold, known);
+ *hashedName, isec->getFile(), isec, value, size,
+ sym.n_desc & N_WEAK_DEF, isPrivateExtern,
+ sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP,
+ isWeakDefCanBeHidden, isCold, known);
}
bool includeInSymtab = !isPrivateLabel(name) && !isEhFrameSection(isec);
- auto *defined = make<Defined>(
+ auto *defined = makeThreadLocal<Defined>(
name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
/*isExternal=*/false, /*isPrivateExtern=*/false, includeInSymtab,
sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);
@@ -793,52 +808,59 @@ static macho::Symbol *createDefined(const NList &sym, StringRef name,
// InputSection. They cannot be weak.
template <class NList>
static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,
- StringRef name, bool forceHidden) {
+ CachedHashStringRef hashedName,
+ bool forceHidden) {
+ StringRef name = hashedName.val();
bool isCold = sym.n_desc & N_COLD_FUNC;
assert(!(sym.n_desc & N_ARM_THUMB_DEF) && "ARM32 arch is not supported");
if (sym.n_type & N_EXT) {
bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
- return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,
+ return symtab->addDefined(hashedName, file, nullptr, sym.n_value,
+ /*size=*/0,
/*isWeakDef=*/false, isPrivateExtern,
/*isReferencedDynamically=*/false,
sym.n_desc & N_NO_DEAD_STRIP,
/*isWeakDefCanBeHidden=*/false, isCold);
}
- auto *defined = make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,
- /*isWeakDef=*/false,
- /*isExternal=*/false, /*isPrivateExtern=*/false,
- /*includeInSymtab=*/true,
- /*isReferencedDynamically=*/false,
- sym.n_desc & N_NO_DEAD_STRIP);
+ auto *defined = makeThreadLocal<Defined>(
+ name, file, nullptr, sym.n_value, /*size=*/0,
+ /*isWeakDef=*/false,
+ /*isExternal=*/false, /*isPrivateExtern=*/false,
+ /*includeInSymtab=*/true,
+ /*isReferencedDynamically=*/false, sym.n_desc & N_NO_DEAD_STRIP);
defined->cold = isCold;
return defined;
}
template <class NList>
macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,
- const char *strtab) {
- StringRef name = StringRef(strtab + sym.n_strx);
+ CachedHashStringRef hashedName) {
+ StringRef name = hashedName.val();
uint8_t type = sym.n_type & N_TYPE;
bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
switch (type) {
case N_UNDF:
return sym.n_value == 0
- ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
- : symtab->addCommon(name, this, sym.n_value,
+ ? symtab->addUndefined(hashedName, this,
+ sym.n_desc & N_WEAK_REF)
+ : symtab->addCommon(hashedName, this, sym.n_value,
1 << GET_COMM_ALIGN(sym.n_desc),
isPrivateExtern);
case N_ABS:
- return createAbsolute(sym, this, name, forceHidden);
+ return createAbsolute(sym, this, hashedName, forceHidden);
case N_INDR: {
// Not much point in making local aliases -- relocs in the current file can
// just refer to the actual symbol itself. ld64 ignores these symbols too.
if (!(sym.n_type & N_EXT))
return nullptr;
- StringRef aliasedName = StringRef(strtab + sym.n_value);
+ // The aliased name is at the string table offset stored in n_value, so it
+ // is the one thing here that is not the symbol's own name.
+ StringRef aliasedName = name.data() + (sym.n_value - sym.n_strx);
// isPrivateExtern is the only symbol flag that has an impact on the final
// aliased symbol.
- auto *alias = make<AliasSymbol>(this, name, aliasedName, isPrivateExtern);
+ auto *alias =
+ makeThreadLocal<AliasSymbol>(this, name, aliasedName, isPrivateExtern);
aliases.push_back(alias);
return alias;
}
@@ -857,16 +879,20 @@ template <class NList> static bool isUndef(const NList &sym) {
return (sym.n_type & N_TYPE) == N_UNDF && sym.n_value == 0;
}
+template <class NList> static bool nonSectionSymbolNeedsSymtab(const NList &sym) {
+ uint8_t type = sym.n_type & N_TYPE;
+ return type != N_INDR && (type != N_ABS || (sym.n_type & N_EXT));
+}
+
template <class LP>
-void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
- ArrayRef<typename LP::nlist> nList,
- const char *strtab, bool subsectionsViaSymbols) {
+void ObjFile::parseSymbolsPrepare(ArrayRef<typename LP::section> sectionHeaders,
+ ArrayRef<typename LP::nlist> nList,
+ const char *strtab,
+ bool subsectionsViaSymbols) {
using NList = typename LP::nlist;
- // Groups indices of the symbols by the sections that contain them.
- std::vector<std::vector<uint32_t>> symbolsBySection(sections.size());
+ symbolsBySection.resize(sections.size());
symbols.resize(nList.size());
- SmallVector<unsigned, 32> undefineds;
for (uint32_t i = 0; i < nList.size(); ++i) {
const NList &sym = nList[i];
@@ -887,18 +913,45 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
"] greater than the total number of sections [" +
Twine(sections.size()) + "]");
}
- Subsections &subsections = sections[sym.n_sect - 1]->subsections;
// parseSections() may have chosen not to parse this section.
- if (subsections.empty())
+ if (sections[sym.n_sect - 1]->subsections.empty())
continue;
symbolsBySection[sym.n_sect - 1].push_back(i);
} else if (isUndef(sym)) {
- undefineds.push_back(i);
+ undefineds.push_back({CachedHashStringRef(strtab + sym.n_strx), i});
+ } else if (nonSectionSymbolNeedsSymtab(sym)) {
+ nonSectionSymbols.push_back(
+ {CachedHashStringRef(strtab + sym.n_strx), i});
} else {
- symbols[i] = parseNonSectionSymbol(sym, strtab);
+ symbols[i] =
+ parseNonSectionSymbol(sym, CachedHashStringRef(strtab + sym.n_strx));
}
}
+ for (size_t i = 0; i < sections.size(); ++i) {
+ if (sections[i]->subsections.empty() || sections[i]->doneSplitting)
+ continue;
+ llvm::stable_sort(symbolsBySection[i], [&](uint32_t lhs, uint32_t rhs) {
+ if (nList[lhs].n_value == nList[rhs].n_value &&
+ nList[lhs].n_type & N_EXT && nList[rhs].n_type & N_EXT)
+ return !(nList[lhs].n_desc & N_WEAK_DEF) &&
+ (nList[rhs].n_desc & N_WEAK_DEF);
+ return nList[lhs].n_value < nList[rhs].n_value;
+ });
+ }
+
+ auto addDefined = [&](uint32_t symIndex, InputSection *isec, uint64_t value,
+ uint64_t size) {
+ const NList &sym = nList[symIndex];
+ StringRef name = strtab + sym.n_strx;
+ if (isExternalDefined(sym, isec, name))
+ pendingDefineds.push_back(
+ {CachedHashStringRef(name), symIndex, isec, value, size});
+ else
+ symbols[symIndex] = createDefined(sym, name, isec, value, size,
+ forceHidden, /*hashedName=*/nullptr);
+ };
+
for (size_t i = 0; i < sections.size(); ++i) {
Subsections &subsections = sections[i]->subsections;
if (subsections.empty())
@@ -923,30 +976,16 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
" at misaligned offset");
continue;
}
- symbols[symIndex] = createDefined(sym, name, isec, 0, isec->getSize(),
- forceHidden, symbols[symIndex]);
+ addDefined(symIndex, isec, 0, isec->getSize());
}
continue;
}
sections[i]->doneSplitting = true;
- auto getSymName = [strtab](const NList& sym) -> StringRef {
- return StringRef(strtab + sym.n_strx);
- };
-
- // Calculate symbol sizes and create subsections by splitting the sections
- // along symbol boundaries.
- // We populate subsections by repeatedly splitting the last (highest
- // address) subsection.
- llvm::stable_sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
- // Put extern weak symbols after other symbols at the same address so
- // that weak symbol coalescing works correctly. See
- // SymbolTable::addDefined() for details.
- if (nList[lhs].n_value == nList[rhs].n_value &&
- nList[lhs].n_type & N_EXT && nList[rhs].n_type & N_EXT)
- return !(nList[lhs].n_desc & N_WEAK_DEF) && (nList[rhs].n_desc & N_WEAK_DEF);
- return nList[lhs].n_value < nList[rhs].n_value;
- });
+ // Create subsections by splitting the section along symbol boundaries.
+ // symbolIndices was sorted by address in parseSymbolsPrepare(). We
+ // populate subsections by repeatedly splitting the last (highest address)
+ // subsection.
size_t sameAddrGroupIdx = 0;
bool sameAddrHasRegularSymbol = false;
for (size_t j = 0; j < symbolIndices.size(); ++j) {
@@ -964,7 +1003,6 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
++sameAddrGroupIdx;
}
}
- StringRef name = getSymName(sym);
Subsection &subsec = subsections.back();
InputSection *isec = subsec.isec;
@@ -987,14 +1025,12 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
if (!subsectionsViaSymbols || symbolOffset == 0 || isInteriorAltEntry ||
!isa<ConcatInputSection>(isec)) {
isec->hasAltEntry = symbolOffset != 0;
- symbols[symIndex] =
- createDefined(sym, name, isec, symbolOffset, symbolSize,
- forceHidden, symbols[symIndex]);
+ addDefined(symIndex, isec, symbolOffset, symbolSize);
continue;
}
auto *concatIsec = cast<ConcatInputSection>(isec);
- auto *nextIsec = make<ConcatInputSection>(*concatIsec);
+ auto *nextIsec = makeThreadLocal<ConcatInputSection>(*concatIsec);
nextIsec->wasCoalesced = false;
if (isZeroFill(isec->getFlags())) {
// Zero-fill sections have NULL data.data() non-zero data.size()
@@ -1007,9 +1043,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
// By construction, the symbol will be at offset zero in the new
// subsection.
- symbols[symIndex] =
- createDefined(sym, name, nextIsec, /*value=*/0, symbolSize,
- forceHidden, symbols[symIndex]);
+ addDefined(symIndex, nextIsec, /*value=*/0, symbolSize);
// TODO: ld64 appears to preserve the original alignment as well as each
// subsection's offset from the last aligned address. We should consider
// emulating that behavior.
@@ -1018,14 +1052,33 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
}
}
+}
+
+// The half that adds this file's symbols to the symbol table. Everything
+// parseSymbolsPrepare() left for here happens in the same order it always has:
+// non-section symbols, then section symbols, then undefined symbols.
+template <class LP>
+void ObjFile::parseSymbols(ArrayRef<typename LP::nlist> nList) {
+ for (const PendingSymbol &p : nonSectionSymbols)
+ symbols[p.symIndex] = parseNonSectionSymbol(nList[p.symIndex], p.name);
+ nonSectionSymbols = {};
+
+ for (const PendingDefined &p : pendingDefineds) {
+ symbols[p.symIndex] =
+ createDefined(nList[p.symIndex], p.name.val(), p.isec, p.value, p.size,
+ forceHidden, &p.name, symbols[p.symIndex]);
+ }
+ pendingDefineds = {};
+
// Undefined symbols can trigger recursive fetch from Archives due to
// LazySymbols. Process defined symbols first so that the relative order
// between a defined symbol and an undefined symbol does not change the
// symbol resolution behavior. In addition, a set of interconnected symbols
// will all be resolved to the same file, instead of being resolved to
// different files.
- for (unsigned i : undefineds)
- symbols[i] = parseNonSectionSymbol(nList[i], strtab);
+ for (const PendingSymbol &p : undefineds)
+ symbols[p.symIndex] = parseNonSectionSymbol(nList[p.symIndex], p.name);
+ undefineds = {};
}
OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
@@ -1037,7 +1090,7 @@ OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
sectName.take_front(16),
/*flags=*/0, /*addr=*/0));
Section §ion = *sections.back();
- ConcatInputSection *isec = make<ConcatInputSection>(section, data);
+ ConcatInputSection *isec = makeThreadLocal<ConcatInputSection>(section, data);
isec->live = true;
section.subsections.push_back({0, isec});
}
@@ -1081,6 +1134,14 @@ static bool isUnwindSection(const Section &sec) {
}
template <class LP> void ObjFile::parse() {
+ parsePrepare<LP>();
+ parseFinish<LP>();
+}
+
+// The per-file half of parsing: header checks, sections, and grouping this
+// file's symbols by section. Touches no global state, so it can be run for many
+// files at once.
+template <class LP> void ObjFile::parsePrepare() {
using Header = typename LP::mach_header;
using SegmentCommand = typename LP::segment_command;
using SectionHeader = typename LP::section;
@@ -1096,10 +1157,9 @@ template <class LP> void ObjFile::parse() {
return;
// We will resolve LC linker options once all native objects are loaded after
- // LTO is finished.
- SmallVector<StringRef, 4> LCLinkerOptions;
- parseLinkerOptions<LP>(LCLinkerOptions);
- unprocessedLCLinkerOptions.append(LCLinkerOptions);
+ // LTO is finished. parseFinish() hands them on, so that they end up in file
+ // order no matter how many files this runs for at once.
+ parseLinkerOptions<LP>(lcLinkerOptions);
ArrayRef<SectionHeader> sectionHeaders;
if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
@@ -1116,12 +1176,44 @@ template <class LP> void ObjFile::parse() {
c->nsyms);
const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/240983
More information about the llvm-branch-commits
mailing list