Skip to content

2.4.0

Choose a tag to compare

@CharlieTap CharlieTap released this 09 Nov 20:10
· 35 commits to main since this release

Whats Changed

Chasm JIT and AOT

Since its inception Chasm has always been an interpreter, an interpreter that has served as the foundation of the runtime and something that I have personally put a silly amount of hours into trying to tweak and tune. We are frankly at the limit, it cannot go much faster and there's still an entire class of problems I would like to solve which require something faster.

So with that I am announcing two new experimental ways to execute: JIT and AOT

Both of these innovations work largely in the same way, taking Chasms existing frontend compiler IR and introducing a new compiler backend to lower into either JVM Bytecode in the case of AOT or DEX in the case of JIT. So no I haven't built my own tiered virtual machine, computation is now run by either Hotspot or ART. In both cases the functions that execute still leverage Chasms existing Wasm resources, like memories, tables, globals, gc etc etc. Both AOT and JIT are highly experimental, their apis may change in the coming releases and they're far from what I would consider polished. That being said they are both functionally correct, both pass the official wasm test suite, Chasms test corpus and every integration test I have ever built for the interpreter.

Chasm JIT (Android Only)

One of the most popular use cases for Chasm is people dynamically downloading code for in process application updates. This is perfectly compliant with the Google Play Store guidelines as long as that code is running as part of a virtual machine and has indirect access to the Android APIs... like Chasm does

“This restriction does not apply to code that runs in a virtual machine or an interpreter where either provides indirect access to Android APIs…”

The new chasm-jit module brings that same workflow to compiled code. When a module is instantiated, Chasm compiles it to DEX on the device and loads it straight from memory. From there ART treats it like any other code in the app, so its own JIT and optimising compiler take over as the code warms up.

The JIT lives in its own module:

dependencies {
    implementation("io.github.charlietap.chasm:chasm-jit:2.4.0")
}

The API mirrors Chasm's own, only the module, validate and instance functions come from the jit package:

import io.github.charlietap.chasm.jit.instance
import io.github.charlietap.chasm.jit.module
import io.github.charlietap.chasm.jit.validate
import io.github.charlietap.chasm.embedding.invoke
import io.github.charlietap.chasm.embedding.store

val module = module(wasmFileAsByteArray)
val validated = validate(module)

val store = store()
val instance = instance(store, validated, imports = emptyList())

val result = invoke(store, instance, "fibonacci")

The module is compiled to DEX when it is instantiated. Stores, imports, host functions, invoke and everything else work exactly as they do with the interpreter.

Chasm AOT (JVM and Android)

Chasm AOT is specifically designed for when you have a web assembly program at build time and you want to optimise it for absolute performance. I've intentionally split it into two pieces, chasm-aot which is the intended for compile time api and a minimal runtime library runtime-aot (it's literally 52KB after R8...)

dependencies {
    // build time, e.g. in a build tool or Gradle task
    implementation("io.github.charlietap.chasm:chasm-aot:2.4.0")
    // runtime, in your app
    implementation("io.github.charlietap.chasm:runtime-aot:2.4.0")
}

Compiling writes the module's classes to a directory or a jar (or to memory), under the package you choose. Store settings are fixed at this point:

import io.github.charlietap.chasm.aot.CompileConfig
import io.github.charlietap.chasm.aot.CompileOutput
import io.github.charlietap.chasm.aot.CompileTarget
import io.github.charlietap.chasm.aot.compile

val config = CompileConfig("com.example.calculator") {
    target(CompileTarget.ANDROID) // or CompileTarget.JVM
}
compile(Path("calculator.wasm"), CompileOutput.Jar(Path("calculator.jar")), config)

At runtime, load the compiled module and use it like any other:

import io.github.charlietap.chasm.aot.compiledModule
import io.github.charlietap.chasm.aot.instance
import io.github.charlietap.chasm.embedding.invoke
import io.github.charlietap.chasm.embedding.store

val module = compiledModule("com.example.calculator.WasmModule", classLoader)

val store = store()
val instance = instance(store, module, imports = emptyList())

val result = invoke(store, instance, "add")

Fixes

  • Stores whose address and offset wrap around into bounds now trap as they should
  • Value stack growth is now bounded by the store's stack limit
  • The decoder keeps its position correctly when peeking near the end of a section
  • Expanded types are now published safely across threads
  • Parallel compilation no longer starts more workers than there are batches
  • Validation no longer uses List.removeLast, so it works below Android API 35
  • The embedding API now rejects foreign and dropped stores

Other

  • minSdk for Chasm android is now lowered to 27