Can a formally verified C compiler be as fast as gcc -O2?
The article covers CompCert, a formally verified C compiler written in Rocq[1] and OCaml. Like gcc, it turns C into assembly and optimizes the code, but it proves that its optimizations preserve the program's behavior under its formal semantics[2]. The author reports that CompCert's benchmarks run roughly 28% slower than when compiled with gcc -O2, and the PolyBench loop-nest benchmarks about 55% slower.
One common theory blames CompCert's stricter models of C and x86-64, which rule out assumptions such as undefined signed overflow, type-based aliasing, fused multiply-add and SIMD. The author tested this by disabling the matching flags in GCC. CompCert was still 26% slower on the CompCert benchmarks and 54% slower on PolyBench, so the author concludes that the semantic models are not the main bottleneck, and most of the gap comes from optimizations that CompCert's rules already allow but CompCert never performs.
By editing CompCert's assembly output by hand, the author found missing transformations. In the spectral benchmark, keeping the accumulator in a register instead of reloading and storing it on every iteration cut runtime from 126 ms to 73 ms, and the author suggests that loop-invariant code motion[3] and loop rotation could achieve this. In sha3, CompCert's dead-code elimination[4] pass did not remove 34 stores per round to a stack array, because its analysis treated loads through a pointer as possibly reading that stack frame.
Read the original ut-infraai.github.ioDiscussion
Discussion
Commenters mostly debate the value of formal verification: one calls it a good choice for a new project and a way to raise trust in code written by agents, while another doubts its practical benefit over gcc, which is tested very extensively. One commenter asks about the status of merging the faster-compcert changes into mainline compcert, and another says fixing CompCert's many bugs and omissions matters more than performance.
Links from the post and discussion
Sources
-
InfraAI Research Blog
Can a formally-verified C compiler be as performant as gcc -O2?
OriginalDiscussion · 5DiscussionDiscussionDiscussionDiscussion


