GeoZL

One kernel source for many GPUs

Every GPU family speaks its own kernel language. NVIDIA drivers load PTX, Apple devices compile the Metal Shading Language, and WebGPU runs WGSL. A decoder written once per language would need every fix made several times, and the copies would slowly drift apart.

Without Slang, the decoder would exist once per GPU language, CUDA C, Metal and WGSL, and a fix made in one copy would still have to be ported to the others. With Slang, one source in gpu/kernels is compiled by slangc into Metal source and PTX, such as pfor2.metal and pfor2.ptx, with WGSL planned, and the compiled modules are embedded in the GeoZL library, so a fix is made once and every target gets it at the next build.
Write the decoder once and let the compiler make each copy.

GeoZL's decoder has exactly one kernel source, in Slang, compiled ahead of time for each backend. CUDA and Metal are the first two backends, and more are planned, WebGPU among them. The compiled kernels ship inside the library, so neither building GeoZL nor running it needs Slang.

This last part follows that source to the GPU, from one line of Slang to a module the device runs.

What Slang is

Slang is an open-source shading language, developed at NVIDIA and now hosted by Khronos. Its syntax is close to HLSL, and a single compiler, slangc, emits code for many targets, including Metal source, CUDA through PTX, SPIR-V for Vulkan and WGSL for WebGPU.

What matters most for GeoZL is that Slang has portable wave operations, a prefix sum, a sum, an OR and a read from another thread. Here is the inner step of the PivCo merge from pivco.slang, with comments added.

uint ones = countbits(bits);                    // 1s in my byte
uint onesBefore = carry + WavePrefixSum(ones);  // 1s before me
uint zerosBefore = p0 - onesBefore;
merge_byte(bits, p0, count, leftCount, onesBefore, zerosBefore,
           outputBase, toLanes);
carry += WaveActiveSum(ones);                   // 1s in the step

The same line becomes different code on each target. Metal has a built-in call, simd_prefix_exclusive_sum. PTX has none, so Slang builds the wave prefix sum from shfl.sync.up shuffles. The kernel source does not change.

How the kernels are organised

gpu/kernels holds three kinds of file. common/payload.slang sets what every kernel shares, a 32-thread wave, four waves per group, eight values per thread, and the loads that read the compressed payload. stages/ holds the decoding steps, PFOR, the band decoder of planar_zigzag_pfor, the PivCo merge and the inverse planar scans. programs/ holds the three programs the library loads, which pull in the stages they need.

Shared payload helpers and decoding stages feed three Slang programs. Compiling each program for its supported sample widths produces nine modules per target.
Three programs, each compiled once per sample width, nine modules per target.

The macro ELT_BYTES picks the sample type, so a program is compiled once per width. pfor holds the PFOR unpack, size_chunks, plan_blocks and decode_strips for planar_zigzag_pfor, and a raw copy. pivco holds the PivCo merge, the byte-lane unpack, the lane join, the three planar scans and the same raw copy. place has one kernel, which copies decoded rectangles into the caller's target. The table of programs in the build script lists them all.

Compiling a module for each target

tools/build_gpu_modules.py builds every module of one target into a directory, normally core/src/gpu/modules. These two lines rebuild both targets we ship.

python3 tools/build_gpu_modules.py slangc metal core/src/gpu/modules
python3 tools/build_gpu_modules.py slangc ptx core/src/gpu/modules

It first checks that slangc reports version 2026.19, because another compiler would write different modules. Then, for each program and width, it runs slangc with every entry point of the program, the target, ELT_BYTES set to the width, and -line-directive-mode none so no file path ends up in the output.

Without a check. The host hands buffers to a kernel by index, and Slang numbers them in the order a program declares them. Swap two declarations and the kernel reads one buffer as another, with no error at all.

With the check. Each compile also writes Slang's reflection JSON, and check_slang_layout.py compares its order with the order the host binds. A swap fails the build instead of the decode.

Without the check, if two buffer declarations of the pfor program were swapped, the kernel would bind strips at 4 and bandFrames at 5 while the host still passes bandFrames at 4 and strips at 5. Both hold 32-byte records, so the kernel would read strip tasks as band frames with no error. With the check, every build has slangc write reflection JSON and check_slang_layout.py compares the binding order with the host order, so the swap fails the build with unexpected pfor buffer layout.
The binding order is checked at build time, so a swap never reaches a GPU.

Metal source builds anywhere slangc runs, a Linux machine included. PTX also needs NVRTC, the CUDA runtime compiler that slangc calls to turn its CUDA output into PTX.

PTX and the oldest driver

PTX is not machine code. The CUDA driver compiles it for the GPU it finds when the module loads, so one PTX file covers every NVIDIA GPU from its target up. Ours target sm_70.

The catch is that a driver only loads PTX up to its own PTX version, and every NVRTC release writes a newer one. So the compiler is chosen by the oldest driver GeoZL should run on, not by the newest toolkit we have.

A driver loads PTX only up to its own PTX version. A 550 driver reads PTX up to 8.4. NVRTC 12.2 writes .version 8.2, which loads, and is what GeoZL ships. NVRTC 12.8 writes .version 8.7, which the 550 driver rejects. Once loaded, the driver compiles the PTX for the GPU it finds, from sm_70, the PTX target, to any newer GPU.
The oldest driver to support picks the NVRTC, and the driver turns the PTX into code for whatever GPU it finds.

The PTX we ship comes from NVRTC 12.2, so drivers from the CUDA 12.2 generation on can load it. NVRTC does not exist for macOS, so we regenerated the PTX on a workstation with two RTX A5500 cards and CUDA 12.2. When a driver refuses a module anyway, the error GeoZL returns carries the first line of the JIT log, which names the PTX feature it rejected.

Where we skip Slang's shuffles

A shuffle is how one thread of a wave reads a value from another, without going through memory. The wave prefix sum above is built from them on CUDA, and CUDA's shuffle takes a mask of the threads that join it. Slang cannot know that every thread is still running at that point, so its CUDA code asks which ones are before each shuffle.

That question costs little once, but the planar>zigzag>pfor strip decoder shuffles about fifty times per block, and it made the decoder more than twice as slow on an A100. In that kernel all 32 threads always take part, so it calls the shuffles directly with a full mask, __shfl_up_sync on CUDA and simd_shuffle_up on Metal.

A shuffle lets each thread of a wave read a value from another thread, here the thread one to its left. Slang's CUDA code asks which threads are active before each of the about fifty shuffles per block, which made the strip decoder more than twice as slow on an A100. GeoZL calls the shuffle directly with a mask of all 32 threads, so there is no question to ask.
When every thread always takes part, the question before each shuffle is pure cost.

Keeping the modules in step with the source

The library loads the compiled modules kept in the repository, not the .slang sources. If someone edits a kernel and forgets to rebuild, the old module ships and nothing notices.

So each build also writes sources.sha256, a hash of every module and a hash of everything the compiler read, meaning the .slang sources, the Slang version, the target and the flags. In CI, build_gpu_modules.py --check computes the hashes again. If a source changed without a rebuild, they no longer match, and the check fails and names the stale modules.

Before, editing stages/pivco.slang without a rebuild left pivco2.ptx unchanged, nothing compared the modules with the source, CI passed and the old kernel shipped. After, sources.sha256 records one hash per module, such as metal:pfor2.metal and ptx:pfor2.ptx, and one digest of the compiler inputs per target. Editing stages/pivco.slang changes the sources digest, so build_gpu_modules.py --check in CI fails with stale metal and ptx modules, rebuild them.
An edit to a kernel without a rebuild now fails CI and names the stale targets.

The check only computes hashes, so it needs no Slang and no GPU and runs on every change. That matters most for PTX, which cannot be rebuilt on macOS at all.

Loading the embedded modules

At build time CMake picks one kind of module, Metal source on Apple and PTX on Linux, and embed_modules.cmake turns each into a C byte array in a table of name, bytes and size. A build without GPU support compiles a stub instead, whose calls report that GPU support is not built in.

At run time the decoder looks a module up by name the first time a batch needs it, pfor2 for two-byte PFOR frames, and hands the bytes to the backend.

At build time CMake picks the Metal source modules on Apple and the PTX modules on Linux, and embed_modules.cmake turns each into bytes in a table, such as pfor2, bytes and size, inside the library. At run time the first batch of two-byte PFOR frames looks up pfor2 and hands it to the backend. Metal compiles the source and builds one pipeline per entry point, rejecting a SIMD-group width other than 32. The CUDA driver JIT compiles the PTX for the device, and libcuda.so.1 is opened at run time so the library loads on machines without CUDA.
Built in once, compiled for the device the first time a batch needs it.

The Metal runtime compiles the source with newLibraryWithSource, builds a pipeline per entry point and rejects any pipeline whose SIMD-group width is not 32. The CUDA runtime opens libcuda.so.1 with dlopen, so the library still loads on machines without CUDA, and passes the PTX to cuModuleLoadDataEx, where the driver compiles it.

On CUDA, kernels read their buffer pointers from one parameter block per module, so one program runs one batch at a time. Callers that need work on several streams at once create one decoder per stream.

Creating a decoder loads or compiles the kernels for its device, so create one decoder and reuse it.

Adding a backend

Capture, planning, task lists and staging are shared by every backend. runtime.c checks each call and forwards it through GeozlGpuBackendOps, a small table of operations that opens a device, allocates memory and staging, queues a batch of kernels and waits for it.

Shared capture and planning call Metal or CUDA through GeozlGpuBackendOps. A new backend also needs runtime and build wiring, a Slang target and module checks.
Capture and planning are shared. Each backend supplies runtime and build wiring, a Slang target and module checks.

A new GPU family, WebGPU, Vulkan or HIP, needs one runtime file that fills in that table and compiles in under its own define, as runtime_metal.m and runtime_cuda.c do, and a backend id that selects it. build_gpu_modules.py gains its Slang target, and the CMake build embeds that kind of module.

The kernels assume a 32-wide wave. A backend whose devices can run another width, as some WebGPU and Vulkan devices do, must ask for 32 or reject the device, as the Metal runtime does today. The formats, the kernels and every caller stay as they are.

Where this leaves us

GeoZL keeps one Slang source for Metal and CUDA, then verifies the generated modules in CI. Across the CPU and GPU decoders, the common idea is to let each lane or thread find its own input.

The one place we paid for it in ratio, skewed data in the lanes format, is still open. If it gets a GPU path, it will be a codec of its own rather than a change to lanes.

If you would rather use all this than read about it, the GPU API is where to start, and the benchmark compares the recipes on real Earth observation data.