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 10 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 some bikeshedding to be done on how we should go about implementing this for other backends, 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 2 times, most recently 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

This comment has been minimized.

@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
@xacrimon
Copy link
Author

xacrimon commented Jul 21, 2025

@rustbot author

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

This was me mistakenly marking the rest @run-fail instead of @run-crash, that was intentional and it has now been fixed. That one is marked no-tce and doesn't use become. There is another one called tce instead of no-tce which uses become and is set to @run-pass.

@xacrimon
Copy link
Author

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?

Ah, my knowledge is outdated then. I've edited my comment, though point still stands for backends that aren't llvm/gcc.

@xacrimon
Copy link
Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 21, 2025
@xacrimon xacrimon requested review from bjorn3 and tmiasko July 21, 2025 18:37
@xacrimon
Copy link
Author

@WaffleLapkin I'm going to go ahead and guess this is something you want to have a look at.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 22, 2025

@bors r+

Thanks! Glad we finally support it!

@bors
Copy link
Collaborator

bors commented Jul 22, 2025

📌 Commit ecf34e1 has been approved by oli-obk

It is now in the queue for this repository.

@rust-log-analyzer

This comment has been minimized.

@xacrimon xacrimon requested a review from fmease July 24, 2025 01:06
@xacrimon xacrimon force-pushed the explicit-tail-call branch from ecf34e1 to 4028c67 Compare July 24, 2025 01:07
@xacrimon
Copy link
Author

@rustbot ready

@xacrimon
Copy link
Author

xacrimon commented Jul 24, 2025

@fmease @oli-obk This has now been rebased, please r+ when possible.

@fmease
Copy link
Member

fmease commented Jul 24, 2025

@bors r=oli-obk

@bors
Copy link
Collaborator

bors commented Jul 24, 2025

📌 Commit 4028c67 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2025
fmease added a commit to fmease/rust that referenced this pull request Jul 24, 2025
…-obk

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

This PR implements codegen of explicit tail calls via `become` in `rustc_codegen_ssa` and support within the LLVM backend. Completes a task on (rust-lang#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 some bikeshedding to be done on how we should go about implementing this for other backends, 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](https://github.com/xacrimon/tcvm).
bors added a commit that referenced this pull request Jul 24, 2025
Rollup of 16 pull requests

Successful merges:

 - #143374 (Unquerify extern_mod_stmt_cnum.)
 - #143838 (std: net: uefi: Add support to query connection data)
 - #144014 (don't link to the nightly version of the Edition Guide in stable lints)
 - #144094 (Ensure we codegen the main fn)
 - #144218 (Use serde for target spec json deserialize)
 - #144221 (generate elf symbol version in raw-dylib)
 - #144232 (Implement support for `become` and explicit tail call codegen for the LLVM backend)
 - #144240 (Add more test case to check if the false note related to sealed trait suppressed)
 - #144247 (coretests/num: use ldexp instead of hard-coding a power of 2)
 - #144276 (Use less HIR in check_private_in_public.)
 - #144278 (add Rev::into_inner)
 - #144317 (pass build.npm from bootstrap to tidy and use it for npm install)
 - #144320 (rustdoc: avoid allocating a temp String for aliases in search index)
 - #144334 (rustc_resolve: get rid of unused rustdoc::span_of_fragments_with_expansion)
 - #144335 (Don't suggest assoc ty bound on non-angle-bracketed problematic assoc ty binding)
 - #144358 (Stop using the old `validate_attr` logic for stability attributes)

r? `@ghost`
`@rustbot` modify labels: rollup
@jieyouxu
Copy link
Member

Failed in rollup: #144387 (comment)

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

error: test did not crash! code=Some(0) so test would pass with `run-pass`
status: exit status: 0
command: cd "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/explicit-tail-calls/recursion-no-tce" && RUSTC="/checkout/obj/build/unpacked-dist/rustc-nightly-x86_64-unknown-linux-gnu/rustc/bin/rustc" RUST_TEST_THREADS="36" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/explicit-tail-calls/recursion-no-tce/a"
--- stdout -------------------------------
1000000
------------------------------------------
stderr: none

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 24, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jul 24, 2025

Yea... I think you can just remove that test, it depends too much on optimizations and stack size and other things outside of our control.

@xacrimon xacrimon force-pushed the explicit-tail-call branch from 4028c67 to 58c6633 Compare July 24, 2025 16:05
@xacrimon
Copy link
Author

@rustbot ready

Alright, that makes sense. It's been removed.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 24, 2025
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-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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants