Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Adding tools and flows

A tool is a plugin: a WebAssembly component built from wit/plugin.wit, which doris embeds or loads from a plugin directory. A flow is compiled into doris. Read Flows and tools, Tool plugins and Code layout first.

Adding a tool

A tool can be added to doris, or kept outside it: the plugin is the same either way. A linter is the simplest case. To add one:

  1. Make the crate, plugins/<name>/, a library that depends on doris-plugin (the SDK, in sdk/), serde and wit-bindgen. Use plugins/slang as the pattern. Bind the world of the capability, mapping the shared interfaces to the SDK’s, so its helpers work on your types:

    #![allow(unused)]
    fn main() {
    wit_bindgen::generate!({
        world: "doris:plugin/linter-plugin",
        path: "../../wit",
        with: {
            "doris:plugin/types@0.1.0": doris_plugin::types,
            "doris:plugin/host@0.1.0": doris_plugin::host,
        },
    });
    }
  2. Implement tool::Guest, which every plugin exports, and export! the type.

    • describe gives the name config binds the tool by, which is also the name of its tools.<name> table, and the resource pools its commands hold, such as the site’s licences for it. Each pool it names gets the default size of a licence pool.
    • probe says how doris finds the tool’s version, so a new version reruns the step: most are a command, such as ["slang", "--version"], which doris runs once when it works out a cache key, never while planning.
    • key-env names any host variable that changes what the tool writes as a capability. Return none unless one does: a variable in it splits the cache between machines that set it differently.
    • check-options reads the options into the tool’s Options type (see step 4), so a mistake in a config is found when it loads.
    • run and judge are the plugin’s callbacks (see below). A tool with none returns an error.
  3. Implement linter::Guest.

    • lint builds the Invocation from the request with the SDK’s builder, and calls .tool() on it. That is what puts the tool’s version, its key-env and the plugin’s hash in the step’s cache key, and the version in the result file. A helper program the command runs, such as a compiler, is added as .program("gcc", &["-dumpfullversion"]).
    • Say what the tool holds while it runs, so the scheduler can count it: .resource(POOL, 1) for a licence, naming a pool describe names, and .threads(...) if it uses more than one. A tool that takes a thread count gets it from Arg::threads (see Resource pools).
    • Declare what the tool needs of the machine on the invocation, with .precheck(...), if it is more than the program being on PATH (see Prechecks).
    • complete-on says which exit codes mean it finished. A linter that exits non-zero for findings must not be reported as a step error.
    • logs names the files parse reads. They become the step’s outputs, so a cached step still has them, and a step that passed keeps them.
    • parse turns the log text into lint-messages. The lint flow calls it once, as the step’s parse function, and writes the messages to messages.json, which the judge and the result file read.
    • judge is given the parsed messages, the exit code and the log to point at: doris_plugin::lint::verdict for most linters, or more for one with more to say, such as Verilator’s, which makes an internal error a step error.
    • backend names the filelist-format it reads. The FuseSoC plugin, in plugins/fusesoc, asks FuseSoC for that tool’s files and flattens them into the linter’s argument file. A tool Edalize writes nothing for at setup is flattened from the EDAM file, as Verible and slang are. A format FuseSoC has no flattening for is a change to that plugin and to the filelist-format enum.
  4. Give it options. Write an Options struct, with deny_unknown_fields and a doris_plugin::StepOptions field for each step the tool is used in, named after the step, and read it with doris_plugin::options. Put each step’s args in that step’s command.

  5. Build it into doris, if it is to ship with doris: add the tool’s name and crate to PLUGINS in build.rs. Where it goes in that list is where it is listed among the tools. A plugin kept outside doris is built with cargo rustc --crate-type cdylib --target wasm32-unknown-unknown and made a component with wasm-tools component new, and a site puts it in a plugins.path directory. wasm-tools is not in the dev shell: get it from nixpkgs’ wasm-tools, or with cargo install wasm-tools.

  6. Test it. Test parse natively, in the crate, with cargo test --workspace, on real log text, including a clean run, a syntax error and a message with no rule. Test the command line lint gives, with and without options, and the judge, through the plugin, in tools::wasm’s tests.

  7. Document it: the Lint page’s tables, and the flatten and lint step pages.

The other capabilities follow the same steps, each with its world and interface:

CapabilityWorldFunctions
Simulatorsimulator-pluginfeatures, coverage-format, build, run
Reference modelreference-model-pluginbuild, serve
Coverage reportercoverage-reporter-pluginreads, report, summary, files, parse
Linterlinter-pluginbackend, lint, logs, parse, judge
CDC or RDC checkercrossing-checker-pluginkind, check, parse, judge
  • A simulator says what it can do. features gives coverage, waves, DPI and UVM. Say only what the plugin’s build and run really handle, because the flow fails a job that asks for more when it plans it. coverage-format names the format of the databases its runs write, such as Cadence.
  • A coverage reporter says what it reads, as reads, a format a simulator names. A job whose config binds no reporter gets the first plugin’s that reads its simulator’s format.
  • A simulator that builds from sources it writes gives a prepare-step for that in its build plan, as Verilator does with instrument.
  • A coverage reporter is either a command or the plugin reading the database itself, as a callback. One that is the plugin reports the plugin’s version from probe, as doris does.
  • A tool with two capabilities is one plugin whose world includes both worlds, as Verilator’s does. Its key-env is asked for each capability, and both share one Options struct.
  • Code two plugins share is a crate of its own, as plugins/verilator-cov is for Verilator and the doris reporter: the names of the cover properties one writes are the names the other reads. Two tools that are one code are one crate that two thin plugin crates export, as plugins/meridian is for plugins/meridiancdc and plugins/meridianrdc: it binds its world with pub_export_macro: true, and each exports its type with plugin_meridian::export!(Cdc with_types_in plugin_meridian).

A callback is all data. What a plugin does itself when a step runs, such as instrument’s rewrite, is named by a callback: data the plugin makes when it plans the step, usually JSON of an enum of the plugin’s own. The step is keyed by it, so put everything that changes the result in it. When the step runs, tool.run is given it back with the step’s directory, and can read files in the job’s directory and write them in the step’s, with doris_plugin::fs. A judge that reads files, such as IMC’s, is a callback given to tool.judge.

Keep tool flags out of the flow. If a flow needs something new from a tool, add it to the request type in neutral terms, in the WIT and the capability trait, and let each tool turn it into its own arguments. If not every tool can do it, make it a feature so that asking for it is checked.

Keep the machine out of the plan. A library path, a port or a version is a host-env request, an Arg::Port or a tool’s probe. A plugin can’t read the environment or run a command while planning, so there is no other way.

Adding a flow

A flow touches more places, because the command line is a fixed list of flows:

  1. The flow module, src/flows/<name>.rs:
    • the config type: a serde struct with deny_unknown_fields, its schema name, such as dv/syn/1, a tool field of type config::RoleTools and a tools field of type config::ToolOptions;
    • NAME, the flow’s name on the command line and in the project file’s tool table;
    • ROLES, the roles the flow needs tools for, each with a name, a capability and whether it is optional. The first is the main role, whose tool is in the job’s ID;
    • a type that implements Flow. Its plan takes a design, the config and a Bindings, gets each tool from its capability’s constructor with tools.tool(ROLE), and returns one job. Use tools.label() for what follows @ in the job’s ID and directory;
    • a validate on the config that calls flows::bound and ToolOptions::check, so a bad binding is found when config loads;
    • a plan function for the command line that calls flows::plan_jobs, which makes a job for each combination of bound tools;
    • a job type that implements Job;
    • the result type, with its own schema name such as dv/syn-result/1, a versions field from flows::tool_versions, and write_result.
  2. Discovery, in src/repo.rs: load files with that schema, attach each to its design, call the config’s validate with the project file’s defaults for the flow, and add the flow’s name and roles where the project file’s tool table is checked.
  3. The report, src/flows/<name>/report.rs, made from the result type alone. Add the result to JobResult in src/flows/mod.rs so doris report and doris report view can read it.
  4. The command line, in src/main.rs: the Flow enum and the places that match on it.
  5. Documentation: a page under Flows, a page for each new step, the schema in Configuration, and the flow’s roles in Tools and roles.

A flow that needs a new kind of tool adds a capability first: an interface for it in wit/plugin.wit, with its tool-neutral request and plan types, a world for its plugins, an export of it in the doris world, and a variant of the tool interface’s capability enum, which key-env is given; a Capability variant with its label, and a trait that is a subtrait of Tool, in src/tools/mod.rs; the interface’s indices on plugin::Plugin, which say whether a plugin exports it, with its entry in Plugin::capabilities and a Call method that loads it; and the trait’s implementation on a plugin, in tools::wasm.

When designing the steps:

  • One command per step, so each is cached by itself.
  • Read artefacts; don’t name dependencies. Take another step’s output with step.artefact("name"), give its path to whatever builds the command, and call .reads(&artefact) on the step that uses it. That is the dependency. A command that names a path in another step’s directory without it is refused by exec::validate. Step::after is only for an order with nothing read.
  • Give an in-process step a param for everything its closure captures that changes its result. The cache can’t see into a function, so a captured option that is not a param, an input or an artefact the step reads is not in the key, and changing it gives a stale hit. Step::param(what, value) keys the value’s Debug text.
  • Declare every output a later step or the result needs. Only declared outputs are cached, and only they are left in the directory of a step that passed. write_result must work for a step that came from the cache, so anything it reads, such as a log it parses, is an output.
  • Declare a file that is only for looking into the step as a debug output, with debug_output: a raw tool log, or an intermediate file. It is kept and cached in a run with --debug and deleted otherwise. Don’t declare scratch at all. Nothing downstream can rely on a debug output, because a plain run deletes it.
  • Parse a tool’s output once, with a parse function, when the judge and the result file both need what it reported: Step::parsed(name, path, function). The judge is given the results, and write_result reads them from the output, which a cached step has. Write paths in them relative to the job’s directory, so they are the same in every checkout.
  • Give the step that decides the result a judge, and say on its page what passed does and does not mean. Use a pass pattern to tell a tool that finished from one that crashed with an accepted exit code, and a fail pattern for an error line no exit code shows. Pattern::text and Pattern::regex name the output to look in, or STDOUT or STDERR.
  • Set run_after_fail on a step that reports on a run, if the report is wanted when the run failed. It then reads a failed step’s outputs, so they must be complete when the step fails.
  • Declare what the step needs of the machine as prechecks, so a run that can’t work stops before its first step. A program run by name needs nothing: it is checked already, and Invocation::needs adds the programs a command runs itself. Put a Precheck on the invocation, in the plugin, for what the tool needs whatever the flow, such as Precheck::Env for a variable. A licence is not a precheck: hold it with .resource(POOL, 1) and the scheduler waits for one. Put one on the step, in the flow, for what the flow knows, such as Precheck::File for a directory the config names.
  • Start services with Service::new, and change only what differs: ready for a service that shows it is ready by a file or a log line, ready_timeout, stop and stop_timeout. Declare what a service writes on the service, with .output, so it is checked once the service has stopped; .port gives it more ports, and .input keys a file it reads.
  • Use a cache unit only for a step that needs one: a long step worth resuming part-way, or one whose raw output should not be cached because it has timestamps or paths in it, or is far larger than what is wanted from it. A unit, a composite step, a parse function and a service’s own outputs are all there for steps that need them; a step that doesn’t stays as simple as it can be. Make the tool one child and the reduction another, with Step::unit, and export only what the rest of the job and the result read. Give a child the unit’s own directory when it needs its siblings’ working files, and one of its own otherwise. Give the unit the verdict: children can’t have a judge or fail patterns. A step whose outputs are already small and stable stays a leaf. See Cache units.
  • Use a composite step to group steps that are each cached by itself, with Step::group, such as a list of commands. Name its children as siblings, and export what the rest of the job reads. See Composite steps.
  • Reuse tools::sources::steps for the sources.
  • Check what the config asks of a tool when the job is planned, as TestRIG does with tools::check_features and tools::check_pairing, so a job that can’t work fails in doris plan.

The roadmap’s Tool capabilities page describes where these interfaces are going: a command line that works on any Flow, and options checked against the steps a tool is used in.

Changing a format

Config files, result files, cache entries and GC roots all carry a schema with a version.

  • A change that old readers would misread needs a new version. Result files outlive the doris that wrote them: doris report view reads results kept in GC roots.
  • A changed cache key is safe. Old entries are simply never hit, and are collected when their roots go.
  • A new field that defaults when absent needs no new version, as the debug and debug_outputs fields of a cache entry did not.
  • Bump KEY_PROTOCOL in src/cache.rs when doris’s own part in a result changes: how a key is made, how a verdict is reached from a step’s rules, or what a flow does around a plugin’s results. Every key holds it, so entries stored before are not used. A plugin needs no bump: its hash is in the key of every step that uses its tool.