Skip to content

[X86] Fix missing ByValTemporaries update in CopyViaTemp path for musttail calls#190540

Merged
RKSimon merged 2 commits into
llvm:mainfrom
xiongzile:musttail-fix
Apr 8, 2026
Merged

[X86] Fix missing ByValTemporaries update in CopyViaTemp path for musttail calls#190540
RKSimon merged 2 commits into
llvm:mainfrom
xiongzile:musttail-fix

Conversation

@xiongzile

Copy link
Copy Markdown
Contributor

This fixes a miscompilation in musttail calls with byval arguments on X86.

In the CopyViaTemp path, a temporary stack object is created and the argument is copied into it.
However, the temporary is not recorded in ByValTemporaries,
so the final lowering phase does not emit the copy to the real outgoing argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the final lowering step correctly copies the argument to the expected stack location.

Reproducer: #190429

@github-actions

github-actions Bot commented Apr 5, 2026

Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot

llvmbot commented Apr 5, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-x86

Author: Zile Xiong (xiongzile)

Changes

This fixes a miscompilation in musttail calls with byval arguments on X86.

In the CopyViaTemp path, a temporary stack object is created and the argument is copied into it.
However, the temporary is not recorded in ByValTemporaries,
so the final lowering phase does not emit the copy to the real outgoing argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the final lowering step correctly copies the argument to the expected stack location.

Reproducer: #190429


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

1 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLoweringCall.cpp (+1)
diff --git a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
index 37c80e27f4bd2..65d77769b3c45 100644
--- a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
+++ b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
@@ -2237,6 +2237,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
         SDValue CopyChain =
             CreateCopyOfByValArgument(Src, Temp, Chain, Flags, DAG, dl);
         ByValCopyChains.push_back(CopyChain);
+        ByValTemporaries[ArgIdx] = Temp;
       }
     }
     if (!ByValCopyChains.empty())

@xiongzile
xiongzile force-pushed the musttail-fix branch 2 times, most recently from b0f1215 to 03109e8 Compare April 5, 2026 18:23
@xiongzile

Copy link
Copy Markdown
Contributor Author

Hi @RKSimon , could you please take a look at this PR when you have a moment?

This fixes a miscompilation in musttail calls with byval arguments on X86. All checks are passing.

Thanks!

@RKSimon
RKSimon requested review from RKSimon and efriedma-quic April 6, 2026 15:31

@efriedma-quic efriedma-quic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,90 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this new test cover any constructs that are significantly different from the existing test coverage in musttail-struct.ll?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — the existing test already covers this case after the update.
I've removed the additional test in the latest commit. Thanks!

…ttail calls

The CopyViaTemp path creates a temporary for byval arguments but does not
record it in ByValTemporaries. As a result, the final lowering phase does
not emit the copy to the outgoing argument slot, leading to incorrect
values being observed by the callee.

Record the temporary in ByValTemporaries to ensure correct lowering.

Fixes: llvm#190429

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

SDValue CopyChain =
CreateCopyOfByValArgument(Src, Temp, Chain, Flags, DAG, dl);
ByValCopyChains.push_back(CopyChain);
ByValTemporaries[ArgIdx] = Temp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix here is consistent with the arm implementation that the x86 implementation is based on

SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
SDValue Ops[] = {Chain, Temp, Src, SizeNode, AlignNode};
ByValCopyChains.push_back(
DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, Ops));
ByValTemporaries[ArgIdx] = Temp;
}
}
if (!ByValCopyChains.empty())
ByValTempChain =

That line must have gotten lost at some point...

@efriedma-quic efriedma-quic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Let's cherry-pick this to 22.

It would also be nice if anyone has suggestions for better testing, to make sure there aren't any further issues...

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@RKSimon
RKSimon enabled auto-merge (squash) April 8, 2026 14:42
@RKSimon

RKSimon commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

LGTM

Let's cherry-pick this to 22.

I'll let this stew in trunk for a few days and then get it cherry picked (probably next week during EuroLLVM)

@RKSimon
RKSimon merged commit abd502a into llvm:main Apr 8, 2026
9 of 10 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Apr 8, 2026
@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown

@xiongzile Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@RKSimon

RKSimon commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

/cherry-pick abd502a

@llvmbot

llvmbot commented Apr 16, 2026

Copy link
Copy Markdown
Member

/pull-request #192507

c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Apr 20, 2026
…ttail calls (llvm#190540)

This fixes a miscompilation in musttail calls with byval arguments on
X86.

In the CopyViaTemp path, a temporary stack object is created and the
argument is copied into it.
However, the temporary is not recorded in ByValTemporaries,
so the final lowering phase does not emit the copy to the real outgoing
argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the
final lowering step correctly copies the argument to the expected stack
location.

Reproducer: llvm#190429
(cherry picked from commit abd502a)
YonahGoldberg pushed a commit to YonahGoldberg/llvm-project that referenced this pull request Apr 21, 2026
…ttail calls (llvm#190540)

This fixes a miscompilation in musttail calls with byval arguments on
X86.

In the CopyViaTemp path, a temporary stack object is created and the
argument is copied into it.
However, the temporary is not recorded in ByValTemporaries, 
so the final lowering phase does not emit the copy to the real outgoing
argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the
final lowering step correctly copies the argument to the expected stack
location.

Reproducer: llvm#190429
vikramRH pushed a commit to ROCm/llvm-project that referenced this pull request Apr 22, 2026
…ttail calls (llvm#190540)

This fixes a miscompilation in musttail calls with byval arguments on
X86.

In the CopyViaTemp path, a temporary stack object is created and the
argument is copied into it.
However, the temporary is not recorded in ByValTemporaries, 
so the final lowering phase does not emit the copy to the real outgoing
argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the
final lowering step correctly copies the argument to the expected stack
location.

Reproducer: llvm#190429
zwu-2025 pushed a commit to zwu-2025/llvm-project that referenced this pull request May 17, 2026
…ttail calls (llvm#190540)

This fixes a miscompilation in musttail calls with byval arguments on
X86.

In the CopyViaTemp path, a temporary stack object is created and the
argument is copied into it.
However, the temporary is not recorded in ByValTemporaries, 
so the final lowering phase does not emit the copy to the real outgoing
argument slot.

As a result, the callee may read incorrect values from the stack.

Fix this by recording the temporary in ByValTemporaries so that the
final lowering step correctly copies the argument to the expected stack
location.

Reproducer: llvm#190429
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

6 participants