Skip to content

[X86] Use StackArgTokenFactor for all stores when setting up tail calls. #126244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Feb 7, 2025

Before this patch, the stack argument token factor was used if any outgoing stack slots needed to be written, but not when writing the return address to a stack slot. Writing the return address stack slot can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return address will always use it, i.e. happen after all input arguments are loaded.

Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-backend-x86

Author: Florian Hahn (fhahn)

Changes

Before this patch, the stack argument token factor was used if any outgoing stack slots needed to be written, but not when writing the return address to a stack slot. Writing the return address stack slot can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return address will always use it, i.e. happen after all input arguments are loaded.


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

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLoweringCall.cpp (+10-10)
  • (modified) llvm/test/CodeGen/X86/swifttailcc-store-ret-address-aliasing-stack-slot.ll (+2-2)
diff --git a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
index 6835c7e336a5cb..ee4bb758102f42 100644
--- a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
+++ b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
@@ -2326,12 +2326,13 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   // shuffling arguments passed in memory.
   if (!IsSibcall && isTailCall) {
     // Force all the incoming stack arguments to be loaded from the stack
-    // before any new outgoing arguments are stored to the stack, because the
-    // outgoing stack slots may alias the incoming argument stack slots, and
-    // the alias isn't otherwise explicit. This is slightly more conservative
-    // than necessary, because it means that each store effectively depends
-    // on every argument instead of just those arguments it would clobber.
-    SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
+    // before any new outgoing arguments or the return address are stored to the
+    // stack, because the outgoing stack slots may alias the incoming argument
+    // stack slots, and the alias isn't otherwise explicit. This is slightly
+    // more conservative than necessary, because it means that each store
+    // effectively depends on every argument instead of just those arguments it
+    // would clobber.
+    Chain = DAG.getStackArgumentTokenFactor(Chain);
 
     SmallVector<SDValue, 8> MemOpChains2;
     SDValue FIN;
@@ -2373,13 +2374,12 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
         Source = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
                              StackPtr, Source);
 
-        MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
-                                                         ArgChain,
-                                                         Flags, DAG, dl));
+        MemOpChains2.push_back(
+            CreateCopyOfByValArgument(Source, FIN, Chain, Flags, DAG, dl));
       } else {
         // Store relative to framepointer.
         MemOpChains2.push_back(DAG.getStore(
-            ArgChain, dl, Arg, FIN,
+            Chain, dl, Arg, FIN,
             MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));
       }
     }
diff --git a/llvm/test/CodeGen/X86/swifttailcc-store-ret-address-aliasing-stack-slot.ll b/llvm/test/CodeGen/X86/swifttailcc-store-ret-address-aliasing-stack-slot.ll
index 78e810bb67f45c..cd669768705e53 100644
--- a/llvm/test/CodeGen/X86/swifttailcc-store-ret-address-aliasing-stack-slot.ll
+++ b/llvm/test/CodeGen/X86/swifttailcc-store-ret-address-aliasing-stack-slot.ll
@@ -24,9 +24,9 @@ define swifttailcc void @test(ptr %0, ptr swiftasync %1, i64 %2, i64 %3, ptr %4,
 ; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %r15
 ; CHECK-NEXT:    callq _foo
 ; CHECK-NEXT:    movq %r14, (%rax)
+; CHECK-NEXT:    movl [[OFF:[0-9]+]](%rsp), %edx
 ; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %rcx
-; CHECK-NEXT:    movq %rcx, [[OFF:[0-9]+]](%rsp)
-; CHECK-NEXT:    movl [[OFF]](%rsp), %edx
+; CHECK-NEXT:    movq %rcx, [[OFF]](%rsp)
 ; CHECK-NEXT:    movq %rax, %r14
 ; CHECK-NEXT:    movq %r13, %rdi
 ; CHECK-NEXT:    movq %r15, %rsi

@fhahn fhahn requested a review from jroelofs February 7, 2025 19:34
fhahn added a commit to fhahn/llvm-project that referenced this pull request Feb 10, 2025
Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

Upstream PR: llvm#126244

rdar://143354182
@fhahn fhahn merged commit f147d67 into llvm:main Feb 10, 2025
10 checks passed
@fhahn fhahn deleted the x86-tail-call-fix-chain branch February 10, 2025 20:01
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 10, 2025
…up tail calls. (#126244)

Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

PR: llvm/llvm-project#126244
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…ls. (llvm#126244)

Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

PR: llvm#126244
cyndyishida pushed a commit to swiftlang/llvm-project that referenced this pull request Feb 12, 2025
Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

Upstream PR: llvm#126244

rdar://143354182
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…ls. (llvm#126244)

Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

PR: llvm#126244
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…ls. (llvm#126244)

Before this patch, the stack argument token factor was used if any
outgoing stack slots needed to be written, but not when writing the
return address to a stack slot. Writing the return address stack slot
can also alias a stack slot for an input argument.

Always use StackArgumentTokenFactor, to ensure the store of the return
address will always use it, i.e. happen after all input arguments are
loaded.

PR: llvm#126244
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants