binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

[wasm-opt] `--minify-imports-and-exports-and-modules` does not report module renames.

Open vivax3794 opened this issue 7 months ago • 3 comments

When using wasm-opt with the --minify-imports-and-exports-and-modules flag, the tool correctly minifies both the import module names and the import function names. It often merges multiple import modules into a single new minified name.

However, the mapping printed to stdout is incomplete. It includes the remapping for the function names but completely omits the remapping for the module names. This makes it impossible for tools that rely on this map to correctly patch the host JavaScript environment, breaking the application.

Environment

  • wasm-opt Version: wasm-opt version 123

Minimal Reproducible Example (MRE)

1. Input .wat file (test.wat):

This file defines a module that imports two functions from two different, uniquely named modules.

(module
  (type (;0;) (func))
  (import "long_module_name_a" "long_function_name_foo" (func (;0;) (type 0)))
  (import "long_module_name_b" "long_function_name_bar" (func (;1;) (type 0)))
)

2. Assemble to .wasm:

wat2wasm test.wat -o test.wasm

3. Run wasm-opt:

wasm-opt test.wasm -o output.wasm --minify-imports-and-exports-and-modules

Results

Actual stdout Output:

The tool only prints the mapping for the function names:

long_function_name_foo => a
long_function_name_bar => b

Actual Contents of output.wasm (decompiled with wasm2wat):

The output Wasm shows that the module names were also minified and merged into "a":

(module
  (type (;0;) (func))
  (import "a" "a" (func (;0;) (type 0)))
  (import "a" "b" (func (;1;) (type 0)))
)

Expected Behavior

The stdout mapping should be complete and reflect all minification operations performed on the import/export interface. A correct and unambiguous output would look something like this:

long_module_name_a => a
long_module_name_b => a
long_function_name_foo => a
long_function_name_bar => b

This would provide all the necessary information for a host environment to correctly wire up the minified module.

vivax3794 avatar Jun 14 '25 22:06 vivax3794

Hmm, it looks like the pass assumes it can merge modules together:

https://github.com/WebAssembly/binaryen/blob/c91c0520a617ce7b72315451645dba0792ea59f4/src/passes/MinifyImportsAndExports.cpp#L109-L110

That is possible in some cases, e.g. Emscripten, and I guess we never documented that on the pass.

If your use case requires keeping modules separate, adding a flag for that seems reasonable, a PR would be welcome.

kripken avatar Jun 16 '25 16:06 kripken

For my usecase we only have one module, my main concern is having to hardcode the renaming of it to "a" as wasm-opt doesn't inform us on stdout about it

vivax3794 avatar Jun 16 '25 20:06 vivax3794

I see. A flag to control the singleton module name also sounds ok to me to add, or to emit it to stdout - either way.

kripken avatar Jun 16 '25 21:06 kripken