Formally Verifying Peephole Optimisations in Lean
Posted11 days agoActive4d ago
l-m.devResearchstory
informativepositive
Debate
20/100
Formal VerificationCompiler OptimizationsLean Programming Language
Key topics
Formal Verification
Compiler Optimizations
Lean Programming Language
Discussion Activity
Moderate engagementFirst comment
6d
Peak period
6
156-168h
Avg / period
4.5
Key moments
- 01Story posted
Dec 22, 2025 at 7:10 PM EST
11 days ago
Step 01 - 02First comment
Dec 29, 2025 at 5:43 AM EST
6d after posting
Step 02 - 03Peak activity
6 comments in 156-168h
Hottest window of the conversation
Step 03 - 04Latest activity
Dec 29, 2025 at 1:07 PM EST
4d ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 46360844Type: storyLast synced: 12/29/2025, 3:40:28 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
Suppose we start when an SSA-style function with inputs x and y:
And we rewrite it as: The original code is a function that takes x and y and returns an instance of iN {n}, namely bitvec 0.The new code also takes any input to bitvec 0, although it may discard a poison value along the way. But this is UB in the C model!
I wonder whether this situation can be avoided by declaring that there is no control flow and that the program representation disallows discarding values. (And select would need to return poison if any argument is poison even if the selector says that the poison is not used.)
Without a constraint that values aren’t ignored, the lack of flow control is certainly not sufficient, so trying to do this right would require defining (in Lean!) what an expression is.
In C, UB is instant-UB--the moment you execute an operation that is UB, your program is undefined. LLVM also has instant-UB (I mean, any language that lets you dereference an arbitrary integer cast to a pointer has to have instant-UB). But poison isn't instant-UB--your program is perfectly safe to execute an operation that produces poison. It's only if a poison value reaches certain operations--essentially any control-flow decision point, or as an input to any operation that may cause instant-UB--that it causes UB in the C sense.
(This also means that operations that could trap--like x / 0--aren't modeled as poison in LLVM, but as instant-UB. It's safe to speculate an operation that causes poison, like x +nsw y, but it's not safe to speculate an operation that causes instant-UB, like x / y).
(Integer-to-pointer conversions beg the question of pointer provenance, which is a long, complicated topic that is still not fully solved, but does go to show that UB is actually a lot more complicated than most people expect.)
For what moral reasons would I avoid linking LLVM? I’m not familiar.
IIRC there was also some case where an LLVM bump caused tcmalloc to explode inside wine.