[llvm] [X86] Fix invalid codegen when passing f80 arguments in __vectorcall (PR #203913)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 07:44:48 PDT 2026


https://github.com/mike-goutokuji updated https://github.com/llvm/llvm-project/pull/203913

>From 5443f68f01ca3d816760de488b17f5f39151acb8 Mon Sep 17 00:00:00 2001
From: Mike-Goutokuji <gfunni234 at gmail.com>
Date: Mon, 15 Jun 2026 10:04:23 -0400
Subject: [PATCH] [X86] Fix invalid codegen when passing f80 arguments in
 __vectorcall

The `__vectorcall` calling convention allocates SSE registers (`XMM0`-`XMM5`) for floating-point types and vector types. However, LLVM's vectorcall assignment logic was previously checking if an argument was a floating-point type simply by calling `ValVT.isFloatingPoint()`. Since `f80` is classified as a floating-point type, LLVM incorrectly assigned an `XMM` register for the argument. Later during compilation, the backend would crash because there is no valid hardware instruction to copy an `XMM` register directly to an x87 stack register (`FP0`-`FP6`).
---
 llvm/lib/Target/X86/X86CallingConv.cpp        | 20 +++++++++----
 .../CodeGen/X86/vectorcall-f80-tailcall.ll    | 30 +++++++++++++++++++
 llvm/test/CodeGen/X86/vectorcall-f80.ll       | 22 ++++++++++++++
 3 files changed, 66 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/vectorcall-f80-tailcall.ll
 create mode 100644 llvm/test/CodeGen/X86/vectorcall-f80.ll

diff --git a/llvm/lib/Target/X86/X86CallingConv.cpp b/llvm/lib/Target/X86/X86CallingConv.cpp
index 5d5a705893242..c80bc1fcb6b22 100644
--- a/llvm/lib/Target/X86/X86CallingConv.cpp
+++ b/llvm/lib/Target/X86/X86CallingConv.cpp
@@ -87,6 +87,17 @@ static ArrayRef<MCPhysReg> CC_X86_64_VectorCallGetGPRs() {
   return RegListGPR;
 }
 
+/// Return true if \p VT is a vectorcall vector type.
+///
+/// Per the vectorcall spec, vector types are float, double, or SIMD types such
+/// as __m128/__m256. x86_fp80 is an x87 type and must use the non-vector rules
+/// (see CC_X86_Win64_C). Argument handling matches RetCC_X86_*_VectorCall in
+/// X86CallingConv.td, which also passes f128 in XMM registers.
+static bool CC_X86_IsVectorCallVectorType(MVT VT) {
+  return VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 ||
+         (VT.isVector() && VT.getSizeInBits() >= 128);
+}
+
 static bool CC_X86_VectorCallAssignRegister(unsigned &ValNo, MVT &ValVT,
                                             MVT &LocVT,
                                             CCValAssign::LocInfo &LocInfo,
@@ -139,8 +150,7 @@ static bool CC_X86_64_VectorCall(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
   // Process only vector types as defined by vectorcall spec:
   // "A vector type is either a floating-point type, for example,
   //  a float or double, or an SIMD vector type, for example, __m128 or __m256".
-  if (!(ValVT.isFloatingPoint() ||
-        (ValVT.isVector() && ValVT.getSizeInBits() >= 128))) {
+  if (!CC_X86_IsVectorCallVectorType(ValVT)) {
     // If R9 was already assigned it means that we are after the fourth element
     // and because this is not an HVA / Vector type, we need to allocate
     // shadow XMM register.
@@ -199,10 +209,8 @@ static bool CC_X86_32_VectorCall(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
   // Process only vector types as defined by vectorcall spec:
   // "A vector type is either a floating point type, for example,
   //  a float or double, or an SIMD vector type, for example, __m128 or __m256".
-  if (!(ValVT.isFloatingPoint() ||
-        (ValVT.isVector() && ValVT.getSizeInBits() >= 128))) {
+  if (!CC_X86_IsVectorCallVectorType(ValVT))
     return false;
-  }
 
   if (ArgFlags.isHva())
     return true; // If this is an HVA - Stop the search.
@@ -216,7 +224,7 @@ static bool CC_X86_32_VectorCall(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
   // In case we did not find an available XMM register for a vector -
   // pass it indirectly.
   // It is similar to CCPassIndirect, with the addition of inreg.
-  if (!ValVT.isFloatingPoint()) {
+  if (ValVT.isVector()) {
     LocVT = MVT::i32;
     LocInfo = CCValAssign::Indirect;
     ArgFlags.setInReg();
diff --git a/llvm/test/CodeGen/X86/vectorcall-f80-tailcall.ll b/llvm/test/CodeGen/X86/vectorcall-f80-tailcall.ll
new file mode 100644
index 0000000000000..e1e71ada89766
--- /dev/null
+++ b/llvm/test/CodeGen/X86/vectorcall-f80-tailcall.ll
@@ -0,0 +1,30 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -O2 -mtriple=i686-pc-windows-gnu < %s | FileCheck %s
+
+; Verify that tail calls to vectorcall functions with two x86_fp80 arguments
+; pass arguments in the correct stack order (X at lower address, Y above).
+; This mirrors atan2l(Y, X) tail calling CIatan2(X, Y).
+
+declare x86_vectorcallcc x86_fp80 @CIatan2(x86_fp80, x86_fp80)
+
+define dso_local x86_vectorcallcc x86_fp80 @atan2l(x86_fp80 %Y, x86_fp80 %X) {
+; CHECK-LABEL: atan2l@@24:
+; CHECK:       # %bb.0:
+; CHECK-DAG:   fldt 40(%esp)
+; CHECK-DAG:   fldt 28(%esp)
+; CHECK-DAG:   fstpt (%esp)
+; CHECK-DAG:   fstpt 12(%esp)
+; CHECK-NOT:   xmm
+; CHECK:       calll CIatan2@@24
+entry:
+  %ret = tail call x86_vectorcallcc x86_fp80 @CIatan2(x86_fp80 %X, x86_fp80 %Y)
+  ret x86_fp80 %ret
+}
+
+define dso_local x86_vectorcallcc x86_fp80 @atan2l_passthrough(x86_fp80 %X, x86_fp80 %Y) {
+; CHECK-LABEL: atan2l_passthrough@@24:
+; CHECK:       jmp CIatan2@@24
+entry:
+  %ret = tail call x86_vectorcallcc x86_fp80 @CIatan2(x86_fp80 %X, x86_fp80 %Y)
+  ret x86_fp80 %ret
+}
diff --git a/llvm/test/CodeGen/X86/vectorcall-f80.ll b/llvm/test/CodeGen/X86/vectorcall-f80.ll
new file mode 100644
index 0000000000000..53b13e11bc734
--- /dev/null
+++ b/llvm/test/CodeGen/X86/vectorcall-f80.ll
@@ -0,0 +1,22 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=i686-pc-windows-gnu < %s | FileCheck %s
+; RUN: llc -mtriple=x86_64-linux-gnu < %s | FileCheck %s --check-prefix=LINUX64
+
+; Verify that x86_fp80 arguments and return values are handled correctly
+; and do not crash the backend when using the vectorcall calling convention.
+
+define dso_local x86_vectorcallcc x86_fp80 @"\01func1@@12"(x86_fp80 %A) {
+; CHECK-LABEL: func1@@12:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    fldt 4(%esp)
+; CHECK-NEXT:    retl $12
+;
+; LINUX64-LABEL: "func1@@12":
+; LINUX64:       .cfi_startproc
+; LINUX64-NEXT:  # %bb.0:
+; LINUX64-NEXT:  fldt
+; LINUX64-SAME:  (%rcx)
+; LINUX64-NEXT:  retq
+entry:
+  ret x86_fp80 %A
+}



More information about the llvm-commits mailing list