binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

wasm2js aborts with "should be removed already; UNREACHABLE executed at /build/source/src/wasm2js.h:1902"

Open willcohen opened this issue 1 year ago • 1 comments

As noted in https://github.com/emscripten-core/emscripten/issues/15768#issuecomment-1183705109, an emscripten-compiled WASM file fails with -O1 or above.

Reducing the emcc-10-postclean.wasm using wasm-reduce yields the following WASM (r.zip):

$ wasm-dis r.wasm
(module
 (type $i32_i32_=>_none (func (param i32 i32)))
 (type $i32_=>_i32 (func (param i32) (result i32)))
 (import "env" "memory" (memory $mimport$0 256 32768))
 (table $0 3453 3453 funcref)
 (elem (i32.const 1) $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0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 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $1)
 (func $0 (param $0 i32) (result i32)
  (unreachable)
 )
 (func $1 (param $0 i32) (param $1 i32)
  (i32.store
   (i32.const 1)
   (i32.xor
    (i32.shl
     (i32.const 1)
     (local.get $0)
    )
    (i32.const -1)
   )
  )
  (call_indirect (type $i32_i32_=>_none)
   (i32.const 1)
   (i32.const 0)
   (i32.const 0)
  )
  (unreachable)
 )
)

It continues to fail with wasm2js:

$ wasm2js -O1 r.wasm
should be removed already
UNREACHABLE executed at /build/source/src/wasm2js.h:1902!
Aborted

The wasm2js.h line in question refers to RotLInt32/RotRInt32.

willcohen avatar Jul 14 '22 20:07 willcohen

This part:

   (i32.xor
    (i32.shl
     (i32.const 1)
     (local.get $0)
    )
    (i32.const -1)
   )

after OptimizeInstruction pass should be simplified to:

(i32.rotl
 (i32.const -2)
 (local.get $0)
)

But OptimizeInstruction should follow before RemoveNonJSOps pass. Hmm

Upd: Well that's a problem: https://github.com/WebAssembly/binaryen/blob/main/src/wasm2js.h#L369

It looks like runner.add("remove-non-js-ops"); needs after this bunch of passes.

MaxGraey avatar Jul 14 '22 20:07 MaxGraey

Apologies for the long delay on this one. #5378 allows the reduced test case to build here.

willcohen avatar Dec 30 '22 18:12 willcohen

Fixed with #5378.

willcohen avatar Jan 04 '23 20:01 willcohen