Files
waoziandClaude Opus 5.5 08f85f5d15 Generate the SIMD headers at build time from a Ziran generator
scripts/simd/ (17 Ziran modules) replaces scripts/simd_header.py and
writes ziran_simd_ops.h, ziran_simd_types.h and zir_simd_go_bodies.h into
$(BUILD_DIR)/simd before anything includes or embeds them. The build
compiles the generator from its seed in bootstrap/simd; check-bootstrap
regenerates and compares that seed and update-bootstrap refreshes it.
The committed headers are deleted; the generated output is byte-identical
apart from the first comment line naming the generator. tests/simd_go.sh
drops its Python comparisons, which the seed check replaces.

Net hand-written code: -38,751 lines (running total 38,799, 13.38% of
the 290,000-line baseline).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-17 10:59:38 -03:00

96 lines
4.4 KiB
Ziran

// Generate the std/simd C helpers (ziran_simd_ops.h), the vector types alone
// (ziran_simd_types.h) and the typed Go helpers zi2go writes
// (zir_simd_go_bodies.h). The build runs this from its bootstrap seed.
// Usage: simd-header [--types|--go] > HEADER
#import "model"
#import "common"
#import "emit"
#import "entries"
#import "native"
#import "go_helpers"
#import "std/text_tools"
using Vectors :: #import "std/vec"
VecDetection :: #string C
/* Native vectors need __builtin_shufflevector: Clang, or GCC 12 and later. */
#if !defined(ZIRAN_SIMD_SCALAR) && \
(defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 12)) && \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define ZI_VEC 1
#else
#define ZI_VEC 0
#endif
C
TypesHeader :: (output: *Vec(u8)) {
AppendLine(output, "/* Generated by scripts/simd/header.zi --types; do not edit. */")
AppendLine(output, "/* The std/simd vector types alone. Generated C headers that only name\n * vectors in declarations include this; code that runs operations\n * includes ziran_simd.h, which includes it too. */")
AppendLine(output, "#ifndef ZIRAN_SIMD_TYPES_H\n#define ZIRAN_SIMD_TYPES_H\n\n#include <stdint.h>\n")
BuilderAppend(<<output, VecDetection)
AppendLine(output, "\n#if ZI_VEC\n#define ZI_AT(v, i) (v)[i]")
types := Types()
for t: types {
AppendLine(output, Render("typedef @0 @1 __attribute__((vector_size(@2), aligned(@3)));",
t.scalar, t.c, Decimal(t.bytes), Decimal(t.bits / 8)))
}
AppendLine(output, "#else\n#define ZI_AT(x, i) (x).v[i]")
for t: types { AppendLine(output, Render("typedef struct { @0 v[@1]; } @2;", t.scalar, Decimal(t.lanes), t.c)) }
AppendLine(output, "#endif\n")
AppendLine(output, "/* Native vectors, such as zi_s16xN: two 128-bit blocks where the build\n * enables AVX2 and one elsewhere. #target clones use the fixed types. */")
AppendLine(output, "#if ZI_VEC && defined(__AVX2__) && (defined(__x86_64__) || defined(__i386__))\n#define ZI_NATIVE_WIDE 1")
for wide: 0..1 {
if wide == 1 { AppendLine(output, "#else\n#define ZI_NATIVE_WIDE 0") }
for t: types {
if t.wide == (wide == 0) { AppendLine(output, Render("typedef @0 zi_@1xN;", t.c, LaneName(t))) }
}
}
AppendLine(output, "#endif\n#define ZI_NATIVE_BLOCKS (ZI_NATIVE_WIDE + 1)\n\n#endif /* ZIRAN_SIMD_TYPES_H */")
}
OpsHeader :: (output: *Vec(u8)) -> bool {
AppendLine(output, "/* Generated by scripts/simd/header.zi; do not edit. */")
AppendLine(output, "/* Included by ziran_simd.h once for the build target and once for each\n * x86-64 tier, with ZI_TIER, ZI_FN and ZI_N(name) set. */")
AppendLine(output, "/* After the common part, a line holding only a comment that starts with @\n * starts a segment: @add_s16x8 the lines of that helper, and @ alone the\n * lines every build keeps. zi2c writes each program a copy without the\n * helpers it never calls (EmitSimdOps). */\n")
Common(output)
NativeMacros(output)
AppendLine(output, "#endif /* ZIRAN_SIMD_OPS_COMMON_H */\n")
helpers := Entries()
for *helper: helpers {
if helper.branches[helper.branches.count - 1].body == "" {
print("simd-header: no lane-array body for %\n", helper.name)
return false
}
}
marker := ""
AppendLine(output, "/* ---- operations for ZI_TIER ---- */")
for *helper: helpers { Marker(output, *marker, helper.name); Prototype(output, helper) }
Marker(output, *marker, "")
AppendLine(output, "")
SplitMacros(output, helpers[:], *marker)
AppendLine(output, "")
for *helper: helpers { Marker(output, *marker, helper.name); Function(output, helper) }
Marker(output, *marker, "")
AppendLine(output, "")
NativeSection(output, helpers[:], *marker)
return true
}
Flag :: (argument: *u8, flag: string) -> bool {
for i: cast(s64)0..flag.count - 1 { if argument[i] != flag[i] { return false } }
return argument[flag.count] == cast(u8)0
}
#scope_export
#program_export
main :: (argc: s32, argv: **u8) -> s32 {
output: Vec(u8)
ok := true
if argc == 1 { ok = OpsHeader(*output) }
else if argc == 2 && Flag(argv[1], "--types") { TypesHeader(*output) }
else if argc == 2 && Flag(argv[1], "--go") { ok = GoHeader(*output) }
else { print("usage: simd-header [--types|--go]\n"); return 2 }
if !ok { return 1 }
print("%", BuilderFinish(output))
return 0
}