Bug 98774 - gcc -O3 does not vectorize some operations
Summary: gcc -O3 does not vectorize some operations
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: vectorizer
  Show dependency treegraph
 
Reported: 2021-01-20 22:13 UTC by Ivan Sorokin
Modified: 2022-12-26 06:39 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-12-25 00:00:00


Attachments
nbody-update-velocity.cpp (247 bytes, text/plain)
2021-01-20 22:13 UTC, Ivan Sorokin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Sorokin 2021-01-20 22:13:50 UTC
Created attachment 50014 [details]
nbody-update-velocity.cpp

In the following sample GCC (-O3 -ffast-math) fails to vectorize operations. The results is that GCC 10.2 does 8 mulsd, while clang 11.0 does 4 mulpd.

struct vec3 { double x, y, z; };

void update_velocities(vec3* __restrict velocity,
                       double const* __restrict mass,
                       vec3 const* __restrict dpos,
                       double const* __restrict mag)
{
    velocity[0] -= dpos[0] * (mass[1] * mag[0]);
    velocity[1] += dpos[0] * (mass[0] * mag[0]);
}

See an attachment for the complete sample.
Comment 1 Hongtao.liu 2021-01-21 02:36:26 UTC
It's fixed in current trunk https://godbolt.org/z/63576n
Comment 2 Richard Biener 2021-01-21 07:54:55 UTC
There's

t.ii:21:9: note:   ==> examining statement: a$x_1 = *dpos_10(D).x;
t.ii:21:9: missed:   BB vectorization with gaps at the end of a load is not supported
t.ii:27:6: missed:   not vectorized: relevant stmt not supported: a$x_1 = *dpos_10(D).x;
t.ii:21:9: note:   Building vector operands of 0x4047278 from scalars instead

which we eventually can improve.  With AVX2 we split into one AVX and one SSE
part and then run into a duplicate PR where we end up with conflicting
vector types for a load which we do not yet support.  We then end up with
only vectorizing the AVX part.
Comment 3 Ivan Sorokin 2021-01-27 10:24:01 UTC
(In reply to Hongtao.liu from comment #1)
> It's fixed in current trunk https://godbolt.org/z/63576n

I can confirm that now GCC does use packed multiplication mulpd. Although it is used somewhat inefficiently. The original program contained 8 multiplications and clang does 4 packed multiplication. GCC trunk does 6 packed multiplications.

https://godbolt.org/z/EabPxT
Comment 4 Ivan Sorokin 2021-09-14 18:21:19 UTC
I retested the sample on GCC 11.2.

https://godbolt.org/z/xrarP3zbY

Compared to Clang 12.0.1 GCC still generates 6 more instructions in total and does 6 mulpd against Clang's 4 mulpd.
Comment 5 Andrew Pinski 2021-09-14 18:28:40 UTC
For the trunk (without -ffast-math), the perms are done too early:
  vect__5.32_53 = VEC_PERM_EXPR <vect__2.31_52, vect__2.31_52, { 1, 1 }>;
  vect__5.33_54 = VEC_PERM_EXPR <vect__2.31_52, vect__2.31_52, { 1, 0 }>;
  vect__5.34_55 = VEC_PERM_EXPR <vect__2.31_52, vect__2.31_52, { 0, 0 }>;
  _3 = *mag_9(D);
  _58 = {_3, _3};
  vect__4.35_59 = vect__5.32_53 * _58;
  vect__4.35_60 = vect__5.33_54 * _58;
  vect__4.35_61 = vect__5.34_55 * _58;

With -ffast-math:
  a$x_1 = *dpos_10(D).x;
  a$y_11 = *dpos_10(D).y;
  _49 = {a$x_1, a$y_11};
  _35 = _49 * _58;
And the PERM is done too early the same way ..