Contents
Weft now writes its own machine code
Until September, RyuJIT compiled every kernel Weft produced. Then I needed instructions it couldn't reach.
28 Sept 2026 · Updated 2 Oct 2026 · 7 min · Ernest
Compilers & SIMD, CorrectnessContents
Weft generates its kernels as .NET IL and hands them to RyuJIT, which does instruction selection and register allocation and mostly does them well. That was the design, and I'd still defend it: on a fused elementwise chain, the generated code matched my hand-written NEON at 1.00×.
The trouble was the kernels where mostly wasn't enough. Widening half-precision to float is one instruction on both x64 and arm64, and .NET 10 has no way to emit it. I checked by reflection and then by disassembly, because I didn't believe it. Weft's version was eighteen vector instructions and a store, and every quantised weight in a language model goes through it.
Register allocation was the other problem. Whether RyuJIT folds a SIMD constant into the code depends on whether its class constructor happened to run first, and in one build the attention kernel's exp loop ended up reloading ten constants from the stack on every iteration. I fixed that in C#. But I found it by reading machine code I hadn't written, after the fact, and that's not a good way to run a compiler.
And Weft picks between kernels by price. The price ought to be for the bytes that will actually run, and with a JIT you only see those bytes once it's too late to choose.
Twins
So I didn't write a new backend. A twin is a native copy of a kernel Weft already has: same inputs, same floating-point rules, but a body Weft emits as x64 or arm64 itself. Before the planner may pick it, it has to match the managed kernel bit for bit. If a single bit differs, the twin is refused and the planner never sees it.
The managed path stays, and that's the trick. I didn't have to trust my encoder. I only had to check it. On x64 the bytes come from the Iced assembler. On arm64 I wrote the encoder myself and checked every instruction form against clang. macOS was the platform that fought back, and that's a lab note of its own.
Native emission went on by default on 27 September. A twin is just another candidate, priced from its own bytes and picked only if it wins. WEFT_NATIVE_EMIT=off gives you a byte-identical compile.
What it bought
On kernels that already had a good managed body, not much, which is what I expected.
| Kernel | Machine | Native ÷ managed time |
|---|---|---|
| Q8_0 matrix-vector, Qwen decode | Ryzen 9 8945HS (Zen 4) | 0.98 |
| int8 SMMLA, Qwen decode | Mac mini M4 | 1.05 |
| Correctly rounded exp, Strict | Mac mini M4 | 0.46 |
The x64 twin won 2%, which is about what RyuJIT's address arithmetic and one spilled bound were costing. The arm64 one lost 5%. It's bit-exact and slower, so it stays in the catalogue with an honest price and never gets picked.
The wins came where RyuJIT had nothing to offer. Strict mode's exp is now correctly rounded on every platform, which is more than glibc's expf can say (it misses on 170,648 of the 2³² inputs), and as a NEON twin it's 2.2× faster than the generated one. And arm64 got fused attention for the first time: 41.9 ms per head on the M4, against 239.4 ms for the six kernels it replaces.
The kernels Weft generates from loop nests still go through RyuJIT. Their native versions are in review.
That 5.7× per head didn't survive the whole model. In SAM 3, on the same M4, fusing attention lost. Working out when to fuse is the next piece.