https://github.com/int3 updated https://github.com/llvm/llvm-project/pull/241684
>From 5a8cc0cac338cce62d5a7d8af23be06a094674c1 Mon Sep 17 00:00:00 2001 From: Jez Ng <[email protected]> Date: Mon, 9 Nov 2026 12:55:24 -0800 Subject: [PATCH] [lld-macho] Collect markLive roots in parallel With the traversal parallel, most of markLive's remaining time goes to the serial loops around it: finding the GC roots, which visits every global symbol, every object file's local symbols and every input section, and the S_ATTR_LIVE_SUPPORT scan over every input section. Run those loops on all threads. The type-erased MarkLive interface now takes a shard index with each root: each thread adds the roots it finds to its own shard, and moves them to nextLevel after every chunk, so with one thread the order is unchanged. The live-support scan does the same with the sections it marks live, reading the live bits of sections that other threads may be marking at the same time through the thread-safe isLive(). A live-support section that refers only to another one marked by the same scan may be found by the next iteration of the loop around it instead; the final live set is the same. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- lld/MachO/MarkLive.cpp | 217 +++++++++++++++++---------- lld/test/MachO/dead-strip-parallel.s | 34 +++++ 2 files changed, 172 insertions(+), 79 deletions(-) create mode 100644 lld/test/MachO/dead-strip-parallel.s diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp index b76d5fb8fca59..8e34f88684585 100644 --- a/lld/MachO/MarkLive.cpp +++ b/lld/MachO/MarkLive.cpp @@ -38,8 +38,14 @@ struct WhyLiveEntry { // graph. class MarkLive { public: - virtual void enqueue(InputSection *isec, uint64_t off) = 0; - virtual void addSym(Symbol *s) = 0; + // These add roots to worklist `shard`. We have one shard per thread. + virtual void enqueue(InputSection *isec, uint64_t off, size_t shard = 0) = 0; + virtual void addSym(Symbol *s, size_t shard = 0) = 0; + // Moves the roots in `shard` into `nextLevel`. Exposed as a performance + // optimization; flushing shortly after the worklist is populated means its + // entries are likely still in cache. + virtual void flush(size_t shard) = 0; + // Flushes all the shards into `nextLevel` before doing the marking. virtual void markTransitively() = 0; virtual ~MarkLive() = default; }; @@ -53,6 +59,23 @@ static bool tryClaim(bool &flag) { !atomicFlag.exchange(true, std::memory_order_relaxed); } +// Splits `items` into chunks of `chunkSize` and calls fn(worker, chunk) for +// each chunk on all threads. `worker` is below parallel::getThreadCount() and +// no two threads use the same one at once, so fn can use it to index +// per-thread state. Chunks are claimed in increasing order. +template <class T, class Fn> +static void parallelForChunks(ArrayRef<T> items, size_t chunkSize, Fn fn) { + const size_t numThreads = parallel::getThreadCount(); + // Each worker starts with the chunk at its own index and then claims the + // chunks after the first numThreads. + std::atomic<size_t> next{numThreads * chunkSize}; + parallelFor(0, numThreads, [&](size_t worker) { + for (size_t begin = worker * chunkSize; begin < items.size(); + begin = next.fetch_add(chunkSize, std::memory_order_relaxed)) + fn(worker, items.slice(begin, std::min(chunkSize, items.size() - begin))); + }); +} + // Marks liveness on as many threads as are available. -why_live output is // deterministic only with --threads=1. template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive { @@ -65,10 +88,14 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive { std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>; using Worklist = SmallVector<WorklistEntry *, 0>; - void enqueue(InputSection *isec, uint64_t off) override { - enqueue(isec, off, nullptr, nextLevel); + MarkLiveImpl() : shards(parallel::getThreadCount()) {} + void enqueue(InputSection *isec, uint64_t off, size_t shard) override { + enqueue(isec, off, nullptr, shards[shard]); } - void addSym(Symbol *s) override { addSym(s, nullptr, nextLevel); } + void addSym(Symbol *s, size_t shard) override { + addSym(s, nullptr, shards[shard]); + } + void flush(size_t shard) override; void markTransitively() override; private: @@ -87,6 +114,8 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive { const InputSection *getInputSection(const WorklistEntry *) const; WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const; + std::vector<Worklist> shards; + std::mutex nextLevelMutex; // Sections that have been marked live and that the next level of drain() will // visit. Literal sections cannot contain references to other sections, so we // only store ConcatInputSections here. @@ -243,35 +272,53 @@ MarkLiveImpl<RecordWhyLive>::makeEntry( } } +template <bool RecordWhyLive> +void MarkLiveImpl<RecordWhyLive>::flush(size_t shard) { + Worklist &worklist = shards[shard]; + if (worklist.empty()) + return; + std::lock_guard<std::mutex> lock(nextLevelMutex); + nextLevel.append(worklist.begin(), worklist.end()); + worklist.clear(); +} + template <bool RecordWhyLive> void MarkLiveImpl<RecordWhyLive>::markTransitively() { + for (size_t shard = 0; shard != shards.size(); ++shard) + flush(shard); do { // Mark things reachable from GC roots as live. drain(); // S_ATTR_LIVE_SUPPORT sections are live if they point _to_ a live // section. Process them in a second pass. - for (ConcatInputSection *isec : inputSections) { - // FIXME: Check if copying all S_ATTR_LIVE_SUPPORT sections into a - // separate vector and only walking that here is faster. - if (!(isec->getFlags() & S_ATTR_LIVE_SUPPORT) || isec->live) - continue; - - for (const Relocation &r : isec->relocs) { - if (auto *s = dyn_cast<Symbol *>(r.referent)) { - if (s->isLive()) { - InputSection *referentIsec = nullptr; - if (auto *d = dyn_cast<Defined>(s)) - referentIsec = d->isec(); - enqueue(isec, 0, makeEntry(referentIsec, nullptr), nextLevel); + parallelForChunks( + ArrayRef(inputSections), 256, + [&](size_t shard, ArrayRef<ConcatInputSection *> chunk) { + for (ConcatInputSection *isec : chunk) { + // FIXME: Check if copying all S_ATTR_LIVE_SUPPORT sections into a + // separate vector and only walking that here is faster. + if (!(isec->getFlags() & S_ATTR_LIVE_SUPPORT) || isec->isLive(0)) + continue; + + for (const Relocation &r : isec->relocs) { + InputSection *referentIsec = nullptr; + if (auto *s = dyn_cast<Symbol *>(r.referent)) { + if (!s->isLive()) + continue; + if (auto *d = dyn_cast<Defined>(s)) + referentIsec = d->isec(); + } else { + referentIsec = cast<InputSection *>(r.referent); + if (!referentIsec->isLive(r.getAddend())) + continue; + } + enqueue(isec, 0, makeEntry(referentIsec, nullptr), shards[shard]); + break; + } } - } else { - auto *referentIsec = cast<InputSection *>(r.referent); - if (referentIsec->isLive(r.getAddend())) - enqueue(isec, 0, makeEntry(referentIsec, nullptr), nextLevel); - } - } - } + flush(shard); + }); // S_ATTR_LIVE_SUPPORT could have marked additional sections live, // which in turn could mark additional S_ATTR_LIVE_SUPPORT sections live. @@ -293,70 +340,82 @@ void markLive() { // Add GC roots. if (config->entry) marker->addSym(config->entry); - for (Symbol *sym : symtab->getSymbols()) { - if (auto *defined = dyn_cast<Defined>(sym)) { - // -exported_symbol(s_list) - if (!config->exportedSymbols.empty() && - config->exportedSymbols.match(defined->getName())) { - // NOTE: Even though exporting private externs is an ill-defined - // operation, we are purposely not checking for privateExtern in - // order to follow ld64's behavior of treating all exported private - // extern symbols as live, irrespective of whether they are autohide. - marker->addSym(defined); - continue; - } + parallelForChunks( + symtab->getSymbols(), 256, [&](size_t shard, ArrayRef<Symbol *> chunk) { + for (Symbol *sym : chunk) { + auto *defined = dyn_cast<Defined>(sym); + if (!defined) + continue; + // -exported_symbol(s_list) + if (!config->exportedSymbols.empty() && + config->exportedSymbols.match(defined->getName())) { + // NOTE: Even though exporting private externs is an ill-defined + // operation, we are purposely not checking for privateExtern in + // order to follow ld64's behavior of treating all exported private + // extern symbols as live, irrespective of whether they are + // autohide. + marker->addSym(defined, shard); + continue; + } - // public symbols explicitly marked .no_dead_strip - if (defined->referencedDynamically || defined->noDeadStrip) { - marker->addSym(defined); - continue; - } + // public symbols explicitly marked .no_dead_strip + if (defined->referencedDynamically || defined->noDeadStrip) { + marker->addSym(defined, shard); + continue; + } - // FIXME: When we implement these flags, make symbols from them GC - // roots: - // * -reexported_symbol(s_list) - // * -alias_list - // * -init - - // In dylibs and bundles and in executables with -export_dynamic, - // all external functions are GC roots. - bool externsAreRoots = - config->outputType != MH_EXECUTE || config->exportDynamic; - if (externsAreRoots && !defined->privateExtern) { - marker->addSym(defined); - continue; - } - } - } + // FIXME: When we implement these flags, make symbols from them GC + // roots: + // * -reexported_symbol(s_list) + // * -alias_list + // * -init + + // In dylibs and bundles and in executables with -export_dynamic, + // all external functions are GC roots. + bool externsAreRoots = + config->outputType != MH_EXECUTE || config->exportDynamic; + if (externsAreRoots && !defined->privateExtern) + marker->addSym(defined, shard); + } + marker->flush(shard); + }); // -u symbols for (Symbol *sym : config->explicitUndefineds) marker->addSym(sym); // local symbols explicitly marked .no_dead_strip - for (const InputFile *file : inputFiles) - if (auto *objFile = dyn_cast<ObjFile>(file)) - for (Symbol *sym : objFile->symbols) - if (auto *defined = dyn_cast_or_null<Defined>(sym)) - if (!defined->isExternal() && defined->noDeadStrip) - marker->addSym(defined); + ArrayRef<InputFile *> files = inputFiles.getArrayRef(); + parallelForChunks(files, 1, [&](size_t shard, ArrayRef<InputFile *> chunk) { + for (InputFile *file : chunk) + if (auto *objFile = dyn_cast<ObjFile>(file)) + for (Symbol *sym : objFile->symbols) + if (auto *defined = dyn_cast_or_null<Defined>(sym)) + if (!defined->isExternal() && defined->noDeadStrip) + marker->addSym(defined, shard); + marker->flush(shard); + }); if (auto *stubBinder = dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder"))) marker->addSym(stubBinder); - for (ConcatInputSection *isec : inputSections) { - // Sections marked no_dead_strip - if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) { - marker->enqueue(isec, 0); - continue; - } + parallelForChunks( + ArrayRef(inputSections), 256, + [&](size_t shard, ArrayRef<ConcatInputSection *> chunk) { + for (ConcatInputSection *isec : chunk) { + // Sections marked no_dead_strip + if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) { + marker->enqueue(isec, 0, shard); + continue; + } - // mod_init_funcs, mod_term_funcs sections - if (sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS || - sectionType(isec->getFlags()) == S_MOD_TERM_FUNC_POINTERS) { - assert(!config->emitInitOffsets || - sectionType(isec->getFlags()) != S_MOD_INIT_FUNC_POINTERS); - marker->enqueue(isec, 0); - continue; - } - } + // mod_init_funcs, mod_term_funcs sections + if (sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS || + sectionType(isec->getFlags()) == S_MOD_TERM_FUNC_POINTERS) { + assert(!config->emitInitOffsets || + sectionType(isec->getFlags()) != S_MOD_INIT_FUNC_POINTERS); + marker->enqueue(isec, 0, shard); + } + } + marker->flush(shard); + }); for (ConcatInputSection *isec : in.initOffsets->inputs()) marker->enqueue(isec, 0); diff --git a/lld/test/MachO/dead-strip-parallel.s b/lld/test/MachO/dead-strip-parallel.s new file mode 100644 index 0000000000000..b69364cde585e --- /dev/null +++ b/lld/test/MachO/dead-strip-parallel.s @@ -0,0 +1,34 @@ +# REQUIRES: x86 +## The symbol table here spans several chunks of the parallel root scan. Check +## that one and eight threads mark the same sections live. +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %s -o %t.o +# RUN: %lld -dead_strip --threads=1 %t.o -o %t1 -final_output out +# RUN: %lld -dead_strip --threads=8 %t.o -o %t8 -final_output out +# RUN: cmp %t1 %t8 +# RUN: llvm-nm %t8 | grep _live | count 1000 +# RUN: llvm-nm %t8 | grep _callee | count 1000 +# RUN: llvm-nm %t8 | FileCheck %s --implicit-check-not=_dead +# CHECK: _main + +.text +.globl _main +_main: + ret + +.macro funcs +.globl _live\@, _dead\@ +.no_dead_strip _live\@ +_live\@: + callq _callee\@ + ret +_callee\@: + ret +_dead\@: + ret +.endm + +.rept 1000 +funcs +.endr + +.subsections_via_symbols _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
