Stream: cranelift

Topic: Is it possible to use FuncCursor after optimization?


view this post on Zulip Stanley (Apr 17 2026 at 01:21):

Is it possible for Cranelift to automatically perform tail call optimization, as in replacing calls with return_calls in Cranelift IR? This is something that I really need for my language.

If not, would I be able to manually replace call with return_call by iterating through the IR with a FuncCursor? And if so, could the FuncCursor be used after Cranelift's optimizations?

view this post on Zulip Chris Fallin (Apr 17 2026 at 03:44):

No, yes, and yes respectively :-)

As in: no, we don't have TCO by default. Yes, it should be fairly straightforward to implement by iterating through a function and, if you see a call followed by a return of its value(s), replacing that with a return_call. (Note that we explicitly don't do this by default because Wasm prohibits the optimization: an infinitely tail-recursive program that doesn't use tail calls must run out of stack, to prevent programs from depending on a sometimes-present optimization; and Cranelift's default settings are sometimes driven by Wasmtime's needs.) And, yes, there's nothing stopping you from doing more with the FuncCursor after you, yourself, use it to replace one instruction with another.

view this post on Zulip Stanley (Apr 17 2026 at 04:06):

Cool. I have implemented it since I last sent that message and it seems to be working, thanks!


Last updated: May 03 2026 at 21:15 UTC