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

lto optimization not usable for wasm #18

Open
KaruroChori opened this issue Jan 21, 2025 · 4 comments
Open

lto optimization not usable for wasm #18

KaruroChori opened this issue Jan 21, 2025 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed wasm-sandbox

Comments

@KaruroChori
Copy link
Collaborator

Somehow, the lto optimizations are breaking the wasm build.
This might be related to limits of the stack being reached.

RuntimeError: Out of bounds memory access (evaluating 'instance.exports')
      at vs.templ.js.wasm-function[_GLOBAL__sub_I_pugixml.cpp]
      at vs.templ.js.wasm-function[__wasm_call_ctors]
      at vs.templ.js.wasm-function[_start]
      at start (node:wasi:1023:26)
      at /archive/shared/projects/vs.templ/build/vs.templ.js:49:11

The solution would be to ask for a bigger stack, but I don't know how to do that in zig cc and the usual -stack-size=xxxx is not working.
If implemented, lto optimization would result in a blob going from 6.5MB to 4.4MB which is very substantial.

@KaruroChori KaruroChori added enhancement New feature or request help wanted Extra attention is needed wasm-sandbox labels Jan 21, 2025
@KaruroChori KaruroChori self-assigned this Jan 21, 2025
@KaruroChori
Copy link
Collaborator Author

For context:

meson setup --reconfigure build/ -Db_lto=true --buildtype=release --cross-file ./platforms/zig-wasm32.ini -Ddefault_library=static
meson compile vs.templ:executable -C build

builds without issues, but the resulting vs.templ.js when run has the issue I reported before. Removing -Db_lto=true works as expected.

[built-in options]
c_args = ['--target=wasm32-wasi','-fno-exceptions']
c_link_args = ['--target=wasm32-wasi','-fno-exceptions']
cpp_args = ['--target=wasm32-wasi','-fno-exceptions']
cpp_link_args = ['--target=wasm32-wasi','-fno-exceptions']

are being passed, but I was unable to find a way to increase the stack size and see if that was the cause.

@KaruroChori
Copy link
Collaborator Author

Just tested the same setup without linking anything from my code or the upstream libraries of this project. So, just the basic libraries linked by default.
Just tring a simple printf, the result is not even deterministic:

$ bun run ./build/vs.test.js
RuntimeError: Out of bounds memory access (evaluating 'instance.exports')
      at vs.test.js.wasm-function[__wasm_call_dtors]
      at vs.test.js.wasm-function[_start]
      at start (node:wasi:1023:26)
      at /archive/shared/projects/vs.templ/build/vs.test.js:49:11

Bun v1.1.37 (Linux x64)
$ bun run ./build/vs.test.js
RuntimeError: Out of bounds memory access (evaluating 'instance.exports')
      at vs.test.js.wasm-function[memmove]
      at vs.test.js.wasm-function[memcpy]
      at vs.test.js.wasm-function[__fwritex]
      at vs.test.js.wasm-function[printf_core]
      at vs.test.js.wasm-function[printf]
      at vs.test.js.wasm-function[__main_void]
      at vs.test.js.wasm-function[_start]
      at start (node:wasi:1023:26)
      at /archive/shared/projects/vs.templ/build/vs.test.js:49:11

@KaruroChori
Copy link
Collaborator Author

Well, I am really failing to make it work as I would expect. I made few tests using zig 14-dev standalone, without meson, just the command to compile and link.

Very simple c++ file:

#include <iostream>

int main(){
	std::cout<<"hello\n";
	return 0;
}

And a c one:

#include <stdio.h>

int main(){
	printf("hello\n");
	return 0;
}

zig c++ --target=wasm32-wasi main.cpp -flto -> fail to run
zig c++ --target=wasm32-wasi main.cpp -> fail, surprisingly as from meson it works.
zig cc --target=wasm32-wasi main.c -flto -> fail to run
zig cc --target=wasm32-wasi main.c -> working

I also tested C++ using the C code, no libc++ and it works without -flto.

I also noticed that the -static attribute does not work, so zig cc main.c -static does not generate a static build, but clang would.

@KaruroChori
Copy link
Collaborator Author

Issues notified upstream @ ziglang/zig#22629

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed wasm-sandbox
Projects
None yet
Development

No branches or pull requests

1 participant