Skip to content

32. Proofs run in parallel, one goal per solver, sliced, cached and selectable

Date: 2026-11-21

Status: Accepted

Context

Build time is a benchmark Pia always watches. Every build proves every goal, so proving is on the critical path of every edit, and it grows with the program: the stream delineation demo (docs/streams.pia.md) has a goal that costs Z3 about 57 million resource units.

The verifier (src/verify/Verifier.cpp, docs/verifier.md) already decides each goal in its own query, with its own z3::context, and calls use the callee's contract rather than its body. But it solved the queries one after another on one thread. Each query held everything before its goal (every parameter's range, every op, every earlier goal as an assumption) whether the goal needed it or not. There was no way to prove one function or one goal on its own, and an unchanged goal was proved again on every run. The Presburger stage of ADR 0020 is not implemented yet.

PR #108 made pia_object decide goals by Z3's deterministic resource limit (--solver-rlimit) instead of the wall clock, so a goal's result no longer depends on load. That is what makes parallel proving safe: running many queries at once can no longer turn a proof into a timeout.

Decision

  • One goal, one solver, many threads. Goals are still listed and encoded in walk order on one thread, because encoding builds smt dialect IR in the module's MLIRContext. The encoded SMT-LIB queries are then decided on a pool of threads, one per core by default (--jobs=N), each in a fresh z3::context. Larger queries start first. Each job writes only its own outcome, so the report keeps the walk order, and with an rlimit the result of every goal is the same on one thread or many. The REPL, pia-lsp and pia_object all get this through verifyModule.
  • Goal slicing, measured and off by default. With --slice, a query is cut to its cone of influence: the goal's own assertion (the encoder asserts the negated goal last) plus, transitively, every assertion that shares an uninterpreted symbol with one already kept. Dropping assertions only weakens the hypotheses, so an unsat slice proves the goal. A slice that is sat or unknown may have lost an assumption that mattered, such as a contradictory path condition, so the whole query is solved again and its model is the counterexample. Slicing therefore changes how fast a goal is proved, never whether it is. On the benchmark it lost: the streams demo took 392 million resource units and 47 seconds on one thread sliced, against 269 million and 33 seconds unsliced, because smaller queries are not always easier for Z3 and a goal whose slice is satisfiable is solved twice. So it stays off until a better slicing rule (for example, slicing only queries above a size, or giving the sliced attempt a fraction of the rlimit) wins on the benchmark.
  • Modular reasoning stays at contracts. A call contributes its callee's requires as a goal and its ensures as assumptions, never the callee's body, so a callee is proved once, in its own function, and a caller's query does not grow with the callee's implementation. Today a query still holds every earlier goal of its function as an assumption; slicing is the tool for trimming that once it pays.
  • Selecting a subset. --verify-only=<spec> (repeatable) proves only the goals a spec selects: a function name, a module (its file name, edges.pia or streams.pia.md), or a location file:line[:col], the same forms as --allow-unproved. Earlier goals are still assumed, exactly as in a full run, since a full build proves them in their own queries. A selective run never generates code: --verify-only implies --verify.
  • Proof cache. --proof-cache=<dir> records each proved goal under the SHA-256 of a domain prefix, the Z3 version, the rlimit and the whole query (<dir>/<2 hex>/<hash>, written to a temporary file and renamed, so concurrent builds can share a directory). A goal whose hash is present is not solved again. Only proofs are cached, so a failing goal always reports a fresh counterexample. The rlimit is in the key so that a warm cache cannot prove a goal that a cold build would give up on. Because a call is encoded from its callee's contract, a callee's body change leaves its callers' hashes alone, and a contract change changes them: the same rule as the interface hashes of ADR 0024, at the granularity of one goal. When those interface hashes exist, a module's cached goals can be keyed by its imports' interface hashes as well. In Bazel, pia_object actions are already cached per file; the proof cache serves piac, the REPL and the language server between edits.
  • Build time is a tracked benchmark. tools/bench/proof_bench.py verifies the files in tools/bench/corpus.txt (the edges and streams demos and the verifier tests; the standard library joins as its modules land) on one thread and on every core. It records, per file, the goals, the solver queries, the total Z3 resource count and the wall times, and compares them with tools/bench/baseline.json. The resource count is deterministic, so CI fails when it grows by more than 10%. Wall times are gated (--wall-threshold) only against a baseline from the same host, and CI reports them in the job summary and keeps the JSON as an artifact. The benchmark also fails if the report differs between one thread and many, or the cost by more than 5%: Z3's resource count varies by about 1% between runs, because some of its hash tables depend on memory addresses. --verify-stats prints the same counters for any piac run.

Consequences

  • Proving scales with cores. A file with many SMT goals finishes in about the time of its costliest goal rather than the sum of all of them.
  • Measured on Dean's 8-core iMac (tools/bench/baseline.json): the corpus verifies in 61 seconds on one thread and 32 on eight. The streams demo goes from 33 seconds to 11, and edges from 13 to 6. With a warm proof cache, streams verifies in 2.6 seconds, of which 1.5 is startup and 1 is encoding. Once there are cores to spare, the costliest single goal sets the time, so splitting such goals (by cases, or through intermediate lemmas) is the next lever.
  • Every piac run used to spend about 1.5 seconds in dyld before main, binding about 365,000 weak symbols (LLVM, MLIR and Z3 templates) against its own 420,000-entry export trie. The tools now export only their Mach-O header and are dead-stripped (tools/link.bzl, issue #120), which brings startup to 8 ms. Since then the corpus verifies in 43 seconds on one thread and 14.5 on eight, and about 97% of that time is spent in Z3. Z3 is built optimised even in fastbuild, so -c opt and --config=lto change the size of piac but not its proof time.
  • One function or one goal can be proved on its own, for editors and for iterating on a hard goal, without loading every proof.
  • The resource count per goal changes with slicing, so the pia_object rlimit is re-measured against the benchmark rather than the old per-goal numbers.
  • Encoding is still sequential and costs time proportional to goals times function size. Making it parallel needs a thread-safe way to build queries outside the module's context (separate contexts per function, or encoding straight to SMT-LIB), and is left for when the benchmark shows it matters.
  • pia_object actions run in parallel in Bazel and each also uses every core, so a large build can oversubscribe the machine. That costs throughput, not correctness, because the rlimit decides results. A solver_jobs attribute can cap it if the benchmark shows a loss.
  • The Presburger stage (ADR 0020) slots in as another kind of job in the same pool.

Amendment, 2026-11-24: CI runs the benchmark only when it can move

Dean asked for the benchmark to run only when something that matters to it changes. It proves the corpus twice and took 69 minutes on Linux and 56 on macOS in the run that added this (PR #407), against 8 to 11 minutes for the build and tests, so it was most of a cached CI run, and BuildBuddy's remote execution cannot help, because the benchmark times proofs on the runner itself.

  • A what changed job in .github/workflows/ci.yml passes the changed paths to tools/bench/needs_bench.py, and both jobs run the benchmark step only when it says yes. The jobs themselves, and their required checks, run as before; only the step is skipped.
  • What can move a gated cost: the files in tools/bench/baseline.json and the library modules they import, transitively; src/ except the language server, the REPL, code generation and certificates, none of which runs under piac --verify; tools/piac/piac.cpp; tools/bench/; third_party/, the module files, .bazelrc and .bazelversion; and the CI workflow. A file that is not in the baseline is reported as new and never gated, so new library modules, demos and tests do not need a run until they join the baseline.
  • When the job cannot tell what changed, the benchmark runs. Pull requests compare with the base they merge into, pushes to main with the commit before, and queue groups with their base.
  • A nightly run on main (16:00 UTC) builds, tests and benchmarks everything, so a change the path list misses is still caught within a day. A night with no new commits skips it. Running the workflow by hand also runs the benchmark.
  • Over the last 60 commits on main, 28 would have skipped it: ADRs, docs, case studies, skills, CI tweaks, and test-size changes.