v0.3.0 on Maven Central · K2 compiler plugin

Write GPU kernels
in plain Kotlin.

Add @Kernel to a Kotlin function. At compile time Kernelin translates it into a WGSL compute shader that runs on any GPU through WebGPU, and the same function still runs on the CPU for your unit tests.

id("io.github.rajumark.kernelin") version "0.3.0"

118×
faster than all 10 CPU cores
on an Apple M5 (waves)
0
lines of WGSL you write
by hand
2
places it runs: GPU via WebGPU,
CPU for unit tests
100%
of generated shaders checked
by the naga validator in tests
01 / TRANSLATE

Kotlin in. WGSL out.
Nothing to hand-write.

A K2 compiler plugin reads your function's IR right after type checking and emits a compute shader. Arrays become storage buffers, Int and Float parameters move into a uniform, and globalId() becomes the GPU thread index. Hover a Kotlin line to see what it became.

real compiler output · Kotlin 2.4.21
Kernels.ktKotlin
saxpy.wgslWGSL
naga validated read / read_write access detected automatically bounds check added for you
waves · 4,194,304 elements · 64 rounds of sin/cosmeasured
CPU · 10 cores · JVM0 ms
GPU · Apple M5 · WebGPU0 ms
Same Kotlin function.
Same results (100% match).
118×
02 / ONE FUNCTION, TWO ENGINES

Test it on the CPU.
Ship it to the GPU.

A kernel is still a normal Kotlin function. Run it with KernelinCpu in a plain unit test, step through it in the debugger, then send the very same code to the GPU. No second language, no copy that drifts out of sync.

  • KernelinCpu.dispatch runs every thread on one core, great for tests
  • dispatchParallel spreads it across all JVM cores
  • WebGpu.create() runs it on the real GPU from Kotlin/Wasm
See both runners →
Kernels.kt — gradle build
1fun fact(n: Int): Int = if (n <= 1) 1 else n * fact(n - 1)23@Kernel4fun factorials(out: IntArray) {5    val i = globalId()6    out[i] = fact(i)7}
e: Kernels.kt:1:48 Kernelin: recursion isn't allowed on the GPU ('fact' calls itself) (in @Kernel factorials)
Fix: write fact as a for loop → builds, runs on CPU and GPU
03 / SAFE BY CONSTRUCTION

Errors at compile time,
not on the GPU.

If your kernel uses something a GPU can't run (strings, Double, Long, lambdas, recursion, exceptions, nullable types) you get a normal Kotlin compiler error with file, line and a hint. In the IDE and in CI, before anything ships.

  • Clear messages that say what to write instead
  • Every shader validated by naga, and kernels proven equal to Kotlin on a real GPU in CI
  • The manifest tells hosts exactly how to bind buffers
What you can write →
04 / INSIDE THE COMPILER

From .kt to silicon
in one build step.

Kernelin plugs into the Kotlin compiler as an IR generation extension. It runs before any platform lowering, so loops, &&, ++ and += still have their original shape and map cleanly onto WGSL.

SOURCE

@Kernel fun

Plain Kotlin in commonMain or jvm

K2 FRONTEND

Resolve + check

Types and calls are resolved by the K2 compiler

KERNELIN

IR → WGSL

Walks the IR tree and writes a compute shader

OUTPUT

.wgsl + manifest

Shader plus a JSON map of bindings

EMBED

kernelSource(::k)

The call is replaced by the shader at compile time

RUN

WebGPU

Dispatched in workgroups of 64 threads

MetalVulkanDirect3D 12Chrome · Edge · Safari · FirefoxAndroid via WebGPU
05 / BENCHMARKS

Real numbers,
honestly measured.

GPU kernel time versus the same Kotlin code on all 10 CPU cores of an Apple M5. Heavy math wins big. Light kernels win only when data stays on the GPU; copying it there and back costs more than the work.

GPU speedup, data already on the GPU (log scale) 1× = same as CPU
MachineApple M5, 10 CPU cores, 8-core GPU. WebGPU in Chromium (Dawn → Metal).
MethodGPU: mean of 20 dispatches, median of 5. CPU: KernelinCpu on JDK 21, median of 5 after warmup.
CorrectnessGPU output equals the Kotlin CPU output for all 5 kernels (mandelbrot: 99.8% of pixels, float rounding).
Also on Androidmoto g57 power: Adreno 7xx GPU in Chrome 155 vs the same Kotlin on ART, all 8 cores.
Speedupwaves 33×, mandelbrot 8.6×, vectorAdd 7×, saxpy 6.5×, blur 4.4× (GPU kernel vs all CPU cores).
CorrectnessAll 5 kernels: 100% of outputs equal the Kotlin CPU output on the Qualcomm GPU.
06 / REAL-WORLD PROJECTS

Not a demo.
Workloads people pay for.

Three open-source apps built with Kernelin 0.1.0 from Maven Central. Each runs the same Kotlin on the GPU and the CPU, checks its answers against exact formulas or ground truth, and shows measured speed.

Source, tests and benchmarks for all three All projects →
07 / DOCUMENTATION

Everything you need,
for humans and for agents.

From your first kernel to the binding layout of the generated shader. The whole guide is also published as one Markdown file, so an AI coding agent can read it and write correct kernels on the first try.

Open source · Apache 2.0 · built for Kotlin 2.4.21 Read the full documentation →

Put your Kotlin on the GPU today

One line in your Gradle build. Kernelin compiles your @Kernel functions into WebGPU shaders and ships them inside your app, on every platform.