-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
wasm output contains compiler_rt exports #2910
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
Comments
That's correct. Unused functions are not garbage collected until linking. Linking happens with build_exe. |
Ah! so I should be using
I guess that |
I'm going to leave this open to investigate because my memory is fuzzy about how this is supposed to work. |
The documentation also suggests to compoile as follows:
https://ziglang.org/documentation/master/#Freestanding As of 0.7.0, compiling the example with
|
This suggest we are not passing the |
I'm not sure what By adding a dummy |
Build an executable instead. If you are building a reactor, still add a std.mem.doNotOptimizeAway(entrypoint1);
std.mem.doNotOptimizeAway(entrypoint2);
... Unused functions will not be present in your module. |
thanks @jedisct1 for the workaround, this brings the binary size down to 300 bytes, but it is clearly neither comfortable nor how things are intended to be used.
This does not appear to be fully correct, compiling the WASM example without an added main entrypoint or any other workarounds for a native target (in this case
I would expect an equivalent result once the wasm target is added - a very small wasm file that contains only the symbols that are marked as exported, and whichever symbols these require to operate correctly. |
@s-ol where you able to get this to work? For me it still results in the exported function not being available. const std = @import("std");
export fn add(a: i32, b: i32) i32 {
return a + b;
}
pub fn main() void {
std.mem.doNotOptimizeAway(add);
} |
I am experiencing the same issue. Nothing I export is actually available, the only exported function is |
A pub export fn myfunction() void {
...
} Then compile with: zig build-lib -OReleaseSmall -target wasm32-wasi -dynamic src.zig Resulting Wasm code:
This is with Zig 0.8.0. |
I think this issue can be closed now. I just did the following testing with zig 0.8.0: $ cat test.zig
pub export fn add(a: u32, b: u32) u32 {
return a + b;
}
$ zig build-lib -OReleaseSmall -target wasm32-wasi -dynamic test.zig
$ ls -l test.wasm
-rwxr-xr-x 1 s-ol s-ol 298 Jun 9 13:59 test.wasm
$ wasm-dis test.wasm
(module
(type $none_=>_none (func))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(global $__stack_pointer (mut i32) (i32.const 66560))
(global $global$1 i32 (i32.const 1024))
(global $global$2 i32 (i32.const 1024))
(global $global$3 i32 (i32.const 1024))
(global $global$4 i32 (i32.const 66560))
(global $global$5 i32 (i32.const 0))
(global $global$6 i32 (i32.const 1))
(memory $0 2)
(export "memory" (memory $0))
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
(export "add" (func $add))
(export "__dso_handle" (global $global$1))
(export "__data_end" (global $global$2))
(export "__global_base" (global $global$3))
(export "__heap_base" (global $global$4))
(export "__memory_base" (global $global$5))
(export "__table_base" (global $global$6))
(func $__wasm_call_ctors
)
(func $add (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $1)
(local.get $0)
)
)
;; custom section "producers", size 16
) 328 bytes doesn't sound excessive to me, and while I'm not sure whether all of the symbols present are needed, I don't see the compiler_rt exports anymore. |
I see! I was missing |
Nod, we should definitely document this! Maybe keep the issue open until this is done. |
When this is documented it would be really useful to have a build.zig version I tried something along the lines of this but it failed after the second build (on Windows) by deleting the .wasm file and leaving unresolvable sym links in the zig-out directory instead of the wasm. It output .a and .o instead too - only after the first build though. .unversioned worked. Adding what @matthewp suggested kept the add function being available More of the build.zig
install() did not seem to put the .wasm file anywhere. Setting the output directory worked though. |
@daurnimator maybe we could close this issue, or perhaps rename it to clarify that at this point what's lacking is mostly just documentation that suggests using |
I'm running into this as well. In particular if the compiler ends up using I ran into this while building for the wasm4 fantasy console, but stripped it down to a minimal example. Somewhat silly code, but returning the array results in a generated call to
If I tweak the code such that no call to
|
@sporksmith I've been looking at that kind of thing too. This is an interesting discussion mentioning memory and wasm4 |
One workaround I've found is to use a
And the same but without the
|
Thanks! Yeah in my real code I'm using the wasm4 template, which uses the |
Workaround for ziglang/zig#2910. See in particular: ziglang/zig#2910 (comment) ziglang/zig#2910 (comment)
In case you're wondering: When we switched to having the linker frontend in stage2 we did no longer have access to exported symbols from stage1, so we export all public symbols by default. In the upcoming week, I'll implement a fix for this. |
Workaround for ziglang/zig#2910. See in particular: ziglang/zig#2910 (comment) ziglang/zig#2910 (comment)
foo.c:
Related #2062 #2290 #2369
The text was updated successfully, but these errors were encountered: