Skip to content

Implement support for become and explicit tail call codegen for the LLVM backend #144232

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

xacrimon
Copy link

@xacrimon xacrimon commented Jul 20, 2025

This PR implements codegen of explicit tail calls via become in rustc_codegen_ssa and support within the LLVM backend. Completes a task on (#112788). This PR implements all the necessary bits to make explicit tail calls usable, other backends have received stubs for now and will ICE if you use become on them. I suspect there is quite a bit of bikeshedding on how we should go about implementing this for WASM, but it should be relatively straightforward for GCC after this is merged.

During development I also put together a POC bytecode VM based on tail call dispatch to test these changes out and analyze the codegen to make sure it generates expected assembly.. That is available here.

@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@xacrimon xacrimon force-pushed the explicit-tail-call branch from 6a6d3c2 to f912c90 Compare July 20, 2025 22:49
@rust-log-analyzer

This comment has been minimized.

@xacrimon xacrimon force-pushed the explicit-tail-call branch from f912c90 to 42c719e Compare July 20, 2025 23:02
@xacrimon xacrimon force-pushed the explicit-tail-call branch from b36cf15 to ac87434 Compare July 20, 2025 23:10
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@xacrimon xacrimon force-pushed the explicit-tail-call branch from 138b2a7 to 3fac279 Compare July 21, 2025 00:14
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
failures:

---- [ui] tests/ui/explicit-tail-calls/recursion-no-tce.rs stdout ----

error: test did not exit with failure! code=None so test would pass with `run-crash`
status: signal: 6 (SIGABRT) (core dumped)
command: cd "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/explicit-tail-calls/recursion-no-tce" && RUSTC="/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" RUST_TEST_THREADS="4" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/explicit-tail-calls/recursion-no-tce/a"
stdout: none
--- stderr -------------------------------

thread '<unknown>' has overflowed its stack
fatal runtime error: stack overflow, aborting

@jeffparsons
Copy link
Contributor

I suspect there is quite a bit of bikeshedding on how we should go about implementing this for WASM

I thought LLVM already supported the Wasm tail-call instructions — what's the missing piece that needs bikeshedding?

@oli-obk
Copy link
Contributor

oli-obk commented Jul 21, 2025

@rustbot author

it seems a tailcall test is still hitting a stack overflow, do you know why?

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 21, 2025
Comment on lines +226 to +229
bx.tail_call(fn_ty, fn_attrs, fn_abi, fn_ptr, llargs, self.funclet(fx), instance);
for &(tmp, size) in lifetime_ends_after_call {
bx.lifetime_end(tmp, size);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

lifetime_end after a musttail call will create ill-formed LLVM IR: "the call must immediately precede a ret instruction, or a pointer bitcast followed by a ret instruction".

It also hints at more fundamental challenge. Arguments using PassMode::Indirect are passed as pointers to stack allocations which will be deallocated by musttail call.

Perhaps it would make more sense to bug! in the situation, until PassMode::Indirect arguments are actually supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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.

6 participants