[llvm-branch-commits] [lld] [lld][MachO] Split ObjFile::parse() and parse extracted archive rounds in parallel (PR #240983)

Liza Burakova via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 16 11:51:18 PST 2026


https://github.com/liza371 updated https://github.com/llvm/llvm-project/pull/240983

>From a19d220c6fdb42c92bd3bed961a78dc844220401 Mon Sep 17 00:00:00 2001
From: Liza Burakova <liza at chromium.org>
Date: Mon, 9 Nov 2026 16:56:13 -0500
Subject: [PATCH] [lld][MachO] Split ObjFile::parse() and parse extracted
 archive rounds in parallel

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.
---
 lld/MachO/InputFiles.cpp | 267 +++++++++++++++++++++++++++------------
 lld/MachO/InputFiles.h   |  37 +++++-
 2 files changed, 217 insertions(+), 87 deletions(-)

diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 6041ef7aefe8e8..9f5befb7b44a11 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -358,7 +358,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");
@@ -376,7 +377,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});
       }
@@ -389,8 +390,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});
@@ -398,7 +399,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);
@@ -421,7 +423,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
@@ -454,9 +456,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;
 }
@@ -713,10 +715,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,
@@ -739,7 +752,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;
@@ -779,13 +793,14 @@ static macho::Symbol *createDefined(const NList &sym, StringRef name,
       isWeakDefCanBeHidden = false;
     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);
+    return symtab->addDefined(*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);
@@ -797,52 +812,58 @@ 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;
   }
@@ -861,16 +882,21 @@ 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];
 
@@ -891,18 +917,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())
@@ -927,30 +980,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) {
@@ -968,7 +1007,6 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
           ++sameAddrGroupIdx;
         }
       }
-      StringRef name = getSymName(sym);
       Subsection &subsec = subsections.back();
       InputSection *isec = subsec.isec;
 
@@ -991,14 +1029,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()
@@ -1011,9 +1047,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.
@@ -1021,6 +1055,23 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
       subsections.push_back({sym.n_value - sectionAddr, nextIsec});
     }
   }
+}
+
+// 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
@@ -1028,8 +1079,9 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
   // 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,
@@ -1041,7 +1093,7 @@ OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
                                    sectName.take_front(16),
                                    /*flags=*/0, /*addr=*/0));
   Section &section = *sections.back();
-  ConcatInputSection *isec = make<ConcatInputSection>(section, data);
+  ConcatInputSection *isec = makeThreadLocal<ConcatInputSection>(section, data);
   isec->live = true;
   section.subsections.push_back({0, isec});
 }
@@ -1085,6 +1137,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;
@@ -1100,10 +1160,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)) {
@@ -1120,12 +1179,44 @@ template <class LP> void ObjFile::parse() {
                           c->nsyms);
     const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
     bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
+    parseSymbolsPrepare<LP>(sectionHeaders, nList, strtab,
+                            subsectionsViaSymbols);
+  }
+}
+
+// The half that inserts into the symbol table, and so has to run in file order.
+template <class LP> void ObjFile::parseFinish() {
+  using Header = typename LP::mach_header;
+  using SegmentCommand = typename LP::segment_command;
+  using SectionHeader = typename LP::section;
+  using NList = typename LP::nlist;
+
+  auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
+  auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
+
+  if (!compatArch)
+    return;
+
+  unprocessedLCLinkerOptions.append(lcLinkerOptions);
+  lcLinkerOptions.clear();
+
+  ArrayRef<SectionHeader> sectionHeaders;
+  if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
+    auto *c = reinterpret_cast<const SegmentCommand *>(cmd);
+    sectionHeaders = ArrayRef<SectionHeader>{
+        reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};
+  }
+
+  if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
+    auto *c = reinterpret_cast<const symtab_command *>(cmd);
+    ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
+                          c->nsyms);
+    bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
     if (config->warnMissingSubsectionsViaSymbols && !subsectionsViaSymbols &&
         !sectionHeaders.empty())
       warn(toString(this) + ": missing MH_SUBSECTIONS_VIA_SYMBOLS");
-    parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);
+    parseSymbols<LP>(nList);
   }
-
   // The relocations may refer to the symbols, so we parse them after we have
   // parsed all the symbols.
   //
@@ -2612,13 +2703,23 @@ void macho::extract(InputFile &file, StringRef reason) {
 }
 
 void macho::parsePendingExtracts() {
-  // Parsing appends to pendingExtracts, so index rather than iterate.
-  for (size_t i = 0; i < pendingExtracts.size(); ++i) {
-    ObjFile &f = *pendingExtracts[i];
-    if (target->wordSize == 8)
-      f.parse<LP64>();
-    else
-      f.parse<ILP32>();
+  for (size_t done = 0; done < pendingExtracts.size();) {
+    size_t end = pendingExtracts.size();
+    std::vector<ObjFile *> round(pendingExtracts.begin() + done,
+                                 pendingExtracts.begin() + end);
+    parallelForEach(round, [](ObjFile *f) {
+      if (target->wordSize == 8)
+        f->parsePrepare<LP64>();
+      else
+        f->parsePrepare<ILP32>();
+    });
+    for (ObjFile *f : round) {
+      if (target->wordSize == 8)
+        f->parseFinish<LP64>();
+      else
+        f->parseFinish<ILP32>();
+    }
+    done = end;
   }
   for (ObjFile *f : pendingExtracts)
     for (Symbol *sym : f->symbols)
diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index b0d018bd0315ef..2156ada339750e 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -165,6 +165,11 @@ class ObjFile final : public InputFile {
   ArrayRef<llvm::MachO::data_in_code_entry> getDataInCode() const;
   ArrayRef<uint8_t> getOptimizationHints() const;
   template <class LP> void parse();
+  // parse() split in two. parsePrepare() is the per-file half: it touches no
+  // global state, so it can be run for many files at once. parseFinish() is
+  // the half that inserts into the symbol table, and has to run in file order.
+  template <class LP> void parsePrepare();
+  template <class LP> void parseFinish();
   template <class LP>
   void parseLinkerOptions(llvm::SmallVectorImpl<StringRef> &LinkerOptions);
   void parseDeferredRelocations();
@@ -192,11 +197,35 @@ class ObjFile final : public InputFile {
   template <class LP> void parseLazy();
   template <class SectionHeader> void parseSections(ArrayRef<SectionHeader>);
   template <class LP>
-  void parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
-                    ArrayRef<typename LP::nlist> nList, const char *strtab,
-                    bool subsectionsViaSymbols);
+  void parseSymbolsPrepare(ArrayRef<typename LP::section> sectionHeaders,
+                           ArrayRef<typename LP::nlist> nList,
+                           const char *strtab, bool subsectionsViaSymbols);
+  template <class LP> void parseSymbols(ArrayRef<typename LP::nlist> nList);
+
+  // Filled in by parsePrepare(), consumed by parseFinish(). The symbols that
+  // go through the symbol table are recorded with their name already hashed,
+  // so that the half of parsing that runs one file at a time has nothing left
+  // to do with the name but look it up.
+  llvm::SmallVector<StringRef, 4> lcLinkerOptions;
+  std::vector<std::vector<uint32_t>> symbolsBySection;
+  struct PendingSymbol {
+    llvm::CachedHashStringRef name;
+    uint32_t symIndex;
+  };
+  std::vector<PendingSymbol> undefineds;
+  std::vector<PendingSymbol> nonSectionSymbols;
+  // An external section symbol, and the subsection it was found to be in.
+  struct PendingDefined {
+    llvm::CachedHashStringRef name;
+    uint32_t symIndex;
+    InputSection *isec;
+    uint64_t value;
+    uint64_t size;
+  };
+  std::vector<PendingDefined> pendingDefineds;
   template <class NList>
-  Symbol *parseNonSectionSymbol(const NList &sym, const char *strtab);
+  Symbol *parseNonSectionSymbol(const NList &sym,
+                                llvm::CachedHashStringRef name);
   template <class SectionHeader>
   void parseRelocations(ArrayRef<SectionHeader> sectionHeaders,
                         const SectionHeader &, Section &);



More information about the llvm-branch-commits mailing list