[llvm-branch-commits] [lld] [lld][MachO] Change direct branch targets to ICF bodies (PR #239472)
Ellis Hoag via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 5 21:09:05 PST 2026
https://github.com/ellishg updated https://github.com/llvm/llvm-project/pull/239472
>From 96ee926bcd8be002e94c57d1a6dbccfc928b8bf1 Mon Sep 17 00:00:00 2001
From: Ellis Hoag <ellishoag at meta.com>
Date: Thu, 5 Nov 2026 20:51:09 -0800
Subject: [PATCH] [lld][MachO] Change direct branch targets to ICF bodies
---
lld/MachO/ICF.cpp | 11 ++++++++++-
lld/test/MachO/arm64-thunk-icf-body-dedup.s | 11 ++++++-----
lld/test/MachO/arm64-thunks.s | 4 ++--
lld/test/MachO/icf-safe-thunks-callers.s | 8 ++++----
4 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/lld/MachO/ICF.cpp b/lld/MachO/ICF.cpp
index 1f26212355dec0..fb216d2feb6be3 100644
--- a/lld/MachO/ICF.cpp
+++ b/lld/MachO/ICF.cpp
@@ -518,10 +518,19 @@ void ICF::run() {
// When using safe_thunks, we need to create thunks for all keepUnique
// functions that can be deduplicated. Since we're creating / adding new
// InputSections, we can't parallelize this.
- if (useSafeThunks)
+ if (useSafeThunks) {
forEachClassRange(0, icfInputs.size(), [&](size_t begin, size_t end) {
applySafeThunksToRange(begin, end);
});
+ parallelForEach(inputSections, [](ConcatInputSection *isec) {
+ if (!isCodeSection(isec) || isec->shouldOmitFromOutput())
+ return;
+ for (auto &r : isec->relocs)
+ if (auto *defined = getDirectCallTarget(r))
+ if (defined->identicalCodeFoldingKind == Symbol::ICFFoldKind::Thunk)
+ r.referent = getBodyForThunkFoldedSym(defined);
+ });
+ }
// Fold sections within equivalence classes
forEachClass([&](size_t begin, size_t end) {
diff --git a/lld/test/MachO/arm64-thunk-icf-body-dedup.s b/lld/test/MachO/arm64-thunk-icf-body-dedup.s
index 06d1065c21a74d..52006d7f2f0886 100644
--- a/lld/test/MachO/arm64-thunk-icf-body-dedup.s
+++ b/lld/test/MachO/arm64-thunk-icf-body-dedup.s
@@ -18,15 +18,16 @@
# BODY-MAP: .thunk.0
# BODY-MAP-NOT: .thunk.0
-## Verify that safe_thunks ICF still creates separate branch-extension thunks
-## when needed.
+## With safe_thunks, direct calls share a branch-extension thunk to the body.
+## The unique entrypoint still needs its own branch-extension thunk.
# RUN: %lld -arch arm64 -lSystem -o %t/icf-safe %t/input.o --icf=safe_thunks
# RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn %t/icf-safe | FileCheck %s --check-prefix=SAFE
-## Each call gets its own branch-extension thunk.
# SAFE-LABEL: <_main>:
# SAFE: bl 0x[[#%x, THUNK_A:]] <_target_a.thunk.0>
-# SAFE-NEXT: bl 0x[[#%x, THUNK_B:]] <_target_b.thunk.0>
+# SAFE-NEXT: bl 0x[[#%x, THUNK_A]] <_target_a.thunk.0>
+# SAFE-LABEL: <_target_b>:
+# SAFE-NEXT: b 0x[[#%x, THUNK_B:]] <_target_a.thunk.1>
.subsections_via_symbols
@@ -71,7 +72,7 @@ _main:
## With safe_thunks, _target_b's ICF thunk is a synthetic section appended
## after all regular inputs. This spacer pushes it out of branch range from
-## _main so it also needs a branch-extension thunk.
+## _target_a so it also needs a branch-extension thunk.
.globl _spacer2
.p2align 2
_spacer2:
diff --git a/lld/test/MachO/arm64-thunks.s b/lld/test/MachO/arm64-thunks.s
index 9c1c9ab8d5418e..fa5c971551d514 100644
--- a/lld/test/MachO/arm64-thunks.s
+++ b/lld/test/MachO/arm64-thunks.s
@@ -199,12 +199,12 @@
# CHECK: bl 0x[[#%x, G]] <_g>
# CHECK: bl 0x[[#%x, H]] <_h>
# CHECK: bl 0x[[#%x, FOLD_LOW_THUNK_0:]] <_fold_func_low_addr.thunk.0>
-# CHECK: bl 0x[[#%x, FOLD_HIGH:]] <_fold_func_high_addr>
+# CHECK: bl 0x[[#%x, FOLD_LOW_THUNK_0]] <_fold_func_low_addr.thunk.0>
# CHECK: bl 0x[[#%x, NAN]]
# CHECK: bl 0x[[#%x, FOO:]] <_objc_msgSend$foo>
# CHECK: bl 0x[[#%x, BAR:]] <_objc_msgSend$bar>
-# CHECK: [[#%x, FOLD_HIGH]] <_fold_func_high_addr>:
+# CHECK: [[#%x, FOLD_HIGH:]] <_fold_func_high_addr>:
# CHECK: b 0x[[#%x, FOLD_LOW_THUNK_0]] <_fold_func_low_addr.thunk.0>
# CHECK: [[#%x, C_THUNK_0]] <_c.thunk.0>:
diff --git a/lld/test/MachO/icf-safe-thunks-callers.s b/lld/test/MachO/icf-safe-thunks-callers.s
index 9add5c0806bc93..1c0aa94a34f13c 100644
--- a/lld/test/MachO/icf-safe-thunks-callers.s
+++ b/lld/test/MachO/icf-safe-thunks-callers.s
@@ -35,19 +35,19 @@
# DISASM-LABEL: _call_b:
# DISASM: {{.*}} bl _leaf_a
# DISASM-LABEL: _call_c:
-# DISASM: {{.*}} bl _leaf_d
+# DISASM: {{.*}} bl _leaf_c
# DISASM-LABEL: _call_only_b:
-# DISASM: {{.*}} bl _leaf_b
+# DISASM: {{.*}} bl _leaf_a
# DISASM-LABEL: _address_a:
# DISASM: add x0, x0, #0x[[#%x,LEAF_A]]
# DISASM-LABEL: _address_b:
# DISASM: add x0, x0, #0x[[#%x,LEAF_B:]]
-## Direct and indirect calls to a thunk preserve the same entrypoint.
+## A saved function pointer reaches the unique thunk; a direct call bypasses it.
# DISASM-LABEL: _call_direct_and_indirect_b:
# DISASM: add x8, x8, #0x[[#%x,LEAF_B]]
-# DISASM: {{.*}} bl _leaf_b
+# DISASM: {{.*}} bl _leaf_a
# DISASM: blr x8
## Runtime-bound direct calls still resolve the original symbol.
More information about the llvm-branch-commits
mailing list