Skip to content

chasm-jit: compile to JVM classes on the JVM - #132

Open
Wavesonics wants to merge 2 commits into
CharlieTap:mainfrom
Wavesonics:jvm-jit
Open

Wavesonics wants to merge 2 commits into
CharlieTap:mainfrom
Wavesonics:jvm-jit

Conversation

@Wavesonics

Copy link
Copy Markdown
Contributor

chasm-jit only targets Android, so a JVM host (a desktop app, say) has to interpret modules it loads at runtime, or compile them ahead of time with chasm-aot, which needs the module at build time. This gives chasm-jit a jvm() target that compiles at instantiation with the JVM backend chasm-aot already uses.

How

  • runtime-jit gains jvm(). The DEX-specific lowering and loading move to androidMain (DexCompile.kt, DexLoader.kt, PackageNames.kt); jvmMain (JvmCompile.kt) lowers with backend-jvm into memory.
  • The pipeline around them is shared in commonMain: the parallel frontend step, the loading step, and the error handling (compile / expect compileUnchecked).
  • Generated classes are defined in a per-module MemoryClassLoader, initialized up front with CodeClassInitializer, and installed through the generated module's CompiledCode.install, the same path DEX takes. The loader is now shared with chasm-aot's CompileOutput.Memory.load (moved to compiled-api's jvmMain); it is parallel-capable and lets go of each class's bytes once defined.
  • backend-jvm: ModuleLowerer takes parallelism and a checkCancelled callback (defaults keep chasm-aot unchanged); JvmModule reports inlineMemory and chunkStarts, so the JIT checks memory 0 is ByteBuffer-backed at install and keeps chunk starts for trap diagnostics.
  • A StackOverflowError while compiling now becomes a generation failure on both targets.
  • chasm-jit gains jvm(), so its spec suite and corpus test also run on the JVM. JVM ABI dumps added.

Testing

  • :chasm-jit:jvmTest: 267 pass, including the core spec suite through chasm.jit.instance, plus a cancellation test.
  • :chasm-jit:corpusTest, :compiler:backend-jvm:test, :chasm-aot:jvmTest, :runtime:compiled-api:jvmTest, :runtime-aot:jvmTest pass.
  • :chasm-jit:connectedAndroidDeviceTest (InstanceTest) passes on an API 34 emulator.
  • checkLegacyAbi, lintKotlin, fmt clean.
  • Used from a host app (Hammer's plugin host): its full desktop suite passes with every module JIT-compiled, including fuel metering, interrupts, stack, memory, table and GC heap limits.

Numbers

Harper grammar checker (8 MB module), desktop, 32 cores, 300-paragraph check:

Load First call Later calls
Interpreted 0.4-1.3 s 45-48 s 3.8-4.0 s
chasm-jit, JVM 3.5-5.3 s 7.1-7.6 s 0.48-0.52 s

Caveats and follow-ups

  • Load time is dominated by backend-jvm lowering: about 50 s of CPU for Harper (276 classes, 12.8 MB of bytecode), spread over the worker threads. Profiling points at ASM serialization (including codeLength, which writes methods to measure them), slot liveness and dead-store removal. I plan to look at that next.
  • backend-jvm still runs its own thread pool per compile; making it lower on a ParallelTaskExecutor like backend-dex would share the JIT's permits across concurrent compiles. That touches chasm-aot, so it seemed better as a follow-up.
  • A disk cache (as in chasm-jit: let the host cache compiled DEX between runs #131 for DEX) would make the lowering cost one-time on the JVM too.

runtime-jit gains a jvm target: the shared frontend compiles in parallel,
backend-jvm lowers into memory, and the classes are defined in a loader of
the module's own and installed as the DEX path does. The DEX-specific
compile, loader and package naming move to androidMain. chasm-jit gains a
jvm target, so its spec suite runs on the JVM too.
The frontend step, loading, and the error handling around them are shared
by the DEX and JVM paths. chasm-aot and the JVM JIT load generated classes
through one parallel-capable MemoryClassLoader in compiled-api, which lets
go of each class's bytes once defined. JVM lowering runs on at most the JIT's
compile workers and stops when the compile is cancelled, the JVM path checks
memory 0 is ByteBuffer-backed and keeps chunk starts for trap diagnostics,
and a stack overflow while compiling becomes a generation failure.
Adds the JVM ABI dumps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant