[llvm-branch-commits] [lld] [lld-macho] Collect S_ATTR_LIVE_SUPPORT sections with the markLive roots (PR #242230)

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 13 02:22:15 PST 2026


https://github.com/int3 updated https://github.com/llvm/llvm-project/pull/242230

>From 2f7f341fcda4c3ad9abf4c0b07bdadd37f6f2ebf Mon Sep 17 00:00:00 2001
From: Jez Ng <me at jezng.com>
Date: Thu, 12 Nov 2026 16:31:24 -0800
Subject: [PATCH 1/4] [lld-macho] Collect S_ATTR_LIVE_SUPPORT sections with the
 markLive roots

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_0151E9Zq69e8fCB8jwD3kGLe
---
 lld/MachO/MarkLive.cpp | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index 8e34f88684585..afa4a16c4f5fb 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -46,7 +46,8 @@ class MarkLive {
   // 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 void
+  markTransitively(ArrayRef<ConcatInputSection *> liveSupport) = 0;
   virtual ~MarkLive() = default;
 };
 
@@ -96,7 +97,7 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
     addSym(s, nullptr, shards[shard]);
   }
   void flush(size_t shard) override;
-  void markTransitively() override;
+  void markTransitively(ArrayRef<ConcatInputSection *> liveSupport) override;
 
 private:
   // Several threads call these at once, each passing its own worklist to
@@ -283,7 +284,8 @@ void MarkLiveImpl<RecordWhyLive>::flush(size_t shard) {
 }
 
 template <bool RecordWhyLive>
-void MarkLiveImpl<RecordWhyLive>::markTransitively() {
+void MarkLiveImpl<RecordWhyLive>::markTransitively(
+    ArrayRef<ConcatInputSection *> liveSupport) {
   for (size_t shard = 0; shard != shards.size(); ++shard)
     flush(shard);
   do {
@@ -293,12 +295,10 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively() {
     // S_ATTR_LIVE_SUPPORT sections are live if they point _to_ a live
     // section. Process them in a second pass.
     parallelForChunks(
-        ArrayRef(inputSections), 256,
+        liveSupport, 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))
+            if (isec->isLive(0))
               continue;
 
             for (const Relocation &r : isec->relocs) {
@@ -396,10 +396,19 @@ void markLive() {
   if (auto *stubBinder =
           dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder")))
     marker->addSym(stubBinder);
+  // S_ATTR_LIVE_SUPPORT sections are rescanned after every drain, but most
+  // links have few or none, so collect them here rather than filtering all of
+  // inputSections each time. Their order only affects which live referent
+  // -why_live reports, which is deterministic only with --threads=1 anyway.
+  std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards(
+      parallel::getThreadCount());
   parallelForChunks(
       ArrayRef(inputSections), 256,
       [&](size_t shard, ArrayRef<ConcatInputSection *> chunk) {
         for (ConcatInputSection *isec : chunk) {
+          if (isec->getFlags() & S_ATTR_LIVE_SUPPORT)
+            liveSupportShards[shard].push_back(isec);
+
           // Sections marked no_dead_strip
           if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) {
             marker->enqueue(isec, 0, shard);
@@ -420,7 +429,10 @@ void markLive() {
   for (ConcatInputSection *isec : in.initOffsets->inputs())
     marker->enqueue(isec, 0);
 
-  marker->markTransitively();
+  SmallVector<ConcatInputSection *, 0> liveSupport;
+  for (SmallVector<ConcatInputSection *, 0> &shard : liveSupportShards)
+    liveSupport.append(shard.begin(), shard.end());
+  marker->markTransitively(liveSupport);
 }
 
 } // namespace lld::macho

>From 78b34b41bfa8577766f84745a3f639114f66ef65 Mon Sep 17 00:00:00 2001
From: Jez Ng <me at jezng.com>
Date: Fri, 13 Nov 2026 00:36:46 -0800
Subject: [PATCH 2/4] Add live-support sections through
 MarkLive::addLiveSupport

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_0151E9ZpY6Krqg1C84uMobNg
---
 lld/MachO/MarkLive.cpp | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index afa4a16c4f5fb..1df9f0ec56867 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -45,9 +45,9 @@ class MarkLive {
   // optimization; flushing shortly after the worklist is populated means its
   // entries are likely still in cache.
   virtual void flush(size_t shard) = 0;
+  virtual void addLiveSupport(ConcatInputSection *isec, size_t shard) = 0;
   // Flushes all the shards into `nextLevel` before doing the marking.
-  virtual void
-  markTransitively(ArrayRef<ConcatInputSection *> liveSupport) = 0;
+  virtual void markTransitively() = 0;
   virtual ~MarkLive() = default;
 };
 
@@ -89,7 +89,9 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
       std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>;
   using Worklist = SmallVector<WorklistEntry *, 0>;
 
-  MarkLiveImpl() : shards(parallel::getThreadCount()) {}
+  MarkLiveImpl()
+      : shards(parallel::getThreadCount()),
+        liveSupportShards(parallel::getThreadCount()) {}
   void enqueue(InputSection *isec, uint64_t off, size_t shard) override {
     enqueue(isec, off, nullptr, shards[shard]);
   }
@@ -97,7 +99,10 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
     addSym(s, nullptr, shards[shard]);
   }
   void flush(size_t shard) override;
-  void markTransitively(ArrayRef<ConcatInputSection *> liveSupport) override;
+  void addLiveSupport(ConcatInputSection *isec, size_t shard) override {
+    liveSupportShards[shard].push_back(isec);
+  }
+  void markTransitively() override;
 
 private:
   // Several threads call these at once, each passing its own worklist to
@@ -116,6 +121,7 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
   WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const;
 
   std::vector<Worklist> shards;
+  std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards;
   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
@@ -284,10 +290,12 @@ void MarkLiveImpl<RecordWhyLive>::flush(size_t shard) {
 }
 
 template <bool RecordWhyLive>
-void MarkLiveImpl<RecordWhyLive>::markTransitively(
-    ArrayRef<ConcatInputSection *> liveSupport) {
+void MarkLiveImpl<RecordWhyLive>::markTransitively() {
   for (size_t shard = 0; shard != shards.size(); ++shard)
     flush(shard);
+  SmallVector<ConcatInputSection *, 0> liveSupport;
+  for (SmallVector<ConcatInputSection *, 0> &s : liveSupportShards)
+    liveSupport.append(s);
   do {
     // Mark things reachable from GC roots as live.
     drain();
@@ -295,7 +303,7 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively(
     // S_ATTR_LIVE_SUPPORT sections are live if they point _to_ a live
     // section. Process them in a second pass.
     parallelForChunks(
-        liveSupport, 256,
+        ArrayRef(liveSupport), 256,
         [&](size_t shard, ArrayRef<ConcatInputSection *> chunk) {
           for (ConcatInputSection *isec : chunk) {
             if (isec->isLive(0))
@@ -396,18 +404,12 @@ void markLive() {
   if (auto *stubBinder =
           dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder")))
     marker->addSym(stubBinder);
-  // S_ATTR_LIVE_SUPPORT sections are rescanned after every drain, but most
-  // links have few or none, so collect them here rather than filtering all of
-  // inputSections each time. Their order only affects which live referent
-  // -why_live reports, which is deterministic only with --threads=1 anyway.
-  std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards(
-      parallel::getThreadCount());
   parallelForChunks(
       ArrayRef(inputSections), 256,
       [&](size_t shard, ArrayRef<ConcatInputSection *> chunk) {
         for (ConcatInputSection *isec : chunk) {
           if (isec->getFlags() & S_ATTR_LIVE_SUPPORT)
-            liveSupportShards[shard].push_back(isec);
+            marker->addLiveSupport(isec, shard);
 
           // Sections marked no_dead_strip
           if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) {
@@ -429,10 +431,7 @@ void markLive() {
   for (ConcatInputSection *isec : in.initOffsets->inputs())
     marker->enqueue(isec, 0);
 
-  SmallVector<ConcatInputSection *, 0> liveSupport;
-  for (SmallVector<ConcatInputSection *, 0> &shard : liveSupportShards)
-    liveSupport.append(shard.begin(), shard.end());
-  marker->markTransitively(liveSupport);
+  marker->markTransitively();
 }
 
 } // namespace lld::macho

>From 5039b7d621d54d0d82c9c7546229686abf9a76e3 Mon Sep 17 00:00:00 2001
From: Jez Ng <me at jezng.com>
Date: Fri, 13 Nov 2026 01:02:11 -0800
Subject: [PATCH 3/4] Make addLiveSupport non-virtual

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_0151E9ZpY6Krqg1C84uMobNg
---
 lld/MachO/MarkLive.cpp | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index 1df9f0ec56867..12d51dbb06d66 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -45,10 +45,16 @@ class MarkLive {
   // optimization; flushing shortly after the worklist is populated means its
   // entries are likely still in cache.
   virtual void flush(size_t shard) = 0;
-  virtual void addLiveSupport(ConcatInputSection *isec, size_t shard) = 0;
+  void addLiveSupport(ConcatInputSection *isec, size_t shard) {
+    liveSupportShards[shard].push_back(isec);
+  }
   // Flushes all the shards into `nextLevel` before doing the marking.
   virtual void markTransitively() = 0;
   virtual ~MarkLive() = default;
+
+protected:
+  std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards{
+      parallel::getThreadCount()};
 };
 
 // Sets `flag` and returns true if no other thread set it first.
@@ -89,9 +95,7 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
       std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>;
   using Worklist = SmallVector<WorklistEntry *, 0>;
 
-  MarkLiveImpl()
-      : shards(parallel::getThreadCount()),
-        liveSupportShards(parallel::getThreadCount()) {}
+  MarkLiveImpl() : shards(parallel::getThreadCount()) {}
   void enqueue(InputSection *isec, uint64_t off, size_t shard) override {
     enqueue(isec, off, nullptr, shards[shard]);
   }
@@ -99,9 +103,6 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
     addSym(s, nullptr, shards[shard]);
   }
   void flush(size_t shard) override;
-  void addLiveSupport(ConcatInputSection *isec, size_t shard) override {
-    liveSupportShards[shard].push_back(isec);
-  }
   void markTransitively() override;
 
 private:
@@ -121,7 +122,6 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
   WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const;
 
   std::vector<Worklist> shards;
-  std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards;
   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

>From c5817383b2728639006b4675bf1fd811602cd98d Mon Sep 17 00:00:00 2001
From: Jez Ng <me at jezng.com>
Date: Fri, 13 Nov 2026 01:15:44 -0800
Subject: [PATCH 4/4] Store the thread count in MarkLive::numThreads

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_0151E9ZpY6Krqg1C84uMobNg
---
 lld/MachO/MarkLive.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index 12d51dbb06d66..053e60f3cf21c 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -53,8 +53,9 @@ class MarkLive {
   virtual ~MarkLive() = default;
 
 protected:
+  const size_t numThreads = parallel::getThreadCount();
   std::vector<SmallVector<ConcatInputSection *, 0>> liveSupportShards{
-      parallel::getThreadCount()};
+      numThreads};
 };
 
 // Sets `flag` and returns true if no other thread set it first.
@@ -95,7 +96,7 @@ template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
       std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>;
   using Worklist = SmallVector<WorklistEntry *, 0>;
 
-  MarkLiveImpl() : shards(parallel::getThreadCount()) {}
+  MarkLiveImpl() : shards(numThreads) {}
   void enqueue(InputSection *isec, uint64_t off, size_t shard) override {
     enqueue(isec, off, nullptr, shards[shard]);
   }
@@ -214,7 +215,6 @@ void MarkLiveImpl<RecordWhyLive>::visit(WorklistEntry *entry,
 // their stacks to the next level. The last join makes every live bit visible
 // to the live-support scan, which reads them with plain loads.
 template <bool RecordWhyLive> void MarkLiveImpl<RecordWhyLive>::drain() {
-  const size_t numThreads = parallel::getThreadCount();
   // The minimum number of visits a worker makes in a level before it may hand
   // back its stack. This keeps levels with fewer sections than workers from
   // ending after a visit or two, which would result in a lot of fork-join



More information about the llvm-branch-commits mailing list