Always good to see a bunch more optimizations, decided to finally submit my pet peev of missed RVO inside the caller that seems to go unnoticed and has existed for quiet a while.
I think posts like this should be kept in mind when people assume the optimiser already does every simple optimisation or invariant hoist for you.
It often doesn't, sometimes due to language semantics or just because it didn't discover the transformation. Instead of hoping for the best, doing it yourself can be a safer bet.
Interesting, I wonder if there's a subtle reason for this. None of the mainstream compilers do this transformation, even if vectorization is removed as a factor:
The only reason I can think of is that the first form is more compact with a shorter dependency chain if n=0. That'd be a pretty weak heuristic, especially with how aggressive Clang is at unrolling.
Might this be due to unsigned overflow being defined behavior and allowed to happen and presumably sum_i(scale * foos[i]) != scale * sum_i(foos[i]) in some legal situations?
On further investigation, this seems to be wrong and can probably be ignored
Thanks for this interesting example, I'll probably use it in teaching.
It's worth noting that it doesn't depend on the -march=skylake-avx512 -mprefer-vector-width=512 (which might seem oddly specific when used for demonstration).
and apparently broke my intellisense due to it crashing while trying to deduce the template parameter for an alias of std::expected, only worked when I explicitly specified it across ~60 files, not so sweet :( (upgraded from 10.3)
The updated compiler performs this reassociation. It groups subexpressions in a ranked order where literals come first, followed by loop invariants, then variants. Grouping invariants next to literals, which are also invariant, helps the compiler to hoist larger expressions from loops. Within the groups, the global frequency of each term is considered, including instances where the term occurs with a different sign. Our algorithm is very similar to the one described in the global reassociation section of the paper Effective Partial Redundancy Elimination by Briggs and Cooper.
This is super interesting. When I was working on ye olde binary black hole collider, the gist was that on the C++ side I'd build up expression trees of the code to be executed (ie a gigantic string of maths equations), and then convert that into generated code on the GPU. Which means that at some point in the middle, I accidentally became a compiler for FP expressions, which is one of the reasons why it ran so fast
This kind of stuff is actually surprisingly important I found, which surprised me the first time I tried it, but it makes a lot of sense because it aids other passes significantly. Its also incredibly easy to get into a local optimum via a minor rearrangement (or minor contractions), which is globally nonoptimal, so it definitely helps to keep a form which is generally more optimisable (and is generally a giant pain). I was never able to figure out a general fully global automatic strategy that didn't lower performance, vs having the ability to shuffle things around manually, but I also wasn't actually a compiler and had to sneak my code past clang
If I could wave a very (very) magic wand, it'd be to somehow integrate the compilers FP expression optimiser with C++ so I could deterministically choose whether or not to perform FP-fast style optimisations by hand, but that's never happening in a million years. I'd also try and gently encourage AMD to improve the quality of clang's register allocation on AMD GPUs, but now I'm really pushing the boundaries of reality
15
u/ReDucTor Game Developer | quiz.cpp-perf.com 9d ago
Always good to see a bunch more optimizations, decided to finally submit my pet peev of missed RVO inside the caller that seems to go unnoticed and has existed for quiet a while.
10
u/nukethebees 9d ago
I think posts like this should be kept in mind when people assume the optimiser already does every simple optimisation or invariant hoist for you.
It often doesn't, sometimes due to language semantics or just because it didn't discover the transformation. Instead of hoping for the best, doing it yourself can be a safer bet.
https://godbolt.org/z/9EP1zoWdG
With Clang 23, the
scale_sum_2's loop is justwith
imul eax, ediat the end.By contrast,
scale_sumdoes the multiplication during every iteration.4
u/fortsnek274 8d ago
Resolving https://developercommunity.visualstudio.com/t/MSVC-emits-inefficient-code-for-loops-wi/11150733 would be impressive.
4
u/Potterrrrrrrr 8d ago
This tool chain update caught a couple of overflowing double bugs in my release build, so that was pretty sweet.
7
u/James20k P2005R0 8d ago edited 8d ago
This is super interesting. When I was working on ye olde binary black hole collider, the gist was that on the C++ side I'd build up expression trees of the code to be executed (ie a gigantic string of maths equations), and then convert that into generated code on the GPU. Which means that at some point in the middle, I accidentally became a compiler for FP expressions, which is one of the reasons why it ran so fast
This kind of stuff is actually surprisingly important I found, which surprised me the first time I tried it, but it makes a lot of sense because it aids other passes significantly. Its also incredibly easy to get into a local optimum via a minor rearrangement (or minor contractions), which is globally nonoptimal, so it definitely helps to keep a form which is generally more optimisable (and is generally a giant pain). I was never able to figure out a general fully global automatic strategy that didn't lower performance, vs having the ability to shuffle things around manually, but I also wasn't actually a compiler and had to sneak my code past clang
If I could wave a very (very) magic wand, it'd be to somehow integrate the compilers FP expression optimiser with C++ so I could deterministically choose whether or not to perform FP-fast style optimisations by hand, but that's never happening in a million years. I'd also try and gently encourage AMD to improve the quality of clang's register allocation on AMD GPUs, but now I'm really pushing the boundaries of reality
2
u/MarekKnapek 2d ago
When talking about C++ optimizations, I created a new bug report about a missing C++ optimization, available at https://developercommunity.visualstudio.com/t/11187328 please up-vote. Sincerely, Marek.