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

16 comments sorted by

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

1

u/Signal-Scholar1299 26d ago

This is exactly the conclusion I came to ^ There is equivalent graph but it does not work well at scale. Semantic understanding of query would be better and so lead to better high level optims

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.

1

u/Signal-Scholar1299 26d ago

Thanks for the response, Could you explain regalloc and inliner ?

Pattern matching seems limited to the deep semantic understanding and reasoning of llm.

Question was more can llm reasoning or semantic understanding do things that a compiler can not

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.

0

u/[deleted] 26d ago

[deleted] — view deleted comment

1

u/Tari0s 26d ago

still you want to run it locally, even if they would return deterministic results. but in servers you have not necessary a graphics card (most don't by design). So running an llm needs to much compute, at least for c/cpp/rust and other low level languages

1

u/Inconstant_Moo 26d ago

Pretty much all compilers are deterministic because can you imagine trying to debug a non-deterministic compiler? It's hard enough when they're deterministic.

"Fast" is a relative term. Anything that figures out which optimizations can be done by an LLM thinking about it is going to be slower than something saying if condition { do thing } else { do other thing }. Insofar as LLMs have a purpose, it's as a cheap inferior substitute for human creativity, not to do things that could already be expressed by if statements.

3

u/yuehuang 27d ago

Yes. But it goes by a different name. Code Review.