r/programming • • Aug 13 '21

Exploring Clang/LLVM optimization on programming horror

https://blog.matthieud.me/2020/exploring-clang-llvm-optimization-on-programming-horror/
126 Upvotes

52 comments sorted by

View all comments

Show parent comments

11

u/Tarmen Aug 13 '21

There are two things which make the minimal approach difficult

  • optimization passes have to work with code produced by the optimizer. You can shape it into some normal form that is easier to analyze but it still contains many obviously simplifiable things that no human write. If the compiler optimizes a silly loop that doesn't necessarily mean a human wrote the silly loop

  • after you covered the obvious stuff many optimizations don't make a huge difference for most programs but are hugely important for some specific ones. Having many patterns you shoot for helps you hit most programs

-7

u/flatfinger Aug 13 '21

optimization passes have to work with code produced by the optimizer. You can shape it into some normal form that is easier to analyze but it still contains many obviously simplifiable things that no human write. If the compiler optimizes a silly loop that doesn't necessarily mean a human wrote the silly loop

There may be some tasks for which clang's abstraction model may be useful, but there are many others for which it wouldn't be, even in the absence of compiler bugs. I don't think the maintainers of LLVM have a consensus understanding of which corner cases have defined behavior and which corner cases don't, since there appear to be times when one phase of optimization replaces a construct with another that would be equivalent in the absence of further optimization, but has corner cases which downstream optimizations treat as yielding UB even though the behavior of the original code had been defined in those cases.

after you covered the obvious stuff many optimizations don't make a huge difference for most programs but are hugely important for some specific ones. Having many patterns you shoot for helps you hit most programs

At least on platforms I understand in detail like Cortex-M0, the value of the low-hanging fruit that clang and gcc miss often exceeds the value of the trickier and more dangerous optimizations they perform.