[llvm] [BOLT][AArch64] Preserve tail-call annotation on local trampolines (PR #228105)

Alexandros Lamprineas via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 05:29:27 PDT 2026


https://github.com/labrinea updated https://github.com/llvm/llvm-project/pull/228105

>From 5ef4c3f686091886349c21052a8aaee7b141aeca Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas <alexandros.lamprineas at arm.com>
Date: Thu, 1 Oct 2026 14:57:52 +0000
Subject: [PATCH 1/2] [BOLT][AArch64] Preserve tail-call annotation on local
 trampolines

Propagate the tail-call annotation so cluster relaxation can
relax the trampoline's outgoing branch with a long thunk.
---
 bolt/lib/Passes/LongJmp.cpp                   | 18 ++++---
 .../conditional-tail-call-skipped-target.s    | 48 +++++++++++++++++++
 2 files changed, 60 insertions(+), 6 deletions(-)
 create mode 100644 bolt/test/AArch64/conditional-tail-call-skipped-target.s

diff --git a/bolt/lib/Passes/LongJmp.cpp b/bolt/lib/Passes/LongJmp.cpp
index 3dab645cfd61a..63639198c7369 100644
--- a/bolt/lib/Passes/LongJmp.cpp
+++ b/bolt/lib/Passes/LongJmp.cpp
@@ -739,11 +739,12 @@ bool LongJmpPass::relaxLocalBranches(BinaryFunction &BF,
     // \p Offset reflects the size delta of BB caused by splitting unconditional
     // branches, or replacing a branch with a longer instruction sequence. It is
     // used to update the output addresses of basic blocks following the
-    // trampoline.
+    // trampoline. If IsTailCall is true, the outgoing branch is a tail call.
     auto addTrampolineAfter = [&](BinaryBasicBlock *BB,
                                   const MCSymbol *TargetSym,
                                   BinaryBasicBlock *TargetBB, uint64_t Count,
-                                  uint64_t Offset = 0) {
+                                  uint64_t Offset = 0,
+                                  bool IsTailCall = false) {
       FunctionTrampolines.emplace_back(BB ? BB : FF.back(),
                                        BF.createBasicBlock());
       BinaryBasicBlock *TrampolineBB = FunctionTrampolines.back().second.get();
@@ -755,7 +756,10 @@ bool LongJmpPass::relaxLocalBranches(BinaryFunction &BF,
       MCInst Inst;
       {
         auto L = BC.scopeLock();
-        MIB->createUncondBranch(Inst, TargetSym, BC.Ctx.get());
+        if (IsTailCall)
+          MIB->createTailCall(Inst, TargetSym, BC.Ctx.get());
+        else
+          MIB->createUncondBranch(Inst, TargetSym, BC.Ctx.get());
       }
       TrampolineBB->addInstruction(Inst);
       if (TargetBB)
@@ -988,9 +992,11 @@ bool LongJmpPass::relaxLocalBranches(BinaryFunction &BF,
               return false;
             }
 
-            TrampolineBB = addTrampolineAfter(/*BB=*/nullptr, TargetSymbol,
-                                              /*TargetBB=*/nullptr,
-                                              /*Count=*/0);
+            // Propagate the tail-call annotation so cluster relaxation can
+            // relax the trampoline's outgoing branch with a long thunk.
+            TrampolineBB = addTrampolineAfter(
+                /*BB=*/nullptr, TargetSymbol, /*TargetBB=*/nullptr,
+                /*Count=*/0, /*Offset=*/0, MIB->isTailCall(Inst));
             SymbolTrampolines[TargetSymbol] = TrampolineBB;
             auto L = BC.scopeLock();
             MIB->replaceBranchTarget(Inst, TrampolineBB->getLabel(),
diff --git a/bolt/test/AArch64/conditional-tail-call-skipped-target.s b/bolt/test/AArch64/conditional-tail-call-skipped-target.s
new file mode 100644
index 0000000000000..cc4efb67cde22
--- /dev/null
+++ b/bolt/test/AArch64/conditional-tail-call-skipped-target.s
@@ -0,0 +1,48 @@
+## Preserve tail-call annotations on local trampolines, including reused ones.
+## The indirect branch makes _start non-simple. Its conditional tail calls
+## are redirected through a shared trampoline whose outgoing B must also be
+## annotated as a tail call, even when the destination is excluded from layout.
+
+# REQUIRES: system-linux
+
+# RUN: %clang %cflags -Wl,-q -Wl,-e,_start %s -o %t -nostdlib
+# RUN: llvm-bolt %t -o %t.bolt --relax-exp --skip-funcs='^skipped$' \
+# RUN:   | FileCheck %s --check-prefix=CHECK-BOLT
+# RUN: llvm-objdump -d %t.bolt | FileCheck %s --check-prefix=CHECK-OUTPUT
+
+# CHECK-BOLT: BOLT-INFO: relaxed 1 calls with long thunks
+# CHECK-BOLT: BOLT-INFO: 1 long thunks created
+
+  .text
+  .globl _start
+  .type _start, %function
+_start:
+  cmp x0, #0
+  b.eq skipped
+  cmp x1, #0
+  b.eq skipped
+  ldr x16, [sp]
+  br x16
+  .size _start, .-_start
+
+  .globl skipped
+  .type skipped, %function
+skipped:
+  ret
+  .size skipped, .-skipped
+
+  .reloc 0, R_AARCH64_NONE
+
+# CHECK-OUTPUT: Disassembly of section .text:
+# CHECK-OUTPUT:      <_start>:
+# CHECK-OUTPUT-NEXT:            {{.*}} cmp x0, #0x0
+# CHECK-OUTPUT-NEXT:            {{.*}} b.eq 0x[[STUB:[0-9a-f]+]] <{{.*}}>
+# CHECK-OUTPUT-NEXT:            {{.*}} cmp x1, #0x0
+# CHECK-OUTPUT-NEXT:            {{.*}} b.eq 0x[[STUB]] <{{.*}}>
+# CHECK-OUTPUT-NEXT:            {{.*}} ldr x16, [sp]
+# CHECK-OUTPUT-NEXT:            {{.*}} br x16
+# CHECK-OUTPUT-NEXT: [[STUB]]:  {{.*}} b 0x[[THUNK:[0-9a-f]+]] <__AArch64_forward_ADRPThunk_skipped_0>
+# CHECK-OUTPUT:      <__AArch64_forward_ADRPThunk_skipped_0>:
+# CHECK-OUTPUT-NEXT: [[THUNK]]: {{.*}} adrp x16,
+# CHECK-OUTPUT-NEXT:            {{.*}} add x16, x16,
+# CHECK-OUTPUT-NEXT:            {{.*}} br x16

>From bd286388baca5e363745c5d77031c2832363b596 Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas <alexandros.lamprineas at arm.com>
Date: Wed, 7 Oct 2026 09:39:26 +0000
Subject: [PATCH 2/2] simplify test

---
 .../conditional-tail-call-skipped-target.s    | 49 +++++++++----------
 1 file changed, 24 insertions(+), 25 deletions(-)

diff --git a/bolt/test/AArch64/conditional-tail-call-skipped-target.s b/bolt/test/AArch64/conditional-tail-call-skipped-target.s
index cc4efb67cde22..b7d1533113320 100644
--- a/bolt/test/AArch64/conditional-tail-call-skipped-target.s
+++ b/bolt/test/AArch64/conditional-tail-call-skipped-target.s
@@ -1,29 +1,29 @@
-## Preserve tail-call annotations on local trampolines, including reused ones.
-## The indirect branch makes _start non-simple. Its conditional tail calls
-## are redirected through a shared trampoline whose outgoing B must also be
-## annotated as a tail call, even when the destination is excluded from layout.
+## Check that a conditional tail call's local trampoline has a tail-call
+## annotation on its outgoing branch. The target is skipped and absent from the
+## output layout, so cluster relaxation needs that annotation for a long thunk.
+## The unresolved indirect branch makes the caller non-simple.
 
 # REQUIRES: system-linux
 
-# RUN: %clang %cflags -Wl,-q -Wl,-e,_start %s -o %t -nostdlib
+# RUN: %clang %cflags -Wl,-q -Wl,-e,non_simple %s -o %t -nostdlib
 # RUN: llvm-bolt %t -o %t.bolt --relax-exp --skip-funcs='^skipped$' \
+# RUN:   --print-cfg --print-only=non_simple \
 # RUN:   | FileCheck %s --check-prefix=CHECK-BOLT
 # RUN: llvm-objdump -d %t.bolt | FileCheck %s --check-prefix=CHECK-OUTPUT
 
+# CHECK-BOLT: Binary Function "non_simple" after building cfg
+# CHECK-BOLT: IsSimple    : 0
 # CHECK-BOLT: BOLT-INFO: relaxed 1 calls with long thunks
 # CHECK-BOLT: BOLT-INFO: 1 long thunks created
 
   .text
-  .globl _start
-  .type _start, %function
-_start:
+  .globl non_simple
+  .type non_simple, %function
+non_simple:
   cmp x0, #0
   b.eq skipped
-  cmp x1, #0
-  b.eq skipped
-  ldr x16, [sp]
-  br x16
-  .size _start, .-_start
+  br x1
+  .size non_simple, .-non_simple
 
   .globl skipped
   .type skipped, %function
@@ -31,18 +31,17 @@ skipped:
   ret
   .size skipped, .-skipped
 
-  .reloc 0, R_AARCH64_NONE
+.reloc 0, R_AARCH64_NONE
 
 # CHECK-OUTPUT: Disassembly of section .text:
-# CHECK-OUTPUT:      <_start>:
-# CHECK-OUTPUT-NEXT:            {{.*}} cmp x0, #0x0
-# CHECK-OUTPUT-NEXT:            {{.*}} b.eq 0x[[STUB:[0-9a-f]+]] <{{.*}}>
-# CHECK-OUTPUT-NEXT:            {{.*}} cmp x1, #0x0
-# CHECK-OUTPUT-NEXT:            {{.*}} b.eq 0x[[STUB]] <{{.*}}>
-# CHECK-OUTPUT-NEXT:            {{.*}} ldr x16, [sp]
-# CHECK-OUTPUT-NEXT:            {{.*}} br x16
-# CHECK-OUTPUT-NEXT: [[STUB]]:  {{.*}} b 0x[[THUNK:[0-9a-f]+]] <__AArch64_forward_ADRPThunk_skipped_0>
+#
+# CHECK-OUTPUT:      <non_simple>:
+# CHECK-OUTPUT-NEXT:           {{.*}} cmp x0, #0x0
+# CHECK-OUTPUT-NEXT:           {{.*}} b.eq 0x[[STUB:[0-9a-f]+]] <{{.*}}>
+# CHECK-OUTPUT-NEXT:           {{.*}} br x1
+# CHECK-OUTPUT-NEXT: [[STUB]]: {{.*}} b 0x{{[0-9a-f]+}} <__AArch64_forward_ADRPThunk_skipped_0>
+#
 # CHECK-OUTPUT:      <__AArch64_forward_ADRPThunk_skipped_0>:
-# CHECK-OUTPUT-NEXT: [[THUNK]]: {{.*}} adrp x16,
-# CHECK-OUTPUT-NEXT:            {{.*}} add x16, x16,
-# CHECK-OUTPUT-NEXT:            {{.*}} br x16
+# CHECK-OUTPUT-NEXT:   {{.*}} adrp x16,
+# CHECK-OUTPUT-NEXT:   {{.*}} add x16, x16,
+# CHECK-OUTPUT-NEXT:   {{.*}} br x16



More information about the llvm-commits mailing list