70. Raw binary embedding: @embed for compile-time fixed byte blobs, hermetic roots, opaque verification and direct LLVM emission¶
Date: 2026-11-26
Status: Accepted (2026-11-26, from Dean's answers on the design questions)
Context¶
Step 1 of the GPU toolchain (ADR 0068, PR #430) builds a compiled Metal library (.metallib) and embeds it into a binary by piping the file through xxd -i into a generated C source file wrapped in a cc_library (tools/gpu/metal.bzl), which C code then reads through extern const unsigned char <symbol>[] and <symbol>_len (tests/gpu/embed_test.c).
In review of PR #430, Dean asked that embedding a compiled .metallib (or any binary asset) become a first-class Pia feature so a pure Pia binary with a main entry point can bind and embed binary blobs directly without a cc_library or xxd wrapper:
- GPU kernels (ADR 0068): a Pia program with
@place(gpu)or a Metal shim needs the compiled.metallibembedded in the object file and passed as a read-only byte slice to the shim at launch. - 0.1.0 LLM & codecs (ADR 0067): small model checkpoints (
stories260K), tokenizer vocabularies (tok512), lookup tables and test fixtures benefit from the same mechanism. - Today's
ConstArrayOpdoes not scale to binary blobs.src/codegen/Codegen.cpplowers literal arrays by materializing an MLIRIntegerAttrper element and emitting amemref.global. That is suitable for small tables, not for kilobyte- or megabyte-sized.metallibor weight files, and it cannot read external files. Meanwhile,src/codegen/Codegen.cppalready emits raw binary data directly as an LLVMConstantDataArrayglobal (with optional custom section and alignment) for proof certificates (pia$cert, ADR 0024).
Decision¶
1. Surface syntax: @embed(file = ..., ...)¶
A top-level let binding embeds a raw binary file via the @embed(...) attribute on its return type, with no = body:
let add_metallib : [u8] @embed(file = "add.metallib")
Because fixed is Pia's default immutability qualifier, an embedded binding is always fixed (read-only); combining @embed with mutable is a type error.
Supported named arguments inside @embed(...):
| Argument | Required | Meaning |
|---|---|---|
file = "<path>" |
Yes | Relative path to the binary file, resolved hermetically (section 2). |
symbol = "<name>" |
No | Optional C/linker symbol name for the emitted byte array (section 4). When omitted, the compiler assigns a private mangled name. |
section = "<name>" |
No | Optional target object section (e.g. "__DATA,__pia_metal" on Mach-O or ".rodata.pia_metal" on ELF). Defaults to the target's read-only data section. |
align = <power-of-two> |
No | Optional byte alignment (positive power of two, default 8). |
At run time, the binding has type [u8] (a fixed read-only slice backed by static storage with lifetime 'static) whose length equals the file's exact byte size N. A pure Pia main can pass it directly to Pia functions or extern shims (such as Metal's device_load_library), or inspect its bytes and length normally:
let add_metallib : [u8] @embed(file = "add.metallib", symbol = "pia_test_add_metallib")
let main () : i32 =
if len add_metallib >= 4 &&
add_metallib.[0] = 0x4d && add_metallib.[1] = 0x54 &&
add_metallib.[2] = 0x4c && add_metallib.[3] = 0x42
then 0 else 1
2. Hermetic file resolution (CLI and Bazel)¶
piac never reads arbitrary paths from the ambient filesystem. Every @embed(file = "...") path is resolved strictly against explicitly declared roots or mappings:
- Relative paths only. Absolute paths (starting with
/or a drive prefix) and parent traversals (..) are compile-time errors. - CLI flags (mirroring
--iface-pathintools/piac/pia.bzl): --embed-path=<dir>(repeatable): search roots checked in flag order for<dir>/<file>.--embed=<logical>=<physical>(repeatable): explicit logical-to-physical mapping, taking precedence over--embed-path(useful when Bazel generates files underbazel-bin/...with target-dependent paths).- Resolving a file not found under any declared mapping or root is a hard error naming the missing path and the searched roots.
- Bazel integration (
tools/piac/pia.bzl):pia_library,pia_objectandpia_binarygain anembedsattribute (label_list(allow_files = True)orlabel_keyed_string_dict) that declares every embedded file as an explicit action input toPiaCompile(and toPiaInterfaceonly when needed; see section 5) and forwards--embed/--embed-pathtopiac. - REPL / MCP (
pia): respects the same--embed-path/--embedflags; with none passed,@embedis refused rather than opening host files.
3. Compile-time metafunction access vs. opaque verification¶
Embedded blobs have two distinct compile-time roles, kept strictly separate so verification stays O(1) regardless of blob size:
- In
let meta(metafunctions): compile-time fixed byte array. The metaprogram interpreter (src/meta/Interpreter.cpp) sees an@embedbinding as a compile-timefixedbyte array of exact lengthNwhose elements are readable by metafunctions (length and byte indexing). A metaprogram can validate magic bytes (e.g."MTLB"for a.metallib), parse a small header or manifest, or extract kernel names at compile time and fail compilation on a malformed blob — without ever sending those bytes to Z3. - In regular code and the Z3 verifier: opaque blob with exact length
N. To Sema, IRGen and the verifier, an@embedbinding is an opaquefixed[u8]slice with one known fact:len blob = N(a constantusizeequal to the file's byte count) and every element has typeu8(0 <= blob.[i] <= 255). No per-byte axioms are emitted into SMT queries. Embedding a 1 MB.metallibor model file adds zero per-byte solver cost; any property of the bytes needed by runtime code is either checked by a metafunction at compile time or validated by proved Pia code at run time (as inPia.Format.Safetensors, ADR 0067). - Spec functions: because the bytes are opaque outside metafunctions,
len blobis exact in contracts (len blob = N), whileblob.[i]is an uninterpretedu8value (deterministic: repeated readsblob.[i]at equalireturn equal values).
4. Codegen and symbol binding¶
Codegen bypasses per-element MLIR attribute expansion and emits the raw bytes directly in LLVM IR:
- MLIR representation: a module-level binary asset table (or
DenseResourceElementsAttr/pia.embed_dataattribute referencing raw bytes held outside the MLIR IR printer's per-element array format) and apia.embedop returning the[u8]slice descriptor{ptr, len}with static lengthN. - LLVM IR emission (
src/codegen/Codegen.cpp): lowers each embedded blob directly to a read-onlyllvm::GlobalVariableinitialized withllvm::ConstantDataArray::get(ctx, llvm::ArrayRef<uint8_t>(...)): isConstant = true(matches Pia'sfixedsemantics; placed in.rodata/__TEXT,__constby default, or the requestedsection).alignment = align(default8).- Linkage & symbol binding:
- Private
@embedwithoutsymbol:PrivateLinkage(internal to the object file, zero dynamic symbol table pollution, respects issue #120). - Explicit
symbol = "<name>"or exported in an interface:ExternalLinkagewith the specified symbol name (or canonical module-mangled name if exported withoutsymbol). Whensymbol = "<name>"is set, codegen also emits<name>pointing at the raw byte array and<name>_len(uint64_t) if C ABI interop is requested, or callers bind directly through the Pia slice{ptr, len}(C ABIpia_slice_u8). - When
sectionis specified, the global is added tollvm.usedso dead-stripping does not drop unreferenced section entries if an external loader scans the section.
- Private
- Replaces
tools/gpu/metal.bzl'scc_libraryhack:metal_libraryproduces the.metallibfile alone; a Pia target embeds it viaembeds = [":add_metallib"]and@embed(file = "add.metallib"), eliminatingxxd -i, generated_embed.c, andcc_library.
5. Interfaces and SHA-256 certificate hashing¶
- Private
@embedbindings (not listed ininterface I = sig ... end) do not appear in.piaiand do not affect the module's interface hash. Rebuilding a private.metallibdoes not re-verify or rebuild importers, andpiac --emit=interfacedoes not need to read private embedded files. - Explicitly exported
@embedbindings may be listed in an interface as a read-only byte slice:Wheninterface Kernels = sig let add_metallib : [u8] @embed endpiac --emit=interfaceemits the.piaifor an explicitly exported@embedbinding, it records both its exact length and its SHA-256 content hash (lowercase 64-char hex, computed withllvm::SHA256as insrc/cert/Certificate.cpp):Becauselet add_metallib : [u8] @embed(len = 4096, sha256 = "e3b0c442...")__pia_cert(ADR 0024) hashes the canonical.piaitext with SHA-256,pia-cert checkautomatically enforces at link time that every importer was verified and compiled against the exact byte length and content hash of the exported embedded blob, catching stale objects with zero extra certificate format changes.
Implementation order¶
Each step is one PR:
- Syntax, hermetic resolution & opaque verifier encoding: parse
@embed(file = ..., symbol = ..., section = ..., align = ...)on top-levelletbindings; add--embed-pathand--embedflags topiacandpia; resolve files hermetically in Sema; encode@embedin IRGen and the verifier as afixed[u8]slice with exact constantlen = Nand opaqueu8elements. - Direct LLVM codegen & Bazel
embeds: addpia.embedlowering directly tollvm::ConstantDataArrayinsrc/codegen/Codegen.cpp(withsymbol,section,align); addembedstopia_library,pia_objectandpia_binaryintools/piac/pia.bzl. - Exported interface
.piai& SHA-256 hashing: supportlet name : [u8] @embedininterface ... = sig ... end, emitlenandsha256into.piai(only when explicitly exported), and verifypia-cert checkrejects a mismatched embedded blob at link time. - Metafunction compile-time byte access: expose
@embedbindings tolet metaas compile-time fixed byte arrays (lenand indexing), with a test validating"MTLB"magic in a metafunction at compile time. - Migrate
tools/gpu/metal.bzlandtests/gpu/: drop thexxd -i/_embed.c/cc_libraryrule fromtools/gpu/metal.bzland replacetests/gpu/embed_test.cwith a pure Piapia_binaryembedding the.metallibvia@embed.
Consequences¶
- Pure Pia binaries (
mainwith no C glue) can embed.metallibarchives, model weights, vocabularies and static fixtures directly, with full bounds verification onlenand zero Z3 blowup. - Metafunctions can inspect and validate embedded binary headers at compile time without pushing raw bytes into SMT queries.
- Private embedded blobs keep incremental builds fast (importers never see them), while explicitly exported embedded blobs are content-addressed by SHA-256 through the existing
.piaiand__pia_certlink check. tools/gpu/metal.bzlno longer depends onxxdorcc_libraryto embed compiled Metal libraries.