Formal verification of the Veryl compiler with Kani
The compiler of Veryl, a hardware description language now in development, performs many expression evaluations that are far more complex than ordinary fixed-width integer arithmetic: each bit can take four values (0/1/x/z) rather than just 0 and 1, and variable sizes range from 1 bit to several thousand bits. Because Veryl inherits SystemVerilog semantics, these evaluations must follow the detailed rules of the IEEE 1800-2023 standard, and the compiler's built-in RTL simulator — which for speed generates Cranelift IR and C source code to obtain native binaries — must also produce code that satisfies all of these rules. Unit tests alone could not fully verify such complex rules, and incorrect evaluation results were reported repeatedly; this is where formal verification[1] comes in, in principle.
The main cost of formal verification is writing proofs, a cost that can now be shifted to an LLM: the effort began from a Gist by @mizchi in which a model is extracted from the implementation and verified with Z3/TLA+. Because that approach leaves a gap between implementation and model, and Veryl is written in Rust, the project uses Kani[2] to verify the Rust code directly, referring to the IEEE 1800-2023 standard itself for SystemVerilog-derived semantics and extracting specifications from the implementation for compiler internals unrelated to SystemVerilog. Codex's Sol 6.1 serves as the LLM, automatically generating Kani proofs, direct tests for the parts Kani cannot prove and mutation tests; since a fully automatic flow tends to abandon proofs in favor of direct tests, occasional hints are given so that proofs complete within Kani. The work is public at https://github.com/veryl-lang/formal, where CI runs verification every night against the latest Veryl, reports errors as Issues, and Codex fixes them.
The bugs found and fixed so far are mostly corner cases of four-state[3] evaluation and sign extension[4], along with coordinate calculation errors in the Language Server Protocol (LSP); among the fixes are corrupted cache blobs, source coordinates and LSP UTF-16 positions, sequential state through synthesis, and cast boundaries with mixed signedness. The work also uncovered an ambiguity in the SystemVerilog standard itself: for a unary operation on a value containing unknown, such as +2'b0x, one rule makes the result of unary + equal to its operand, giving 2'b0x, while another makes any arithmetic with unknown values entirely unknown, giving 2'bxx. The project adopted the first interpretation because unary + generates no circuit, so there is no reason for the unknown to propagate across the full width, and because the existing implementations Icarus Verilog and Synopsys VCS behave that way.
Sources
-
JPZenn JA
KaniによるVerylコンパイラの形式検証


