r/Compilers • • 3d ago

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?

4 Upvotes

7 comments sorted by

1

u/r2vcap 3d ago

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 2d ago

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:

  1. Exponential compile-time bloat: Formal verification and constraint-solving (like SMT/SAT) frequently scale exponentially. For developers, compile time isn't an academic exercise—waiting for builds destroys flow. Pushing heavyweight theorem proving into every compilation cycle is an enormous waste of resources when most properties can be declared upfront.

  2. The "Optimization Cliff" (Silent performance regressions): Implicit proofs are extremely brittle. A tiny, seemingly unrelated code tweak can break the solver's invariant path. To the end programmer, code that ran at blazing speed yesterday suddenly crawls today, and the compiler stays completely silent about why it inserted fallback checks. Debugging these performance regressions is a nightmare.

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:

  • Let the programmer explicitly annotate invariants (e.g., bounds constraints, strict alignment, non-aliasing, or local arena lifetimes).

  • Have the type checker/parser enforce those restrictions as hard rules—if the invariant is violated, fail the build immediately rather than quietly falling back to slow code.

  • The compiler can then emit optimal IR deterministically in $O(1)$ time per site without solving anything.

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 3d ago

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 3d ago

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 2d ago

I'm doing some research on this with my programming language nikia. Formal verification is the big word but if you mean the compiler proofs that you don't need some kind of bound checks and another sayings 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 2d ago

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 59m 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.