Artem Andreenko

Running five AI systems on Forth and pure LLVM IR

Inspired by Alexey Fateev’s llm.asm, I built Cygnet: a Forth virtual machine written in hand-crafted LLVM IR that runs all five phases, from Q8_0 Qwen3 inference and a 16.3 KiB bare-metal OS to a 114-nanosecond eBPF-Jev RNN.

Recently Alexey Fateev shared his experiments with llm.asm: five projects pushing AI workloads down to bare x86-64 assembly, bare-metal Ring 0, and Linux 6.12 eBPF hooks. It is an inspiring body of work, and it made me wonder what happens if you take the same five problems from a different angle: not replacing his hand-tuned assembly, but testing how far a different pair of abstractions can push both compute efficiency and storage footprint.

That experiment became Cygnet: a written-from-scratch Forth virtual machine and colon compiler in pure hand-written LLVM IR (.ll), executing portable Forth (.fs) programs across all five phases. Zero C in our engines, zero Docker, and the exact same .fs application code running on both x86-64 and Apple Silicon (ARM64).

Every number and screenshot in this post comes from live hardware-accelerated QEMU/KVM runs on an AMD Ryzen 9 7950X booting Debian trixie Linux 6.12.117+deb13-amd64 (CONFIG_BPF_LSM=y) and bare-metal Ring 0, plus cross-architecture parity checks on an Apple M2 Pro.

Why Forth plus pure LLVM IR works so well

At first glance, putting an interpreted stack language inside hand-written LLVM IR sounds like adding overhead in the wrong place. In practice, the two layers divide the work right along the boundary between control flow and raw math.

Forth keeps control flow and orchestration tiny. Everything that is not a tight inner loop (model forward passes, RoPE indexing, AdamW schedules, kernel fault triage, interactive REPLs) is expressed as factored Forth colon definitions. Postfix stack semantics eliminate local variable boilerplate, calling-convention glue, and AST overhead. Because the language is a flat sequence of words, factoring repeated idioms and stripping comments and whitespace at build time (minify_forth.py) shrinks all four .fs programs by 60.0%, from 14,981 bytes down to 5,989 bytes total.

Pure LLVM IR gives the virtual machine direct access to the silicon. Writing the VM in .ll lets us pin the Forth inner interpreter’s hot state (%ip, %sp, %rsp, %lsp) in SSA registers across dispatch, inline CONSTANT and VARIABLE literals directly into bytecode at compile time, fuse LIT <k> + into a single OP_ADD_LIT <k> superinstruction, and replace a 16 KB linear dictionary scan with a 1 KB O(1) 32-bit FNV-1a hash table. For the math that dominates runtime, we expose explicit vectorized Forth primitives (<8 x float> AVX2+FMA on x86-64 and <4 x float> NEON on ARM64, plus 4-row unrolled Q8_0 matvec and fused V-SGEMM-NT-SILU) and compile with opt -O3 and -function-sections -data-sections -s --gc-sections.

You get the best of both worlds: the application layer stays a few kilobytes of portable, inspectable Forth, while the hot loops run at hand-tuned SIMD speed with zero unused runtime baggage.

Summary across all five phases

Phase Workload Ryzen 9 7950X KVM vs Ref C (-O2) Stripped ELF + .fs Parity / Verification
01 Qwen3 Q8_0 4L (D=128, GQA, RoPE, SwiGLU) 33,236.0 tok/s (0.030 ms/tok) 9.76× (3,406.5 tok/s) 19,712 B + 1,882 B 16/16 greedy tokens, cos=0.9999999
02 Bare-Metal Ring 0 Forth OS (Multiboot 1) ~45 ms cold boot (253.7M cycles) 16.3 KiB vs 25.4 KiB (02-baremetal-os) 16,664 B total Ring 0 VGA + COM1 REPL + /bench
03 Mini-GPT Trainer (TinyShakespeare, AdamW) 206,940.9 tok/s (0.05 s / 160 steps) Fused AVX2+FMA SGEMM 34,888 B + 1,683 B loss 4.1740 → 2.0828, grad_cos=0.9999999
04 Ring-1 Kernel Surgeon + Live RWX Traps 122/122 (100.00%), 139.2 ns/dec vs 119/122 GRPO MiniCPM5-2B 16,136 B + 902 B 0 dangerous continues, 3/3 live traps
05 Linux 6.12 eBPF-Jev 21 KB INT8 RNN 114.0 ns median (111.8 ns peak) 3.19× (363.5 ns) 14,912 B + 1,522 B 500/500 exact 0-LSB parity

Phase 01: Qwen3 Q8_0 Inference

Phase 01 runs a 4-layer Qwen3 Q8_0 transformer (D=128, grouped-query attention 4Q/2KV, QK-Norm, NeoX RoPE, SwiGLU) driven by a 1,882-byte minified qwen3.fs script on top of a 19,712-byte stripped LLVM IR Forth engine.

Precomputing the RoPE cos/sin tables, eliminating KV-cache copies, and exposing a 4-row unrolled Q8_0 AVX2+FMA matrix-vector primitive pushes throughput to 33,236.0 tok/s (0.030 ms/tok) on Ryzen 9 7950X KVM, which is 9.76× faster than the -O2 C reference (3,406.5 tok/s), with 16/16 exact greedy token matches and cosine = 0.9999999. Even under macOS QEMU x86-64 TCG emulation, it hits 207.3 tok/s (2.06× over Ref C -O2).

Live Ryzen 9 7950X KVM VGA console showing Phase 01 Qwen3 Q8_0 benchmark reaching 33,236.0 tok/s with 9.76x speedup over C reference and 16/16 greedy token parity
Phase 01 live KVM run: Qwen3 Q8_0 inference reaching 33,236.0 tok/s (9.76× over Ref C -O2) with 16/16 exact greedy tokens and cosine 0.9999999.
Live KVM console showing the factored and minified qwen3.fs Forth source and stripped ELF binary sizes
Factored and minified qwen3.fs (1,882 B) alongside the 19,712 B stripped LLVM IR Forth engine.

Phase 02: Bare-Metal Ring 0 Forth OS

Phase 02 strips away Linux and libc altogether: a Multiboot 1 bare-metal 64-bit operating system booted straight into Ring 0 via QEMU -kernel. The entire stripped ELF32-wrapped image is 16,664 bytes (16.3 KiB), compared to 25,404 bytes in 02-baremetal-os.

To be transparent about scope: Cygnet’s Phase 02 is a compact Ring 0 Forth + micro-INT8 OS rather than a 649 MiB Qwen3-0.6B weight loader. It boots in roughly 45 ms (253.7M RDTSC cycles on Ryzen 9 7950X KVM), initializes a scrolling 80×25 16-color VGA text console and COM1 serial REPL, and executes interactive Forth words including /bench (1,000× INT8 Forth steps) directly on bare metal.

Live QEMU/KVM 80x25 16-color VGA console of the 16.3 KiB Cygnet bare-metal Ring 0 Forth OS immediately after cold boot
Phase 02 cold boot on Ryzen 9 7950X KVM: scrolling 80×25 16-color VGA console running in Ring 0 with zero Linux and zero libc.
Live QEMU/KVM VGA console showing the bare-metal Ring 0 Forth REPL running interactive words and /bench for 1,000 INT8 Forth steps
Interactive Ring 0 Forth REPL and /bench executing 1,000 INT8 Forth steps directly inside the 16.3 KiB kernel.

Phase 03: From-Scratch Mini-GPT Trainer

Phase 03 trains a Mini-GPT language model from scratch on TinyShakespeare inside Forth (train.fs, 1,683 bytes minified) backed by a 34,888-byte stripped LLVM IR engine with 4-column AVX2+FMA SGEMM, fused V-SGEMM-NT-SILU, AdamW updates, and a Braille terminal UI.

On Ryzen 9 7950X KVM, 160 training steps complete in 0.05 seconds at 206,940.9 tok/s, driving loss from 4.1740 → 2.0828. Analytical gradients are verified against FP64 central finite differences at grad_cos = 0.9999999.

Live KVM console showing Phase 03 Mini-GPT training on TinyShakespeare at 206,940.9 tok/s with loss dropping from 4.1740 to 2.0828 and Braille loss curve
Phase 03 live KVM run: 160 Mini-GPT training steps in 0.05 s (206,940.9 tok/s), loss 4.1740 → 2.0828, and FP64 gradient cosine 0.9999999.
Live KVM console showing the factored train.fs Forth trainer and binary footprint
The 1,683-byte minified Forth training loop and 34,888-byte stripped LLVM IR SGEMM/AdamW engine.

Phase 04: Ring-1 Kernel Surgeon

Phase 04 implements a deterministic 3-action kernel fault triage policy and static x86-64 machine-code patcher in 902 bytes of minified Forth (surgeon.fs) on a 16,136-byte stripped engine.

On the held-out scenarios_600.jsonl evaluation set, it scores 122/122 (100.00% pass@1), compared to 119/122 for the GRPO-trained MiniCPM5-2B baseline, with 0 dangerous continues, 92/92 valid machine-code patches, and 139.2 ns/decision. It also handles 3/3 live hardware traps (#PF, #DE, #UD) in RWX memory, patching the faulting instruction in RAM and resuming execution cleanly.

Live KVM console showing Phase 04 Kernel Surgeon achieving 122/122 pass@1 at 139.2 ns per decision and patching 3/3 live RWX traps
Phase 04 evaluation on Ryzen 9 7950X KVM: 122/122 (100.00% pass@1), 0 dangerous continues, 139.2 ns/decision, and 3/3 live #PF/#DE/#UD traps patched in RAM.
Live KVM console showing the 902-byte surgeon.fs Forth policy and patcher alongside the stripped ELF binary
The 902-byte minified surgeon.fs policy and x86-64 instruction patcher.

Phase 05: Linux 6.12 eBPF-Jev 21 KB INT8 RNN

Phase 05 runs the exact ebpf_jev_model.bin (21 KB INT8 RNN) and verifies all 500 vectors from parity_vectors.json down to 0-LSB exact bit parity. The stripped LLVM IR Forth engine is 14,912 bytes, and the minified jev.fs program is 1,522 bytes.

Across 300,000 inferences per implementation on Ryzen 9 7950X KVM, the Forth + LLVM IR engine runs at a 114.0 ns median (111.8 ns peak, 8.95M inf/s, p99 = 161 ns) versus 363.5 ns for the -O2 C reference (3.19× faster). Take that exact same unmodified jev.fs over to an Apple M2 Pro (ARM64, 500,000 inferences) and it runs at 115.0 ns (8.69M inf/s) versus 438.8 ns for Ref C -O2 (3.81× faster). Under macOS QEMU x86-64 TCG emulation, it runs in 5.49 μs versus 12.08 μs (2.20×).

Live Ryzen 9 7950X KVM console showing Phase 05 eBPF-Jev 21 KB INT8 RNN benchmark trials reaching 114.0 ns median (3.19x faster than Ref C -O2) with 500/500 0-LSB parity
Phase 05 live KVM benchmark on Linux 6.12: 114.0 ns median (111.8 ns peak, 8.95M/s) vs 363.5 ns Ref C -O2 (3.19×), with 500/500 exact 0-LSB parity.
Live KVM console showing the 1,522-byte minified jev.fs Forth implementation and 14,912-byte stripped ELF engine
Unmodified jev.fs (1,522 B minified) running identically on x86-64 AVX2 and Apple M2 Pro ARM64 NEON.

What this leaves me thinking about

Modern AI stacks tend to bundle orchestration, memory layout, and compute kernels into the same multi-gigabyte runtime. Watching Alexey’s llm.asm and then building Cygnet made that coupling feel optional.

When you split a system cleanly, using Forth for compact, portable control flow and pure LLVM IR for register-pinned dispatch and SIMD primitives, the whole five-phase suite fits in a handful of kilobytes without giving up single-core throughput. All of the source code, LLVM IR engines, Forth programs, minifier, and reproducible KVM/ARM64 benchmarks are in github.com/miolini/cygnet.