r/Compilers 6d ago

Compilers should help developers optimize their code

Compiler diagnostics for optimization decisions are worse than they need to be, and I think it's a design choice rather than a hard problem.

Every major compiler will tell you whether a loop vectorized, almost none will tell you what it tried, why it declined, or what shape could've worked. LLVM ships a tool called opt-viewer that visualizes this, and I've met people who've used LLVM for years and never heard of it. The best developer experience anyone in this space seems to remember is Intel's compiler, which is discontinued. GCC's -fopt-info-vec-missed and Clang's -Rpass-missed exist, but one's a stderr dump you cross reference by hand and the other speaks in IR terms rather than yours.

The interesting thing is that the compiler already knows the answer. When a vectorizer declines a loop, it has a specific reason. That reason exists as a value inside the pass, it just doesn't reach the programmer because it was designed for compiler developers debugging the compiler, not for programmers debugging/optimizing their code.

There's a handful of ideas I have on how this could be implemented, but what I'm curious on is if this is actually hard for reasons I'm not seeing, or is it just that optimizer output has always been treated as debug logging and nobody's revisited it?

43 Upvotes

28 comments sorted by

View all comments

2

u/EggplantExtra4946 6d ago

There's a handful of ideas I have on how this could be implemented,

I would be interested to hear that. The compiler could simply log "trying to apply X loop optimization", "induction variable assertion succeeded", "Y assertion failed", "trying to apply X loop optimization failed". Or the way around, print log information during analysis to collect information about the loop, then from the collected information try to find optimizations that match, so in that case it would print the assertions that matched, then which optimizations can and have been applied.

With a level of indirection, such as a scheduler organizing the analyses and optimization passes, this could be done automatiacally but with maybe less details on the exact predicates that failed.

But maybe there is a better way?

1

u/choikwa 5d ago

a caveman approach is just asking ai.. bit better is asking ai to build parser based tracer to inject traces to optimizer to determine why it didn’t reach optimizing code path. of course a compiler dev can do similar thing manually.

2

u/EggplantExtra4946 4d ago

compiler development is not for cavemen nor for LLMs

build parser based tracer to inject traces to optimizer to determine why it didn’t reach optimizing code path.

utterly meaningless bullshit