Skip to main content

bb_compiler/
gate_contract.rs

1//! Open `GateContract` inventory consumed by
2//! [`crate::validate_runtime_complete`].
3//!
4//! Each gate-insertion pass declares a `GateContract` impl + emits
5//! `inventory::submit! { GateContractRegistration { ... } }`; the
6//! validator iterates the inventory and runs every registered
7//! contract's `assert_inserted` against the post-insertion graph.
8//! Adding a new gate is "ship the inserting pass + register its
9//! contract" — no edit to the validator.
10
11use bb_ir::proto::onnx::GraphProto;
12use bb_ir::registry::inventory;
13
14use crate::error::CompileError;
15
16/// A single insertion contract a gate-inserting pass owns.
17///
18/// `assert_inserted` runs on every per-role sub-graph after the
19/// gate-insertion phase; it returns `Ok(())` when the gate this
20/// contract represents is consistent with the graph the validator
21/// observes (either absent because nothing needs it, OR present in
22/// the canonical insertion shape).
23pub trait GateContract: Send + Sync {
24    /// Diagnostic label for the contract (e.g. `"DeadlineCheck"`).
25    /// Surfaced in `CompileError::RuntimeIncomplete` so the host
26    /// sees which contract failed.
27    fn name(&self) -> &'static str;
28
29    /// Assert the contract holds on `sub_graph`. Returns
30    /// `Err(CompileError::RuntimeIncomplete { ... })` with a
31    /// human-readable description when the gate's insertion is
32    /// incomplete.
33    fn assert_inserted(&self, sub_graph: &GraphProto) -> Result<(), CompileError>;
34}
35
36/// Inventory-collected pointer to a `GateContract` implementation
37/// shipped by a gate-insertion pass. Library makers introducing a
38/// new gate emit one inventory submission alongside their pass.
39pub struct GateContractRegistration {
40    /// Static pointer to the contract impl.
41    pub contract: &'static dyn GateContract,
42}
43
44inventory::collect!(GateContractRegistration);
45
46/// Iterate every `GateContract` the binary links in.
47pub fn contracts() -> impl Iterator<Item = &'static GateContractRegistration> {
48    inventory::iter::<GateContractRegistration>.into_iter()
49}