Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-memory support in AssemblyScript? #2716

Open
titzer opened this issue Jul 7, 2023 · 5 comments
Open

Multi-memory support in AssemblyScript? #2716

titzer opened this issue Jul 7, 2023 · 5 comments

Comments

@titzer
Copy link

titzer commented Jul 7, 2023

Feature suggestion

Hello, the WebAssembly multi-memory proposal has advanced to Phase 3, and there are several engines that have implemented the proposal. One perennial problem is that no mainstream language has support for multiple memories, so there is a bit of a chicken-and-egg problem. However, there are several important use cases for multiple memories, such as virtualizing/layering APIs, linking, and instrumentation with Wasm.

I filed WebAssembly/multi-memory#45, where some discussion is happening on how this would work for C/C++ (and maybe by extension, Rust, using LLVM?).

I'm curious if there are any plans on supporting multiple memories in AssemblyScript?

@dcodeIO
Copy link
Member

dcodeIO commented Jul 7, 2023

I believe support for multi-memory wouldn't be too hard to add to the respective intrinsics, though some of them would become a little unweildy (e.g. store<T>(ptr: usize, value: auto, immOffset?: usize, immAlign?: usize, immMemory?: i32))). Also, last time I checked, Binaryen was occasionally missing arguments for the necessary memory immediate on some C API functions. Affected functions might be SIMD, threads, GC and/or stringref related iirc. Not entirely certain about the current status there.

@ashleynh
Copy link

Binaryen has arguments for the necessary memory immediate on SIMD and threads. GC doesn't have memory needs afaik, but you're right, Binaryen has no support of multi-memory in stringref. Let me know if there are any other C API functions you run into that are missing the memory argument.

@tareksander
Copy link

I'd like to use multiple memories for an ECS system, what's the status on this? I wouldn't need many features, just a memory immediate for the standard WASM types.

@HerrCai0907
Copy link
Member

it is not too hard to add some builtin load / store api to support multiple memory.
But if we want to integrate the whole as with multiple memory, The major issue is how can we map the current allocator and gc object to multiple memory.

@tareksander
Copy link

C++ (and Rust as an experimental feature) have allocator interfaces and you can specify the allocator to use for the standard library collections when instantiating them. Full language support would likely need to tag pointers with the memory index they point to at compile time (because IIUC the load instruction only take a memory index immediate, so the destination has to be known at compile time) and/or you specify a range of memories as a compiler option for which simple functions with all the load/store operations for each memory are generated to allow dynamic access (that would then need something like a fat pointer type which has the dynamic memory index included). You would then instantiate an allocator for each additional memory you want. I don't know enough about garbage collectors to know if it is even possible to have multiple GCs in a program like this. Are there destructors in AssemblyScript? I think using a traditional allocator in another memory with reference-counted pointers should be no problem, right?

For my use case that's all unneeded: I made a generator for accessing structures in ArrayBuffers with Typescript type support, and I could easily make it so that AssemblyScript functions to load and store are generated, too. Since the number and order of memories is defined for me, I can make simple access functions with the memory index immediate. I only need the WASM builtins with a memory immediate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants