r/cpp • • 9d ago

C++ Optimizations in MSVC Build Tools 14.52 - C++ Team Blog

https://devblogs.microsoft.com/cppblog/c-optimizations-in-msvc-build-tools-14-52/
102 Upvotes

10 comments sorted by

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

auto scale_sum(unsigned const scale , unsigned const*const foos, unsigned const n) noexcept -> unsigned {
    unsigned sum{0};
    for (unsigned i{0}; i < n; ++i) {
        sum += scale * foos[i];
    }
    return sum;
}

auto scale_sum_2(unsigned const scale , unsigned const*const foos, unsigned const n) noexcept -> unsigned {
    unsigned sum{0};
    for (unsigned i{0}; i < n; ++i) {
        sum += foos[i];
    }
    return sum * scale;
}

With Clang 23, the scale_sum_2's loop is just

    vpaddd  zmm0, zmm0, zmmword ptr [rsi + r8]
    vpaddd  zmm1, zmm1, zmmword ptr [rsi + r8 + 64]
    vpaddd  zmm2, zmm2, zmmword ptr [rsi + r8 + 128]
    vpaddd  zmm3, zmm3, zmmword ptr [rsi + r8 + 192]

with imul eax, edi at the end.

By contrast, scale_sum does the multiplication during every iteration.

    vpmulld zmm5, zmm0, zmmword ptr [rsi + r8]
    vpmulld zmm6, zmm0, zmmword ptr [rsi + r8 + 64]
    vpaddd  zmm1, zmm5, zmm1
    vpaddd  zmm2, zmm6, zmm2
    vpmulld zmm5, zmm0, zmmword ptr [rsi + r8 + 128]
    vpaddd  zmm3, zmm5, zmm3
    vpmulld zmm5, zmm0, zmmword ptr [rsi + r8 + 192]
    vpaddd  zmm4, zmm5, zmm4

3

u/ack_error 8d ago

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:

https://godbolt.org/z/MnfExaxaj

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.

2

u/Novermars Robotics 9d ago edited 9d ago

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

1

u/DuranteA 4d ago

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).

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.

1

u/Potterrrrrrrr 8d ago

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)

7

u/James20k P2005R0 8d ago edited 8d ago

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

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.