Skip to content

Conversation

@folkertdev
Copy link
Contributor

tracking issue: #112788

After discussion in #144855, I was wrong and it is actually possible to support tail calls with PassMode::Indirect { on_stack: false, .. } arguments.

Normally an indirect argument with on_stack: false would be passed as a pointer into the caller's stack frame. For tail calls, that would be unsound, because the caller's stack frame is overwritten by the callee's stack frame.

Therefore we store the argument for the callee in the corresponding caller's slot. Because guaranteed tail calls demand that the caller's signature matches the callee's, the corresponding slot has the correct type.

To handle cases like the one below, the tail call arguments must first be copied to a temporary, and can only then be copied to the caller's argument slots.

// A struct big enough that it is not passed via registers.
pub struct Big([u64; 4]);

fn swapper(a: Big, b: Big) -> (Big, Big) {
    become swapper_helper(b, a);
}

I'm not really familiar with MIR and what tricks/helpers we have, so I'm just cobbling this together. Hopefully we can arrive at something more elegant.

@folkertdev folkertdev added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Jan 14, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2026
@folkertdev folkertdev force-pushed the tail-call-indirect branch 2 times, most recently from 8a1cd78 to 6f7eede Compare January 14, 2026 21:41
Comment on lines +1270 to +1284
PassMode::Indirect { on_stack: true, .. } => {
// FIXME: some LLVM backends (notably x86) do not correctly pass byval
// arguments to tail calls (as of LLVM 21). See also:
//
// - https://github.com/rust-lang/rust/pull/144232#discussion_r2218543841
// - https://github.com/rust-lang/rust/issues/144855
span_bug!(
fn_span,
"arguments using PassMode::Indirect {{ on_stack: true, .. }} are currently not supported for tail calls"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've kept the span_bug! for now.

It looks like a fix for x86 might get cherry-picked into LLVM 22. If so, I think that is enough support to allow this variant too. At that point riscv would be the next most commonly used target that would miscompile.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the tail-call-indirect branch 2 times, most recently from 029786e to 414b969 Compare January 15, 2026 17:28
@folkertdev
Copy link
Contributor Author

@bors try=x86_64-msvc-1,x86_64-msvc-2

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 15, 2026

Unknown command "try". Run @bors help to see available commands.

@folkertdev
Copy link
Contributor Author

@bors try job=x86_64-msvc-1,x86_64-msvc-2

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 15, 2026
explicit tail calls: support indirect arguments


try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 15, 2026

💔 Test for e8cb9bb failed: CI. Failed job:

@folkertdev
Copy link
Contributor Author

@bors try job=x86_64-msvc-1,x86_64-msvc-2

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 15, 2026

⌛ Trying commit 33942c6 with merge 282e2c8

To cancel the try build, run the command @bors try cancel.

Workflow: https://github.com/rust-lang/rust/actions/runs/21048427234

rust-bors bot pushed a commit that referenced this pull request Jan 15, 2026
explicit tail calls: support indirect arguments


try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-explicit_tail_calls `#![feature(explicit_tail_calls)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants