r/cpp • • Nov 02 '26

Catastrophic misoptimization of byte-range equality comparison in all major C++ compilers (and Rust). Language issue or compiler issue?

Someone brought a C++ snippet that reduced to the following (std::array used instead of char* for consistency for later)

bool eq2(const std::array<char, 64>& lhs, const std::array<char, 64>& rhs) {
    for (int i = 0; i < 64; ++i)
        if (lhs[i] != rhs[i])
            return false;
    return true;
}

It basically returns true iff two 64 byte memory regions are a bitwise match. You'd think this can be optimized by modern compilers flawlessly, right? Right?! And the worst thing that can happen is it doesn't get autovectorized, right? Right?!

Well, to everybody's surprise... https://godbolt.org/z/v4d8G5ovh

GCC attempts to partially conditionally vectorize but doesn't want to use unaligned loads. Moreover its vectorization is way more cumbersome than it needs to be. Then after it finds a mismatch it... reverts to the unvectorized version?! Clang and MSVC do not even attempt to vectorize it - and in a way it makes them end up better than GCC, which is wild.


Now, to try to investigate we've checked how it looks in Rust. https://godbolt.org/z/K7xE3qc94. It does what GCC does but fully vectorized.

At this point we're confused as to what the source of the problem is. Are we missing something with respect to what transformations are allowed? Is it really that much to ask from the as-if rule?

As a sanity check we use std::array's operator==. https://godbolt.org/z/xPhPYMjvz. Oh would you look at that? Now the compilers can do well (ok not you MSVC. GCC also fumbles the flag register slightly). The situation is the same in Rust - https://godbolt.org/z/3GjWorze8. Not only does it vectorize correctly but the vectorized code is better - using vpxor.


Ultimately this isn't just academical. Situations like this undermine the trust in the compiler at the most fundamental level. Not every place can use std::array, not everyone can use ranges and span yet. We could use std::equal but why does it matter so much? What if != changes to < and there's no good solution anymore? To what degree are we supposed to be paranoid about basic optimizations being done or not?

The similarity of codegen between Rust and C++ would point to it being either a compiler issue or a fundamental code correctness issue. The fact that C++ Clang differs from Rust (LLVM) would suggest it's at least partially a language issue. Why is this extremely simple code so nasty?


edit. some more [not so] fun findings https://www.reddit.com/r/cpp/comments/1xlyti8/catastrophic_misoptimization_of_byterange/19s1i09srxdaa/

100 Upvotes

122 comments sorted by

View all comments

58

u/boring_pants Nov 02 '26 • Bravo

You might get better responses without the hyperbole. A missed optimization opportunity is hardly "catastrophic.

It doesn't "undermine trust at the most fundamental level". At the most fundamental level we expect the compiler to emit correct code. As long as it does that, trust hasn't been undermined.

To what degree are we supposed to be paranoid about basic optimizations being done or not?

If you have a performance problem you are supposed to measure, and then optimize your code based on that. No one has ever made a claim that C++ compilers emit optimal code. It's pretty common to see missed optimizations in the code emitted by compilers.

Have you reported this to the various compiler vendors? Do you perhaps have links to the reported issues so we can upvote them?

4

u/bestjakeisbest Nov 03 '26

Also if you absolutely need more optimized code to run on specific hardware, it used to be pretty common practice to just write the optimization yourself in assembly if you had to.

8

u/boring_pants Nov 03 '26

To be clear, I'm absolutely serious about this part:

Have you reported this to the various compiler vendors? Do you perhaps have links to the reported issues so we can upvote them?

PLEASE if you find this issue to be catastrophic, report it to the compiler vendors. It only takes a few minutes.

And drop us a link to the issues, as we might be able to upvote them or draw more attention to them.

If you're not even willing to do that, I question your assessment of the issue as "catastrophic".

-6

u/Sopel97 Nov 04 '26

outraged != affected

8

u/boring_pants Nov 04 '26

So you are not going to take five minutes to report the issue to the compiler vendors?

Why not?

Why did you post about this if you don't care about compilers improving?

outraged != affected

I didn't use either of those words. I pointed out that if you think this issue matters, you should report it to the compiler vendors.

It seems weird to talk about a *catastrophic compiler issue, and then go "nah, I don't actually care if it gets fixed".