r/Compilers • u/Signal-Scholar1299 • 27d ago
Llm optimization in compiler ?
Has anyone found an optimization that a llm could do when compiling code?
I wonder if llm could bring new kind of optimization to compiler
The only thing I can think of, would be semantic equivalence.
But compiler already does it too (with egraph for example)
0
Upvotes
2
u/IndividualFill1155 26d ago
I think the bigger opportunity is optimization guided by program intent rather than just local instruction patterns. An LLM could potentially suggest algorithm substitutions, data layout changes or specialization based on usage patterns that traditional compilers wouldn't easily infer. The challenge is proving correctness and consistently outperforming existing optimization passes. It might work best as a higher level optimization advisor rather than replacing the compiler's deterministic passes
2
u/adityazero 26d ago
Yes LLMs have huge advantage being inside the compiler. LLMs are a powerful pattern matching engine, and guess what compilers are?
Most people dont get it as we have been seasoned to expect compilers to be of a certain kind. The last impediment to this is determinism (LLMs are not quite deterministic yet) but I would not hold it against LLMs as there are ways to make them deterministic (and this is a novel problem statement for current compiler PhDs)
It is only a matter of time 1-2 years max where you'll see LLMs (at least the fast models) being used in compilers. LLVM already has some (regalloc and inliner using MLGO) which are early stage but I estimate LLMs to be prevalent in compilers, maybe in a different compiler stack.
9
u/excuse-my-lisp 27d ago
There are some good reasons why you wouldn't want the compiler itself to invoke an LLM; it'd be much harder to prove that no miscompile occurs (the LLM could unintentionally change the semantic effects of the code), it would be very expensive in terms of time and probably actual cost (compile times are orders of magnitude faster than LLM responses), but most importantly the output would not be deterministic - by design, the LLM would produce different code for the same input when run repeatedly, which is generally a bad property for a compiler to have. It makes much more sense for the LLM to optimize the source code itself, potentially using remarks/diagnostics from the compiler itself as an aid.
1
u/ElephantElectrical31 25d ago
yes I do gave a plan to build an interface for llms to propose invariants, this is part of the solver which is invoked by the compiler. research has shown that llm can be used for that.
2
u/Tari0s 27d ago
compilers are designed to run fast and optimize code as mucv as possible in this short amount of time. There are analysis that would help optimize code better but are to slow, so no compiler uses them. Invoking an LLM would take much longer than that. At least at the Moment. In addition compilers are designed to compile deterministically (multiple runs with the same input, produces the same output) LLMs do not output deterministic results. They might be similar but not equivalent. So this are a few reasons why llms at the moment do not fit into compilers.
3
u/yuehuang 27d ago
Yes. But it goes by a different name. Code Review.