Bug 102564 - Missed loop vectorization with reduction and ptr load/store inside loop
Summary: Missed loop vectorization with reduction and ptr load/store inside loop
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: alias, missed-optimization
Depends on:
Blocks: vectorizer 118175 118215
  Show dependency treegraph
 
Reported: 2021-10-02 10:07 UTC by Dávid Bolvanský
Modified: 2024-12-27 08:36 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-10-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dávid Bolvanský 2021-10-02 10:07:39 UTC
void test1(int *p, int *t, int N) {
    for (int i = 0; i != N; i++) *t += p[i];
}

void test2(int *p, int *t, int N) {
    if (N > 1024) // hint, N is not small
        for (int i = 0; i != N; i++) *t += p[i];
}

void test3(int *p, int *t, int N) {
    if (N > 1024) { // hint, N is not small
        int s = 0;
        for (int i = 0; i != N; i++) s += p[i];
        *t += s;
    }
}

test3 is successfully vectorized with LLVM, GCC, ICC. Sadly, only ICC can catch test1 and test2.

https://godbolt.org/z/PzoYd4eEK
Comment 1 Andrew Pinski 2021-10-02 12:55:07 UTC
I suspect the vectorizer is not adding an alias check in the case of reduction.
Comment 2 Richard Biener 2021-10-04 07:06:14 UTC
The issue is that t can point anywhere into p[], Andrew is correct in that we could in theory do a runtime check but unfortunately vectorization relies on
reductions being done on registers and thus store-motion to have taken place.
But store-motion does not do any runtime alias checks.

The fix is at the source level to add __restrict__ to p for example or to
perform the store-motion yourself as you've done in test3.

In principle the vectorizer could do reduction vectorization on
stride zero memory accesses as well, but currently we give up on
such stores completely.