dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.37k stars 4.75k forks source link

tail calls in wasm #70957

Open unicomp21 opened 2 years ago

unicomp21 commented 2 years ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

Could someone hold my hand on this one? Given tail calls are not currently supported in webassembly, how is the "tail" instruction being implemented in Blazor?

Describe the solution you'd like

I'm wondering if we can reuse the same implementation on the emscripten side for co_await, until tail calls land in webassembly?

Additional context

No response

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

EgorBo commented 2 years ago

I hope the mono team will correct me if I am wrong but it looks like explicit tails calls are handled well in Mono-Interpreter (including wasm) but it's unlikely they're handled in WASM-AOT since, as you said, there is no built-in WASM support for that. So maybe mono-aot fallbacks to interp for methods with explicit tail calls inside?

PS: quoting ECMA 335: image

ghost commented 2 years ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
### Is there an existing issue for this? - [X] I have searched the existing issues ### Is your feature request related to a problem? Please describe the problem. Could someone hold my hand on this one? Given tail calls are not currently supported in webassembly, how is the "tail" instruction being implemented in Blazor? ### Describe the solution you'd like I'm wondering if we can reuse the same implementation on the emscripten side for co_await, until tail calls land in webassembly? ### Additional context _No response_
Author: unicomp21
Assignees: -
Labels: `question`, `arch-wasm`, `untriaged`, `area-Codegen-meta-mono`
Milestone: -
unicomp21 commented 2 years ago

Is this an argument for having clang target wasm3?

unicomp21 commented 2 years ago

Or maybe clang could target the byte code being used by the mono interpreter?

lambdageek commented 2 years ago

Egor is correct. The interpreter supports tailcalls, but AOT doesn't.

Or maybe clang could target the byte code being used by the mono interpreter?

The interpreter's bytecode is failry specific to the needs of a .NET runtime. There would not be much value in trying to use it as a general-purpose clang backend.


For Emscripten, it might be possible to use something like Asyncify (or some rewriting approach inspired by Asyncify) to transform C++ coroutines to tailcalls by bouncing back into JS.

lambdageek commented 2 years ago

Keeping this open - when wasm adds tailcall ops, we should revisit AOT support

unicomp21 commented 1 year ago

wasm tail calls get released in chrome 113, right?