https://kotlinlang.org logo
#webassembly
Title
# webassembly
s

sdeleuze

08/14/2022, 12:19 PM
With Kotlin
1.7.20-Beta
, I try to use Binaryen to optimize the generated Wasm but it fails:
Copy code
wasm-opt build/compileSync/main/productionExecutable/kotlin/add-wasm.wasm -o build/compileSync/main/productionExecutable/kotlin/add-wasm-opt.wasm --enable-nontrapping-float-to-int --enable-typed-function-references --enable-gc --enable-reference-types --enable-exception-handling -O3 --inline-functions-with-loops --traps-never-happen --fast-math
[parse exception: Recursion groups not allowed with equirecursive typing (at 0:15)]
Fatal: error parsing wasm
Could you please share what version/parameters I should use? The related sample is available on https://github.com/sdeleuze/wasm-footprint/tree/main/kotlin.
The app I compile is:
Copy code
fun main() {
    add(2, 3)
}

fun add(a: Int, b: Int) = a + b
And in the generated 1.1M wasm most of the footprint is taken by collection and exception infra:
Copy code
❯ grep $kotlin.collections build/compileSync/main/productionExecutable/kotlin/add-wasm.wat | wc -l
774

❯ grep Exception build/compileSync/main/productionExecutable/kotlin/add-wasm.wat | wc -l
346
Is it due to the fact I am not using
wasm-opt
?
s

Svyatoslav Kuzmich [JB]

08/17/2022, 2:13 PM
Flags that we use by default listed here. I think that the lack of
--hybrid
is whats causing the error.
We use version 109
s

sdeleuze

08/18/2022, 5:43 PM
Nice with
--hybrid
that works as expected:
Copy code
exa -l build/compileSync/main/productionExecutable/kotlin/
.rw-r--r--  15k seb 18 Aug 19:41 add-wasm-opt.wasm
.rw-r--r-- 4.5k seb 14 Aug 13:58 add-wasm.mjs
.rw-r--r-- 156k seb 14 Aug 13:58 add-wasm.wasm
.rw-r--r-- 1.1M seb 14 Aug 13:58 add-wasm.wat
After optimization the generated Wasm file is 10x smaller.
@Svyatoslav Kuzmich [JB] Do you know how
wasm-opt
is able to optimize the size that much? Is it doing a second pass of code removal of unreachable code paths after Kotlin/Wasm?
10 Views