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

POC: Shrinking generated binary #223

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

POC: Shrinking generated binary #223

wants to merge 4 commits into from

Conversation

Acaccia
Copy link
Collaborator

@Acaccia Acaccia commented Dec 19, 2023

This is a POC trying to reduce the size of the generated binary.

The main issue in the generated binary is that the entire standard lib is copied inside.
As proposed in #119, I tried using wasm-opt, but not on the standard lib but on the generated Wasm.
It also has the advantage of applying other optimizations, like inlining, or precompute compile-time evaluatable expressions.

It works for small examples, such as (+ 2 2), which results in a Wasm that contains the int 4 immediately. No more standard lib, and the result is precomputed.

However, for most of our tests, it's a failure. It seems wasm-opt fails to parse our generated Wasm. It needs more investigations...

@Acaccia Acaccia self-assigned this Dec 19, 2023
@Acaccia
Copy link
Collaborator Author

Acaccia commented Dec 20, 2023

So the problem is that binaryen (which contains the tool wasm-opt) supports only s-expressions in Webassembly code. So something like this is not understood:

(func $foo (export "foo") (local i32)
        (i32.const 1)
        (local.set 0)
    )

This kind of syntax is used extensively in our code generation. The simple assignment of a clarity value to wasm variables for example works like this.

@cylewitruk
Copy link
Member

So the problem is that binaryen (which contains the tool wasm-opt) supports only s-expressions in Webassembly code. So something like this is not understood:

(func $foo (export "foo") (local i32)
        (i32.const 1)
        (local.set 0)
    )

This kind of syntax is used extensively in our code generation. The simple assignment of a clarity value to wasm variables for example works like this.

What happens if you compile the WAT and then decompile it? Maybe it outputs in the "understood" format?

@Acaccia
Copy link
Collaborator Author

Acaccia commented Dec 22, 2023

@cylewitruk

What happens if you compile the WAT and then decompile it?

No sadly, this is not how it works. You don't really compile Wat, you translate it to Wasm.
But yes, I'm looking for tools to convert the generated Wasm to s-expressions, I think it's the way to go if we don't want to make a serious reorganization of the generator.

@Acaccia
Copy link
Collaborator Author

Acaccia commented Dec 22, 2023

it seems I made a false assumption. I tried compiling Wat using wasm-as, their webassembly compiler, and assumed that the compilation error would be the same as wasm-opt. It seems it's not the case, and code that does not compile with wasm-as but that compiles with wat2wasm can sometimes use wasm-opt, sometimes not. It still needs more investigation.

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

Successfully merging this pull request may close these issues.

None yet

2 participants