Lowering: designing the CGT
Status: built through §8 step 11. Stage 4 — lowering the TST to the code-generation tree — and the codegen that reads it follow this design, and every step §8 lists is built and tested. Each decision is numbered (L1…). §8 lists the order they were built in, and §9 the questions still open.
The CGT is the one input codegen reads (stages.md). The TST
represents what a program means, in the language’s own terms: calls to overloads,
concepts, block arguments, handlers, owners and references. The CGT represents what the
machine does: functions over primitive storage, with every allocation, copy,
move, reference and death written out. Lowering is the only stage that turns one
into the other, so codegen never reads a type declaration, never resolves a
name, and never asks what a & means.
1. The target is LLVM IR
L1. Codegen emits LLVM IR, and the CGT is shaped for it. A CGT function becomes one LLVM function, a CGT type becomes one LLVM type, and a CGT statement becomes a run of instructions over basic blocks. Codegen does no analysis of its own: whatever LLVM needs that the language does not say (sizes, alignments, which slot a value lives in, when it dies) is decided by lowering and is already in the tree.
L2. Codegen builds the module through LLVM’s OCaml bindings. The opam
llvm package wraps LLVM’s C API, so codegen makes types, functions and
instructions as values in the compiler’s own process, and LLVM checks each
one as it is made: a wrong operand type fails at the line that built it, not
in a tool that reads a file later. The same bindings verify the module, run
LLVM’s own passes, and write the object file through the target machine, so
no text is written and read back. The target machine tunes code for the
processor clang would pick: x86-64 on any x86-64 target, core-avx2 on
x86_64h, the Haswell architecture, and LLVM’s default elsewhere. Each
allows only the instructions every processor of the architecture has. Linking that object with the runtime (§6) is done by LLD,
linked into the compiler as a library, with the arguments the C compiler
would give it (§7).
The bindings are LLVM’s own, a copy of LLVM 21.1.8’s llvm/bindings/ocaml
at vendor/llvm-ocaml, built with the compiler (lib/llvm) against the
environment’s LLVM 21 on every platform (pixi.toml). The build refuses any
other LLVM (lib/llvm/config/discover.ml), since the bindings and the
compiler’s C++ stubs are written against LLVM 21’s interfaces.
The module’s text is LLVM’s to print and changes between releases, so the tests read the CGT and what a built program writes instead (§7).
L3. The CGT is a tree, not a control-flow graph. It keeps structured
control flow — a block, a branch, a counted loop — and names every exit
explicitly (§4). Codegen turns that into basic blocks, which is mechanical.
The tree is what an optimization pass reads and writes (stage 5), and a tree
is where rewrites such as inlining and constant folding are easiest to state.
SSA form is LLVM’s job: every CGT local is a stack slot, and mem2reg promotes
the ones that can live in registers.
2. What the CGT holds
L4. Only instances. A generic verb reaches the CGT as its instances, one
function each, named by its declaration and its arguments. The TST already
checks one body per instantiation (semantics.md D12), so
lowering takes those bodies as they are. No type parameter, no Ty.Param and
no concept survives lowering.
L5. Concepts become primitives, and every type has a layout. A CGT type is one of:
- a scalar:
i1,i8,i32,i64,f64; - a struct of CGT types, in decreasing order of alignment, members of one
alignment in declaration order (
Nodes.Ty.order). A struct that C’s layout would pad, at its tail or between members, is packed: its members follow one another with no padding and its alignment is 1, so it is exactly as large as its members (Nodes.Ty.tight). A struct with no padding keeps C’s layout and its alignment. The order and the choice are functions of the members’ layouts alone, so every object that lays a type out agrees on them, and lowering reaches a member by its place in it (Type_layout.member_index). Placement is the compiler’s (memory.md§3.3). A reference type’s instance carries nothing else. A pooled variant’s node (experimental) places a struct it holds in the same order, so one index names a member in both. C sees a struct only as a copy in its own declaration order (ffi.mdF10); - a sum, in one of three forms (
Nodes.Ty.tight). A sum whose one case carrying data has a payload with a spare byte, a byte with values it never holds, is that payload alone, and each other case is one of those values at that byte (Nodes.Ty.byte_niche): aBoolholds 0 or 1, a packed sum’s tag one value per case, and a sum with a spare byte leaves the values its own cases do not take, so an optional optionalBoolis one byte. A sum of nothing or one box or reference, under 32-bit boxes, is that word, 0 for nothing (Nodes.Ty.niche). Any other sum of at most 256 cases is packed: a one-byte tag and then the widest case’s bytes, alignment 1. A sum with more than 256 cases keeps a 4-byte tag and its payload room from offset 8; - a pointer: a reference, the address of the settled owner it names
(
memory.md§4.1, and §9 below for the representation); - a fixed array:
@primitives$Array<T, n>, and@primitives$ArrayRef<T, n>which has its layout (generics.md§8.4), isnelements ofTinline, one after another atT’s stride (its size rounded up to its alignment), so it is statically sized and sits in a slot like a struct. Positions count from 1 (control-flow.md§5.1): elementiis at byte offset(i - 1) × stride. What an index outside1..ndoes is not yet specified (control-flow.md§5.2), so lowering emits one runtime check for it and leaves its outcome to §9; - a handle: the fixed-size part of a
List, aStringor a boxed member (memory.md§3.6), whose payload lives in the dynamic region.
Unit has no storage, and a verb that returns it returns nothing. A value
struct of one member has that member’s layout, so a struct around one
@primitives$I64 is an i64, and an empty one has none. A distinct
type is its underlying type. A concept literal (Integer_lit, Text_lit) is
already gone, because the TST put an implicit constructor around every one
(semantics.md D9); lowering turns @primitives$I64(3) into the constant i64 3.
Which members are boxed is lowering’s decision too, and only members on a
cycle of owning edges are
(adt.md §4).
One box per cycle is enough: a variant’s payload that leads back to the
variant is boxed, and a struct’s member only when it leads back without
passing through such a payload, whose box already breaks the cycle
(optimizations.md, “One box per cycle”).
L6. Calls name one function and pass places. A CGT call names the
function it calls, so overload resolution, method lookup and the operator and
flip forms are gone. What each argument passes follows its parameter’s mode
(memory.md §2.9):
| Parameter | Passed as |
|---|---|
| value type (a borrow) | the address of the caller’s slot, or the value itself when it is a scalar; one the body copies once into what it keeps takes the value instead, a fresh argument moved in and a place copied where the call is (§9) |
T, a reference type (a borrow) |
the address of the caller’s slot, which keeps the owner |
^T (a take) |
the moved value itself, which the callee holds in its body’s arena, so the body’s drain ends it unless the body moves it on (lifetimes.md §1.5), or in a slot of its own when every way out moves it on (§9) |
&T (a reference) |
the address of the settled owner it names |
this |
the address of the subject’s place, taken once at the call |
A call site that mints a reference for an &T parameter, or moves an owner
into a ^T one, does so with an explicit CGT operation (§3), not inside the
call.
A function also lists its locals that hold a value type. No reference names
one (memory.md
§2.4), so only the addresses the function takes of it reach its slot, which
is what folding counts on when code it cannot run is given other addresses
(optimization.md O6).
L7. A result is written into a destination the caller names. Every verb
that returns something other than Unit or a scalar takes a pointer to the
slot its result goes in. That is the spec’s own model: a move into a return
slot is initialization in place (memory.md §3.7), and a fresh value
is built directly where it will live (memory.md §2.3). A let of a call passes the new
local’s slot; a field or element store passes the field’s.
3. Memory is explicit
Every death point is known from the program text (lifetimes.md
§2.1), so lowering writes each one down. Nothing in codegen or the runtime
tracks what is live.
L8. Each scope is an arena, entered and drained by name. A CGT block
that owns storage opens its scope’s arena on entry and drains it on every way
out: falling off the end, return, abort, or an exit (§4). Draining first
waits for the scope’s spawned work (the water tower, concurrency.md
§4.1). Then it releases the scope’s fixed-size and dynamic chunks together,
with every value still in them (memory.md §3.2).
There is no per-object pass at a drain; an object that dies earlier —
overwritten, or its container gone — is destroyed there, by its own
destroy (L9). Lowering
may fold nested scopes into one arena when nothing observes the difference
(memory.md §3.1); the first version gives every block that
declares a local its own.
L9. Storage operations are CGT nodes, each a call into the runtime or a few
instructions. The set lowering writes, with the sections of memory.md
that define each:
slot— a fixed-size slot in a scope’s arena, at an offset in the scope’s frame that codegen fixes (§9);copy— a value-type copy, recursive through its boxed payloads (§2.3);move— a roaming owner’s inline bytes copied into a destination of the same type, its source spent; its dynamic blocks stay where they are unless the move is an escape, which relocates them first (§3.5);overwrite— a replacement written in place at the occupant’s address, each boxed member’s block kept and written into, recursively, and the rest of the occupant’s blocks returned (§2.2, §3.6);destroy— an owner’s or a value’s death, returning its dynamic blocks (§3.2).
A reference needs no operation of its own: minting one takes the address of a settled place, which the semantic pass alone may admit (§2.8), and a settled owner never moves, so the address holds until its scope drains (§4.2).
Where the TST has a Let, an Assign or an owning argument, lowering picks
from this list using what the move analysis already computes: whether the source
is a place or a fresh result, whether the destination is fresh or already
holds a value, and whether the type is a value or a reference.
L10. A place is an address inside a call, and a reference is one when
stored. A field read, an element read or a this is an LLVM pointer for as
long as one expression needs it, and & storage — a local, a field, an
element, a parameter — keeps that pointer. So reading through a reference is
one load, which is what the spec says it costs.
4. Control flow is explicit
L11. A block-taking verb is expanded at its call site. A verb with a
@concepts$Block parameter has no function in the CGT. Each call to it is
replaced by its body, with the block argument spliced in where the body uses
the parameter, and so is every call that body passes the block on to
(control-flow.md §2.3). @controlflow$branch becomes a CGT if,
@controlflow$repeat a counted loop. A return or abort in a spliced block
still leaves the function it was written in, because after expansion that is
the function it is in.
A worker is a loop, and no expansion. @controlflow$work and
@controlflow$register take a function value rather than a block
(spec control-flow.md §3.7, §4.1), so the verb that calls one keeps its
function, and the call becomes a CGT while. Its condition calls the step
with the subject’s place as its this, as subject!step() would, and reads
the result through the test lambda the checker supplies, which converts it
to @primitives$Bool by the implicit constructor that applies
(Supplied.worker_test). The step runs on every pass, so the condition is
lowered as a loop body is, and a small function it calls is written out
there (Lower.worker_step, State.loop); what a pass holds is in an arena
of the pass’s own, which drains before the next. Before the first step the
call settles its operands once, in written order (Lower.worker_subject,
Lower.worker_held): a subject place’s subscript indices are pinned, and a
temporary subject, or a step that is neither a variable nor a lambda
written in place, runs into a slot of the call’s scope, so every pass
works on the place and step the call named. register reads its event
source once too, a timer’s interval, and takes its first deadline just
before the first step (zane_tick_start). Its loop’s body waits for the
next deadline through zane_tick, so a step that returns false leaves at
once without a last wait. A pool thread that waits there has a stand-in
thread for the wait (zane_block_begin, runtime/spawn.c), as one that
joins a call running elsewhere does, so the wait holds back only the
caller of the waiting call (spec concurrency.md §3.4). The step’s value is
a lifted lambda’s address, a constant LLVM sees, so a step written where
the worker runs is inlined, and an optimized build makes one loop of it
with no call through the function value (tests/objects, the workers
perf program).
The same holds for a verb with any other concept parameter, such as an
implicit Int(value @concepts$Int): a literal has no storage, so the verb is
expanded and the literal is embedded where the body uses the parameter. In an
expanded body the subject names the caller’s own place, so a mut method
such as to advances the caller’s counter; every other argument is stored in
a new slot first, in the order it was written. A return in the body itself
stores the call’s result and leaves the expansion.
An expression verb, one whose body is a single expression it returns, is
expanded at each direct call as well, in every build, but it keeps its
function for every use that needs one: a spawned call, a library’s export,
and an object built apart. That is each of core’s one-line wrappers, such
as Int +(left Int, right Int) => Int(@operators$add(left._value, right._value)), so a program’s arithmetic reaches stage 5 and codegen as
intrinsics rather than calls, a generic one’s instance included. The same
binding rules hold, with one more: a verb that writes none of its parameters
reads a local it is given where the caller keeps it, since nothing else in
the call writes what the call borrows
(memory.md
§2.9.1) and nothing its body runs can. A verb qualifies when it declares no
abort and nothing it would write out spawns or holds a lambda, whose
function is named after the verb it is written in. Its size counts what it
writes out with it: each expression verb it calls, and the whole body of a
verb that always expands and of a declared subscript. A verb over 40 nodes
with them is called, so a chain of verbs that each call the next twice
stays a chain of calls a few levels up. One that would be written out
inside itself, as a recursive verb would be, is called there instead. So a
recursive verb’s own function writes each call to itself out once, and
calls the function from the copy: a tree’s walk does two levels of work per
call, which is most of what writing out gains copies and optimized
trees. And a call that gives a fresh value to a value
parameter that owns blocks calls the function: written out, the value would
be held in a slot of the caller’s and copied wherever the body keeps it,
where the function takes it as it is (Regions.taken_params).
A walk, a function a loop’s body calls, is written out there too: a
search through a table or a tree written as a function, as
find(nodes, root, keys[i]), becomes a loop inside the caller’s loop,
whose reads codegen’s loop analyses see as the caller’s own. The rules of an expression verb hold, and a return
in it leaves only the copy, as for any verb written out: from inside the
walk’s own loop it ends that loop and the copy, and the caller’s loop goes
on, as a break would (Zane has none,
control-flow.md
§3.5). A function is a walk when:
- the call is in a loop’s body: a
repeat’s block, a block arepeatruns wherever it was written, and what those call while written out (State.loop). A call outside every loop calls the function, which every walk keeps, as an expression verb does; - it reads what it is given and writes none of it: no
mutmethod is a walk, since one that writes through the caller’s place is an update, not a search, and written out it measured slower (flips’advance, +1.3% estimated cycles optimized); - it is given something to read memory through, a parameter that holds an
address (a list, a reference, a box) or an owner it borrows, and takes no
owner (
^T). A function given only numbers computes rather than walks, and is what memoization may give a table (memoization.mdM1), which a copy would not consult; one that takes an owner it may drop keeps the arena Regions gives its function (§9); - it calls itself nowhere, through any chain of the declared verbs it
calls: a tree’s recursive
findis not a walk, and a loop over its levels is; - it declares no abort, is no foreign verb, and nothing it writes out spawns or holds a lambda;
- its body is at most 120 nodes, each statement counted with the verbs it writes out, as an expression verb’s size counts them.
Nothing inside a walk is written out as a walk again, its own loops’ bodies included, so a loop writes out one level of the functions it calls, each at most 120 nodes: a loop that calls k walks grows by at most 120k nodes, and compiling it by that much more. The searches this is for are 52 nodes (a walk along a chain of links) to 84 (an open-addressed hash table’s probe), a binary tree’s search 70; 120 holds them with room for a few more tests, and keeps a body several times their size a call.
Lent conversions. A verb whose body returns a member of one of its
value parameters, such as core’s implicit @primitives$String(v String) => v._value, copies that member: a return through a borrow binds into
fresh storage
(memory.md
§2.9). Where such a call’s result is only lent to a borrow parameter of a
direct call and dies with it, nothing can tell the copy from the member
itself, so lowering rewrites the argument as the member path, f(conv(x))
as f(x._value), and the call reads the member where x keeps it
(Lower_calls.lent). A value has no identity and the member is only read,
never stored, returned or minted into a reference; what the call lends a
foreign verb, C may not keep past it (ffi.md F10). So only
time and allocation change, and only where nothing could write x while
it is lent. The checker’s rule against writing what a call borrows
(§2.9.1) sees a temporary here, not x, so lowering checks what it would
have checked for f(x._value):
- the call is a direct call that is not spawned: a spawned call reads its
arguments while the caller goes on, and may write
x(concurrency.md §4.4); - the callee is no
mutmethod, so its subject cannot bex, and takes only value parameters, none an&T,^T, reference-type borrow or pooled variant, so it writes nothing it was given; - every other argument of the call, and of the conversion, is inert: a
read of a local or a constant and its members, a literal with nothing
interpolated, or an operator, math intrinsic, primitive constructor or
lent conversion of inert operands. A block argument, a
!call or any other call may writex, so a call with one keeps the copy, and so does one reading a local bound to a spawned call: the read settles the call, which runs the handler written at the spawn; - the conversion is no
mutmethod, declares no abort, takes only value parameters, and its body is onereturnof a parameter’s member path; xis no such local, and it and every member on the path are value types reached through no reference, owner,^T, subscript or case read, so nothing outside the caller’s own slot can reach them. A conversion of a conversion is taken apart the same way, the inner one’s path becoming the outer’s root.
A verb written out where it is called is left as it is: its argument binds a slot of its own, whose body the rules above do not cover.
L12. A verb has up to three outcomes, and a call checks for them. A function returns a small outcome tag when it can end in more than one way:
- done — the primary result is in its destination;
- aborted — the abort value is in the abort slot the caller passed;
- exit — an
exitFromCallin its body: its caller’s invocation ends too (control-flow.md§4.2).
A verb that can only finish returns nothing, which is the common case, and its
calls check nothing. A call with a ? handler branches on aborted into the
handler; resolve writes the call’s destination and jumps past it. A call
that may exit is followed by a return of Unit from the caller, after the
caller’s scopes are drained.
L13. match is a switch on a tag. Each arm is a CGT block; a binder is
the address of the case’s payload. A match is abort-transparent
(error-handling.md §3.5): an arm’s aborted outcome
reaches the match’s own handler exactly as a call’s does. A case read with
its handler is a one-arm match whose other cases run the handler.
L14. A lambda is a top-level function. A lambda captures nothing
(concurrency.md §5.2), so it is lifted out as-is, and its value
is the function’s address. A call through a function value is an indirect
call with the same outcome convention, read from the value’s type: the
caller sees no body, so the type alone determines how the call can end and how
each argument is passed. A lambda that does not declare mut may be held by
a mut function type
(functions.md
§7.2), so a function value’s subject is always passed by address, as a mut
subject is (L6), and one function serves both types. A package
lambda-variable is its lambda, and each lambda is lifted once, however many
times it is read. A call marked comptime is lifted as a lambda of no
parameters, and called through its address where it is written, until its
value replaces the call (comptime.md K3). A lambda’s body does not exit
(control-flow.md
§4.2), so a call through a function value finishes or aborts, and a spawned
one carries the function’s address in its frame, ahead of its arguments.
5. Programs and packages
L15. One LLVM module per program. Lowering reads every package the TST
holds and writes one module: every instance, every non-generic verb, every
lifted lambda. A package does not reach codegen as a unit of its own.
separate-compilation.md designs one object per
package, which a library’s prebuilt release needs.
L16. Constants live in the program’s scope. A package constant is
evaluated once, in dependency order, into the program’s own scope, before
main runs. A program’s main is called, through the start function
below, by the runtime’s C main after it sets itself up.
Each constant has a function of its own, named by the constant, and two variables: its value and how far its making has got. The first call makes the value in a scope of its own, stores it, and moves the blocks it owns into the constants’ region, which lives as long as the program; every call returns the value’s address, and a read of the constant is a read through it. A constant that reads another calls that one’s function first, so dependency order holds whatever order they are declared in, and a constant that reads itself, through any chain, stops the program. A context that finds another making a constant waits for it.
Those waits can form a cycle across contexts: making a constant may spawn
a call that reads, through any chain, the constant being made, and joining
that call waits for a context that waits for the joiner. The runtime keeps
the graph of waits (runtime/value.c). A context about to block names what
it waits for, a constant’s state, whose maker it waits for while the
constant is made, or a call it joins, whose context it waits for while the
call runs. A call run where it is joined is waited for the same way. Each
edge is read from what it names when it is followed, so a made constant or
a finished call leads nowhere, and a reused context is never reached
through a wait that has ended. Edges are set, cleared and followed under
one lock, so the last wait to close a cycle follows the edges from its own
context back to itself, and the program stops with “package constants read
each other while they are made” and status 1, the program’s error as an
index out of range is. A maker reading its own constant again is the
one-step case of the same test.
A program’s entry is always a start function, zane.start, that calls each
constant’s function, the packages a package depends on first, and then
main, discarding what main returns. A main that declares its abort
type, @primitives$String, returns the sum of its outcomes (L12), and the
start function switches on it: a return is dropped as above, and an abort
passes the message’s address to zane_main_aborted (runtime/main.c),
which flushes the program’s output, writes the message and a line feed to
stderr, and ends the program with status 1 by exit, as an index out of
range does
(packages.md
§6.3). main never exits (Tst.Exits), so the start function has no exit
case. The runtime calls the entry as
void zane_main(void) (runtime/zane.h), and main may return any type,
so calling main itself would give the runtime a function of another type
whenever main returns a value, and whether it did would depend on whether
the program has constants. A discarded result that is an owner or owns a
block is held in the start function’s own arena, as any dropped result like
it is (§3), so a large one takes a frame of the main context’s range, not
the machine stack, and its blocks drain with that arena. An optimized build
inlines the start function; an unoptimized one pays one call.
A function value (a lambda-variable) is its lambda (L14) and has none of
this. A library’s constants are made by whichever program links it: a
library or a stamped dependency defines a constant’s function and variables
in every object that reads it, shared as a generic instance is
(separate-compilation.md C4).
6. The runtime is a C library
L17. The runtime is written in C and linked with every program. It owns
the chunk directory, the scope arenas and their size stacks, the thread pool,
and the few primitives a program cannot state in the
language: printing, the program’s arguments, reading a number from text,
List growth, String storage. Its interface is a small
set of C functions that CGT storage operations (L9) and intrinsic calls lower
to, so a change to how an arena works changes the runtime and nothing in the
compiler. What walks a type’s blocks – to copy, end, move or overwrite a
value of it – is the one part the compiler emits instead, for each type
(§9); the runtime runs the walks it is handed. It lives in runtime/, is
built by clang, which already links every program, and is tested in C on
its own.
C because it adds no toolchain beside the LLVM the compiler already uses, and because an arena is exactly the kind of code C states without ceremony.
The math functions of @math$ (math.md
§2) need no runtime function. A call to one lowers to a Math node, which
codegen emits as an instruction or an LLVM intrinsic when the function is
exact, a shift’s count masked to the width first, and as a call to the C
math library’s function of the same name when it is approximate, marked
nobuiltin so that LLVM neither computes it ahead of time nor rewrites it.
Its result is then the library’s where the program runs, as §2.6 has it
(platforms.md).
A call to a foreign verb, a Zane function whose body is C
(ffi.md), lowers to a Foreign_call node naming a thunk the
package’s glue defines. Codegen lays each argument out as C lays out its
type and calls the thunk with their addresses (ffi.md F8, F10), so codegen
implements no C calling convention.
7. How it is built, and how to look at it
Lowering lives in lib/cgt/, beside lib/tst/: nodes.ml for the tree,
lower.ml for the TST → CGT walk over a verb’s body (its expressions,
the storage they read, and its blocks and statements), the lower_*.ml
modules for the rest of that lowering, each taking the walk as an argument
so that it stays outside the walk’s recursion (lower_intrinsics.ml,
lower_data.ml, lower_calls.ml, lower_match.ml, lower_spawn.ml and
lower_lambda.ml), program.ml for a whole program built with it, traverse.ml for what each node is made of
and the walks and rewrites every pass over the tree uses, and
to_tree_graph.ml to render it. Codegen
lives in lib/codegen/: emit.ml builds the module through the bindings,
with what it builds from in the parts it includes (emit_types.ml,
emit_runtime.ml, emit_frames.ml and emit_lists.ml), llvm_extra.ml
binds what they lack (llvm_stubs.c), and build.ml writes the object file
and links it with the runtime, reading a failed link’s errors with
link_errors.ml. The
runtime is written as twelve parts in runtime/ (main.c, arena.c,
region.c, block.c and so on), with zane.h as the ABI that emitted code calls,
zane_abi.h as the offsets and sizes emitted code relies on, which codegen
reads as Abi, and zane_internal.h as what the parts share. runtime/dune joins the parts in
a fixed order into one generated zane.c and embeds it, with the headers,
into the compiler as strings, so a build needs nothing beside the compiler
and a C compiler (clang, or ZANE_CC). The C compiler compiles the runtime
apart from the link, and the object is kept in a cache directory
(ZANE_CACHE_DIR, else $XDG_CACHE_HOME/zane, ~/.cache/zane, or
%LOCALAPPDATA%\zane on Windows) under a hash of the runtime’s sources, the C
compiler’s command and its --version, the target triple, and the flags it
compiled with. The --version answer is itself kept there, under the path,
size, modification time and inode of the file the command runs, so a build
asks again only once that file has changed; on Windows, whose command search
it does not follow, it asks every time. A
later build that would compile the same object links the kept one instead,
which saves most of a small program’s build. A build with no cache directory,
or with a C compiler that will not say its version, compiles the runtime every
time.
The runtime is compiled with -fzero-call-used-regs=skip,
-fno-stack-protector, -fno-stack-clash-protection and -fno-wrapv, and,
on every target but Apple’s, whose ABI keeps frame pointers,
-fomit-frame-pointer, whatever the C compiler would add by itself. On ELF
it also takes -ftls-model=local-exec: the runtime is only ever linked into
an executable, so a thread’s variable is read at a fixed offset from the
thread’s pointer, rather than through the descriptor a position-independent
compile asks for. Some toolchains add the rest by default, hardened
distribution clangs among them, and they cost the runtime’s small, hot functions more than
anything else in them; -fwrapv, signed arithmetic that wraps, keeps LLVM
from simplifying the runtime’s rounding of sizes. The C library’s checked
forms of its functions, which such a toolchain turns on with a
_FORTIFY_SOURCE it defines after every flag, are turned off in
zane_internal.h itself. A C compiler that refuses any of the flags
compiles the runtime without them, and reports nothing of it.
An optimized executable takes the whole runtime into its module instead of
linking the runtime’s object: the runtime compiled to LLVM bitcode, kept in
the cache beside its objects under the same kind of key, is linked in before
the passes, every definition in it made the program’s own, internal, or
hidden when stamped dependencies’ objects linked with the program call the
runtime too, and an empty object takes the runtime’s place in the link.
There is then one copy of the runtime, so LLVM may inline any of it and sees
the runtime and the program as one. Where a call is the slow path of
something emitted code already does itself, such as a push past a list’s
room, it is marked noinline, since the runtime’s body would only repeat
the fast path there. A program built so is checked against the same golden
files as every other build. --ll prints an optimized module as the
builds below take it, with only the runtime’s bodies, since the whole
runtime’s text would bury the program’s.
A tiered build’s two tiers and an optimized library object (--object)
cannot take the whole runtime, since the runtime they run is another
object’s: tier 0’s, or the program’s that links them (tiers.md
T5 says what that leaves tier 1). Before the passes
they link into the module the bodies of the runtime’s functions it
calls, and of the functions those call, as
available_externally (lib/codegen/runtime_bodies.ml); a body linked in
replaces the function’s declaration and its attributes, which are then put
back (§9, “What LLVM is told”). LLVM inlines them
where it judges a call worth it, and every call it leaves still goes to the
runtime the program links, so nothing the runtime keeps is copied: each of
its variables comes in as a declaration of the runtime’s own, a thread’s
read at a fixed offset from the thread’s pointer as the program’s own are.
The bitcode is compiled position-independent, so no definition in it is
taken for the program’s. A body is left out, and its function stays a call,
when it would not mean the same copied: it reads a variable of the
runtime’s own translation unit, which no other unit can name, takes the
address of one of the unit’s functions, which a copy would not share, or
calls one marked noinline or cold, which would be copied whole; a
function that calls such a function is left out too. A function marked
cold, such as zane_hold, which runs only when drains are checked or
a slot’s value may hold a foreign handle, is
left out as well, since inlined it costs its caller registers on the path
that never runs it. The variables the runtime’s hot paths read, such as
its page table, are therefore not static. In a tiered build a body that
calls outside the runtime is left out as well, since tier 1 imports
everything through tier 0’s table (tiers.md), and each C
library function in it would cost a symbol lookup as every run starts. A
body taken loses any stack protection the C compiler gave it: it becomes
the program’s code, which has none, and inlined it would give its caller
the protection too. It also loses the processor the C compiler compiled it
for, which LLVM inlines only into a caller whose processor has every
feature it has: clang’s default on macOS, penryn for x86-64 and
apple-m1 for AArch64, is newer than the baseline the program is
compiled for, so no body would be inlined there. Bitcode is read only by the LLVM that wrote it or a
later one, so the C compiler that compiles it, ZANE_BITCODE_CC or else
the runtime’s own, must be a clang of the compiler’s LLVM version or older,
as the pixi environment’s is; a release bundles bitcode a matching clang made (below).
With any other, or one that makes no bitcode, a
build links the runtime’s object and calls its functions, and the cache
keeps an empty file so the C compiler is asked once.
The link itself runs in the compiler. Starting the C compiler and its linker
as programs of their own costs about eight times the link: each loads LLVM’s
shared library again. So the first build for a C compiler, target, set of
flags and objects to link asks the C compiler, with -###, for the linker
command it would run, keeps it in the cache under those, and every build
runs that command with its own files put in, through the LLD linked into
zanec (lib/codegen/lld_stubs.cpp): ld.lld for ELF, ld64.lld for
Mach-O and lld-link for COFF. What the C compiler prints is the work of
its driver, which finds the target’s start-up files, C library and
compiler runtime on this machine; LLD finds none of them. LLD runs the
command whichever linker the C compiler would have run: GCC’s collect2
takes the linker’s own arguments, so it is run as ld.lld without GCC’s LTO
plugin, and zig ld, Zig’s Mach-O linker, takes ld64’s, so it is run as
ld64.lld with the target’s architecture and the libSystem stubs Zig
carries (docs/design/platforms.md). A C compiler whose own linker is another,
such as clang with the system’s ld, is asked again with -fuse-ld=lld.
LTO’s -mllvm options are left out, since they would set options of the
compiler’s own LLVM.
A C compiler whose command LLD cannot run, because it prints none of those or
does not name the program’s files, links the program itself; that is kept in
the cache too, so it is asked only once, and ZANE_VERBOSE makes each such
build say why. When LLD cannot link with a command and the C compiler can,
the build warns. A command kept by an earlier build may have gone stale, as
when a libc update moves its start-up files, so it is forgotten and asked
for again; one just asked for is not, so the C compiler links for that
target and those flags until the kept answer is removed. A program neither
can link is an error, and nothing is forgotten.
A released toolchain runs no C compiler at all for the targets it bundles.
Its zanec finds the toolchain from the toolchain.coda one directory above
its executable, and, unless ZANE_CC names another C compiler, links with
the toolchain’s own zig cc; then, unless ZIG names another zig, a build
looks in the toolchain’s share/runtime/ before the cache
(Build.released). A compiler that is not a release’s looks in the
directory ZANE_RUNTIME_OBJECTS names, as for one run apart from the
toolchain whose runtimes it links. A build looks there for an object named by a hash of the runtime’s
sources, the target triple in LLVM’s normal form and the flags besides the
target’s, and in its link/ for the linker command named by a hash of the
triple, those flags and the libraries every program for the target links
with. Spellings of a target with one normal form, such as
x86_64-linux-gnu and x86_64-unknown-linux-gnu, and a build for the host
that names no target, so find one object and one command; a spelling LLVM
normalizes apart, such as one naming another vendor, compiles its own.
Packaging fills the directory with zanec --bundle-runtime DIR --target TRIPLE [--optimize], for each target dev/release/common.py lists.
It compiles the runtime as a build for that target would, then asks the C
compiler for the command linking a program with it, through a program object
defining only what the runtime takes from a program, and copies every file
the command links besides the program’s own into link/files/, named by its
contents, where the command names it relative to the bundle. With
--optimize it also compiles the runtime’s bitcode beside the object, with
the C compiler ZANE_BITCODE_CC names when it is set. Zig’s clang is newer
than the compiler’s LLVM, which cannot read its bitcode, so packaging names
dev/release/bitcode-cc: the pixi environment’s clang, of the compiler’s LLVM
version, run with the C library headers and definitions Zig compiles the
same target with, and an Apple target’s oldest system. Bitcode this LLVM
cannot read is not bundled, and an optimized build with that release links
the runtime’s object instead. The loader a
program names, --dynamic-linker, is a path on the machine the program runs
on, and is not copied. A program’s other objects, its dependencies’, are put
in after its own. A target is always named there, since a C compiler given
none compiles for the processor it runs on. A build for any other target, or
with another C compiler or Zig, compiles its runtime and keeps it in the
cache, and asks for its linker command as above.
Lowering starts at the root package’s main and lowers each verb the first
time a call reaches it, so a program’s unused declarations never need to
lower. Whatever it cannot handle yet it refuses with a diagnostic at that
node, never by lowering it wrongly.
Lowering does not handle these yet, and refuses each where it is written:
- a control-flow intrinsic that takes a block, such as
@controlflow$branch, used as a value rather than as a statement (a worker used as one is itsUnit, after the loop); - a store into the result of a spawned call that returns
Unit, which has no storage.
Each has a reject fixture in tests/codegen/fixtures/reject/. Lowering also
refuses one error in the program that semantics cannot see: a literal handed
to a verb’s literal parameter that does not fit the primitive the verb builds
from it, which is known only where the verb is written out. Anything else
lowering finds wrong is an invariant an earlier stage broke, and is an
internal error (stages.md).
The binary takes the same --package flags as the semantic views:
| Flag | Does |
|---|---|
--cgt |
prints the code-generation tree, as stage 5 left it when --optimize is given |
--ll |
prints the LLVM module |
--build OUT |
builds the program into the executable OUT |
--object OUT |
writes the object file OUT of the project’s own packages, with no runtime and no link (separate-compilation.md C3), and beside it STEM.c.N.o, the objects of their C sources holding assembly (ffi.md F15) |
--tiered |
with --build, makes tier 0 of a tiered build, whose calls go through slots tier 1 fills while it runs, linked with the runtime compiled with -O2 (tiers.md T1, T4, T5) |
--tier-up OUT |
makes tier 1 of a tiered build, the program optimized into the shared library OUT (tiers.md T1, T2) |
--write-backs OUT |
writes OUT, the file of the places the program’s spawned calls write back into, and builds nothing; with a root only (§9, “Snapshots”) |
--program-write-backs FILE |
with --no-root and --cgt, --ll or --object, lowers the library for the one program whose places FILE names, reading every other place plainly (§9, “Snapshots”) |
--stamp PATH=STAMP |
names the symbols of the package at PATH with STAMP, as --package STAMPPATH=DIR does; a dependency given one arrives as objects of its own, so its verbs are declared rather than lowered (C1, C6, C10) |
--import PACKAGE:KEY=PACKAGE |
the package the first imports by KEY, each named by its identity (C10) |
--link FILE |
links the object FILE into the program --build makes, as a stamped dependency’s objects are (C7) |
--target TRIPLE |
compiles --ll, --build and --object for the LLVM target TRIPLE instead of the host, and has the C compiler link --build for it; LLVM gets the triple’s normal form, and the C compiler the triple as written (platforms.md) |
--optimize |
runs stage 5 over the tree (optimization.md), then LLVM’s -O2 pipeline over the module, with the whole runtime in it for an executable and the runtime’s bodies safe to copy for --object (§7), generates optimized code, and compiles the runtime with -O2 |
--fixed-region BYTES |
the size of the main context’s range of frames, a whole number of MiB, 256 MiB when absent (§9) |
--spawned-fixed-region BYTES |
the size of each spawned call’s range of frames, a whole number of MiB, 8 MiB when absent (§9) |
--pack-aggregates |
passes an aggregate that fits in the registers a call gives it as packed 8-byte words rather than as itself or by address; off by default, an option to measure (§9) |
--no-memoize |
builds without the tables of results calls are remembered in while the program runs (memoization.md) |
zanec --rewrite STAMP INPUT OUTPUT takes no packages: it writes the
library object INPUT to OUTPUT with its ! placeholder turned into
STAMP, as fetching does (separate-compilation.md C9).
zanec --remap FROM TO INPUT OUTPUT writes INPUT to OUTPUT with every
reference to the package version stamped FROM moved to the version stamped
TO, as remapping does (C11).
--no-root refuses --build, since a build without a root is not an executable.
With it, lowering starts from every verb each of the project’s own packages
declares that is not generic and has a function of its own, and from their
lambda-variables, rather than from main, and names them with the !
placeholder (separate-compilation.md C1, C5),
or with their stamp when they have one (C6).
Without --optimize no optimization runs over the program, neither stage 5
nor LLVM’s, which makes the build about three times faster; only the
runtime linked by tier 0 of a tiered build is compiled with -O2
(tiers.md T5). zane build
optimizes, and zane run makes both builds at once and moves the program
from the unoptimized one to a lighter optimized one while it runs
(tiers.md). A program means the same every way, since the
semantics leave nothing for an optimizer to decide, so tests/codegen/ builds
each fixture plain, optimized and tiered and holds all three to one golden
file.
tests/codegen/ lowers and builds each fixture, runs it, and compares the
tree and what the program wrote against golden files. tests/runtime/ compiles
the runtime with a C program of its own that calls it directly.
8. The order it is built in
Each step is one PR, ends with programs that run, and keeps every earlier test passing.
- This design, and
concepts-vs-primitives.mdbrought in line with it. - Scalars and calls. First a program that prints a string literal
through
@program$console: the CGT, codegen, the runtime and the test that builds and runs it. Then lowering forInt,FloatandBoolarithmetic, functions, returns, andbranch/repeatexpanded fromifandtoverbs a program declares. A test that builds and runs a program. Printing anIntgoes through aString, and waits for a conversion from one to the other, which the spec does not name yet. - Values. Value structs and sums: layout, copies, fields,
match, and enum maps. Amutsubject is passed by address. A value type that contains itself needs a boxed member, which waits for step 7, and a case read, which takes a handler, for step 4. - Aborts and exits. The outcome tag, handlers,
resolve,guard, and case reads. - Reference types. Arenas, hosting and moves. A reference-type local is hosted in its block’s arena, and a move copies the instance into its new host. The runtime checks that scopes drain in order and that none is left open, and is tested in C on its own.
- Guests. The anchor pool,
mint,resolve, anchor merges, the backpointer (L5), andfloat, all since replaced by step 11. A reference-type subject and a swallowed argument are passed by address (L6), and amatchbinder is its payload’s address (L13). The runtime keeps, arrives, vacates, merges and floats identities from a per-type layout of where each host’s backpointer is, and a drain retires the anchors its scope still hosts. - Handles.
String,List, boxed members, anddestroyreturning dynamic blocks. A string’s or a list’s handle, and a boxed member, own a block in the dynamic region of the scope that holds the owner, which is returned when the owner dies, at an overwrite or at its scope’s drain; a value that leaves a scope takes its blocks out first, and a value copied whole gets copies of its own. A subscript is a place, generic types and verbs lower per instance, and the anchors of hosts in a list follow them when its block grows. The runtime is tested in C on its own, and a checked drain stops a program that ends with a block still out. spawn. The thread pool, futures, and the water tower. A spawned call’s arguments are read where it is written, into a frame in its block’s arena, and it runs on a thread of the pool in a context of its own. A local bound to it waits for it where it is read, and the block waits for every call spawned in it before it drains; a result comes home with its blocks and its anchors, read or not. A call that can abort or exit settles on the spawning thread, and the program’s runtime resizes the pool. The runtime is tested in C on its own.- Function values. Lambdas lifted to functions of their own (L14), lambda-variables in a body and at package scope, and calls through a function value, which may abort or be spawned. Function values are passed, returned and stored in members.
- The rest of what the TST accepts. Package constants (L16),
@primitives$Array<T, n>with its literal and checked elements,@primitives$I32, a number parameter read as its number, field constructors called as the verbs they are with their defaults filled in, a program value such as@program$consolepassed as a guest, and spawned calls to an intrinsic or to a verb expanded where it is called. After this step lowering refuses no program the TST accepts. - Settled and roaming owners. The memory model of spec
#212 and
#214. A reference is the
settled owner’s address, so the anchor pool, the backpointer, forwarders
and floating are gone, and a reference-type instance is its members
alone. A
^Targument is moved into the callee, which holds it in its body’s arena; a bare reference-type parameter is a borrow, passed by address. Every overwrite is made in place, keeping each boxed member’s block, and a block relocates only when its owner escapes.@primitives$ArrayRef<T, n>lowers asArraydoes from any of its literal constructors,.refsand.roamingincluded, withfilla counted loop that makes each element asmake(index(position)):indexis the checker’s conversion of a position to the typemaketakes it as (Supplied.fill_index), and its body is written out in the loop rather than called (Lower_data.fill).@primitives$Stringis a value type. The runtime is tested in C on its own: an overwrite keeps a boxed member’s address, and arrival leaves blocks that outlive their destination where they are. - Where blocks are made. Once every function is lowered, a pass over
the whole tree (
lib/cgt/regions.ml) works out which callees keep the^Targuments they are given. A function whose arena holds only owners that every way out moves on opens none, so what it makes is made where its result goes and nothing relocates it, and a value parameter copied once into what the callee keeps takes a fresh argument as it is, through a local copy of a callee whose calls keep their signature. A returned local is moved out of its slot. A block that moves a fresh owner into a callee that can drop it keeps an arena, so the owner goes with the block. Issue #210. - Worker pools.
@primitives$WorkerPool<T, S, E>(concurrency.md§6) adds no node to the CGT. A pool is a 16-byte handle, the runtime object’s address and a stamp, laid out as a foreign handle whose free thunk is the runtime’szane_workers_free(Type_layout.is_pool). So the scope that holds a pool lists it as it lists a foreign handle, and its drain ends the pool, which waits until every element’s step has returned false and the funnel has applied every event (§6.7). Each operation is a runtime call (Lower_workers): the constructor copies the state into the pool,startmoves or copies the element into a region of its own,statereads a snapshot from a buffer of the runtime’s thread,elementsmakes a list, and the checker’s publish lambda copies the event into the funnel’s queue. The runtime calls a step and a funnel through a thunk,zane.step.Nfor eachstartandzane.funnel.Nfor each constructor with state, which reads a frame the runtime fills and calls the function value, aszane.spawn.Ncalls a spawned verb. Each step and funnel call runs in a context of its own, which names the pool, so a publish made in a call that the step spawns reaches the same pool. The runtime runs the pools’ work as jobs after the spawned calls, and keeps a worker that waits on a timer in a heap of deadlines, holding no thread (runtime/workers.c). A program that makes a pool is not single-threaded (Program.spawned). The runtime is tested in C on its own.
9. Open questions
-
How many scopes get an arena. L8 gives every block that declares a local its own. Lowering opens one only for a block that holds a reference-type local, and a value-type local stays an LLVM stack slot, since nothing distinguishes the two placements until a value owns dynamic storage, which it does only through a boxed member; such a value is held in the arena too. Once every function is lowered,
lib/cgt/regions.mlfolds more of them away (memory.md§3.1), from what it works out about every function at once:- A
^Tparameter is kept when every way out of its function has moved it on: into the result, into a place the caller lent, or into a callee that keeps it in turn. A callee another object defines, one called through a function value, and a verb expanded where it is called are taken to drop what they are given. - A slot written with a new owner holds a second owner, which every way out must move on too. The first must have gone on before the write, since writing over an owner drops it.
- A function whose whole body is one arena, holding only owners that every way out moves on, opens none. It holds them in slots of its own, and what it makes is made in its caller’s innermost region, which is where its result goes, so nothing relocates it (§3.5). A function with a spawn, a reserved slot, an arena nested in its body, or a callee lent one of its slots that opens an arena keeps its own, since the runtime takes a slot outside every arena to be in the innermost one’s region.
- A value parameter the body only reads to copy it once into what it
keeps takes its argument: a fresh one is moved in rather than held by
the caller and copied. A function no other object calls, and none
calls through its address, is changed in place, and a place is copied
where the call is. Any other function with a body here keeps the calls
it was compiled for: one another package’s object defines, one this
object exports, one called through its address. It gets a local copy,
<symbol>.taken, that takes the parameter, and only a call that passes a fresh argument there calls the copy; a call that passes a place calls the function as it is, which copies. So core’sString(@primitives$String)takes a foreign call’s fresh string with no copy, and the arena that only held it goes, so the bytes are made where the result ends and its escape moves nothing. The copy is kept and judged as its function is. - A
return, anabortor a return to an expansion of a value held in a slot of the function’s own moves it out of the slot instead of copying it: the exit ends the slot, so nothing reads it again (memory.md§2.3). A return to an expansion does so only for a slot in an arena the expansion opens. A function that spawns keeps the copy, since a spawned call may read the slot until the drain joins it. - A block that moves a fresh owner into a callee’s
^Tparameter keeps an arena when the callee can drop it, so the owner’s blocks go when the block drains (below); for one that keeps it, the block opens none for it, and an arena left holding nothing goes.
- A
-
A scope’s slots are one frame, in one range per context. Every slot a scope places has a size known at compile time, so
lib/codegen/frames.mllays the scope out as one frame: the runtime’s record of the scope at offset 0, then each slot and each spawned call’s header and frame at a constant offset. Scopes nest last-in-first-out, so each context reserves one contiguous range of addresses for its frames: a scope’s frame starts where the frame around it ended, and draining it moves that point back. Wherememory.md§3.1 gives each scope 1 MiB chunks of its own, this places the frames back to back. The spec leaves arena granularity to the implementation and fixes only that a scope’s memory is released together, which this does.- Entry and drain are inline. Only its own thread changes a context’s scopes (below), so emitted code opens a scope itself: it takes the frontier as the frame’s start, moves it past the frame, zeroes the record and links it to the scope around it. A slot is the frame’s start plus its offset. A drain that has no spawned call to wait for, no dynamic chunk to give back, no foreign handle to end and no check to run moves the frontier back to the record. Only a scope that spawns a call looks for one to wait for: a spawned call waits on the innermost open scope where it starts, which is the scope its frame is placed in. A scope that holds a worker pool holds it as a foreign handle, whose end waits for the pool’s work, so its drain is the runtime’s. Only a scope that places a slot that may hold a handle looks for one. Otherwise both call the runtime, which does the same. A reserved slot and a spawned call’s header and result slot are zeroed where they are placed; a held slot is written at once.
- The range is reserved, and becomes usable as it is touched.
runtime/region.creserves the range with no access, then a guard of 1 MiB. The first touch of each further MiB faults, and the fault handler makes it usable and lets the access run again (mprotecton POSIX,VirtualAlloc(MEM_COMMIT)on Windows), so a system with strict overcommit charges only what is used. Under Valgrind the whole range is usable from the start instead: Valgrind gives a fault handler stale registers, so an access run again could land at an older address (tests/runtime/faults.c). If the system cannot commit a step, the program reports “out of memory: cannot commit a context’s fixed-size region” and exits with status 1. A frame of up to 1 MiB that does not fit faults in the guard; a larger one is checked against the range’s end before it is placed, on a branch marked unlikely. Either way the program stops with “scopes nested too deep”, the range’s size, and the manifest field that sets it, and exits with status 1. So reading any slot of an open scope’s frame either reads the program’s memory or stops it as the frame was owed, whichever slot is read first, which the facts codegen gives LLVM (“What LLVM is told”). - The sizes come from the root manifest. The main context reserves
--fixed-regionbytes, 256 MiB by default, and each spawned call’s context--spawned-fixed-region, 8 MiB by default.zanepasses them from the root manifest’sfixed-regionandspawned-fixed-region(dependencies.md§2.1), and the module that defineszane_maindefines them for the runtime. - No cap on nesting. A scope’s record links to the scope around it and to one it skips to (Myers’ skew-binary jump pointers), so finding the scope whose frame holds an address takes steps logarithmic in the depth, and nothing keeps an array of scopes by depth. The chunk map holds a pointer for each MiB, to a dynamic chunk’s scope or to the context whose range it is in, so it caps no depth either.
- The machine stack. A program’s calls also nest on the machine
stack, which for most programs fills long before the range does. Every
worker has 8 MiB of it; the main thread’s size is set by its launch
environment on POSIX and to 8 MiB by the linker on Windows
(
platforms.md). Emitted functions probe it page by page as they grow it, and the fault handler runs on a stack of its own, so a full machine stack stops the program with “recursion too deep” and status 1 rather than a crash. - The program’s output. A print writes into a buffer of the
runtime’s own (
runtime/main.c), never through stdio, so that the fault handler can flush it with only async-signal-safe calls. It has stdio’s size for stdout, the block sizefstatreports orBUFSIZ, and flushes where stdio’s would: when it fills and when the program ends, and at every print that writes a newline when stdout is a terminal, which the runtime asks once at startup. A mutex orders ordinary writers, and an atomicbusyflag grants the buffer: a writer claims it from free to 1 after taking the mutex, and touches the buffer only then. A failed write marks the output failed, and the program then ends with status 1. The exit path, an index out of range, a cycle of constants andzane_brokenflush it first.- From the fault handler. A stop for a full machine stack, a
range full of frames or a failed commit claims
busyfrom free to 2 without the mutex, and flushes only if that succeeds, so it loses at most the buffer when another thread, or the faulting one, was mid print, and a writer that later finds 2 waits for the program to end. A write to a full pipe would block, and a check before it cannot rule that out, since another process may fill the pipe in between. So the handler writes a socket withMSG_DONTWAIT, and a pipe, on Linux, through/proc/self/fd/1opened anew withO_NONBLOCK, a description of its own that no process sharing fd 1 sees, dropping what it cannot write at once. Elsewhere it drops a pipe’s output. On Windows, where a pipe offers no way to ask whether a write would block, it writes only a terminal or a file. The message on stderr appears either way.
- From the fault handler. A stop for a full machine stack, a
range full of frames or a failed commit claims
-
A reference is an address.
memory.md§4.1 stores a reference as theu32segmented offset of the settled owner it names. The runtime addresses everything else with native pointers rather than segmented offsets – slots, handles and blocks alike, as the next item says – so a reference is the owner’s 64-bit address too. Minting one is taking the place’s address, and reading through one is a load. A settled owner never moves, and the semantic pass admits a reference only to a settled place, so the address holds until the owner’s scope drains (§4.2). Nothing a program does can tell the two representations apart; moving the runtime’s addressing to segmented offsets is one change for all of them. -
Every overwrite is in place.
memory.md§2.2 requires it of a settled owner: the replacement is written at the occupant’s address, and each boxed member reached through struct fields andArrayRefelements keeps its block, which the incoming payload is written into, recursively. The runtime does the same for every overwrite of a value that owns blocks – a roaming owner, a list’s element, a variant payload of the same case, a value – since nothing references those and reusing a block of the right size is never observable. -
Each scope’s dynamic region. A scope’s blocks are in a region of its own, as
memory.md§3.1–3.2 has it: chunks of its own, a bump frontier, and a stack of returned blocks per size and alignment, all given back at its drain. A block of up to 16 words aligned to a word is in a size class, whose stack is one of a fixed array at the start of the region’s mark, beside its frontier, its frontier’s end and its count of blocks out, so emitted code reads each at a fixed offset (below); any other size has a stack found by a search. A list’s block doubles from 128 bytes (§3.6): up to 1 MiB into a returned block of that size, in place when it is last at the frontier, or else into new bytes. Past 1 MiB it moves once into a span at the start of a reservation of 64 times the span’s size, made with no access as a context’s range is (below), and each later doubling makes more of it usable instead of moving the elements. Where the system will not reserve that much, as under a cap on the address space, the reservation is the span alone, and a list that outgrows its reservation moves into a fresh one. The span it leaves gives its pages back to the system a chunk at a time as the elements are copied out, all but its first page, which holds its mapping’s header, and goes to no stack; its addresses stay with the region until it drains. A span that is returned whole, by an end or an overwrite of its list, goes onto its size’s stack with its pages, and a block past 1 MiB, grown into or copied, first takes such a span whose reservation holds the addresses a new one would get: one of its own size, else the smallest on top of the region’s stacks above 1 MiB, so a small block leaves a larger one for a larger request. So a list a field assignment replaced is where the next list grows, rather than resident memory nothing asks for until the region drains. A box’s payload past 1 MiB is never such a span, which may lie anywhere: it takes whole chunks of its own, which lie within the image’s reach as every chunk does, so a 32-bit box reaches it (memory.md§3.6). Returned, its chunks go to a stack of their own, which only another box’s payload of its size takes from, and no list’s or string’s block is taken for it; they go back to the system at the drain. A push is inline: while the block has room, the emitted code takes the element’s address and counts it itself, and only a full block callszane_list_push, which grows it. An empty list’s first block, four elements’ bytes kept between 32 and 128, is made inline too, in a function with a scope of its own open (below). Together these makelistgrowthin the spec’slangbench/, which appends two million entities to a list twenty times, take less than half the time it did, and as much as C’sreallocdoes within a few percent. A new list followed by a run of pushes, with nothing else naming it or able to leave until the last, has room for the whole run made at its first push (Loop_shapes.runs), in that first block when the run fits in it and byzane_list_reserveotherwise, and the run’s other pushes are stores with no check of the room. A list never shrinks: a removal moves the element out of its place and closes the list over it, by moving the later elements down or the last one in, and the list keeps the block until it dies. Where the runtime departs from the spec:- A block is placed where its value is made, which is the innermost
scope. When the value arrives in a place – a slot, an overwritten
place, a list’s element – its blocks stay where they are if their
region outlives the place’s: the same scope, an enclosing one of the
same context, the program’s own region, or the constants’ region.
Otherwise the arrival is an
escape (§3.5), and they move into the region that holds the place
first. The spec builds a fresh value in its destination directly
(§2.3); here a value built for an older scope’s place is moved there
once. A region of another context, such as a spawned call’s, is taken
as shorter-lived, so a result coming home always moves its blocks, and
a subject region outlives only itself, so what arrives in one moves in.
A
list’s growth goes to the region that holds the list. A box and a hold
are made in the innermost region, which every block of what they take
already outlives, so nothing arrives at either. For the same reason a
value arriving in the innermost region moves nothing, and its blocks are
not walked:
zane_arrivereturns at once there, and for a place in one of that region’s chunks, as a list’s element is, the check is the place’s chunk-map entry againstzane_open, which an optimized build inlines (tier 1 too, when LLVM can read the runtime’s bitcode); an unoptimized build and tier 0 callzane_arrive. A result coming home from another context is the one arrival that walks its blocks wherever it lands. - A value leaving scopes that drain – a
return, aresolve, anabort, areturnfrom a block argument or an arm – moves every block it owns in them into the scope the exit returns to, before the drain (§3.1). Results are returned by value (below), so that scope is the caller’s innermost, and the value moves again if the caller places it further out. - A scope’s first blocks are in its own frame, not in a chunk. While the
scope is its context’s innermost, the frontier moves on past them: the
frame stretches beyond its slots by at least 4 KiB at a time, to at
most 64 KiB with its record and slots (
ZANE_STRETCHandZANE_STRETCHED,runtime/zane_abi.h), as long as the range holds it. A list whose block is last there grows in place by stretching the frame further, to at most 1 MiB. A frame already past 64 KiB, by such a list or by its own slots, stretches 4 KiB more for a small block, as the record of the stack a list’s old block goes to, rather than taking a chunk, while it stays within a chunk’s size. An empty list’s first block comes from the emitted push itself, with no call, when the list’s handle is in the innermost scope’s region and the push is in a function with a scope of its own open: the next line of the scope’s heap while it has room and no returned block, or, while nothing has been allocated in the scope, a stretch of the frame. A function with none, as a recursive one that only builds what it returns, keeps the call, so its code stays small enough for LLVM to unroll its loops. A block that comes once an inner scope has opened, or that the stretched frame cannot hold, goes into a chunk of the scope’s own as the spec has it. A drain moves the frontier back to the record, which gives back the stretched bytes with the frame, so a scope whose blocks all fit there maps no chunk, and its drain stays inline. The spec keeps a scope’s fixed-size and dynamic storage apart (§3.1); a block here is still the scope’s alone and is released with it. Ontests/perfscopes, whose every call makes and fills a small list in a scope of its own, this takes 29% of the optimized build’s estimated cycles, 33% of the plain build’s and 26% of the tiered build’s, since such a scope no longer maps, lists and gives back a chunk. - The runtime finds a block’s region from a map of every chunk it made, and a slot’s from where each open scope’s slots began, trying the innermost scope first, rather than from a segmented offset (§3.1). A dynamic chunk begins with a cache line of its own bookkeeping, and an oversized block’s chunks are one mapping.
- Every block is at least a word, and aligned to one; a list’s is aligned to a cache line.
- Dynamic chunks come from the system directly, reserved at a 1 MiB
boundary and committed, not from the C allocator: memory
mallocowns cannot be decommitted on Windows, and on POSIX its pages are not the runtime’s to give back. A spare chunk, one a drained region gave back, is warm, its pages resident, or cold, its addresses kept and its pages after the first given back. The first page holds the chunk’s header and the link of the list it is on, so it stays.- Warm. A context keeps
ZANE_WARM_KEPT(4) drained chunks warm for good, and any others while they have sat unused through fewer thanZANE_COOLING(8) of its later drains. A context whose call is over gives its warm chunks to the pool, which keeps up toZANE_POOL_WARM(16) of them warm. - Cold. Every other spare turns cold: its pages go back with
madvise(MADV_DONTNEED)on Linux, a fresh anonymous mapping over them elsewhere on POSIX, since macOS may keep a page’s memory and contents throughMADV_DONTNEED, andVirtualFree(MEM_DECOMMIT)on Windows. A cold chunk is reused like a warm one; its pages come back zeroed when touched, after a commit on Windows, and a failed commit stops the program as a new chunk’s does. PastZANE_COLD_KEPT(256) cold chunks, which hold 1 MiB of first pages, a drained chunk goes back to the system whole, as an oversized block’s chunks always do. - Reuse. A region takes its context’s latest warm spare, then one from the pool, then a cold one, then a new chunk. Counts beside each list keep every limit O(1).
- Lists’ reservations. A drained list’s oversized span keeps its
reservation and its pages: a context keeps up to
ZANE_WARM_SPANS(16) of them holding at mostZANE_WARM_SPAN_BYTES(64 MiB) of usable bytes, the most glibc’smallockeeps of what a program frees before trimming its heap (twice its largest mmap threshold). Past either, the one kept longest goes back first, and a span larger than the budget goes back itself. An unbounded pool’s reservation (experimental) lists one chunk however much of it was touched, so it counts nothing against the budget, and a context keeps at mostZANE_WARM_POOLS(2) of them, for its next pool alone: a list taking one would leave the pool’s touched pages past its own end uncounted. Each stays until it has sat unused throughZANE_COOLINGof its later drains, and all go back when its call is over, or when the system will not reserve addresses for a new chunk or span, which the kept reservations may be what used up. A block past 1 MiB that no stack of its region holds takes one whose reservation holds what it would reserve, so a call that builds several big lists each time round grows them into pages already resident rather than faulting fresh ones in. Two kept reservations were too few: a search that drains three (LangArena’s Maze::BFS) faulted one in every call. - Windows. A 1 MiB-aligned reservation costs an extra MiB of
addresses there, and
VirtualFree(MEM_RELEASE)frees only a whole reservation, so chunks are carved from slabs of 64 MiB, or one of its own for a larger block. A slab counts its chunks that are live or warm. A chunk given back whole is decommitted entirely, and once every chunk carved from a slab is cold or given back, and no more will be carved from it, its cold chunks leave the cold list and the slab is released. - The delay, measured. Langbench at spec
8298a3a, core02852ef, built with--optimizebefore and after, on a 4-core x86_64 container, medians with ranges in brackets. With chunks turning cold at once (five runs each), ntree 10 went from 0.380 s [0.354–0.456] to 0.523 s [0.494–0.630] and treecopy 18 from 0.257 s [0.249–0.266] to 0.395 s [0.349–0.412]: both drain more chunks per scope than a context keeps for good, and faulted their pages back in at every drain. With the delay at 8 drains (seven runs each), ntree took 0.391 s [0.367–0.448] before and 0.383 s [0.370–0.439] after, treecopy 0.295 s [0.292–0.311] and 0.283 s [0.271–0.297], and binarytrees 16, listgrowth 2000000 and allocfree 1000000 overlapped too; delays of 64 and 512 drains measured the same. Every other langbench program overlapped without the delay. Peak memory did not change for any of them.
- Warm. A context keeps
- A value that owns a block is held in its scope’s arena like an owner,
and so is a fresh owner or value that nothing keeps, such as a result
that is dropped or an operand, so its blocks are in a region that
drains with it. A dropped result that owns no block has nothing for a
drain to end, so nothing holds it: the element a discarded
swapRemovemoves out needs no scope. - A drain walks none of its values but those that may hold a foreign
handle, whose C object only its free function releases: a slot of one
is listed as its value arrives whether or not drains are checked, and
the drain ends it (
ffi.mdF12). Nothing that outlives the scope owns a block in its region, since an escape moves its blocks out first, so every block still there dies with it. A value of the scope may own blocks in an outer region, which a move into a deeper owner, such as a^Tparameter, leaves where they are (§3.5). They are dead space there until that region drains. They cannot pile up. A symbol moved there is spent, and is refilled only in the block where it is declared (lifetimes.md§1.6), which drains no later than that region. A fresh owner moved into a callee that can drop it was made in an arena the block that made it keeps for it (above), which drains on every pass of a loop. - A program run with
ZANE_CHECKset checks every drain instead. The scope’s values and the values write-backs retired there return their blocks one at a time, and a block still out in the region after that is one whose owner is somewhere the scope cannot reach, which stops the program. The codegen and object tests run every program both ways, each held to the same output.
Nothing a program does can tell these apart.
- A block is placed where its value is made, which is the innermost
scope. When the value arrives in a place – a slot, an overwritten
place, a list’s element – its blocks stay where they are if their
region outlives the place’s: the same scope, an enclosing one of the
same context, the program’s own region, or the constants’ region.
Otherwise the arrival is an
escape (§3.5), and they move into the region that holds the place
first. The spec builds a fresh value in its destination directly
(§2.3); here a value built for an older scope’s place is moved there
once. A region of another context, such as a spawned call’s, is taken
as shorter-lived, so a result coming home always moves its blocks, and
a subject region outlives only itself, so what arrives in one moves in.
A
list’s growth goes to the region that holds the list. A box and a hold
are made in the innermost region, which every block of what they take
already outlives, so nothing arrives at either. For the same reason a
value arriving in the innermost region moves nothing, and its blocks are
not walked:
-
Each type’s walks. Copying, ending, moving and overwriting a value each walk the blocks it owns, down through the blocks inside them. Codegen emits the four walks for each type whose layout lists a block, and a fifth, an overwrite of a settled place, for a type that holds a foreign handle such an overwrite replaces through its setter (
ffi.mdF12); the table of any other type names its overwrite there too. It emits them the first time the program walks a value of the type, and a table of them, named for the type, which is what the runtime is handed wherever it is to walk a value of the type: a copy, a slot whose value a checked drain ends, an arrival, a promotion, an overwrite, a spawned call’s result, a write-back and a constant. A type that owns no blocks has no walks, and is handed as none. Types whose layouts list alike positions, each nesting layouts that walk alike in turn, share one table and its walks, named for the first of them by name, soList<String>under three names is walked by one set. A walk is built from its type’s positions, the offset of each and the variant tags each lies under, so it reads each handle and box where it is, and calls the walk of what a box or a list’s elements hold directly, one level deeper. An array whose elements own blocks is one position, its count, the bytes between its elements and the element’s layout, so a type’s positions do not grow with an array’s length: its walks loop over the elements and call the element’s walk for each, one level deeper. They never hand an element’s walk on, since an element is inline and an overwrite writes over its bytes before the work would run; how deep arrays nest is bounded by the type. Past 256 levels it hands that walk to the runtime’s work instead, which runs it from the top once the walk handing it on returns, so a value nested any number of boxes deep is walked without the machine stack growing with it (memory.md§2.3 sets no depth limit). An end hands on the return of a block before the walks of what lives in it, so the block is returned after them, and an overwrite hands on its own arrival before it writes anything below it, so the deepest places arrive first. Codegen emits the vacating of a value of the type too, which the runtime never needs. A copy takes all its blocks in the innermost region, which only its thread changes, so it takes a boxed payload’s block inline: the top of its size class’s stack, or else the next bytes at the frontier, and calls the runtime only when neither has one. A box takes its block the same way, in the region the runtime nameszane_open: the innermost one of the context running on the thread. A payload larger than the largest size class is left to the runtime, which past 1 MiB maps it whole chunks of its own (zane_box_held), below. This makes binarytrees, which boxes every node, use a third of the instructions it did. A move, an end and an overwrite return blocks to other regions, so they call the runtime for each block. Walking a table instead decoded each position from it at run time, for each block; the emitted walks make treecopy, which copies a tree whole, 2.6 times faster (tests/runtime/README.md). -
Each spawned call has a context of its own. A spawned call runs with its own nest of scopes and its own range of frames, so a thread places frames only in its own; a thread that runs one while waiting for it switches to that context for the call. The call’s result is kept in its frame, with its blocks in the first scope of the call’s context, until it comes home: at a read of the local it is bound to, or at its block’s drain. There it is copied into a slot the block reserved, and its blocks move into the block’s region, as any value’s do where it arrives from another context. The context then goes back to a pool for the next call, and keeps its range for it. The chunk map names the context each MiB of a range belongs to, and the scope each dynamic chunk does.
Only its own thread changes a context’s scopes and their regions, even while calls it spawned are out, so nothing about them is locked. A spawned
mutcall whose subject owns blocks would otherwise grow them in the spawner’s region, so the subject goes into a region of its own (runtime/spawn.c): a subject region, which belongs to no scope. The spawner makes it and fills it before the call starts, the call alone allocates in it while it is out, and the joining thread takes it back. The subject’s blocks move in when no other call is out that could be reading them, and are copied in otherwise. At the join the call’s result comes home first, since what it owns may be in the subject region. Then a subject region holding a copy of a subject reached through an owner has it written back (below), and its chunks are given back. Any other has its chunks merged into the scope that holds the subject, or, for a local, is kept for the local’s next spawned call until its scope drains, so a loop of such calls reuses one region rather than making one each time. A package constant, which any thread may make, lives in one more such region, changed under the constants’ lock. A build withZANE_TSAN=1, anddocs/experiments/subject-regions/tsan-ccasZANE_CC, runs under ThreadSanitizer. The spec leaves all of this to the implementation. -
The pool steals work. Each pool thread keeps a deque of the calls it spawned and runs its own newest first; a thread with none left steals another’s oldest (
concurrency.md§2.4). Calls spawned from the program’s own thread go to a deque every pool thread steals from. A thread that waits for a call no thread has taken yet runs it itself, so a pool of one thread never deadlocks on a call that spawns and waits. Each deque has a lock of its own rather than being lock-free, which is left to measurement.setThreadsresizes the pool while it runs: more threads start at once, and a thread over the count leaves when it next finds no work, its deque kept for the next thread to start. The pool keeps at most 4095 threads. -
A write through an owner from spawned work. A spawned
mutcall whose subject is reached through an owner – a member of a reference-type instance, of what a reference names, or an element of a list – is the one way spawned work writes where another thread may read at the same time (concurrency.md§4.2–§4.4). Such a call works on a copy of its subject in its frame, deep for a value that owns blocks, which is copied into the call’s subject region, and writes it back: when it returns, or, for a copy in a subject region, at the join, on the joining thread. What the copy owns moves into the subject’s region, what the subject owned is retired there, whole, until that region drains, and the bytes are replaced while one global count of write-backs begun is ahead of the count done. Each piece is stored in one atomic access of the widest width, up to 8 bytes, that its address is aligned to, so every naturally aligned 1, 2, 4 or 8 bytes of the subject changes in a single store. A subject reached any other way is written where it is, since no other thread can reach it (§4.3). Other threads see the call’s writes all at once, when it returns, which is one of the orders §3.7 already allows. -
Snapshots. A value read through an owner into a fresh binding – a local, an argument, an operand – is read as a snapshot (§4.4): its bytes are taken when every write-back begun is done, and taken again if one began meanwhile. Nothing a snapshot names is ever returned while a reader could follow it, since a write-back retires what it replaces, so a value that owns blocks is then copied whole from the snapshot with no further checks: none of §4.4’s bounds on a walk are needed, and no attempt allocates anything it has to give back. A
matchor a case read on such a place reads it where it is. A value of 8 bytes aligned to 8 is read in one atomic load that codegen emits: a write-back changes exactly those 8 bytes in a single store, since its pieces are aligned blocks of at most 8 bytes and such a value holds no smaller place a write-back could target, so the load sees all of the old value or all of the new one, which is the snapshot. A narrower value can sit inside a wider piece that one store writes, which LLVM does not make atomic with a narrower load, so it stays a call. The load isunordered, which LLVM may move and combine as it does plain loads, unless the value holds an address, such as a box’s: that one is an acquire load, so what the address names, written before the write-back’s release fence, is seen with it on a processor that reorders loads. A wider value is a call into the runtime, which checks the two counts and fences the same way. The load makes trialdiv, fannkuch and entities 1.8 to 4.6 times faster than the call, and a diagnostic build with plain loads in its place is no faster (#203). How long retired values are kept is left to measurement.A read of a place no write-back can touch is a plain load, read where it is. Only a spawned
mutcall whose subject is reached through an owner writes back, and it writes only its subject’s place. A value holds no owner, so every byte of a value inside an owner is reached from that owner alone, by one path of members, cases and elements: a place is the type of the innermost owner it is reached through and the steps from there, and two places overlap only in owners of one type, when one’s steps begin the other’s. Any two cases count as one step, since their payloads share one room, and so do any two elements. A place a declared subscript names is not known, since the subscript’s body may name any place inside its target, so a read of one, or of anything when one is written back, races a write-back when its type holds the written value’s or that holds its type, member by member, boxed members included. Lowering collects the places from the source of every package in the build, a stamped dependency’s included, since its objects are built from that source (separate-compilation.mdC2), and a spawn anywhere counts, reached or not. A subject whose type, or whose owner’s, names a type parameter is in a generic verb’s body, which is lowered only as its instances, and they are collected too. A program that spawns nothing has no such places, so every read is plain.A library’s object built on its own keeps every snapshot, since a program that spawns may write back any value into it. One built for a single program reads plainly too:
zanec --write-backs FILEwrites the program’s places, and a library’s build given that file with--program-write-backs FILEtakes them as its own, adding its own spawns’ places as any build does. Its packages know the program’s types by name alone, so the file spells each place out: the innermost owner’s type and the steps from it, or-when the place is not known, and the written value’s type with every type it holds, member by member, asTy.to_stringspells each. Two places overlap as above, compared by those spellings; a read whose place or the written one is not known takes a snapshot when what its type holds, itself included, meets what the written value’s holds, which is every case above and more. The lines are sorted with repeats dropped, so programs that write back into the same places write the same bytes, whichzanekeys a dependency compiled from source on: an object built for one program is linked only into a program that writes back into the same places. Remapping moves what an object names after it is built, which those places do not follow, sozanebuilds for any program while it remaps. The plain load is what lets LLVM forward a value just written to the read after it, keep a field in a register across a loop and vectorize the loop, which no atomic load allows. -
An owner lent to a running spawn. The spawning block may not write an owner it lent a spawn that may still be reading it; the checker rejects that write (
concurrency.md§4.3), so the only writes that race a reader are spawned write-backs, which the snapshots above cover. -
Where a spawned call is waited for. Only a spawned call bound by a
letis waited for where its local is read (concurrency.md§3.2). One read where it is written – an operand, an argument, a value assigned to a local that already exists – is waited for at once, so lowering calls it there. Only timing distinguishes the two. -
A spawned call that can abort or exit. Its frame holds the call’s whole outcome (L12), which comes home into a slot laid out for it: the result’s blocks under the done tag, the abort value’s under the aborted one. The call settles once, on the spawning thread, where it is first read or where its block ends (
concurrency.md§3.3). A flag the spawn sets records whether it has. An abort takes the abort value out of the slot and runs the handler written at the spawn, lowered where it settles but in the context of the spawn, so itsabort,returnand exit go where they would from there; itsresolveputs the result in the slot. An exit ends the run of the block the spawn is in. -
An abort value no binder names is held. A handler without a binder, such as
??’s, still holds the abort value in its own scope when it is an owner or owns a block, so the handler’s drain ends it. -
A value parameter is borrowed. A value that owns a block is copied whole where it is stored from a place (
memory.md§2.3), and passed as it is where it is only read: a value parameter is read-only (§2.9), so the caller keeps it, and a fresh one is held in the caller’s scope first. A callee that stores its parameter copies it, except where it copies the parameter once into what it keeps: then a fresh argument is moved in, into the callee or its local copy, and not held (§9). -
A case read of an owner. What a case read gives is its payload, which the variant keeps, or what its handler resolves, which is fresh. So a read whose type is a reference type, or a value that owns a block, gives an address: the payload’s, or that of a slot reserved in the reading scope before the read, which the handler’s
resolvefills. Either way what it gives has one owner, and the drain ends the slot. -
A literal’s bytes stay where the program keeps them. A string’s handle points into the dynamic region (
types.md§2.7). A literal’s points at the bytes the program embeds instead, and the handle also holds the room of the block it owns, which is 0 for a literal’s, so a literal takes no block and returns none, and a copy of one shares the embedded bytes. -
A
thisaddress is taken once per call. The subject is a borrow (memory.md§2.9), so nothing in the call moves what it names: a settled owner never moves, and a roaming one is moved only in the block that declares it, which is not inside the call. -
Moving to a newer LLVM. llvm-project’s bindings track every release, but opam packages them only up to 19. A newer release means building them from that release’s
llvm/bindings/ocaml, or waiting for opam. -
String literals. The lexer keeps a literal’s text as written, and
lib/cst/text_literal.mlreads the escaped quote and backslash and the\%namesites (lexical.md§7.1). Any other backslash sequence stays in the text:@primitives$Stringembeds it as it is, and only@primitives$String.escapedreads\n,\t,\r,\0and\\in the literal’s fragments (Literals.escaped).- An interpolated value is copied where the literal is written. Where
a literal becomes a concept argument (
Lower_calls.captured), each value is evaluated in written order and held in a slot of the caller’s scope, and the literal the parameter is bound to reads those slots, so the body may read it any number of times, or pass it on, and see the same text (types.md§2.8). A spawned call’s function takes a string parameter for each value instead (Lower_spawn.specialized). - The constructor joins.
Lower_data.textjoins the fragments and values in the runtime, each join but the last held in the scope so its drain returns the bytes; a literal of fragments alone is one constant, an optimized build can fold one whose values are all known, within its folding budget.
- An interpolated value is copied where the literal is written. Where
a literal becomes a concept argument (
-
How a value crosses a call. L6 lends a value parameter by the address of the caller’s slot and L7 has a result written into a destination the caller names. Lowering passes and returns value types, a reference-type result and a
^Targument as values in the CGT, and codegen determines per type how each crosses (Emit.passing, besidefn_type), the way clang, rustc and swiftc keep a small aggregate in registers and pass a large one by pointer. Amutsubject, a reference-type subject and a borrowed reference-type argument are addresses already.- The count. A type takes as many registers as the scalar pieces LLVM
splits it into (
Call_pieces.pieces): a struct’s and a sum’s are its members’, a sum’s tag and each word of its payload room one apiece, an array’s its element’s times its length, andUnitnone. It is counted on the CGT type, not LLVM’s, so it cannot depend on the data layout, and worked out once per type.{ I32; I32; I32; I32; }is 16 bytes but 4 registers; a string or list handle is 3. - The limit. An argument of no more pieces than the target’s C
calling convention gives one aggregate passes as itself: 2 on x86-64
Linux and macOS and on AArch64, and 1 on x86-64 Windows
(
Call_pieces.registers). A larger one passes by address. - A result’s limit. A result comes back as itself while it takes no
more pieces than LLVM returns in registers (
Call_pieces.results,Emit.returning): 3 on x86-64 and AArch64, on every system, at most 2 of them floating-point, and the argument limit on any other target. On x86-64 that israx,rdxandrcx, andxmm0andxmm1; a third floating-point piece would come back on the x87 stack, and a fourth piece through memory LLVM allocates itself. So a string or list handle, and a reference-type result that holds one, comes back in registers. A larger result is written through a leadingsretpointer to a fresh slot of the caller’s. The result’s limit is wider than the argument’s because a result throughsretis stored by the callee and loaded again by the caller after every call, which a recursive function that builds list-holding nodes pays at each level (#276). - Fixed while compiling. Both choices are made per signature while compiling, so no call branches on them, and a function value’s call (L14) follows the same rules, since they depend only on the type and the target. Objects for different targets never link, so every object a program links agrees on them.
- The address passed. An argument that is a place, a local or what an
address names, is lent where it is: nothing else in the call writes what
the call borrows (
memory.md§2.9.1), so no copy is needed. A call that returns by address writes straight into a fresh slot, and any other value is stored in a fresh slot of the caller’s frame first. The callee uses the pointer as the parameter’s slot. Lowering never writes a parameter’s own slot, a borrow being read-only and a^Tparameter held in a slot of the body’s own first, and codegen stops with an internal error if one does (Emit.unwritten). - What LLVM is told. A parameter passed by address is
readonly,noaliasanddereferenceablefor its whole size: nothing in the call writes what it borrows, so the optimizer may treat the value behind it as a private copy. It claims no alignment, since what it borrows may be a member at any byte of a packed value (L5). It is notnocapture, since a call the callee spawns can hold the address for as long as it runs. Ansretresult isnoaliasand aligned, its slot being the caller’s own, which nothing else names. Each call site carries the same attributes as the function. - Packing, an option.
--pack-aggregatespasses an aggregate that fits in the registers a call gives it as that many 8-byte words, every word an integer, as clang passes a small C struct: the caller stores the value in a slot of words and passes them, and the callee stores them back into one it uses as the parameter’s slot.{ I8; I8; I8; }then takes one register and{ I32; I32; I32; I32; }two, so on a two-register target both travel in registers, and on x86-64 Windows only the first. It costs a few instructions around each call. The option is off by default. Since it changes how a call crosses, every function but the entry is named with~packedafter its symbol when it is on, so an object built with it links only against objects built with it too, and a mismatch is an undefined symbol rather than a miscall. Packing only calls within one compilation would keep separate objects compatible, but the benchmark buildscoreas an object of its own, and its calls are the ones being measured; a function value, called without knowing its function, would also need one convention for both. It is kept only if the benchmark below is faster with it on some target and slower on none; following the platform’s separate floating-point registers is a further variant, to measure only if packing is kept. - Measuring.
just bench-abi(dev/bin/abi-bench) builds the spec repo’slangbench/programs, the binarytrees reference among them (depth 16, as in../experiments/cross-package-optimization/results/binarytrees-reference.json), anddev/bench/calls, which makes non-inlined calls on a 16-byte two-field struct, a three-byte struct, a string handle and a variant with a 64-byte payload. It builds them with the compiler before this rule (main before audit batch 3), with the rule, and with the rule and packing, optimized and unoptimized, checks every output, and prints the medians. On Windows, from WSL2:ZANE_CC=dev/bin/zig-cc just bench-abi --target x86_64-windows-gnu. The expected result is that the rule is no slower than the compiler before it; a benchmark that is slower is to be understood before the limit changes. The measurement, and with it the decision whether packing is kept, waits on the maintainer’s run of that recipe on each first-tier target, since timings from a container are not comparable; the table goes here when it is made. - The backstop. Before the target machine sees a module,
lib/codegen/big_moves.mlchecks every defined and declared function’s parameter and return types, and reports one still too large to move as one value as an internal error: the rule above makes it unreachable.
- The count. A type takes as many registers as the scalar pieces LLVM
splits it into (
-
Large aggregates as memory. LLVM’s code generator gives one value at most 65,535 parts, and is slow well before that. Before the target machine sees a module,
lib/codegen/big_moves.mlturns each load of an aggregate of 4 KiB or more whose only uses are stores into a copy per store (amemmovewhen it reads the source directly, sincexs[i] = xs[j]may name one element twice). A value of more than 65,535 parts read only for its parts, as a match arm reads one field of a large payload, is read a part at a time from its address, and one built a part at a time and only stored, as a constructor builds its struct, is stored a part at a time; so a struct past 1 MiB can be a variant’s payload. A value still too large to move whole inside a function is reported as a limit rather than handed to LLVM. -
A type’s size is limited. A type takes fewer than
2^48bytes, the addresses the runtime’s chunk map covers (platforms.md, “Address range supported by the runtime”), and no more than the main context’s range of frames,--fixed-region, could hold. A type past either is refused where it is named, as “@primitives$Array<@primitives$I64, 4294967296>takes 32 GiB, which the--fixed-regionof 256 MiB cannot hold”, rather than built into a program that stops when it runs. A size is built up with checked arithmetic (Ty.size_align,Type_layout.stride,Frames.layout): a productstride * nis formed only aftern <= limit / strideholds and a suma + bonly aftera <= limit - b, so no step can overflow on the way to the check, as2^60I64s once wrapped to 0 bytes. LLVM’s array types are made and read throughLLVMArrayType2andLLVMGetArrayLength2(lib/codegen/llvm_stubs.c), since the bindings’ ownarray_typeandarray_lengthtruncate a length to 32 bits. -
An outcome as a sum, for now. L12 returns a tag and has the caller pass slots for the result and the abort value. Until results are written into destinations, a function that can abort or exit returns a sum of three cases instead: done with its result, aborted with its abort value, and exited. A function that can only finish returns its result as before. An exit ends the run of the block the call is written in (
control-flow.md§2.3), so each run of a block argument has a label to leave. -
A 64-bit target. Lowering sizes every type, a sum’s payload room included, for 8-byte pointers,
i64anddoublealigned to 8, and C struct layout, which holds for x86-64 and AArch64, and the runtime’s records have fixed offsets on the same terms. So a target whose data layout differs is refused before anything is emitted for it (platforms.md, “64-bit targets only”). -
Integer division by zero, and a float truncated out of range.
operators.md§2.6 gives a division by zero the result zero, andtypes.md§2.9 saturates atruncatewhose integer part the target cannot hold and gives zero for a NaN. Neither instruction LLVM has behaves that way by itself, sincesdivby zero andfptosiout of range are undefined. So codegen sends a division by 0 or -1 down a branch marked unlikely to the result those divisors take, and thesdivon the other branch sees only divisors it is defined for. The test is a branch, which the processor predicts, so thesdivstarts as soon as its operands are ready. Codegen truncates withllvm.fptosi.sat, whose result is exactly §2.9’s: toward zero, the target’s nearest end past it, and zero for a NaN. The one other quotient an integer cannot hold, the most negative value over-1, wraps, as+and*do. A conversion is one CGT node,Convert, whose instruction the source and target types pick, and the optimizer folds a truncation by the boundsScalar.truncationgives. -
An index out of range. The program stops (
control-flow.md§5.2), with a status other than0(packages.md§6.3): what it wrote so far is kept, the runtime writesindex out of rangeto stderr, and the status is 1, for a list and an array alike, and for a list’sremoveandswapRemove, which find the element they move out throughList_at. The CGT names the element’s address asList_atorArray_at, and codegen checks the index where the address is made: one unsigned compare of the index less one against the count covers both ends, and only an index outside callszane_out_of_range, which is cold and does not return. LLVM so sees the count, the index and the address as ordinary arithmetic, and can hoist or drop a check it proves redundant. -
What LLVM is told. Codegen states what it has established about the code it emits in LLVM’s own terms, so LLVM’s passes can drop the checks and reloads it proves redundant, rather than lowering trying to. Each is a contract, which LLVM may act on however it likes, so each holds for every program:
-
A list’s header is not an element. A handle is never a member or an element of a scalar’s place, so a handle’s fields and a scalar place are disjoint storage, which type-based alias tags (
!tbaa) say: the header fields read and written where an element is located, pushed or removed, and a handle loaded or stored whole, carry one tag, and a scalar loaded or stored through an address, or moved into a fresh place such as the element a push makes room for, carries the other, one for every scalar type. A removal’s move of the elements after it carries the scalar tag too when no handle is among an element’s bytes, since then it writes only scalar places. Every other access, a frame’s, a walk’s, an aggregate’s, a move of elements that hold a handle and the runtime’s, carries none, which LLVM takes as one that may touch both. So a write of an element’s field leaves the count and the block LLVM read before it, and LLVM keeps them, and the check made with them, across the write, pushes of scalars in a row keep the count and room they read, and a loop that removes scalar elements in one of its ways keeps the count through the others. -
A place’s whole slot. A
mutsubject, or an owned value passed by address, is the address of a slot of the caller’s, which outlives the call. An&Tis the address of a settled owner’s slot (memory.md§4.1), which outlives the reference (§4.2): it never moves, and an overwrite writes the slot in place, a boxed member’s block included. Nothing references a list element or a variant payload (§2.1), so no&Tnames storage a push or a removal could move or free. Each such parameter whose slot has a layout lowering has computed, a declared type, a list or a text (the CGTFunc’splaces), isnonnullanddereferenceablefor the slot’s size, with no alignment, since the slot may be a member at any byte of a packed value. It is notreadonlyornoalias, since the function may write the slot and another parameter may name it. -
An open scope’s frame. Once a scope is open, any slot of its frame may be read (above), and the frame is on a 16-byte boundary, since every frame is a multiple of 16 bytes placed where the one before it ended. The open scope states this with
llvm.assumeand itsdereferenceableandalignoperand bundles, which LLVM’s OCaml bindings cannot build (llvm_stubs.c).Both let LLVM read a slot ahead of a branch: a list reached through
thisor kept inmain’s frame has its count and block loaded once before a loop that checks its indices, rather than after each check. -
What some runtime functions do.
zane_out_of_rangeandzane_too_deepstop the program, so they are cold and do not return;zane_text_equalonly reads memory, so what LLVM read before a call to it still holds after. A list’s push, reserve and room look the handle’s address up to find its region but keep no pointer to it (captures(address)), so a handle in a slot of the caller’s own does not escape to them: a fresh list is still empty at its first push after another call, and that push needs no check for room. One table holds them (lib/codegen/runtime_facts.ml), and the C notes the push’s beside it. A body linked in from the runtime’s bitcode replaces its declaration, attributes included, so they are added again once the bodies are linked (Runtime_bodies). Further attributes the C would justify were measured and left out, gaining nothing (experiments/runtime-attrs). -
The list operations small enough to see. A new list is the constant empty handle, and a
removeorswapRemovemoves the elements with LLVM’smemmoveormemcpyof the element’s own stride, and lowers the count, in the function that removes. LLVM then sees exactly which bytes change, as it sees an element’s address and a push’s (runtime/list.ckeeps the runtime’s own forms of all three). A short counted loop that leaves early also carries a mark asking for it to be unrolled twice (llvm.loop.unroll.count2; “The passes” below).
-
-
The passes.
lib/codegen/passes.mlnames what LLVM runs over the module. An unoptimized build and tier 0 runPasses.unoptimized(optimization.mdO12). An optimized build and tier 1 runPasses.optimized, LLVM’sdefault<O2>with two passes added. Each runs jump threading where it still sees eachelifas written:core’selifbranches on~this and condition, wherethisis what theifbefore it branched on, so each way into theelifalready determines that half. Jump threading sends theif‘s body straight past theelifand the other way to the condition alone, which is one chain, as C’selse ifis. Once the CFG simplifier has made a cheapifbody aselect, both bodies’ work runs on every pass round a loop instead. The unoptimized passes therefore merge blocks without making selects before they thread, and the optimized ones thread at the end of-O2’s first function simplification, once each function’s locals are in registers and before the inliner’s passes run. Promoting the locals in a pass of their own ahead of-O2would also do, but leaves-O2a different order of values to allocate registers for, which cost tier 1 oftests/perffloats2.7%.The other added pass is inductive range check elimination (IRCE), run where
-O2has just rotated each loop and hoisted what it reads, ahead of the passes that would turn an index check into an exit test IRCE no longer recognizes.lib/llvm/config/discover.mlwrites the pipeline out at build time from theoptbeside the LLVM the compiler is built against, so it tracks that LLVM’s-O2. IRCE splits a loop whose element reads and writes are checked into the iterations its checks may fail on and a main loop between them that it has proved needs none. The split costs each entry to the loop a few dozen instructions, which a short loop does not earn back, so IRCE splits a loop only when it expects the split to remove enough checks for each entry, which it reckons from the loop’s branch weights, or from LLVM’s guess where there are none (-irce-min-eliminated-checks, left at its default of 10). Codegen marks which loops are long: arepeatthat no other loop holds, that holds no loop, spawn, join,returnor call back into its own function, that nothing leaves early, and that reads or writes an element, is emitted twice (Emit.versioned) in an optimized build and in both tiers of a tiered one, which emit the same module (tiers.mdT3); a plain build, whose passes have no use for the copies, emits it once. A count of at least 16 runs the copy whose branch weights say it goes round 1,000 times; a smaller one runs the copy weighted to go round 4 times, which IRCE leaves alone. A list’s count is loaded with the range[0, 2^63), which IRCE’s bounds need.A
repeatthat can leave before its count runs out, through areturnor a block it is in, that holds no loop, and that reads or writes an element at an index it steps by a constant itself, apart from its counter, is a search (Loop_shapes.searches). Its count is only a ceiling, which is how the spec has a loop whose real stop is a test written (control-flow.md§3.5), and it usually stops long before. Left unweighted, IRCE split a partition scan that stops after one or two elements, and the split’s setup, about 25 instructions on each entry, cost LangArena’s Sort::Quick 27% more instructions. So in an optimized build and both tiers of a tiered one a search’s test is weighted as going round 4 times, as the short copy’s is, and IRCE leaves it whole; unlike the short copy, it still reads ahead. Every other loop keeps its old shape. IRCE splits only at an index that moves by a constant, so weighting another loop only changes how LLVM lays it out: weighted,tests/perffloats’ escape test, arithmetic alone, took 10.5% more estimated cycles andpaths6.3%, and LangArena’s Graph::AStar, whose heap sift halves its index, 4.3% more instructions. A loop indexed by its counter walks the range its count names, and IRCE’s split pays when it runs long: weighted, Distance::Jaro’s match-window search ran 2% more instructions. A loop around another may go round many times between the inner loop’s exits. A loop whose counter is read nowhere but by its own step, and that reads or writes an element, is a search too (Loop_shapes.idle_counter): atoover a ceiling that stands for awhile, whose index a method it calls moves, as a tokenizer’s whitespace skip does (tests/perfcursor, 10% more estimated cycles unweighted). A loop that reads no element has no check for IRCE to split, sofloats’ escape test stays unweighted.Any other loop codegen emits once has no branch weights, and LLVM guesses it goes round often enough for IRCE to split it. In
tests/perfflips,advancemoves its first few elements down one place in such a loop, nested in another, usually over one or two elements; IRCE splits it, and loop idiom recognition would make the split’s main loop amemmovecall of about 12 bytes. An optimized build turnsmemmoveandmemcpyformation off (-disable-loop-idiom-memcpy,Passes.llvm_options): the copies such loops make are mostly a few elements of a length known only at run time, which a call into the C library costs more than it saves (tests/perfrotations, 11% more estimated cycles in a tiered build with the call).A
repeatthat can leave before its count runs out, through areturnor a block it is in, and that holds no loop, spawn, join or call, has its back edge marked to be unrolled twice (llvm.loop.unroll.count2,Emit.unrolled), in every build; only the optimized passes and tier 1’s act on it. On x86-64-O2‘s cost model allows partial unrolling only of loops smaller than a dozen arithmetic operations, and in a copy whose exits lead to the same place it merges the two exit tests, after which the trips cannot be counted at all, so such a loop otherwise stays rolled: unrolled,tests/perffloats’ escape test takes 16.5% fewer estimated cycles. A loop that calls is left as it is, since its call costs far more than the test unrolling saves: unrolled, the loop the memo pass checks madedepths2.5% slower.A
repeatwhose body branches, switches or loops, and that reads a list at an index worked out from another list’s element, prefetches at the top of each time round the element it will readLookahead.distance(32) iterations on, in an optimized build (lib/codegen/lookahead.ml,Emit.prefetch;ZANE_PREFETCH=0turns it off), as in both tiers of a tiered build, which emit one module. The processor cannot see such a read coming, and the loop’s own work keeps it from having many of them in flight, so each time round waits on a miss (prefetch-reorder). The address qualifies when it is made, through the body’slets and expression verbs’ results, of constants, the loop’s counter (moved on by its step times the distance), locals the body neither binds nor assigns, integer arithmetic and bit operations that cannot fail, and element reads of lists whose handle is reached from slots bound before the loop through struct members only: a list in a variant’s payload is there only while that case is held, which the top of the loop does not know, and another case’s bytes may read as a list with a count and no block. A local the bodylets and assigns again only in a loop inside it, and not from an element, is taken at itslet’s value, which is the address a probe’s first time round reads; one assigned from an element walks a chain, whose head is the least of its misses, and is left alone. The element reads in the address run early, each only when its index is inside its list as the list’s handle stands at the top of the loop, read then afresh unless the loop hoisted it; any that is not skips the prefetch, so no early read reaches past an end or stops the program. Nothing in the body is moved: its reads, their checks and where a check stops the program stay as they were. A prefetch never faults, so its own address needs no check, though one outside its list is skipped too. The top of the loop reads a list’s handle the body itself does not hand it, so a body that spawns, joins or moves a value out of a place, which could leave a slot holding a handle whose block is gone, fetches nothing. A body that does not branch, such ass += a[idx[i]], gains nothing with 4 KiB pages in the model and is left alone too, as is the short copy of a versioned loop.The same options start every function on a 64-byte line (
-align-all-functions=6), where LLVM would start it on 16 bytes. Which branches share a predictor’s entries depends on where they lie, so with 16-byte starts a change that only resized code before a function moved its branches into new conflicts: an unchanged loop oftests/perfcopiesmispredicted 40% more after an edit to other functions, and 64-byte starts made both layouts mispredict as the first had.The same options keep instruction combining from sinking an instruction into the one block that uses it (
-instcombine-code-sinking=false). It splits the load of a struct with no padding into a load of each member, so a packed sum (L5) read whole is read as its tag and its room. With sinking on, it then moves the room’s load into the case that reads the payload, and the CFG simplifier, which cannot tell that the load is safe to make before the tag is tested, leaves the case a branch on the tag. When which case a value holds follows no pattern, the predictor misses that branch about half the time. With the room’s load left beside the tag’s, the case’s work becomes aselect. A sum whose tag is a 4-byte word has padding after it, so its load was never split; the packed layout is what exposed the sinking. An optimized build often recovered theselectanyway, wheremaininlined into the start function (L16) runs the function passes over its loops a second time, but tier 1 runs them once, andtests/perfentitiesmispredicted 2.4 times as often there. With sinking off it runs 24% fewer estimated cycles tiered and 7.5% fewer optimized,floats4.7% fewer, andgrowth, whose push loop gains an instruction from a different exit test, 2.5% more (experiments/tight-layoutsstep 14).The same options raise LLVM’s unrolling budgets above
-O2‘s. Full unrolling (-unroll-threshold) is 250, where-O2allows 150. A loop of a few constant iterations whose body tests the counter, asd!to(Int(4))does over fourbranch(d == k, ...), stays a loop at 150, with the four tests oneswitchon the counter that a predictor without history cannot learn; unrolled, the counter is a constant in each copy and the tests are gone. The budget is a step: 150 and below leavetests/perfpathsandreturnsas they were, 200 takesreturns, 250 takespaths, and nothing up to 1000 moves another program. Partial unrolling (-unroll-partial-threshold) is 100: a counted loop over a list unrolls, sharing its copies’ index arithmetic and bounds checks, andborrowsruns 28% fewer instructions. A budget of 150 also takesdivision(3.8%) and a point more onentitiesandrepeats, at 6% to 56% more.textand a worse result on langbenchnbody, whose pair loopj!to(n)runs one to four times: the runtime unroller makes it a four-wide copy behind a prologue and guards that the loop’s few iterations never reach, so each step runs 63 more instructions (+10.4%, +6.8% cycles; at 100, +6.4% and +3.0%). Turning runtime unrolling off, or capping the factor at 2, losesborrows’ gain (experiments/unroll-thresholds/nbody.md). In estimated cycles against the options without them:paths42.8% fewer,borrows11.9%,returns10.0%,kepttiered 5.2%,particles3.6%,entities3.4%,repeats3.1%;removal1.1% more, where the cold loop’s copies leave the hot loop beside it a register and it reloads its items pointer on every iteration, andnbody3.0% more. The price is code: atests/perfexecutable’s.textgrows up to 10.5%, tier 1 ofkept8% and ofrepeats25%. The langbench programs that build (experiments/unroll-thresholds) grow 2% to 14% (11% for the two tiny ones) and run within 0.2% of before exceptentities(8.6% fewer) andnbody, and compiling takes as long (experiments/unroll-thresholds).For an AArch64 target,
-O2’s loop unswitching also copies a loop whose body branches on a condition the loop does not change, once for each way it goes (Passes.unswitch);-O2itself copies only a loop the branch leaves. A packed element’s stride is rarely a power of two (L5), and a loop whose element a loop-invariant condition picks otherwise multiplies the index by the stride on every pass rather than stepping a pointer: on Apple silicon that made such a loop up to 28% slower than over the unpacked layout, and the copies make it 32% faster. On x86-64 the copies cost more than they save (tests/perfentities7–11% slower), so they are left out there (experiments/tight-layouts, steps 6, 11 and 12). -
After the passes. An optimized build and tier 1 rewrite the module once LLVM’s passes are done, where inlining has put each loop’s calls and checks in front of them, and run a short cleanup after each rewrite (
Build.rewrite):- a local array of constants, written only with the same constants each
time, becomes a constant global (
Const_arrays); - a recursion’s base case is made at each of its self-calls, so a leaf
takes no call, in a function no memo table answers and whose frame
allocates at most 4 KiB of machine stack (
Leaf_peel); - a call in a loop to a leaf of the program of at most 128 instructions
is inlined there (
Loop_inline); - a frame’s slots that are loaded and stored whole get a copy in a
register, written back to the slot before a call that may read it
when stored since the two last agreed, filled again after a call only
where the call may write it (at the next load, where every path finds
it stale there), and after a wider store that holds the slot; a frame
whose address a spawned call or a stored pointer may reach is written
through on every store instead, a frame placed on two paths is the
phi that joins them, and a frame its scope enters inline, whose
record’s skip and room check name its address, is a frame too
(
Frame_shadow); - a check of an index that a later check of the same index follows on a
straight path takes the smaller bound, and the later one always passes
(
Merged_checks); a check both arms of a branch begin with is made once above the branch, and the arms’ stores of one address are sunk into one store (Arm_checks); the checks ofx,x + 1, …x + kon one path become one check of the lowest and one of the highest, and in a strip-mined loop an exit test the loop’s own counter can never meet, below the lowest end the loop’s stop tests give it, is removed (Range_checks).
A failed check stops the program after flushing its output, so moving a stop earlier on a path where no call writes output in between is not seen outside the program (
control-flow.md§5.2 says what a stop is). The emitted code itself also takes, in every build: a scalar’s alias tag by its size, so a one-byte store leaves the wider scalars read around it in registers; and a constant count from 2 to 8 marked to be unrolled whole. An optimized build’s loop whose pushes were given their room before it reads the list’s block once and keeps its count in a local the pushes step (Emit,Stat.Repeat). A loop that calls a function with one call site is emitted once rather than versioned, so the callee keeps LLVM’s single-caller inline bonus, except in a tiered build, whose tier 1 names every function in its table (Loop_copies.single_caller). - a local array of constants, written only with the same constants each
time, becomes a constant global (
-
A type argument passes nothing. A generic verb is lowered once per instance the TST checked (
semantics.mdD12), with a symbol of its own, and a type written where a value goes, or a number given to an explicit number parameter, has already picked the instance, so the call passes nothing for it. The body reads a number parameter as the number its instance was given. -
A spawned call with no function of its own. A call is spawned by running a function on another thread (step 8), and two kinds of call have none. A verb expanded where it is called because it takes literals (L11) gets one per set of literals it is spawned with,
zane.expanded.N, with each literal bound where the body reads it. An array literal’s elements are values, made where the spawn is written, so that parameter becomes one the function takes, of the array’s type. A block argument would capture the spawning frame, and the spec forbids spawning one (concurrency.md§3.1). A call to an intrinsic runs throughzane.intrinsic.N, which takes its arguments and makes the call. For amutmethod, such as a value list’spush, that function is amutmethod too, so its subject is lent by its address and goes into a subject region (§9) as a declared method’s does. -
A field constructor is a call. A field-constructor call calls the constructor, whose body runs as any verb’s does, with each entry’s value in its slot. An entry the call leaves out passes the default the TST typed for it (
semantics.md§6). The entries run in the order they are written, then the defaults in declaration order (types.md§3.3).