[llvm] [X86] Allow "sibcall" optimization on call sites marked `musttail` (PR #150157)

Brandt Bucher via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 22 19:50:16 PDT 2025


https://github.com/brandtbucher created https://github.com/llvm/llvm-project/pull/150157

Fixes #147813.

Use the same code path to detect sibcall-eligibility for both `musttail` and normal tail calls.

>From ca121ebfe070c2752fb28d62a904a5079263d4b2 Mon Sep 17 00:00:00 2001
From: Brandt Bucher <brandtbucher at microsoft.com>
Date: Tue, 22 Jul 2025 19:44:53 -0700
Subject: [PATCH] Allow "sibcall" optimization on call sites marked musttail

---
 llvm/lib/Target/X86/X86ISelLoweringCall.cpp | 13 ++++++---
 llvm/test/CodeGen/X86/musttail-sibling.ll   | 30 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/musttail-sibling.ll

diff --git a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
index b4639ac2577e8..9835586407042 100644
--- a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
+++ b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
@@ -2095,7 +2095,12 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
       isTailCall = false;
   }
 
-  if (isTailCall && !IsMustTail) {
+  if (IsMustTail && !isTailCall) {
+    report_fatal_error("failed to perform tail call elimination on a call site "
+                       "marked musttail");
+  }
+
+  if (isTailCall) {
     // Check if it's really possible to do a tail call.
     isTailCall = IsEligibleForTailCallOptimization(CLI, CCInfo, ArgLocs,
                                                    IsCalleePopSRet);
@@ -2109,9 +2114,9 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
       ++NumTailCalls;
   }
 
-  if (IsMustTail && !isTailCall)
-    report_fatal_error("failed to perform tail call elimination on a call "
-                       "site marked musttail");
+  if (IsMustTail) {
+    isTailCall = true;
+  }
 
   assert(!(isVarArg && canGuaranteeTCO(CallConv)) &&
          "Var args not supported with calling convention fastcc, ghc or hipe");
diff --git a/llvm/test/CodeGen/X86/musttail-sibling.ll b/llvm/test/CodeGen/X86/musttail-sibling.ll
new file mode 100644
index 0000000000000..91a0ae059b01a
--- /dev/null
+++ b/llvm/test/CodeGen/X86/musttail-sibling.ll
@@ -0,0 +1,30 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=x86_64 < %s | FileCheck %s --check-prefix=X64
+; RUN: llc -mtriple=i686 < %s | FileCheck %s --check-prefix=X86
+
+declare void @callee1(ptr)
+define void @caller1(ptr %s) {
+; X64-LABEL: caller1:
+; X64:       # %bb.0:
+; X64-NEXT:    jmp callee1 at PLT # TAILCALL
+;
+; X86-LABEL: caller1:
+; X86:       # %bb.0:
+; X86-NEXT:    jmp callee1 at PLT # TAILCALL
+  musttail call void @callee1(ptr %s)
+  ret void
+}
+
+declare void @callee7(ptr)
+define void @caller7(ptr %r0, ptr %r1, ptr %r2, ptr %r3, ptr %r4, ptr %r5, ptr %s) {
+; X64-LABEL: caller7:
+; X64:       # %bb.0:
+; X64-NEXT:    jmp callee7 at PLT # TAILCALL
+;
+; X86-LABEL: caller7:
+; X86:       # %bb.0:
+; X86-NEXT:    jmp callee7 at PLT # TAILCALL
+  musttail call void @callee7(ptr %r0, ptr %r1, ptr %r2, ptr %r3, ptr %r4, ptr %r5, ptr %s)
+  ret void
+}
+



More information about the llvm-commits mailing list