r/Compilers • • 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?

5 Upvotes

13 comments sorted by

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:

  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 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/loxos58 26d ago

Github?

1

u/ElephantElectrical31 25d ago

warning all ai generated (still hard to read, everthing far from ready to use)

https://nikaia-lang.org/research/performance/ there you also find the github link.

1

u/loxos58 24d ago

You mean checking all inputs to deduce metadatas alllowing removing check ?

2

u/ElephantElectrical31 24d ago

yes excactly bounds-check elimination based on meta data using a solver.

1

u/loxos58 24d ago

I like the idea. Do you know open source solver ? In java ?

1

u/ElephantElectrical31 23d ago

I do benchmark my solver against Z3 and cvc5, it will also be opensource but it's a long way to production ready and any help is welcome. the goal is to provide a useful and fast FFM interface for java (>java 22). Meanwhile you can use Z3 or others. there are also solvers written in java itself, they may have less features and they may be slower in theory (but the thing is z3 via ffm does have some overhead). I do not know SMTInterpol, Princess and other Java based solvers in detail but I guess they do not cover what Z3 can do so have to choose or combine them based on your usecase.

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.