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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Brandt Bucher (brandtbucher)

<details>
<summary>Changes</summary>

Fixes #<!-- -->147813.

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

---
Full diff: https://github.com/llvm/llvm-project/pull/150157.diff


2 Files Affected:

- (modified) llvm/lib/Target/X86/X86ISelLoweringCall.cpp (+9-4) 
- (added) llvm/test/CodeGen/X86/musttail-sibling.ll (+30) 


``````````diff
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
+}
+

``````````

</details>


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


More information about the llvm-commits mailing list