-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
wasm cannot import globals #4866
Comments
This one is really interesting! Do you have any particular example in mind? The example you pasted in in the issue description is actually not corresponding to the Zig's source since ; globals.wat
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "env" "frobinate" (func $frobinate (type 0)))
(import "env" "glob" (global (;0;) i32))
(export "foo" (func $foo))
(func $foo (type 1)
global.get 0
call $frobinate
return)) Then, we could link that with the following module at runtime: ; globals_env.wat
(module
(type (;0;) (func (param i32)))
(global i32 (i32.const 10)) (export "glob" (global 0))
(export "frobinate" (func $frobinate))
(func $frobinate (type 0) (param i32)
return)) You can actually verify that it (at least) doesn't trap using Wasmtime like so:
Now, to the issue at hand, I think this might be a problem with |
Update: I’ve confirmed that it’s the limitation of LLVM. I’m not sure what the best course of action here is. We could submit an upstream proposal/request for this I guess and work with the LLVM devs at getting it implemented. Thoughts? |
With the latest compiler, this is no longer a compile error but instead a miscompilation: (module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "env" "frobinate" (func $frobinate|env (type 0)))
(func $__wasm_call_ctors (type 1))
(func $foo (type 1)
i32.const 0
i32.load
call $frobinate|env)
(memory (;0;) 2) Instead of an import, this function uses the (non-exposed) 0 address in the internal memory. |
This is actually working as intended. This does mean above use case is not supported (in no other language such as Rust, C, etc really). While above behavior is correct, it means the user must be aware of the semantics of how declarations live in the linear memory rather than wasm's globals which is very unfortunate. One possible solution would be to support something like |
There is, actually. Since this patch got merged, you can create wasm globals by using |
Thanks for that! Not sure how I missed that but glad I was proven wrong. Considering stage2 supports address spaces we could support this in both backends then. |
Hi, do you have any workaround to this issue? Or should I ignore it and just use bunch of extern constant functions? |
Wasm allows you to import global variables:
But I can't seem to do this in Zig because lld attempts to resolve the variable symbols at link time:
The text was updated successfully, but these errors were encountered: