Rust & Wasm multi-memory feature

Wasm has been steadily progressing and acquiring many new interesting features over the past years, and Rust continues to be a first-class citizen of the Wasm ecosystem. One such very interesting feature, is the multi-memory proposal.

Based on some very basic preliminary research though, it seems that actually using the feature in Rust will be tricky at best, since Rust (and LLVM for that matter, I think) views memory as a single contiguous region. This seems to be almost at odds with how Wasm implements multiple memories - as an additional immediate for the memory instructions.

My question is henceforth - are there yet any plans to implement this feature for the Rust+Wasm combo? Considering all the implications, I imagine this'll be a rather formidable task.

1 Like

LLVM does have support for multiple address spaces, but exposing them to Rust is not going to be a very easy task indeed. I am not aware of any public work on this.

Well, good to know that at least LLVM supports this! Though I can scarcely even imagine what kind of mechanism or syntax could be used in Rust to implement that.

The AVR backend has support for different address spaces, because it has to target a Harvard architecture.

On the AVR, it doesn't need any additional syntax. What would you use this for? Would the allocator API suffice?

AVR either makes this work automatically by assuming all code is in flash and all data is in RAM (even read-only data), or by using fat pointers everywhere that carry their address space with them, which would be very inefficient.

AVR-GCC takes the former approach. It still requires even read-only data to be loaded into RAM on startup. To avoid that, it also provides PROGMEM, which exposes data in flash memory at the language level and avoids the RAM copy. This sort of mechanism would be required to make the WASM feature work in a useful fashion (keep in mind that the WASM proposal allows an arbitrary number of address spaces, AVR always has exactly 2, so it is somewhat easier to deal with).

It's unfortunate that this is apparently not documented anywhere for Rust/LLVM.

Perhaps WASM folks will come up with a smart solution that doesn't require language-level support for this feature, but I'm not sure how that would look.

Pardon me if I'm wrong, but as far as I can tell the WASM multi-memory proposal doesn't exactly use fat pointers. There's simply a new immediate added to the memory instructions which indicates which memory to operate on - so the compiler has to know which ops go to which memory during compilation, which complicates things. That's why I was talking about special syntaxes.

One use-case I was hoping to explore was to expose shared memory between modules while also giving each their own private heap. I think there's also interesting potential for exposing host-side memory as readonly to modules.

Yeah, I really hope so. Would be a shame for Rust to miss out on such a useful feature