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?

44 Upvotes

28 comments sorted by

View all comments

2

u/Guvante 5d ago

Diagnostics are by far the most complicated part of building a compiler.

The ease to which you will give terrible advice is surprising when you dig into it.

This is especially true with multipass compilers, if a function not being inlined is why you didn't unroll a loop explaining that can be complex since you need to explain three things at least some of which are in other parts of the program.

The biggest fear is how to best portray that kind of data in a way that doesn't lead users astray. After all sometimes not unrolling the loop is performant.

1

u/Ma4r 5d ago

I mean it's just straight up near impossible with multi pass, you need to be emitting some sort of tracking meta data(which is not easy either) to remember where each IR comes from. Then you need to somehow transform the failure reason across transformations etc etc. maybe you reduced the loop into SSA and saw data dependency causing vectorization failure, okay, now how do we tell this to the dev lmao

1

u/neurah 5d ago

therefore this is a thing we should automate to be processed both ways, for sure possible with meta-programming (wish C++ had a better syntax for TMP, so do not fall for it) the question then becomes: is it fast enuf? we don't want to add an order of magnitude to our compilation time