max-dau opened PR #12506 from COLORADIO-Project:fibers-on-riscv32imac to bytecodealliance:main:
This adds an implementation for
riscv32imac(and other architectures with identical registers) to the internal fiber crate. This enables usage of theasyncfeature of wasmtime on such platforms.This feature was first discussed here on Zulip: #wasmtime > Wasmtime-fiber on RISCV32
Our concrete implementation was also discussed here: #wasmtime > Question about 64-bit RISC-V fiber implementation
max-dau requested cfallin for a review on PR #12506.
max-dau requested wasmtime-core-reviewers for a review on PR #12506.
cfallin submitted PR review:
Thanks! This looks basically fine to me except for the alignment issue below and formatting nits.
cfallin created PR review comment:
The spacing/formatting here and below is nonstandard. Could you ensure that there is no space before commas (see e.g. line 46
addi sp , sp , -0x40), and one space after commas (see e.g. this linesw ra,-0x4(sp))?
cfallin created PR review comment:
If we want to be 16-byte-aligned, these two fields together are 8 bytes (two 32-bit pointers) so the whole struct is going to be off by 8 bytes, right?
Do we need additional padding between
raandlast_sp?
max-dau updated PR #12506.
max-dau created PR review comment:
I've adjusted the spacing around the commas in the newest commit. The spacing was also copied from the riscv64 implementation, so I've adjusted the spacing there too.
max-dau submitted PR review.
max-dau submitted PR review.
max-dau created PR review comment:
I was confused about this as well. In
nostd.rs(andunix.rs), only the spot betweenraandlast_spis explicitly documented to be 16-byte aligned. We also only move the stack pointer to that address, not to the very end of the struct.I've tested both a version with and a version without 8 bytes of padding between
raandlast_sp. Both work fine on my device. I'm honestly not sure whether padding is needed there. If you think we should add padding there, I am happy to add a commit introducing this padding.
cfallin submitted PR review.
cfallin created PR review comment:
So I think a few things are going on here:
- The 16-byte alignment is required by the RISC-V ISA. Nothing in hardware enforces it (unlike aarch64, where an unaligned
spwill actually cause a trap), but other functions are free to rely on stack alignment; so, for example, if we call some function in the fiber that eventually stores something to the stack with an aligned store, lack of alignment could cause a hard-to-diagnose segfault somewhere else. So we do need to ensure the stack as seen by the entry function to the fiber is aligned.- On our 64-bit architectures, taking the end of a page and aligning the end of this struct with it will naturally provide 16-byte alignment to the top of the fiber save-frame, because the two words at the end -- the
last_spand therun_result-- are both pointers, i.e. 64-bit (8-byte) values.- This is the first time we have (i) a 32-bit ISA with (ii) a 16-byte alignment requirement. So we need to be mindful of alignment here.
Given all that, I think if we put 8 bytes of padding before
last_sp, we'll have satisfied the alignment requirements.
cfallin edited PR review comment.
max-dau updated PR #12506.
max-dau submitted PR review.
max-dau created PR review comment:
I've added the padding in the newest commit, thanks for taking the time to look into this!
cfallin submitted PR review:
Thanks!
cfallin has enabled auto merge for PR #12506.
cfallin added PR #12506 Add fiber implementation for riscv32imac to the merge queue.
cfallin merged PR #12506.
cfallin removed PR #12506 Add fiber implementation for riscv32imac from the merge queue.
Last updated: Feb 24 2026 at 04:36 UTC