binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Name section: Support label names (id 3)

Open jrmuizel opened this issue 6 months ago • 5 comments

I get this when running wasm-opt on the attached .wasm file

binary-trees.zip

It was generated with the wat crate.

jrmuizel avatar Jul 10 '25 15:07 jrmuizel

I believe name section ID 3 is for labels (names of blocks and loops inside functions). Looks like we don't support that atm, so we will drop those names after that warning (but this does not cause any other breakage, even other names still work).

kripken avatar Jul 10 '25 16:07 kripken

No other issues in that wasm file, and looks like it optimizes well,

Metrics
total
 [exports]      : 7       
 [funcs]        : 10            -3
 [globals]      : 0             -1
 [imports]      : 0       
 [memories]     : 1       
 [memory-data]  : 10      
 [tables]       : 0       
 [tags]         : 0       
 [total]        : 353         -125
 [vars]         : 14           -15
 ArrayGet       : 1       
 ArrayLen       : 1       
 ArrayNewFixed  : 6       
 Binary         : 33            -9
 Block          : 13           -11
 Break          : 6             -5
 Call           : 28            -6
 Const          : 140           -4
 If             : 9             +2
 Load           : 5       
 LocalGet       : 57           -47
 LocalSet       : 26           -19
 Loop           : 6       
 RefIsNull      : 1       
 Return         : 1             -6
 Store          : 9       
 StructGet      : 3       
 StructNew      : 5       
 Unary          : 3       

However, I see this:

warning: active memory segments have overlap, which prevents some optimizations.

@jrmuizel perhaps the generator can avoid that issue, specifically,

 (data $0 (i32.const 512) "true")
 (data $1 (i32.const 516) "false")
 (data $2 (i32.const 520) "\n")

Note how segment $1 overlaps with $2 ("false" is 5 characters, so it writes to 520).

kripken avatar Jul 10 '25 16:07 kripken

When I run wasm-opt --enable-gc --enable-reference-types binary-trees.wasm -o /tmp/out.wasm the resulting out.wasm doesn't have a names section:

$ wasm-objdump -h /tmp/out.wasm
out.wasm:	file format wasm 0x1
000000e: error: expected valid field type (got -0x1d)
0000257: error: expected valid local type

Sections:

     Type start=0x0000000a end=0x00000039 (size=0x0000002f) count: 9
 Function start=0x0000003b end=0x00000046 (size=0x0000000b) count: 10
   Memory start=0x00000048 end=0x0000004b (size=0x00000003) count: 1
   Export start=0x0000004d end=0x000000b7 (size=0x0000006a) count: 7
     Code start=0x000000ba end=0x00000402 (size=0x00000348) count: 10
     Data start=0x00000404 end=0x00000421 (size=0x0000001d) count: 3

which I assumed was because of the warning. Should it be there?

jrmuizel avatar Jul 11 '25 00:07 jrmuizel

wasm-opt does not emit the names section by default (to avoid the risk of shipping wasted bytes in production). Use -g to get it:

https://github.com/WebAssembly/binaryen/blob/1d2e23d5e55788091a51420ba3a9889d4efe7509/test/lit/help/wasm-opt.test#L642-L644

kripken avatar Jul 11 '25 00:07 kripken

Ah cool. That works

jrmuizel avatar Jul 11 '25 01:07 jrmuizel