Wasm module defined ___ globals which exceeds the maximum number allowed 200

Thanks @ktimam! I completed the investigation.

The Wasm binary declares 1387 globals, however only 138 of them are used.

The unused globals are marked as exported as shown below. That is the reason why they are not eliminated by Wasm optimizers.

I think the fix could be implemented on the icpp-pro side:

  • Remove all (export global) declarations by modifying the Wasm binary and run a Wasm optimizer (wasm-opt or ic-wasm). Note that globals don’t need to be exported on the IC. This should remove the unused globals.
  • If that doesn’t help, then it should be possible to implement a script that looks for (global.get X) and (global.set X) statements in the Wasm binary and removes all globals that are not accessed.
  • There might be also a way on the C++ toolchain side to optimize such that globals are not exported in the first place (e.g. using link time optimization (LTO) flags)
  (global (;1386;) i32 (i32.const 1486192))
  (global (;1387;) i32 (i32.const 1486248))
  (export "memory" (memory 0))
  (export "_ZTVN10__cxxabiv120__si_class_type_infoE" (global 139))
  (export "_ZTINSt3__219__shared_weak_countE" (global 140))
2 Likes