Kiln Generators
Kiln is Wado's compiler plugin mechanism for generated imports. A generator is a
Wado program compiled to a component. The compiler runs it at build time to turn
an input file into Wado source, and then compiles that source like any
hand-written module. A use names the input file, and its with clause names
the generator (Import Attributes).
Which use goes through Kiln is stated in
How an Import Is Read.
use { model } from "./model.spec"
with {
generator: { module: "./outer.wado" },
};
Rationale: WEP: Kiln.
Importing a Generated Module
The generator Fields
The literal after from is the primary input. It is a ./ or ../ path
resolved against the declaring file, like a local module import, or the URL of
a remote file. The generator object holds the rest of the
invocation.
| Field | Required | Meaning |
|---|---|---|
module |
yes | The generator: a ./ / ../ path to its source, or a <namespace>:<name>[@<version>] coordinate or lib:<nick> alias resolved against [build-dependencies]. A bare name is an error. |
version |
no | Exact version of a coordinate module, for a file with no wado.toml. An error beside a path module, or when the manifest declares the generator. |
registry |
no | Registry of a coordinate module (oci://<host>[/<prefix>]), under the same conditions as version. |
options |
no | Record literal whose shape matches the generator's export struct Options. See Options. |
inputs |
no | Supplementary inputs the generator cannot discover from the primary alone, such as a sibling lexer grammar: each a ./ / ../ path or a URL, or { path, type, integrity }. A schema that refers to other files lists every one of them here. |
output_dir |
no | A ./ / ../ directory, resolved against the declaring file, that receives the generated files. Default build/kiln/<synthesized-id>/ under the package root. |
Each field has one type, and any other key or type is an error at the use site.
The package root is the directory of the wado.toml that governs the
declaring file. A file outside any package has none, and the directory of the
file being compiled stands in for it. Every path a generator receives is
relative to the package root, and so is the default output directory.
A file's type says how the generator reads it, since no generator looks at
an extension. The primary input's is the type beside generator; a
supplementary input's is the type in its { path, type }. Each is a string
the generator receives as the file's InputFile.type, and decides the meaning
of. A type makes the invocation a different one, so the same file under two
types is generated twice.
Beside generator, the with clause holds only type and integrity. The
integrity pins a remote primary input (Remote Inputs).
A [build-dependencies] coordinate names a published generator. Here Gale, a
generator that builds a parser from an ANTLR4 grammar
(WEP: Gale), comes from a registry. The use site
supplies every option, for the reason Options gives:
use calc from "./Calc.g4"
with {
generator: {
module: "wado-lang:gale",
options: { highlight: false, trace: false },
},
};
A lib: alias names a generator the package lists under a nickname. This clause
also hands the generator a supplementary input, typed as a highlight query:
use { highlight as highlight_impl } from "../grammar/Wado.g4"
with {
generator: {
module: "lib:gale",
inputs: [
{ path: "../grammar/Wado.highlights.scm", type: "tree-sitter-highlights" },
],
options: {
fragment_entries: ["statement"],
},
},
};
Every file a generator sees is named literally at the use site. There is no glob and no directory listing, so the whole input set is known before any generator runs.
Remote Inputs
An input may be a URL, under the rules of Remote Modules. Its pin covers what the generator reads: the bytes up to the input's extent, or the whole file when the generator exports no probe. The host fetches no byte past the extent, so a model's header crosses the network and its weights do not. A probe run again, as a changed generator needs, reads what a pin covers from the bytes an earlier build fetched, so it reaches the server only past them.
use { hello } from "https://example.com/data/header_and_payload.schema"
with {
generator: { module: "./sub/kiln/header_probe_generator.wado" },
integrity: "sha256:10159baf262b43a92d95db59dae1f72c645127301661e0a3ce4e38b295a97c58",
};
test {
assert hello() == 7;
}
The pin of the primary input is the integrity beside generator. A
supplementary input writes its own as { path, integrity }. A pin, like a
type, makes the invocation a different one.
A generated import inside a remote module works as one in a local module, with
two differences. Its generator is named by a [build-dependencies] specifier,
never a path. It writes into the build's default output directory, and an
output_dir is an error.
Binding the Import
A generator emits exactly one entry module and zero or more supplementary
modules. The use binds against the entry module, under the ordinary
visibility rules. Supplementary modules are ordinary Wado files that the entry
reaches with ordinary use statements. The entry module does not need to exist
before the first compile, because the generator runs before the import is
resolved.
Clauses are collected from every module the program reaches, so a module deep in the graph can import a generated module of its own.
Generated files are compiled exactly like hand-written source, so a generator that emits invalid Wado fails with an ordinary error against the generated file on disk. No diagnostic points into a generator's input files: a generator names the file in its message.
Import Errors
A use whose generator produced no module for that schema is
KILN_NO_GENERATED_MODULE. The compiler never falls back to parsing the schema
as Wado.
Two clauses that agree on module, version, registry, the primary input,
inputs, options, and output_dir are one invocation, wherever in the
program they appear. The generator runs once for them. Two clauses that share a
primary input but disagree on any of those are an error naming both.
Options
A generator's options are the Options struct its entry module names. The
entry module declares it or re-exports it with pub use, so another entry
point of the same package, such as a command line, can read the same struct.
generate is an export fn, so Options crosses the component boundary and
is published like any type an export reaches
(Published Types): it is declared
export, and so is every type its fields name. Every field is pub, since a
WIT record shows them all. Gale declares three, one of them an enum it
re-exports with export use:
export struct Options {
/// Emit trace logging to stderr in the generated parser: rule
/// enter/ok/fail frames plus multi-alt scan/pick/try decisions. Off by
/// default; set `trace: true` to debug why a grammar rejects an input.
pub trace: bool = false,
/// Rule names a fragment is a sequence of, for tooling (highlight / LSP);
/// e.g. `["statement"]`. Empty is byte-identical. See "Parsing a fragment"
/// in the README.
pub fragment_entries: List<String>,
/// Which decisions the parser asks the runtime ATN simulator. "Choosing the
/// simulator" in the README lists the modes.
pub atn: AtnMode = AtnMode::Auto,
}
Every use site's options is checked against the struct before the generator
runs, so a typo or a type mismatch is reported on the offending key.
- A field the use site leaves out takes the value the table below gives it.
Omitting
optionsleaves out every field. - A field the table gives no value must be supplied. An unknown key is an error.
- A default is a literal, and a list or map field takes a
[…]or{ k: v, … }one. - An
enumoption is written as its case's Component Model name in a string, soAtnMode::Neveris"never"and a caseHighContrastis"high-contrast". That is the name the generator's WIT gives the case, so a generator takes the same values whether it is named by its source or by its component. - A
TreeMap<String, V>option is written as an object whose keys are the author's own. The generator receives it sorted by key.
What an omitted field takes depends on how the generator is resolved. A
generator named by a relative path, or by a [build-dependencies] entry with a
path, is built from source and keeps its field defaults. Any other coordinate
resolves to a prebuilt component, which carries no defaults, since a WIT record
has none:
| Omitted field, without a default | Built from source | Prebuilt component |
|---|---|---|
Option<T> |
None |
None |
List<T>, TreeMap<String, V> |
the empty list or map | the empty list or map |
| Any other type | required | required |
| Omitted field, with a default | Built from source | Prebuilt component |
|---|---|---|
Option<T> |
the default | None |
List<T>, TreeMap<String, V> |
the default | the empty list or map |
| Any other type | the default | required |
So a use site that must work with both writes every field that is not an
Option, a List, or a TreeMap.
Loam's options hold three maps, a list of strings, an optional struct, and a
flag. They are declared in compile.wado, which the loam command reads too,
and the entry module re-exports them:
export struct Options {
/// Static extents for the graph's symbolic dimensions, keyed by `dim_param`.
pub dims: TreeMap<String, i32> = {},
/// The graph outputs `compute` returns. Empty keeps every output.
pub outputs: List<String> = [],
/// Outputs `compute` returns only the last position of, each by the axis
/// it is cut along. What computes each position from the same position of
/// its operands computes the last alone; what reads across them, as
/// attention does, still computes them all.
pub last: TreeMap<String, i32> = {},
/// Outputs `compute` returns the index of the greatest element of, each by
/// the axis it ranks along, kept at extent 1: a greedy pick reads one
/// index rather than a vocabulary's scores. Applied after `last`.
pub arg_max: TreeMap<String, i32> = {},
/// The tensors a `decode` entry loops over, beside `compute`, which then
/// takes how many positions its cache keeps room for.
pub decode: Option<Decode> = null,
/// Whether the weights a `type: "onnx"` file carries are compiled into the
/// module. Off, the build reads past them, and `Params::load` reads them
/// out of that file where the build found them, so a model too large to
/// compile in still builds.
pub embed: bool = true,
}
Only the flag's default changes what a use site of the source generator may
leave out; the other fields may be left out without one. The rest are
for the loam command, which reads the struct from JSON through core:serde,
where a field without a default is required. From a prebuilt Loam, embed is
required.
The dims map is written as an object:
use { Model, N, Params, X1, X2, compute } from "./specialize.onnxtext"
with {
type: "onnxtext",
generator: {
module: "lib:loam",
options: {
dims: { N: 2 },
outputs: ["Y"],
},
},
};
A map reaches the generator in key order. Given
options: { sizes: { small: 1, large: 3 } }, this generator spells out
large=3,small=1:
export struct Options {
pub sizes: TreeMap<String, i32>,
}
export fn generate(req: Request<Options>) -> Result<Response, Error> {
let mut spelled: List<String> = [];
for let [k, v] of req.options.sizes.entries() {
spelled.push(`${k}=${v}`);
}
return Result::Ok(Response {
files: [OutputFile {
path: "greeting.wado",
content: `pub fn greeting() -> String { return "${spelled.join(",")}"; }`,
is_entry: true,
}],
});
}
An Options field is one of bool, a fixed-width integer, f32, f64,
String, a payload-less enum, a non-recursive struct of such fields,
Option<T>, List<T>, or TreeMap<String, V>. A field default is a literal.
Any other field fails the generator's own compile, since no use site could
supply it.
Manifest
A generator named by a coordinate or a lib: alias is declared in
[build-dependencies] of wado.toml. Those entries form a graph of their own,
used only at build time and apart from the program's [dependencies]:
[build-dependencies]
"wado-lang:gale" = { version = "^0.0.9" }
A generator named by a relative path needs no entry. The path names its entry module directly.
A consumer whose generated code calls the generator's runtime library also lists
the package under [dependencies] (Runtime Libraries).
A Dependency's Generators
A build runs the clauses of every source dependency it compiles, at any depth.
Such a clause names its generator through the dependency's own
[build-dependencies], so the consumer declares none of them. Its output is
written into the consumer's build/kiln, whatever output_dir the clause
gives, and the dependency's tree is never written to. A clause in another
package's file reached by a path is that package's, declared as a dependency or
not.
A dependency that commits its output ships it with the <primary>.kiln.json
beside it, and a consuming build uses that output without running the generator
(WEP: Package File Exports).
Not yet implemented: a consuming build runs the generator even where the dependency commits its output.
Authoring a Generator
The Generator World
A generator targets the core:kiln/generator world. It imports two of the
wasi:random interfaces, Insecure and InsecureSeed, and wasi:cli's
Stdout and Stderr, and nothing else (The Sandbox):
#[cm("core:kiln/generator@0.1.0")]
pub world Generator {
import Insecure;
import InsecureSeed;
import Stdout;
import Stderr;
}
A generator package maps the world to its entry module in its [world] table
(Selecting a World). Grog, a Protocol
Buffers compiler, declares only this world:
[world]
"core:kiln/generator" = "src/generator.wado"
The entry module exports generate. It takes one Request<Options> and returns
a Response or an Error:
use { Request, Response, OutputFile, Error, read_text } from "core:kiln";
export struct Options {
pub namespace: String,
pub emit_comments: bool = true,
}
export fn generate(req: Request<Options>) -> Result<Response, Error> {
let Ok(schema) = read_text(req.primary.content) else {
return Result::Err(Error::InvalidSchema("not UTF-8"));
};
let source = emit(&schema, &req.options); // the generator's own work
return Result::Ok(Response {
files: [OutputFile { path: `${req.options.namespace}.wado`, content: source, is_entry: true }],
});
}
test {
let options = Options { namespace: "calc" };
assert options.emit_comments && emit(&"x", &options) == "// calc\nx";
}
A generator with no configuration declares no Options and writes
fn generate(req: Request), as Grog's does:
export fn generate(req: Request) -> Result<Response, Error> {
let path = req.primary.path;
let source = match read_text(req.primary.content) {
Ok(text) => text,
Err(e) => {
return Result::Err(Error::InvalidSchema(`grog: '${path}' is not UTF-8: ${e}`));
},
};
let runtime = runtime_of(&req.module)?;
let file = read(&source).map_err(|e| at(&path, EmitError::Invalid(e)))?;
let generated = emit(&file, runtime).map_err(|e| at(&path, e))?;
let files: List<OutputFile> = [
OutputFile {
path: `${stem(&path)}.wado`,
content: generated,
is_entry: true,
},
];
return Result::Ok(Response { files });
}
Request exists only on the Wado side. At the component boundary generate
takes the four fields as four parameters, and a generator with no Options
takes no options parameter. Each generator's world is therefore its own:
export generate: func(
primary: input-file,
inputs: list<input-file>,
module: string,
options: <the generator's own options record>,
) -> result<response, error>;
The Request
pub affine struct Request<T = NoOptions> {
pub primary: InputFile,
pub inputs: List<InputFile>,
/// How the use site named this generator: its `module:` specifier as
/// written, or for a path, that path from the package root.
pub module: String,
pub options: T,
}
primaryis the input afterfrom.inputsare theinputsof the clause, in the order written.moduleis how the use site named the generator: themodule:specifier as written, or for a relative path, that path from the package root.optionsis the use site's options, with defaults filled in.
An input file carries its path from the package root, normalized, the content
as a byte stream, and the type the use site gave it. So ./sub/../sub/v.txt
written in src/main.wado arrives as src/sub/v.txt:
record input-file {
path: string,
content: stream<u8>,
%type: option<string>,
}
read_all and read_text read a whole file
(core:kiln). Gale reads every input as text, and
reports one that is not UTF-8 as an invalid schema. Each file moves out of the
list as it is read, so the return drops the streams of the files left unread:
while let Some(file) = pending.pop() {
let text = match read_text(file.content) {
Ok(text) => text,
Err(e) => {
return Result::Err(Error::InvalidSchema(`gale: '${file.path}' is not UTF-8: ${e}`));
},
};
A generator that needs only a prefix reads the stream itself and drops it early. Loam reads a checkpoint only as far as its header ends:
pub fn read_header(mut content: Stream<u8>) -> ByteList {
let mut prefix = ByteList::with_capacity(HEADER_CHUNK);
loop {
let [end, chunk] = content.start_read(HEADER_CHUNK).finish();
content = end;
prefix.extend(&(chunk.items as ByteList));
if chunk.result != CopyResult::Completed || holds_header(&prefix) {
break;
}
}
content.drop();
return prefix;
}
The Response
A successful run returns the generated files:
record output-file {
path: string,
content: string,
is-entry: bool,
}
record response {
files: list<output-file>,
}
pathis relative to the invocation's output directory.contentis the Wado source.- Exactly one file has
is-entryset. It is the entry module theusebinds against.
The generator chooses the file names. Gale names its entry after the grammar:
let entry_name = to_snake_case(&grammar.name);
let files: List<OutputFile> = [
OutputFile {
path: `${entry_name}.wado`,
content: wado_source,
is_entry: true,
},
];
return Result::Ok(Response { files });
Output Directory
Outputs are written to the invocation's output directory, each stamped with a
#![generated(by = "...", sources = [...])] header
(#![generated]). The header names the
generator and its inputs from the package root:
#![generated(by = "gale", sources = ["tests/grammars/calc_ll.g4"])]
That output was written to a directory the use site chose:
use calc from "./grammars/calc_ll.g4"
with {
generator: { module: "../src/generator.wado", output_dir: "./generated/cst_calc" },
};
A file in the output directory that carries the header belongs to the invocation, and a later run may overwrite or remove it. A file without it is left alone.
Generator Errors
A generator that cannot produce output returns an Error:
variant error {
invalid-schema(string),
unsupported(string),
other(string),
}
invalid-schema says an input is wrong. unsupported says the generator does
not handle what it was asked to do. other covers the rest. Each carries a
message, and the build fails reporting it.
A generator that traps fails the build too.
An error carries no span. Grog names the file in every message instead:
fn at(path: &String, e: EmitError) -> Error {
return match e {
Invalid(m) => Error::InvalidSchema(`grog: '${path}':${m}`),
Unsupported(m) => Error::Unsupported(`grog: '${path}':${m}`),
};
}
Diagnostics
A generator reports to the build by writing to stdout and stderr:
- Each non-empty line written to stdout becomes an info diagnostic, and each one written to stderr, a panic's message included, a warning.
- A line is reported the moment it ends, so the lines of both streams come in the order they ended. A line left unended is reported when the call returns, and of two, the one begun first is reported first.
- Every line is reported, whether the run then succeeds or fails.
- A diagnostic carries no span.
A generator logs through core:log rather than writing a stream itself. The
compiler runs generate's body under KilnLogSink, which writes each event to
a stream by its level:
| Event level | Stream | Diagnostic level |
|---|---|---|
Error, Warn |
stderr | warning |
Info |
stdout | info |
Debug, Trace |
none: dropped | none |
The line is the event's target and message, then its fields as key=value.
An Error event does not fail the build. A generator fails only by returning an
Error.
Gale logs the warnings of its own analysis, and collects its errors to fail the run with:
for let d of &generated.diagnostics {
if let Some(level) = diagnostic_level(&d.kind) {
let message = `${d.owner.label()}: ${d.message}`;
if level matches { Error } {
refusals.push(`gale: ${message}`);
} else {
event(level, message, NoFields {}, "gale");
}
}
}
Loam reports a successful check as an info:
info(`${model.graph.name}: ${model.graph.nodes.len()} operators checked`, NoFields {}, "loam");
The Probe
A generator may export probe beside generate. It reports how many leading
bytes of an input determine the output. The host calls it once per input file.
The parameters are matched by name, so a probe may leave out any it does not
read. The host supplies four: path and content, type (the file's type,
as input-file carries it), and options when the generator declares an
Options struct. Each has the type generate is handed it as, and the probe
returns Result<u64, E> with generate's E. A probe taking any other
parameter or type does not compile.
This probe says the first line decides everything:
export fn probe(path: String, mut content: Stream<u8>) -> Result<u64, Error> {
let mut n: u64 = 0;
loop {
let [end, chunk] = content.start_read(1).finish();
content = end;
if chunk.items.len() > 0 {
n += 1;
if chunk.items[0] == b'\n' {
break;
}
}
if chunk.result != CopyResult::Completed {
break;
}
}
content.drop();
return Result::Ok(n);
}
- The host reads a file only as far as the probe pulls its stream, and then reads it to its extent and no further. A probe of a checkpoint reads the header, never the payloads behind it.
generatesees each input cut to its extent. Reading further gets the end of the stream.- The extent must be decided by the bytes inside it.
- An extent that reaches the end of the file counts as the whole file.
- A generator that exports no probe depends on every byte of every input.
The Sandbox
A generator runs in a deterministic sandbox. It has no clocks, network, environment, filesystem, or secure randomness. It is a function of what the invocation hands it: the input files, the name it was invoked under, and the options.
- The host provides
wasi:random/insecure,wasi:random/insecure-seed,wasi:cli/stdoutandwasi:cli/stderr, and nothing else. Stdout and stderr reach the build as diagnostics. - A generator that logs through
core:log'sTextSink<WallClock>imports a clock, so it is refused as below. - Both random interfaces behave as
wasi:random/insecure-seedspecifies: what they return may be fixed. wasi:random/randomis not provided, because WASI requires a deterministic environment to omit it.- A map under the default seed works as in any other program:
HashMap::new(), a map literal,Default, andDeserialize. - A generator opens no file. Each input arrives as a stream, and the host decides what is on it.
- A generator writes no file. Every output is returned in the response.
- A generator that imports a
wasi:*interface its world does not import, directly or through acore:*module, is a compile error (KILN_GENERATOR_FORBIDDEN_IMPORT). - A generator's source may import a remote module. Its pin fixes what the build compiles in, and the generator itself still reaches no network.
Running Again
A generator runs again only when something it depends on changes:
- the generator's identity and source, including the generated modules its own
source imports, and the
wado.lockentry of each remote module it reads that nousepins; - the content of each input, up to its extent;
- the options;
- the name the use site invoked it under;
- the version of the
core:kiln/generatorworld.
The compiler version is not among them. An invocation whose dependencies are unchanged reuses its recorded outputs without running the generator.
| Situation | Behaviour |
|---|---|
| Nothing recorded | Run the generator. Write its outputs and the record. |
| Dependencies unchanged, outputs as recorded | Skip the generator. |
| Dependencies unchanged, an output was edited | Skip the generator, warn (KILN_GENERATED_MODIFIED), and compile the edited file. |
| Dependencies changed, an existing output now differs | Run the generator, warn (KILN_GENERATED_REGENERATED), and overwrite the file. |
The record is a file beside the outputs. Losing it is never an error: the next compile runs the generator again.
The default output directory is under build/, so a fresh checkout runs the
generator again. A tracked output_dir commits the outputs and their record
together, so a fresh checkout reuses them.
wado check runs generators exactly as a build does. It also treats every Kiln
warning as an error, so a hand-edited output fails the check.
Generators That Use Generators
A generator is an ordinary Wado package, so its own source may import a
generated module. Here the outer generator's source imports what the inner one
produces from value.txt:
use { answer } from "./value.txt"
with {
generator: { module: "./inner.wado" },
};
- The inner invocation runs first, since the outer generator is built from its output.
- An inner clause names a generator the way an outer one does. A
lib:name is resolved against the[build-dependencies]of the generator's own package. - An invocation is placed relative to its package root, however it is reached. One schema has one output directory, whether a program or a generator imports it.
Loam is built this way. It reads a binary ONNX model through the types Grog
generates from onnx.proto:
use onnx from "./onnx.proto"
with {
generator: { module: "lib:grog" },
};
and ONNX's text format through the parser Gale builds from a grammar:
use onnx from "../grammar/Onnx.g4"
with {
generator: { module: "lib:gale" },
};
Its own manifest lists both:
[build-dependencies]
"lib:gale" = { path = "../package-gale", package = "wado-lang:gale", version = "^0.0.28" }
"lib:grog" = { path = "../package-grog", package = "wado-lang:grog", version = "^0.0.28" }
An invocation whose generator is built from its own output, directly or through other invocations, is a cycle. The cycle is an error naming the invocations in it. This generator imports a module it would itself produce:
use { answer } from "./loop.spec"
with {
generator: { module: "./cyclic.wado" },
};
Runtime Libraries
Generated code may call a library that the generator's package also ships. The
generated module imports it like any other dependency, so the consumer lists
the package under [dependencies] as well as [build-dependencies]. Grog's
generated types are encoded by Grog's runtime:
[build-dependencies]
"lib:grog" = { path = "../..", package = "wado-lang:grog", version = "^0.0.28" }
[dependencies]
"lib:grog" = { path = "../..", package = "wado-lang:grog", version = "^0.0.28" }
The consumer imports the runtime and the generated types side by side:
use grog from "lib:grog";
use { Account, AccountAddress, AccountContact, Spread, SpreadPick, Status } from "./demo.proto"
with {
generator: { module: "lib:grog" },
};
Only the use site knows what the consumer calls the package, so the generated
code imports it through req.module. core:kiln's package_of takes the
package part of that name, and is None for a path, which names no package.
Grog refuses a path:
fn runtime_of(module: &String) -> Result<String, Error> {
return package_of(module).ok_or(Error::Unsupported(
`grog: invoked by the path ${module}; name it by the package spec its runtime is a dependency under`,
));
}
The name is among what a generator depends on, so renaming the dependency runs the generator again.
Dialects
A dialect is a superset of Wado, or another surface for it, implemented as a
generator. A dialect file is reached only through a use naming its generator,
since without one any file is read as Wado source. Its files take their own
extension anyway, so wado test and wado format, which walk a directory for
.wado files, pass them by. The generator's output is ordinary Wado on disk,
and every tool that reads Wado reads it unchanged.
Rationale: WEP: Kiln, Dialects.
