Stream: cranelift

Topic: Implementing Winch call for aarch64


view this post on Zulip marin (Dec 05 2024 at 16:45):

Hello!
Today, I took a stab at implementing call for aarch64 in winch, I got the basic plumbing going, taking inspiration from x64, and cranelift, but I'm left with a couple of questions:
Right now, I have hardcoded the calling convention to SystemV, but my understanding is that we should somehow get this information from the assembler instead. I'm unsure how to do this, though, and the x64 implementation has a hardcoded x64. Can you guide me here?

view this post on Zulip Chris Fallin (Dec 05 2024 at 16:54):

Re: register offsets: what you're probably seeing (just my educated guess) is that Wasm parameters come after the "caller vmctx" and "callee vmctx" parameters in Wasmtime's internal calling convention; so a wasm function with one arg will indeed take that one arg in x2/w2 (depending on width)

view this post on Zulip Chris Fallin (Dec 05 2024 at 16:54):

cc @Saúl Cabrera for the design questions around calling convention

view this post on Zulip marin (Dec 05 2024 at 16:59):

ohh that makes a lot of sense! is that internal calling convention described anywhere?

view this post on Zulip Chris Fallin (Dec 05 2024 at 17:02):

I don't think we've formally documented it separately (there are probably comments somewhere); it's kind of implicit in the code unfortunately, sorry :-/

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:02):

You can find the documentation in the ABI module https://github.com/bytecodealliance/wasmtime/blob/main/winch/codegen/src/abi/mod.rs#L15

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant - bytecodealliance/wasmtime

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:02):

It's explicitly stated how the ABISig behaves.

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:06):

It's great that you've gotten calls almost working! For future reference and to avoid the risk of duplicated efforts it's advised to reach out through Zulip or comment in this issue https://github.com/bytecodealliance/wasmtime/issues/8321 whenever you'd like to implement a new Aarch64 instruction. For now calls are assigned to me, since it's the next immediate instruction that I was planning on working next, however, I haven't started, so if you have no objections, I'll add your github handle next to it.

Winch's support for Aarch64 is minimal. The objective of this issue to track the missing items in order to bring the Aarch64 MacroAssembler to parity with the x64 implementation. If you're interest...

view this post on Zulip marin (Dec 05 2024 at 17:08):

I will next time for sure! I just started out of curiosity and then fell into the rabbit hole. I'll assign myself the other instruction I had to implement to get things working then.

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:24):

I think you won't be able to edit the issue -- I can do that for you though. Feel free to send me your GH handle and the other instruction either here or DM!

view this post on Zulip marin (Dec 05 2024 at 17:29):

here's my handle: MarinPostma

the other instruction I had to implement was trapz and address_at_vmctx

view this post on Zulip marin (Dec 05 2024 at 17:35):

@Saúl Cabrera how would you trigger a libcall from wasm?

view this post on Zulip marin (Dec 05 2024 at 17:37):

a memcpy, I guess?

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:39):

It depends what exactly you mean by libcall in this context. Are you trying to call out to some builtin function?

view this post on Zulip marin (Dec 05 2024 at 17:40):

I am trying to enter the CalleeKind::LibCall branch of the Masm::call's callee argument

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 17:43):

Usually in that case you'd load the address of the function into a register and emit a call using that register, similar to what's in there for x64 https://github.com/bytecodealliance/wasmtime/blob/main/winch/codegen/src/isa/x64/asm.rs#L1290

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant - bytecodealliance/wasmtime

view this post on Zulip marin (Dec 05 2024 at 17:47):

sorry, I did not express myself correctly. What I mean to do is to write some wasm code that will emit a libcall instruction. I tried with Memory.fill, but it is just a regular call towasmtime_builtin_memory_fill

view this post on Zulip marin (Dec 05 2024 at 17:48):

I wan to test my implementation

view this post on Zulip Saúl Cabrera (Dec 05 2024 at 18:00):

I don't think there's a great way to hit the libcall case in aarch64, the libcall is used as a fallback mechanism for when the target architecture doesn't offer support for certain operations (e.g., f32_floor). Most (if not all) of the float operations for aarch64 have been implemented and none of those require a libcall https://github.com/bytecodealliance/wasmtime/blob/main/winch/codegen/src/isa/aarch64/masm.rs#L353

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant - bytecodealliance/wasmtime

view this post on Zulip marin (Dec 06 2024 at 15:27):

@Saúl Cabrera I believe it is done: https://github.com/bytecodealliance/wasmtime/pull/9751, can I do anything else? More instructions?

Implement the following MASM instruction for the aarch64 platform: call address_at_vmctx trapz I initially planned to only implement call, but implementing the tests uncovered the necessity to im...

view this post on Zulip Saúl Cabrera (Dec 06 2024 at 15:48):

Thanks, I'll take look today! If you're interested, here's the list of missing instructions for aarch64 https://github.com/bytecodealliance/wasmtime/issues/8321 feel free to pick -- by the way, we also have the #winch stream, so feel free to move any other conversation there regarding other instructions/questions/etc.

Winch's support for Aarch64 is minimal. The objective of this issue to track the missing items in order to bring the Aarch64 MacroAssembler to parity with the x64 implementation. If you're interest...

Last updated: Dec 23 2024 at 13:07 UTC