r/Compilers • u/r2vcap • Oct 29 '26
Could source-level formal verification enable better IR optimizations?
Recent advances in LLM-assisted proof construction seem to make formal verification increasingly practical.
This got me wondering whether the results of formal verification could also benefit compiler optimizations.
For example, a compiler frontend or a separate tool could formally verify annotated source code, then propagate verified properties, such as constraints, invariants, and preconditions, into the IR for subsequent optimization passes.
Could this enable optimization opportunities that conventional static analysis might otherwise miss?
1
u/r2vcap Oct 29 '26
Self-answer: I found a similar idea in the Clef programming language developed by Houston Haynes. Their patent application (US 20260319770 A1) describes an approach quite similar to what I had in mind.
5
u/KATO-Kanryu Oct 30 '26
Having built a compiler from scratch myself, I think relying on implicit formal verification for optimizations introduces more practical problems than it solves.
Here are two major reasons why this approach is dangerous in production compilers:
Instead of asking the compiler to deduce assumptions out of thin air, a far better approach is explicit programmer annotations that enforce syntactic and type constraints:
Rather than making the compiler guess and verify behind the scenes, give programmers the syntax to express hardware-friendly contracts directly. It keeps compilation fast, makes optimizations 100% predictable, and eliminates the magic.
2
u/rantingpug Oct 29 '26
this is basically dependent types and refinement types. I think we're highly due for a usable language with either (or both).
Shameless plug: I'm trying my best with my lang :p
1
u/ianzen Oct 29 '26
In theory yes. In practice, I think it’s just a matter of time before something like this gets built and put into use.
1
u/ElephantElectrical31 Oct 30 '26 edited 25d ago
I'm doing some research on this with my programming language nikaia. Formal verification is the big word but if you mean the compiler proofs that you don't need some kind of bound checks and other things that can be used to optimize the runtime performance yes that can be done. My experiments show that the compiler can find a lot of improovements that way, I did some messurements with the one billion row challange (1brc) and the more things you proove the more runtimechecks you can avoid.
1
u/flatfinger Oct 30 '26
If one wants to generate the best machine code satisfying the requirements of applications that will be exposed to untrustworthy data, preconditions and postconditions will need to be evaluated based on generated code, rather than source code.
For example, if the nature of a data format is such that some files could take unacceptably long to process, making necessary a means of externally terminating code that would process them, there may be no reason for the application to distinguish inputs that would cause an endless loop from inputs that would take billions of years to process. It may be acceptable if code that processes valid inputs most efficiently would get stuck in an endless loop when fed some invalid inputs.
If a compiler sees that a seemingly side-effect-free loop as written would only complete if some post-condition is established, but also sees that nothing in the code as written would rely upon any computations performed in the loop, optimal code satisfying requirements might either process the loop as written and exploit the post-condition, or omit the loop and refrain from exploiting the post-condition. Modern languages may allow a compiler to omit the code for a loop but exploit the post-condition anyhow, but would require that any correct program include logic guaranteeing that every loop's exit condition would always be satisfiable, even if such logic would not otherwise be needed to satisfy application requirements.
1
u/damster05 29d ago
That's already done, no? But sure, LLMs of course can help implement more in that direction, already do.
I wonder if NNs could help at compiler runtime with pruning search trees to make currently very expensive optimizations feasible. But idk.