[lld] [lld][macho] Ignore cstrings in bp orderer (PR #165757)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 30 11:06:42 PDT 2025


https://github.com/ellishg created https://github.com/llvm/llvm-project/pull/165757

`cstring` sections are laid out by `{Deduplicated}CStringSection::finalizeContents()` and ignores the order determined by `runBalancedPartitioning()`.

https://github.com/llvm/llvm-project/blob/e63f0f50fae479b4eaec98ac97de0745735a90b7/lld/MachO/SyntheticSections.cpp#L1729-L1746

Do not consider `cstring` sections or any section already finalized in `runBalancedPartitioning()`. This may result in fewer sections passed to BP, which could result in faster linktime and better results. In some large binaries, I didn't not see a meaningful difference in compressed size.

>From 66369a52c8e309451465cc2fbf31ca56b86fff66 Mon Sep 17 00:00:00 2001
From: Ellis Hoag <ellishoag at meta.com>
Date: Thu, 30 Oct 2025 10:38:37 -0700
Subject: [PATCH] [lld][macho] Ignore cstrings in bp orderer

---
 lld/MachO/BPSectionOrderer.cpp      | 4 ++++
 lld/test/MachO/bp-section-orderer.s | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/lld/MachO/BPSectionOrderer.cpp b/lld/MachO/BPSectionOrderer.cpp
index d50abc22fc6c1..328c33e6cfb65 100644
--- a/lld/MachO/BPSectionOrderer.cpp
+++ b/lld/MachO/BPSectionOrderer.cpp
@@ -118,6 +118,10 @@ DenseMap<const InputSection *, int> lld::macho::runBalancedPartitioning(
         auto *isec = subsec.isec;
         if (!isec || isec->data.empty() || !isec->data.data())
           continue;
+        // CString section order is handled by
+        // {Deduplicated}CStringSection::finalizeContents()
+        if (isa<CStringInputSection>(isec) || isec->isFinal)
+          continue;
         // ConcatInputSections are entirely live or dead, so the offset is
         // irrelevant.
         if (isa<ConcatInputSection>(isec) && !isec->isLive(0))
diff --git a/lld/test/MachO/bp-section-orderer.s b/lld/test/MachO/bp-section-orderer.s
index 90924e5797b64..d7de90d6cd7b3 100644
--- a/lld/test/MachO/bp-section-orderer.s
+++ b/lld/test/MachO/bp-section-orderer.s
@@ -106,6 +106,11 @@ r3:
 r4:
   .quad s2
 
+# cstrings are ignored by runBalancedPartitioning()
+.cstring
+cstr:
+  .asciz "this is cstr"
+
 .bss
 bss0:
   .zero 10



More information about the llvm-commits mailing list