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/
View all comments
112
u/ts826848 25d ago edited 25d ago
When I saw "misoptimization" in the title I thought I would see an incorrect optimization, not a missed optimization.
Passing
-Rpass-missed=.* -Rpass-analysis=.*to Clang results in the following:So I think the early bailout is confuzzling LLVM's autovectorizer.
And indeed, if you replace the early return with a
resultvariable you get a significantly better result, albeit one that still isn't quite as good aslhs == rhs. Looking at the LLVM IR there's something funky going on with the way it combines intermediate variables (involving freezing uninitialized bits?), and it seems this is sufficient to result in differences in code generation. If you replace theifin the loop with a&=to eliminate the early return entirely you get the same assembly aslhs == rhsThe extent to which your trust is undermined depends entirely on what you want and/or need out of your compiler. For some people, performance is a "nice to have": if the compiler performs a given optimization, great! If not, eh, it's probably fine. For others, performance is effectively a correctness requirement, and the ability to express such requirements is somewhat patchy for different languages (e.g., emplace-like operations, tail calls, guaranteed inlining/autovectorization, etc.).
Keep in mind that rustc and Clang are separate frontends with different internals and might output quite different LLVM IR for semantically identical source code. For instance (at least when I last looked) rustc leaned on LLVM's native extended-width integers while Clang performed SROA before emitting LLVM, which means LLVM saw very different IR.