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.
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.
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.
zi2go wrote every std/simd value helper as a wrapper that copied its
operands into 64-bit lane arrays and ran one port of zi_ref, so each
operation dispatched on its op and lane kind at run time. The helpers
now have typed bodies, generated by scripts/simd_header.py --go into
cmd/zir/zir_simd_go_bodies.h: straight-line code over each lane, or a
loop past eight lanes of one per-lane expression, which Go inlines. The
lane functions they call (saturation, masks, float to integer) and the
math, strconv and unsafe imports come only where a helper uses them;
the reference port is written only for a helper without a typed body,
which no helper the checker accepts lacks.
tests/simd_go.sh writes every helper the checker accepts beside its
reference form (GoSimdWriteCheck), checks the file is gofmt-clean, runs
each pair on the same edge-heavy operands, and checks the three
generated headers match scripts/simd_header.py. All 1060 agree.
AV1 352p decode built as Go, best of 3 on one core: 346 -> 44 ms, with
the same decoded planes.
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.
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.
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.
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.