https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128923
Bug ID: 128923
Summary: [14/15/16/17 Regression] SLP blends two float max
updates in a bounding-box loop, 1.5x slower than
scalar on x86-64
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: fchelnokov at gmail dot com
Target Milestone: ---
Compiler Explorer (GCC 13.4 and 16.2 x86-64, 16.2 AArch64, -O2):
https://godbolt.org/z/4dvjjWP3q
struct Vec3 { float x, y, z; };
struct Point { Vec3 coord; int id; };
struct Box
{
Vec3 min{ 3.4e38f, 3.4e38f, 3.4e38f }, max{ -3.4e38f, -3.4e38f, -3.4e38f };
void include( const Vec3 & p )
{
min.x = p.x < min.x ? p.x : min.x; max.x = p.x > max.x ? p.x : max.x;
min.y = p.y < min.y ? p.y : min.y; max.y = p.y > max.y ? p.y : max.y;
min.z = p.z < min.z ? p.z : min.z; max.z = p.z > max.z ? p.z : max.z;
}
};
Box boxOf( const Point * p, int n )
{
Box b;
for ( int i = 0; i < n; ++i )
b.include( p[i].coord );
return b;
}
GCC 13 emits six scalar minss/maxss. Since GCC 14.1 (-O2 and -O3), SLP
vectorizes max.y and max.z into a 2-lane cmpltps + andps/andnps/orps blend
(fcmgt + bsl on AArch64), a chain carried from one iteration to the next,
although "p > max ? p : max" is exactly maxps (p, max), NaN included.
Best of 5 runs, 2^20 points, GitHub-hosted runners, time in ms:
-O2 -O2 -fno-tree-slp-vectorize
x86-64 AMD EPYC 9V74, GCC 13.3 0.95 0.95
x86-64 AMD EPYC 9V74, GCC 14.2 1.63 1.05
x86-64 AMD EPYC 9V74, GCC 16.2 1.56 1.05
AArch64 Neoverse-N2, GCC 13.3 1.86 1.86
AArch64 Neoverse-N2, GCC 16.2 2.17 1.86
Timing driver (the code above with __attribute__((noinline)) on boxOf; calls it
50 times on 2^20 points and prints the best time):
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <vector>
int main()
{
std::vector<Point> pts( 1 << 20 );
unsigned s = 1;
for ( auto & p : pts )
{
auto r = [&] { s = s * 1664525u + 1013904223u; return float( s >> 8 ) /
16777216.0f; };
p = { { r(), r(), r() }, int( &p - pts.data() ) };
}
double best = 1e30;
float sum = 0;
for ( int k = 0; k < 50; ++k )
{
auto t = std::chrono::steady_clock::now();
auto b = boxOf( pts.data(), int( pts.size() ) );
best = std::min( best, std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - t ).count() );
sum += b.min.x + b.max.z;
}
std::printf( "%.3f ms (%g)\n", best, sum );
}
The same loop in Clang: https://github.com/llvm/llvm-project/issues/244603