InferWidths Pass in FIRRTL
- InferWidths Pass is a FIRRTL compilation phase that infers unspecified bit-widths by translating circuit elements into FIRWINE constraints.
- It employs dependency graph decomposition and SCC analysis, ensuring a unique, minimal solution through efficient constraint solving techniques.
- The approach is formally verified using Rocq and outperforms traditional tools, offering improved speed and reliability in hardware design compilation.
The InferWidths pass is a compilation phase within FIRRTL (Flexible Intermediate Representation for RTL), a domain-specific IR for register-transfer-level hardware designs. Its primary purpose is to infer missing bit-widths for circuit components where widths are unspecified, generating a well-typed hardware representation conforming to FIRRTL’s semantics. Recent research has formalized the width inference problem and demonstrated the existence of unique least solutions, alongside validated procedures for robust, verifiable inference (Wang et al., 19 Jan 2026).
1. FIRRTL Syntax and Width Typing
FIRRTL modules consist of ports, wires, registers, nodes, memory constructs, connect statements, and expressions. The width-inference procedure operates over components with unspecified widths—denoted “UInt-?” at module, wire, register, or node declaration—using only ground-typed elements and connects as relevant input. Connect statements x <= e induce width constraints; the required width of must satisfy , with expression width determined recursively according to FIRRTL’s specification, e.g., and . The width-inference targets components , leveraging the program’s AST structure.
2. Translation to FIRWINE Constraints
Width inference is formalized by converting FIRRTL circuit elements to conjunctions of linear integer inequalities termed “FIRWINE constraints.” Each width variable appears linearly and only with nonnegative coefficients inside width terms built from variables, constants, addition, and minimum () operations. For every connect statement , an inequality is produced; all operations in are flattened, while terms are retained. The aggregate system for the entire circuit comprises all inequalities originating from connects and recursively from expressions. This translation preserves the semantic requirement that inferred widths are sufficient for each circuit topology and operator.
3. Existence and Structure of Solutions
A central theoretical result is the “Unique-least” property: any satisfiable FIRWINE constraint system admits a unique pointwise-minimal solution ; that is, if and are both solutions, then componentwise. The solution space forms a meet-semilattice under , and one verifies by induction that the minimum of any two solutions remains a solution. This property is critical to soundness and determinism for the InferWidths pass: if a solution exists, it is unique, and any inference procedure must recover it.
4. Complete Solving Procedure
The width-inference algorithm proceeds in two major phases:
- Dependency Graph Decomposition: Variables are represented as vertices. Directed edges encode linear dependencies derived from constraints of the form . Strongly connected components (SCCs) are identified using Tarjan’s algorithm. The SCC quotient graph is then topologically sorted to arrange SCCs such that dependencies are respected.
- SCC-by-SCC Solution: SCCs are processed in reverse topological order. For singleton SCCs, the trivial solution is per constraints. For larger SCCs:
- Expansive case: If any edge has weight , or multiple edges exist with the same label out of a vertex, compute finite upper and lower bounds for variables. Solve the constraint box using branch-and-bound, halving or shrinking the search volume recursively.
- Nonexpansive case: All dependencies are unit-weight. Construct a weighted graph with an auxiliary zero vertex; edges reflect additive constants. Use the “max-Floyd–Warshall” algorithm to compute maximal-weight paths and detect positive-weight cycles (implying unsatisfiability). Least solution: .
All steps prior to branch-and-bound are polynomial in system size; only expansive blocks exhibit potentially exponential complexity, mitigated in practice by their rarity.
OCaml-style pseudocode
1 2 3 4 5 6 7 8 9 10 11 |
(* Global solve: assume Γ maps x -> list of ineq's *)
let infer_widths Γ =
let G = build_dep_graph Γ in
let sccs = strongly_connected_components G in
let order = topo_sort sccs in
let η = ref Map.empty in
List.iter_right order ~f:(fun C ->
let Γ_C = substitute Γ !η C in
let θ = solve_SCC Γ_C C in
η := Map.union !η θ);
!η |
5. Formal Verification via Rocq (Coq)
The complete algorithm is formalized and machine-checked in the Rocq interactive theorem prover. The formalization proves:
- Constraint-generation correctness (the map from FIRRTL to FIRWINE).
- Soundness (computed solutions satisfy all constraints).
- Minimality (solution is least in componentwise order).
- Unsatisfiability detection (failure only when no solution exists).
- Termination of recursive procedures (structural decreasing measures).
- SCC decomposition (built on verified MathComp libraries).
- Branch-and-bound invariants (search region shrinks, correctness of bounds).
- Floyd–Warshall correctness (adapted from Isabelle/HOL).
The main theorem is: For all , the result of infer_widths Γ is either the unique least solution or a proof of unsatisfiability.
6. Complexity Analysis
Algorithmic complexity is broken down as follows:
- FIRWINE constraint generation: where is the number of inequalities.
- SCC decomposition (Tarjan): .
- Nonexpansive SCCs: max-Floyd–Warshall algorithm yields per component.
- Expansive SCCs: upper bound computation is polynomial; branch-and-bound search is exponential in block size and bound width, worst-case. Empirical evidence shows most SCCs are size 1 or nonexpansive, resulting in average-case performance dominated by .
In contrast, the firtool InferWidths implementation may fail for instances with circular dependencies or exhibit superlinear blowup on large designs. General-purpose ILP solvers like Gurobi or Z3-OMT can solve FIRWINE constraints but lack formal verification and, for Gurobi, are often slower on small and medium FIRRTL benchmarks.
7. Experimental Evaluation
A comparative evaluation was performed on 75 FIRRTL programs, using:
- BFWInferWidths (OCaml extractor from Rocq-verified procedure).
- firtool (CIRCT/FIRRTL official pass v1.74.0).
- Gurobi (ILP on ; multi-threaded).
Benchmark suites included “MANUAL” (72 examples, 1–2000 width variables) and “REALWORLD” (NutShell, RocketChip, BOOM; 5000–200,000 vars).
Correctness
| Suite | #prog | firtool | Gurobi | BFW |
|---|---|---|---|---|
| MANUAL | 72 | 60/72✔ | 72/72✔ | 72/72✔ |
| REALWORLD | 3 | 3/3✔ | 3/3✔ | 3/3✔ |
- firtool failed on 12 circular-dependency cases in MANUAL.
- BFWInferWidths and Gurobi solved all instances with identical solutions.
Performance (per-file average)
| Suite | #vars | firtool (ms) | Gurobi (ms) | BFW (ms) |
|---|---|---|---|---|
| MANUAL | 119 | 7.5 | 12.1 | 1.0 |
| NutShell | 7,152 | 190.7 | 194.6 | 158.3 |
| RocketChip | 4,882 | 127.9 | 120.6 | 22.2 |
| RISC‑V BOOM | 205,608 | 8,338.3 | 3,326.9 | 3,467.8 |
BFWInferWidths is 7–12× faster than firtool and 10–100× faster than Gurobi for small–medium designs; on large designs, BFW matches or exceeds firtool and is comparable to Gurobi.
8. Implications and Availability
Formulating FIRRTL width inference as FIRWINE constraint solving, with proof of unique minimal solutions and a fully verified implementation, addresses correctness and completeness gaps in prior FIRRTL compiler passes. The available Rocq formalization, OCaml extraction, and benchmark suite (BFWInferWidths) advance the verification and reliability of hardware compilation workflows and set the stage for a fully verified FIRRTL toolchain (Wang et al., 19 Jan 2026).
Repository: https://github.com/wky17/VerinferWidth (“BFWInferWidths”).