TinyCC left with the native engine's ziran run tier; no build, test or
consumer compiles generated C with it. The SIMD generator drops its
TinyCC macro bodies and SSE2 assembly macros, ziran_simd.h, the bounds,
Vec and slice headers and the emitted checked-arithmetic helpers drop
their __TINYC__ branches, and #asm no longer writes TinyCC variants,
cpuid probes or volatile selection caches. Other compilers keep the
plain lane-array and portable-body fallbacks. The SIMD tests stop
compiling with -D__TINYC__ and check helper segments by their real
spelling.
The small runtime headers (zir_bounds.h, zir_slice.h, zir_string.h,
zir_vec.h) are split into helper segments by /*@Name*/ marks, the
convention ziran_simd_ops.h already uses; a helper's mark also lists the
helpers that call it. EmitRuntimeHeaders takes the names a closed
program's generated files spell and writes only the segments they name.
Other programs keep every helper, and the marks never reach generated
output.
Kryon's window headers go from 8524 to 6628 bytes: zir_bounds.h drops
EmptyArrayIndex and zir_slice.h drops the range helpers.
One commit, because the parts below only build together.
Allocator: an interval needed at the current instruction is never
evicted, and the allocated-program verifier checks that every operand sits
where the allocation says.
AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the
shared ARM byte helpers, maps the function-address relocation,
sign-extends signed 8- and 16-bit loads and rejects what it cannot encode
with a named diagnostic. It has no AOT ELF writer yet.
Native default with checked pointers and no fallback: on linux-x86_64,
`ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon
players run a bundle's verified program as native code in W^X memory.
Stray and freed pointers, double free, bounds and read-only stores trap. A
native gap is a named preflight error, nothing falls back to the engine,
and `--backend engine` picks it explicitly. zx2native uses the same
preflight. Native code exchanges module state for hot reload, checks the
stack, zeroes fresh locals, spends fuel at the engine's checkpoints and
returns records, arrays and strings from host calls. `zi2zx --debug` source
origins name lines in native traps and `zx2native --emit map`. The host
value API drops its VM prefix (HostValue, HOST_*). A call with a record
result loads the hidden result address into rax only after its stack
arguments are stored, and an indirect target stays in r10: rax could hold
a stack argument (Kryon's text-field clip guard arrived as an address) or
the target (indirect calls returning records crashed).
Self-hosting ports: compiler_backend_process, compiler_backend_cache,
compiler_native_names and compiler_runtime_emit replace the zir_c_build,
zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile
rules. zir_c.toolchain again reports "compiling OUT failed:" with the head
of the compiler output and a truncation marker. The parallel scheduler no
longer stalls on a held compiler after reaping a finished job, and poll or
read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build)
builds: generated files have pattern rules, objects wait for the headers
they include, and engine flags are per object instead of leaking into zi2c.
Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its
spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec
tests, vec_capacity, the zx_metadata and native_cache_names benchmarks,
build-android-host.sh and two shell wrappers are deleted; zx2native is
machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is
regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the
remaining-work plan record the current position.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tools are renamed after their input and output: zi2zx lowers source
or ZIR to executable ZX, zx2native turns ZX into a native ELF, zx2wasm
into a wasm32 module, and zi2c, zi2go and zi2zir keep their roles under
cmd/ folders of the same names. zi2asm and the cmd/zir-* folders are
gone, and tests/tool_names.sh keeps the old names out. zx2native keeps
the direct encoder as its default; the machine pipeline replaces it once
it reaches parity.
The embedding API can run a verified bundle as x86-64 machine code in
memory. Code is written to RW pages, relocated, then switched to RX,
never RWX, and each instance gets its own data
(cmd/zir/zir_native_memory.c, zir_host_native.c). Backend selection is
BundleBackendPolicy: Engine and Native are strict, and Auto chooses by
platform only, never by whether one program fits. A native gap is a
named error, never a switch to the engine. Desktop x86-64 Auto stays on
the engine until the native backend is complete.
cmd/compiler_machine_image.zi adds the target-neutral machine image
contract. The machine verifiers reject unknown relocation kinds,
out-of-range symbols and addends, non-host external calls, native-width
mismatches and missing stack or fuel facts. The machine pipeline lowers
and encodes more of LIR. Docs, site pages, Kryon's build references and
the benchmarks use the new names.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
zi2zx bundle and ziran run always write and run revision-28 executable
ZX. The semantic bundle reader and writer, the portable VM, the
TinyCC-compiled and linked native bundles with the vendored TinyCC,
bundle libraries, the VM profile, --vm, BundleInstantiatePortable,
libziran-vm.a and the ZIR-based Wasm lowering with zi2wasm are removed;
the Wasm tier, the embedding API and every tool run revision 28. Tests
of the removed VM internals and libraries are removed, and the rest run
on revision 28. Engine problems read as the diagnostics users rely on.
Lowering covers using fields, every Simd operation, target variants,
generic records shared between modules, constant tables as read-only
data and owned globals. The embedding API gives revision-28 bundles
slice copy-back, step limits over initialization, state export and
import, components, capability signatures and assets. The Wasm lowering
has the typed host ABI and a linear-memory heap, and the bundle Wasm
tier runs revision-28 bundles. With ZIRAN_BUNDLE_EXEC=1, 247 of 280
local tests pass; the default suite passes.
Compare five real examples across four targets on three measured platforms, preserving raw samples, independent output verification and artifact provenance.
Complete the WebAssembly support needed by the examples, inspect ZX bundles, prune generated C dependencies while preserving C++ linkage, and validate native script execution. Optimize only the temporary bootstrap seed and retain fresh-build measurements and historical benchmark records under bench/results.
Local tests: all 522 cases passed across the full run and corrected header-test rerun; all 16 bootstrap modules match. Playground runtime, bundle runtime, private browser layouts and matrix controls, published artifacts and local site links passed.
u8xN .. f64xN hold two 128-bit blocks in a #target procedure's AVX2 clone
and in AVX2 builds, and one in base C, other clones, Go, TinyCC and the
VM. Simd.Lanes(T) and Simd.Blocks(T) give the count: a constant for fixed
vectors, the tier's width for native ones. Per-block operations work on
each block; WidenLow/High, MulWideLow/High and Narrow number lanes across
the vector; Make and Splat repeat one block.
Native vectors stay plain values: the checker rejects them in globals,
fields, arrays, pointers, slices, size_of, #foreign signatures and across
#target boundaries. tests/simd_native.sh checks that every compiler,
width, forced tier, Go, bundle and VM run gives the same hash, and that
Blocks reports the width each build picks.
TinyCC read the whole 2.25 MB ziran_simd_ops.h for every module of a
SIMD program, so an uncached `ziran run` of the AV1 decoder spent about a
third of its TinyCC time on the header.
- scripts/simd_header.py marks each helper's lines in the header with a
/*@name*/ comment line; /*@*/ marks lines every build keeps.
- zi2c notes the helpers each generated file names and, once the program
is lowered, writes its ziran_simd_ops.h with those helpers and the ones
they call, in full-header order and without the marks (EmitSimdOps).
- Under TinyCC zi_ref is static inline, so it is compiled only where an
ARMv7 NEON helper calls it.
GCC and Clang build byte-identical objects from the copy and the full
header: the simd.sh program at -O0, -O2, SSE4.1, AVX2, scalar and TinyCC
flags, and all 52 AV1 decoder modules at -O0, -O2 and AVX2.
TinyCC on the AV1 decoder's modules: 702 -> 470 ms on the Moto (ARMv7,
same generated C), 38.5 -> 28 ms on x86-64.
The ARMv7, AArch64 and WebAssembly row kernels move from #asm to std/simd,
making the same bytes. Each tap's bytes widen to 32 bits and take its whole
weight; the horizontal kernel takes four taps a step and walks pointers with
32-bit counts, which ARMv7 and wasm32 keep in one register.
On a Cortex-A53 (ARMv7) resizes run 3 to 10% faster than the assembly, and
under wasmtime about twice as fast.
ziran_simd.h: Clang's ARMv7 backend copied an unaligned 8-byte load that it
merged into a widening through the stack; Simd.LoadLow now keeps it one
vld1.
tests/lanczos_isa_bitexact.sh runs the WebAssembly modules standalone with
WASI_RUN (such as wasmtime run), without JavaScript.
A forced tier made #target procedures act as on an older CPU while #asm
probes such as std/resample's HasAvx2 still saw AVX2, so the AVX2 kernels
ran in SSE2 copies no real SSE2 CPU reaches. x86 vector features now carry
the tier a CPU with them reaches, and an #asm variant needing one beyond the
forced tier is passed over, read from the environment as ziran_simd.h reads
it.
Under TinyCC every std/simd helper is now a statement-expression macro
on any target, not only x86-64, where the common integer ones stay SSE2
assembly. Loads, stores and gathers copy through structures of bytes,
which TinyCC copies at any address, instead of calling memcpy.
TinyCC's 32-bit ARM code generator copies a structure of up to 32 bytes
through lr a word at a time on ARMv6 and later, rather than calling
__aeabi_memmove, and the ZX engine's TinyCC targets the CPU's own
architecture version, so ARMv7 builds constants with movw and movt
instead of loading them from beside the code.
A 352p AV1 decode under `ziran run` on a Cortex-A53 phone (armeabi-v7a),
best of five on one core: 1.81 s cached, from 3.57 s; 7.27 s uncached,
from 9.42 s. The decoded planes hash the same, and every helper agrees
with zi_ref under TinyCC on the phone and, with the assembly left out,
on x86-64. x86-64 builds keep the same macros.
Under TinyCC on x86-64 the 32-bit multiplies, rounding shifts, half
widening and the signed narrowings are SSE2 assembly macros, and a gather
reads its lanes straight from the table, two to a 64-bit word, instead
of four memcpy calls. Each result is stored as one 128-bit block: the
next operation's 64-bit half loads still forward from it. TinyCC's
assembler learns pmuludq and pshufd.
Where nothing inlines (TinyCC, unoptimized GCC and Clang), zi_simd_at,
zi_simd_gather_check and SliceRange check in place and call only to
report. A whole slice a[:] is the view itself in C, and a vector load,
store or gather of a whole named array addresses the array directly.
TinyCC zeroes small braced locals with stores instead of memset.
AV1's s32x4 self-guided restoration (tree-s3) against its scalar form
(tree-s2), `ziran run`, best of 3 on one core:
d352 cached 95 -> 76 ms (scalar 79 -> 71), uncached 356 -> 336 ms (scalar 328 -> 325)
d1080 cached 488 -> 408 ms (scalar 484 -> 408), uncached 793 -> 703 ms (scalar 802 -> 703)
The bootstrap C is regenerated.
Where a compiler never inlines (TinyCC, unoptimized GCC or Clang), checked
division and shifts (ZiDivS32 and the rest) and index reads (ZIRAN_INDEX,
SliceIndex, ZirVecIndex) are statement-expression macros. Their
temporaries were spelled zi__a, zi__index and the like, so an operand
named the same read the macro's own uninitialized local: ZiDivS32(zi__a,
zi__b, ...) divided garbage, and a slice index named zi__slice_index
aborted. They are now spelled __zi_*, which C reserves and the C emitter
renames in programs, as the std/simd macros already are.
tests/macro_temporaries.sh checks both spellings at -O0, -O2 and under
`ziran run`. The bootstrap C carries the new prelude.
TinyCC inlines nothing, so every std/simd helper under `ziran run` cost a
call with its vectors copied in and out, and a typed kernel ran slower
than the scalar loop it replaced. Under TinyCC on x86-64 every one of
the 1064 helpers is now a statement-expression macro that evaluates each
argument once. The common integer operations are one SSE2 asm statement
that reads each vector argument in place through a memory operand and
writes the result into the local the expression yields; the rest run
their lane-array C on argument locals. Shifts compute their lanes
without branches and Make builds its lanes in one initializer, so they
convert too.
Macro temporaries, here and in the 256-bit split and pack macros, are
spelled __zi_*, which C reserves for the implementation. The C emitter
now renames locals, parameters and globals spelled with two leading
underscores as it renames keywords, so no argument can name a temporary;
fields keep their C spelling. tests/simd.sh checks a program whose own
values are named like the temporaries.
AV1 batch 10, best of 3 on one core: a cached 1080p decode takes 503 ms,
from 697 ms (the assembly build takes 547 ms), and 352p 75 ms, from 90.
Uncached, 352p takes 324 ms, from 387, and 1080p 784 ms.
TinyCC spent about 8 ms of every translation unit parsing ziran_simd.h,
600 KB once preprocessed, even in modules that only pass vectors
around. The ZI_VEC detection and the vector typedefs now live in a
generated ziran_simd_types.h. Module headers and C that only names
vector types include it, and C that calls an operation still includes
ziran_simd.h, which includes the types header too. On the AV1 352p
bundle, an uncached ziran run compiles in 38 ms instead of 80 ms, and
the generated code is otherwise unchanged.
Generated C now starts every body, out of line, a #target clone or a
header's inline copy, with (void)name; for each parameter it never reads,
so a C file including another module's header passes -Wall -Wextra
-Werror. USAGE.md documents the ZIRAN_<MODULE>_H_SOURCE opt-out for C
mocks, and tests/c_warnings.sh covers both. The commit also takes the
working tree's other in-progress changes (C cache, SIMD ops, check runner
and tests), which passed make check alongside it.
std/simd helpers below AVX2 take 256-bit vectors by pointer and run them
as two 128-bit halves, so no helper passes a 32-byte vector by value
(no -Wpsabi note) and GCC no longer scalarizes generic 256-bit operations.
Halves come from a 16-byte aligned union: memcpy went through the stack,
and an under-aligned union made GCC 14 fold misaligned memory operands
into SSE instructions (an internal compiler error).
Faster lowerings: NEON single-instruction reductions, x86 lane-swap
reduction trees and SSE4.1 phminposuw, pmuludq/pmuldq MulWide,
saturating Narrow from unsigned and 32-bit sources, AddPairs, MulAddPairsSat,
SSE2 Abs, u16 Min and Max, AVX2 16-bit ShiftLanes, and WebAssembly
MulHighRound.
Sign(a, b) and AbsDiff(a, b) have C (SSSE3 psign, NEON vabd, SIMD128),
Go, VM and scalar versions.
zi_ref fixes found on ARMv7: f32 arithmetic uses the exact scalar path
because NEON flushes subnormals, and the byte-shift loop is flat because
NDK Clang 19 miscompiled the nested one. zi_ref is marked unused for
programs that never take a scalar path.
TinyCC and unoptimized GCC or Clang never inline, so every bounds check,
checked shift and checked division in generated C cost a helper call.
Under those compilers the helpers are now statement-expression macros that
check in place and call out only to fail; optimized builds keep the
functions. A 1080p AV1 decode runs about 17% faster under `ziran run`
(TinyCC) and 24% faster at -O0, with the same output; -O3 is unchanged.
The SSE2 and AVX2 #asm row kernels are now #target std/simd procedures
that make the same bytes as the scalar reference. The 128-bit kernels run
in each SSE2 to SSE4.1 copy; the AVX2 kernels take eight weight pairs a
step. On a Ryzen 9 9950X, summed over four sizes, the AVX2 horizontal
pass is about 11% faster than the assembly with GCC and 12% with Clang,
up to 23% on large downscales, and the vertical pass is unchanged. The
128-bit kernels are about 6% faster with GCC and 15% with Clang. NEON and
WebAssembly SIMD128 keep their #asm kernels. tests/lanczos_isa_bitexact.sh
also runs the SSE2, SSSE3 and SSE4.1 copies. ziran_simd.h now silences
Clang's -Wpsabi warnings too.
std/simd's C helpers now load and store whole vectors with unaligned
vector moves (SSE2, AVX2, NEON and SIMD128) instead of letting GCC copy
the 4-byte-aligned vector types as two 64-bit halves. Widening a half,
taking or joining 256-bit halves and the 64-bit low loads also use one
instruction on each target where one exists. Lane results are
unchanged; scripts/simd_header.py --test checks them against the
scalar definitions.
A #target procedure gets a clone per x86-64 SIMD tier in C, and the
procedure itself picks one through CPUID and the OS's YMM state, read
once. ZIRAN_SIMD_TIER lowers the choice for tests. Checking keeps 256-bit
vectors from crossing the procedure's boundary by value, since AVX2
clones would pass them in YMM registers. Saved IR records the modifier
(format 61). TinyCC, NEON and other targets compile the procedure once.
ziran_simd_ops.h, generated by scripts/simd_header.py, defines every
vector type and operation for SSE2, SSSE3, SSE4.1, AVX2 and NEON, with
portable vector-extension and plain-struct fallbacks that agree lane for
lane with the reference semantics. Slice and array loads, stores and
gathers check their bounds; pointer access stays unchecked.
tests/simd.sh compares lane results across the vector, scalar, TinyCC,
SSE4.1 and AVX2 builds, from source and saved IR, and under ASan.
u8x16, s8x16, u16x8, s16x8, u32x4, s32x4, u64x2, s64x2, f32x4 and f64x2,
and their 256-bit forms, are builtin value types laid out as lane arrays.
Simd.Op calls through an import of std/simd check their operands and
constants, and become reserved zi_simd_* calls in saved IR; operators on
vectors are rejected. include/ziran_simd.h holds the exact lane semantics
every backend follows.
Native C builds lower qualifying regions to OpenGL ES 3.1 compute shaders
and load EGL and GLES at run time. Regions a shader cannot compute as the
CPU does stay on CPU threads with a marker in the generated C, and a failed
bounds check, used up loop budget or refused shader reruns on the CPU.
A #parallel body may now write its own slot of a slice, S[it], or its
own window of one, row := S[it * K : it * K + K], so image and buffer
loops can hand out rows. Native code checks the written slice against
the other slices, strings and arrays the body reads and runs the region
in order when they share storage. A body that writes through slices
calls only pure code and reads no global arrays, since callees and
globals could reach the same storage. Writes into arrays the body
declares itself are accepted; they were refused.
Native builds now start one worker per processor instead of four;
ZIRAN_PAR_THREADS still chooses.
BundleInstantiate lowers a bundle's verified program to C with the backend
native builds use, compiles it in memory with a bundled TinyCC and calls
it, on x86-64, 32-bit ARM and ARM64 hosts. The portable VM ran Shovey's
frames at a fraction of native speed on phones; compiled code runs CPU-bound
work about 70 times faster on a Moto G.
Host capabilities keep their VmHostValue contract through generated
trampolines, including by-name record matching, fixed arrays, slices
written back and modules that declare the same capability. Compiled code is
cached by the bundle's SHA-256, the CPU and the compiler's sources, so a
second launch links in milliseconds. A bundle that cannot be compiled fails
with zx.native rather than falling back to the VM; bounds, unreachable
code, the step limit and runaway recursion stop a run as they do in the VM,
through guards C lowering writes only for the engine.
TinyCC (LGPL-2.1) is vendored in third_party/tinycc with one fix: ARM calls
that split a structure across r3 and the stack lost the next argument's
alignment padding. The browser, Windows and macOS keep the VM; zi2zx-vm
and libziran-vm.a run it everywhere for tests of the VM itself.
Ziran now generates C, Go, Python and portable .zx. zi2cpp, zi2rust, the
C++ mode of the shared C emitter, the Plan 9 post-pass and the std Plan 9
modules are gone; --target=cpp|rust|plan9-c and zi2c --plan9 are rejected
as unknown targets. Generated C headers keep avoiding C++ keywords in
exported names, so C++ hosts can still include them.
GTK's first initialization adopts the user's locale for the whole process.
In a decimal-comma locale strtod stopped at the '.' of every portable float
literal, so Werkzeug's Self profile failed on every frame after a file
dialog opened. Literals, global initializers, verification and compile-time
float text now use the locale's decimal point only internally, and float
formatting keeps digits only. tests/vm_locale.sh loads and runs a bundle
under a decimal-comma locale when one is installed.
A host and a bundle built against different layouts of a record failed every
call that returned it, because a returned record had to match the declared
fields one for one. An app that updates its bundles separately from its host
needs both to keep working while one side adds a field.
BundleInstanceMatchFieldsByName (MatchBundleFieldsByName in bundle_host)
lets an instance take each declared field from the host field of the same
name: a field the host lacks takes its zero value and a field the bundle does
not declare is ignored. A field of the wrong kind still fails the call, and
instances that do not opt in keep the exact layout check.
zi2c and zi2cpp embed the runtime headers and copy each one a program
includes into its output directory, leaving unchanged copies untouched,
so the output builds without Ziran's include directory and its helpers
are visible next to the code that uses them. The float printing helper
is print_float, like the other plain runtime names.