[llvm] x86: fix musttail sibcall miscompilation (PR #168956)

Folkert de Vries via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 22 11:52:56 PST 2025


================
@@ -2404,16 +2533,23 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
       FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
 
       if (Flags.isByVal()) {
-        // Copy relative to framepointer.
-        SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset(), dl);
-        if (!StackPtr.getNode())
-          StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
-                                        getPointerTy(DAG.getDataLayout()));
-        Source = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
-                             StackPtr, Source);
-
-        MemOpChains2.push_back(
-            CreateCopyOfByValArgument(Source, FIN, Chain, Flags, DAG, dl));
+        SDValue ByValSrc;
+        bool NeedsStackCopy;
+        if (auto It = ByValTemporaries.find(OutsIndex);
+            It != ByValTemporaries.end()) {
+          ByValSrc = It->second;
+          NeedsStackCopy = true;
+        } else {
+          ByValSrc = Arg;
+          NeedsStackCopy = !isTailCall;
+        }
+
+        auto PtrVT = getPointerTy(DAG.getDataLayout());
+        SDValue DstAddr = DAG.getFrameIndex(FI, PtrVT);
+
+        // Copy the struct contents from ByValSrc to DstAddr.
+        MemOpChains2.push_back(CreateCopyOfByValArgument(
+            ByValSrc, DstAddr, Chain, Flags, DAG, dl));
----------------
folkertdev wrote:

So in the arm backend this logic is guareded with an `if (NeedsStackCopy)`, but even in my simple cases the `ByValTemporaries` appears to be empty (that does make sense, given how `ByValNeedsCopyForTailCall` exits early where I commented), but the `CreateCopyOfByValArgument` is still needed.

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


More information about the llvm-commits mailing list