llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LV] Vectorize loops with store to uniform addresses as profitability rather than legality check

Open annamthomas opened this issue 8 years ago • 4 comments

Bugzilla Link 38546
Resolution FIXED
Resolved on Oct 31, 2018 12:38
Version trunk
OS All
CC @ayalz,@hfinkel,@RKSimon

Extended Description

Currently in the loop vectorizer, we prevent vectorization when we're storing to a uniform address (loop invariant address).

It could be the case that for some reason store promotion in LICM didn't kick in, but vectorizing stores to uniform addresses should be a cost model decision right?

I think the vectorization of uniform stores should be a profitability check and not a legality check. Is there something I'm missing here?

Today we do not vectorize a loop that contains either of the following:

  1. Loop varying value stored into a loop invariant address (uniform stores)
  2. Loop invariant value stored into a loop invariant address

Consider this example where we're storing the IV into a loop invariant address. If I have this diff [A] and force-vectorize, we will vectorize the loop and the store is a pattern of "extractelement + store", i.e. a scalarized store. Note that with avx512 support, this just becomes a masked store, rather than a extractelement + store.

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"

define i32 @​consecutive_ptr_forward(i32* %a, i64 %n, i32* %b) { entry: br label %for.body

for.body: ; preds = %for.body, %entry %i = phi i64 [ %i.next, %for.body ], [ 0, %entry ] %tmp0 = phi i32 [ %tmp3, %for.body ], [ 0, %entry ] %tmp1 = getelementptr inbounds i32, i32* %b, i64 %i %tmp2 = load i32, i32* %tmp1, align 8 %tmp3 = add i32 %tmp0, %tmp2 %i.trunc = trunc i64 %i to i32 store i32 %i.trunc, i32* %a %i.next = add nuw nsw i64 %i, 1 %cond = icmp slt i64 %i.next, %n br i1 %cond, label %for.body, label %for.end

for.end: ; preds = %for.body %tmp4 = phi i32 [ %tmp3, %for.body ] ret i32 %tmp4 }

[A] Diff allowing vectorization to uniform stores --- a/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp +++ b/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp @@ -754,7 +754,7 @@ bool LoopVectorizationLegality::canVectorizeMemory() { if (!LAI->canVectorizeMemory()) return false;

  • if (LAI->hasStoreToLoopInvariantAddress()) {
  • if (false && LAI->hasStoreToLoopInvariantAddress()) { ORE->emit(createMissedAnalysis("CantVectorizeStoreToLoopInvariantAddress") << "write to a loop invariant address could not be vectorized"); LLVM_DEBUG(dbgs() << "LV: We don't allow storing to uniform addresses\n");

annamthomas avatar Aug 13 '18 18:08 annamthomas

The work for vectorizing in presence of uniform stores is complete as per comment 2.

annamthomas avatar Oct 31 '18 19:10 annamthomas

FYI first patch landed couple of weeks back: https://reviews.llvm.org/rL343028 Second patch in review: https://reviews.llvm.org/D52656 With this the work on loop vectorization with uniform stores is complete.

annamthomas avatar Oct 05 '18 19:10 annamthomas

Vectorizing stores to uniform address needs runtime checks to make sure the dependency distance between this store versus other memory accesses is safe (it is greater than VF?). I believe we have support for this already.

The second constraint is a cyclic dependency between the load of that invariant address and the store to the invariant address in the same iteration. Something like: for (i=0; i<n;i++) for (j=0; j<m; j++) c[j] = ++b[i];

++b[i] is a load from loop inv address for the inner loop, inc by 1 and store into the same address.

However, none of these should affect a store of a uniform value into a uniform address. I've tried this example below through ToT clang and we fail to vectorize the inner loop: #include <stdio.h> void f(int n, int *p, int *z) { int x = 100; int i=0, j; for (i=0; i<n;i++) #pragma clang loop vectorize(enable) for (j=0; j<n; j++) { p[i] = n; z[j] += p[i]; }
}

typedef void fty(int,int*,int*); fty* ff=f; int main() { int p[] = {1,-2,3,-4}; int z[] = {1,2,3,4}; ff(4, p, z); printf("%d %d %d %d\n", z[0], z[1], z[2], z[3]); }

Looking through the opt it is because of the store to uniform address: p[i] = n; As a first step, it makes sense to teach about vectorizing stores of uniform values to uniform locations.

annamthomas avatar Aug 13 '18 18:08 annamthomas

assigned to @annamthomas

annamthomas avatar Aug 13 '18 18:08 annamthomas