Skip to content

Explicit Tail Calls - #3407

Open
phi-go wants to merge 97 commits into
rust-lang:mainfrom
phi-go:guaranteed-tco
Open

phi-go wants to merge 97 commits into
rust-lang:mainfrom
phi-go:guaranteed-tco

Conversation

@phi-go

@phi-go phi-go commented Apr 6, 2023 •

Copy link
Copy Markdown

View all comments

This RFC proposes a feature to provide a guarantee that function calls are tail-call eliminated via the become keyword. If this guarantee can not be provided an error is generated instead.

Rendered

For reference, previous RFCs #81 and #1888, as well as an earlier issue #271, and the currently active issue #2691.

Rust tracking issue: rust-lang/rust#112788

Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
@ehuss ehuss added the T-lang Relevant to the language team, which will review and decide on the RFC. label Apr 6, 2023
Comment thread text/0000-guaranteed-tco.md Resolved
@Robbepop

Robbepop commented Apr 6, 2023

Copy link
Copy Markdown
Contributor

thanks a ton @phi-go for all the work you put into writing the RFC so far! Really appreciated! 🎉

Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
@clarfonthey

Copy link
Copy Markdown
Contributor

While I personally know the abbreviation TCO, I think that it would be helpful to expand the acronym in the issue title for folks who might not know it at first glance.

@phi-go phi-go changed the title Guaranteed TCO Guaranteed TCO (tail call optimization) Apr 6, 2023
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
Comment thread text/0000-guaranteed-tco.md Resolved
@VitWW

VitWW commented Apr 6, 2023 •

Copy link
Copy Markdown

An alternative is to mark a function (like in OCaml), not a return keyword:

recursive fn x() {
    let a = Box::new(());
    let b = Box::new(());
    y(a);
}

@Robbepop

Robbepop commented Apr 6, 2023 •

Copy link
Copy Markdown
Contributor

An alternative is to mark a function (like in OCaml), not a return keyword:

recursive fn x() {
    let a = Box::new(());
    let b = Box::new(());
    y(a);
}

The RFC already highlights an alternative design with markers on function declarations and states that tail calls are a property of the function call and not a property of a function declaration since there are use cases where the same function is used in a normal call and a tail call.

@digama0

digama0 commented Apr 6, 2023 •

Copy link
Copy Markdown
Contributor

Note: this may be suitable either as a comment on #2691 or here. I'm assuming interested parties are watching both anyway.

The restriction on caller and callee having the exact same signature sounds quite restrictive in practice. Comparing it with [Pre-RFC] Safe goto with value (which does a similar thing to the become keyword but for intra-function control flow instead of across functions), the reason that proposal doesn't have any requirements on labels having the same arguments is because all parameters in all labels are part of the same call frame, and when a local is used by different label than the current one it is still there in memory, just uninitialized.

If we translate it to the become approach, that basically means that each function involved in a mutual recursion would (internally) take the union of all the parameters of all of the functions, and any parameters not used by the current function would just be uninitialized. There are two limitations to this approach:

  • It changes the calling convention of the function (since you have to pad out the arguments with these uninitialized locals)
  • The calling convention depends on what other functions are called in the body

I don't think this should be handled by an actual calling convention label though. Calling these "rusttail" functions for discussion purposes, we have the following limitations:

  • You can't get a function pointer with "rusttail" convention
  • You can't use a "rusttail" function cross-crate

The user doesn't have to see either of these limitations directly though; the compiler can just generate a shim with the usual (or specified) calling convention which calls the "rusttail" function if required. Instead, they just observe the following restrictions, which are strictly weaker than the one in the RFC:

  • If you become a function in another crate, the arguments of caller and callee have to match exactly
  • If you only become a function in the same crate, there are no restrictions
  • (If f tail-calls a cross-crate function g and also an internal function h, then f,g,h must all have the same arguments)

Comment thread text/0000-guaranteed-tco.md Resolved
@haberman

Copy link
Copy Markdown

Note that if we have (1), a third party crate could provide (2)/(3) with a macro [...]

I don't think that's quite right, because the constraints imposed by (1) will probably be more restrictive than necessary on some architectures.

Take this example from Clang, which mostly does (1) now: https://godbolt.org/z/7dqqYTvEz

Without musttail, the compiler can trivially tail-call optimize this call. But if you use musttail, the example fails to compile because the call does not satisfy the "portable" rules. If we did (3) instead, this code could work on platforms that support it (like x86-64).

Also I think (3) and (2) looks the same, only that (3) lacks documentation on what platform is not supported.

I think (3) can better accommodate the fact that different platforms have different constraints on when a tail call is possible to perform.

For example, here is a variation of the previous example where we compile the code for both x86-64 and x86. For x86-64, the call can be tail-call optimized because that platform uses a register-based calling convention. On x86, the same code cannot be tail call optimized because arguments are passed on the stack: https://godbolt.org/z/xc4oPze4b

Since different platforms can have different levels of support for tail calls, it's not granular enough to make it a binary choice whether a platform supports tail calls or not.

It would be terrible if in unsupported platforms, become compiled into a regular call.

I fully agree on this.

@haberman

Copy link
Copy Markdown

Regarding emulation, I'm curious about two things:

Is there a discussion somewhere about the technical details of how this emulation would work? Is there prior art for it? Can tail calls be emulated in cases where the target function is not statically known?

Is there a clear use case where a user would benefit from this emulation? In all of the cases where I've used musttail, it's because we want the machine code to have true tail calls in it. If Rust were a LISP-like language where tail recursion was the primary idiomatic means of iteration, it would make more sense to me, but Rust has rich facilities for imperative iteration. In a low-level language like Rust, it seems beneficial to make the compiler bias towards being predictable; emulating a missing arch-specific feature seems somewhat against that spirit, at least from my perspective as a Rust outsider. I would hate to see a lot of work expended on emulation if few users want it in practice.

@Jules-Bertholet

Jules-Bertholet commented Jul 12, 2025 •

Copy link
Copy Markdown
Contributor

Whether tail calls can be performed depends on the function call ABI for the platform, and Rust’s default ABI is unstable. Whether tail calls can be performed on a particular platform must not depend on the unstable implementation details of extern "Rust".

I think, if we were to go down the “per-call-and-target support” route, it would need to apply only to stable, non-default ABIs like "C" and "system". The RFC also mentions the possibility of a dedicated tail-call ABI in its alternatives section.

Comment thread text/0000-explicit-tail-calls.md Resolved
@ssokolow

Copy link
Copy Markdown

Note that if we have (1), a third party crate could provide (2)/(3) with a macro [...]

I don't think that's quite right, because the constraints imposed by (1) will probably be more restrictive than necessary on some architectures.

(3), maybe, depending on how you interpret it... but (2) could work if the crate's purpose is to abstract over a big cfg expression of which platforms support musttail.

@DemiMarie

Copy link
Copy Markdown

I think the best approach is to make extern "rust" use a callee-pops calling convention for which LLVM always supports tail calls on all platforms. I believe this includs fastcall. This is the approach used by GHC, though they use a custom calling convention.

Comment thread text/0000-explicit-tail-calls.md Outdated

Since `#[track_caller]` adds an argument, `#[track_caller]` functions can only tail call other `#[track_caller]` functions. So the following example would not compile.
However, removing `#[track_caller]` should not be a breaking change, as [it is not part of the stable API surface](https://github.com/rust-lang/rust/issues/87401#issuecomment-910628405) for Rust.
For tail calls, however, removing an argument is a breaking change, so functions with `#[track_caller]` are **not** allowed to use `become`. ([Though, loosening this restriction might be possible.](https://github.com/rust-lang/rfcs/pull/3407/files/3304fee7542a56e3c671b8ce8a85070904cbf2ba#r2202286180)). Additionally, tail calling into a `#[track_caller]` function from a non-tracking function [might require growing the stack](https://github.com/rust-lang/rfcs/pull/3407/files/3304fee7542a56e3c671b8ce8a85070904cbf2ba#r2202286180), as this would defeat the purpose of tail calling, this is also **not** allowed.

@Jules-Bertholet Jules-Bertholet Jul 19, 2025 •

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.

Adding track_caller should also not be breaking. So tail-calling a track_caller fn from a non-track_caller should at most be a warning. (Note that the risk of unexpected stack growth is limited, because the track_caller callee cannot itself use become.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Just to make sure, you mean adding track_caller to a tail-called function should not be breaking? So the equivalent of adding track_caller?

If that is what you meant, I'm a bit confused. On the one hand I can see that we want track_caller to be usable transparently but on the other hand tail calls are pretty much incompatible with the idea of tracking the caller anyway. Ignoring the matching function signature issue, what caller would you want tracked for a tail called function? So should this just be added as another action that is not allowed? Or should we move track_caller into the "Unresolved questions" section as we need to figure out how this needs to work?

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.

what caller would you want tracked for a tail called function

The call would have to go through the shim that is also used for function pointers, so no caller would be tracked.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah, I didn't know there was precedent for this. If we are just not tracking the caller then we can allow tail calls into caller tracking functions, other than how I understood it. I'll move this to the drawbacks section as it is a complication we need to implement.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated: 4dfb991

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what caller would you want tracked for a tail called function

The call would have to go through the shim that is also used for function pointers, so no caller would be tracked.

The shim however is not going to be tail-calling the actual fn implementation. This means that if I have a track-caller and a non-tracker-caller and have them mutually tail-call each other, the stack usage would grow.

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.

@nbdd0121 As I point out in the thread OP, the #[track_caller] function cannot itself use become, so mutual tail calling is impossible.

@phi-go phi-go Aug 2, 2025 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The RFC was also updated earlier, let me know if anything is unclear.

rework track_caller section and move it into the drawbacks section
@phi-go

phi-go commented Aug 3, 2025

Copy link
Copy Markdown
Author

Based on the comments made by @haberman and following discussions, I think we need to consider the following open questions:

  • While for Clang matching function signatures do not guarantee portability, is this also an issue for Rust?
    • (Though, support seems to be improving in Clang / LLVM.)
    • Can we find cases for Rust code where tail calls of matching function signatures fail compilation or fail to actually tail call with LLVM?
    • What about other backends?
  • If matching function signatures cannot ensure portability, is there even any option other than to coordinate with the backend on a per-call basis?
  • Is there any push in LLVM to support [[clang::nonportable_musttail]]?
  • Is it possible to support a calling convention for per-call support?
    • A similar discussion was already had: here.
    • The choice of matching function signatures and matching calling conventions between caller and callee should allow simply substituting with new values, see discussion linked above. In theory, the calling convention should not matter in that regard (one of the reasons this RFC includes the matching signature restriction).
  • A design decision needs to be made. Should tail calls only provide the semantic guarantee (fully portable) or should a best effort for performance also be made (per-call)?
    • Note that, both can be supported independently.
    • Also note that, we will want to support the semantic guarantee (no stack overflows) in any case.
    • Caveats for fully portable (details above)
      • It might not actually be possible to be fully portable, even for matching function signatures.
      • Emulation will take extra effort to implement and it is not clear how to actually support dynamic calls using emulation.
      • Performance, a main motivation for this feature, will not be easily "controllable". Unexpected degradation is possible when switching from real tail calls to an emulated implementation. What assurances does Rust want to provide?
      • It is easier for users that just want the semantic guarantee of no stack overflows.
    • Caveats for per-call (details above)

@Robbepop

Robbepop commented Oct 26, 2025 •

Copy link
Copy Markdown
Contributor

I was wondering: would it make sense to add a way to query if the target has support for tail calls? E.g. target_feature = "tail-calls" similar to how it is already done for other architecture features: https://doc.rust-lang.org/reference/conditional-compilation.html#target_feature

This way one could use explicit tail-calls via become when compiling for a target that supports them and otherwise provide a fallback at compile-time. Without such a query we have to introduce a crate feature and shift responsibility to the user of the crate OR simply accept that the crate won't compile on platforms without tail-call support.

If we agree that this is needed: do we require a separate RFC for that?

@WaffleLapkin

Copy link
Copy Markdown
Member

@Robbepop so, two parts:

  • this RFC is not merged yet and could be changed
  • I think an informal T-lang consensus from a nomination (or a formal FCP if they so desire) would be enough

@programmerjake

Copy link
Copy Markdown
Member

C++ may be getting explicit tail calls as part of the language (instead of just being a non-standard extension): https://discourse.llvm.org/t/experience-with-clang-musttail/89085?u=programmerjake

@Robbepop

Robbepop commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

In case anyone wants to toy around with an interpreter using Rust's experimental become keyword for dispatch:

Wasmi v2.0.0-beta.2 includes support for the become keyword when building with the unstable and without stable crate feature like so:

cargo +nightly build -p wasmi --no-default-features --features unstable --release

There also is a CLI application to interact with Wasmi: https://crates.io/crates/wasmi_cli/2.0.0-beta.2

To install it with support for become use the following command:

cargo +nightly install wasmi_cli --version 2.0.0-beta.2 --no-default-features --features unstable,run,wasi,wat,wast,simd

This installs the wasmi binary with full support for all relevant Wasmi features and uses the become keyword for dispatch.

Note: requires a nightly Rust compiler since become is unstable. Without the unstable feature Wasmi still uses tail-call dispatch but based on LLVM's sibling call optimization and no guarantees of that actually working out - though in practice (and with optimizations enabled) it magically does. Since that's not portable, there also is the portable-dispatch crate feature to enable portable instruction dispatch at the cost of execution performance.

@fitzgen

fitzgen commented Mar 4, 2026

Copy link
Copy Markdown
Member

FWIW, you can also play with Wasmtime's Pulley interpreter in its explicit tail calls mode via:

# Compile Wasmtime with Pulley and `become` enabled.
RUSTFLAGS=--cfg=pulley_tail_calls \
    cargo +nightly build --release --features pulley

# Pre-compile a Wasm module into Pulley bytecode.
./target/release/wasmtime compile --target pulley64 example.wasm -o example.cwasm

# Execute the pre-compiled Pulley bytecode.
./target/release/wasmtime run --allow-precompiled --target pulley64 example.cwasm

Note: if you are on a 32-bit architecture, you'll need to change pulley64 to pulley32, and if you are on a big-endian architecture you will additionally need to append be, e.g. pulley32be.

The "match loop" implementation that does not use tail calls or become is here: https://github.com/bytecodealliance/wasmtime/blob/dfc36b61915fe75f0956d1fcb8279131eb280bc2/pulley/src/interp/match_loop.rs

The "tail loop" implementation that either assumes LLVM optimizes calls in tail position into true tail calls or uses the unstable become keyword depending on build configuration is here: https://github.com/bytecodealliance/wasmtime/blob/dfc36b61915fe75f0956d1fcb8279131eb280bc2/pulley/src/interp/tail_loop.rs

@haberman

Copy link
Copy Markdown

I just posted an RFC to LLVM that I hope will help the effort to adopt tail calls in Rust: https://discourse.llvm.org/t/rfc-tail-chaincc-a-maximum-efficiency-tail-call-convention/92661/1

It's just an RFC for now, but I would love to hear feedback about whether this could help address any of the difficulties you have run into trying to add first class tail call support to Rust.

@folkertdev

folkertdev commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

That is a very exciting proposal! It helps Rust in some ways, and not in others.

I'm a big fan of the ability to reach maximal performance and lifting the same-signature restriction. I think we'd just adopt that as extern "tail", which currently lowers to tailcc but intuitively already has the semantics of "do whatever makes tail calls the most efficient".

For rust we run into some specific issues


Firstly, we'll probably want tail calls to work for (the default) extern "Rust". I recently put up a PR for making become work across platforms (e.g. on wasm32, powerpc and s390x, whose standard ABI just cannot guarantee tail calls) for direct calls as rust-lang/rust#165044. That PR transforms a function that is tail-called into a continuation, and the chain of continuations is then executed.

That feature is the current way forward to at least land basic guaranteed tail call support, though the codegen is poor it at least guarantees constant stack space.

Btw I like the idea of implicitly promoting private functions (whose ABI cannot be observed) to extern "tail", but I suspect that won't be enough for rust.


A major issue with guaranteed tail calls in practice is how non-portable they really are. Rust generally values features working consistently across platforms more than C (at least historically), and so for us it isn't that valuable if only x86_64 and aarch64 support a feature: we need wasm, riscv, loongarch, powerpc, s390x, etc. to also support the feature. They don't need to be ultimately efficient, but should at least compile and not miscompile.

As an example, as far as I'm aware, musttail is currently only miscompile-free on x86_64 (and maybe x86). My PR for fixing support for structs on aarch64 llvm/llvm-project#206718 has not only not been reviewed, but there is some actual resistance to the very idea that a tail call should be able to pass a struct as an argument. Such arbitrary (platform-specific) restrictions are a non-starter for rust.

So for us, backends actually implementing the feature (or LLVM somehow being able to do so in a cross-platform way) is more important than the exact details of the ABI. Without support across a wide range of targets, the feature is unusable to us.


So, hopefully that provides some context from the rust side. Overall I really like the proposal though, and would love to see it adopted. Thanks for proposing it!

@DemiMarie

Copy link
Copy Markdown

What about:

  1. Require extern "tail" for become.
  2. On non-x86_64 backends that can make tail calls work, destructure the arguments into SSA values.
  3. On other backends, use explicit continuations.

One problem is that general tail call support tailcc is seen as an optional feature on the LLVM side, rather than a mandatory one.

@folkertdev

Copy link
Copy Markdown
Contributor

I think that is insufficient, because for any practical use of tail calls for performance (e.g. in interpreters), you want to tail call function pointers (so that the dispatch logic is duplicated at each use site, the whole thing is not inlined causing worse register allocation, etc.). The continuation approach does not work for indirect calls, because we don't know their implementation and can't generate the continuation shim.

Destructuring the structs is interesting. Still, I think it would only give use aarch64 right now, I'm not aware of any other backend (intentionally) implementing tailcc.

One problem is that general tail call support tailcc is seen as an optional feature on the LLVM side, rather than a mandatory one.

Right, I think the hope is that the current proposal might actually get standardized, which would hopefully make more backends support it properly. I'd also hope that some amount of code can be shared between LLVM backends.

@haberman

Copy link
Copy Markdown

So, hopefully that provides some context from the rust side. Overall I really like the proposal though, and would love to see it adopted. Thanks for proposing it!

Thanks for the feedback! I'll look into the issues that you raised. Some of the context you linked to is new to me.

Require extern "tail" for become.

One other idea I've entertained is limiting become to cases where caller and callee are in a single crate, in which case you could transparently flip the calling convention to tailcc when become is used. But that might be tricky to statically analyze when function pointers are in play (which is a common case when using become in interpreters or parsers).

@DemiMarie

Copy link
Copy Markdown

The continuation approach does not work for indirect calls, because we don't know their implementation and can't generate the continuation shim.

Could one generate a shim that indirectly called the function pointer? That might only require the signature.

@Jules-Bertholet

Jules-Bertholet commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

The continuation approach does not work for indirect calls, because we don't know their implementation and can't generate the continuation shim.

If we have a dedicated tail-call ABI, we can just say that the "shim" is the function's ABI. So indirect calls should work, at least for matching argument types.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-lang Relevant to the language team, which will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.