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

Loop vectorization does not deal with a simple char reduction

Open jmolloy opened this issue 12 years ago • 4 comments

Bugzilla Link 21369
Resolution FIXED
Resolved on Aug 27, 2015 09:12
Version trunk
OS All
CC @hfinkel

Extended Description

Clang currently cannot do a simple char reduction. The following program: char arr[5000];

char f() { char a = 0; for (int i = 0; i < 5000; ++i) a += arr[i]; return a; }

Is not vectorized:

/bin/clang -O3 -o - -S test-vec.c -target arm64 -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize test-vec.c:8:1: remark: loop not vectorized: value that could not be identified as reduction is used outside the loop [-Rpass-analysis=loop-vectorize] } ^

It is not immediately obvious what value has caused the vectorizer to bail out.

jmolloy avatar Oct 24 '14 15:10 jmolloy

Committed r246149.

llvmbot avatar Aug 27 '15 16:08 llvmbot

Hi,

I'm definitely interested in having this solved. I'm going to be dealing with essentially the same problem from the SLP perspective (teaching SLP to look through ANDs when identifying horizontal reductions) soon too.

I think your first approach is the best one; we've previously decided that we shouldn't create illegal types in the IR stage.

Cheers,

James

jmolloy avatar Aug 13 '15 09:08 jmolloy

James,

This is an old bug, but I see that we are still not vectorizing your test case. The reason the reduction is not recognized is that the "add" is evaluated at i32 rather than i8. InstCombine creates an "and" that the vectorizer can't understand is part of the add reduction. See the loop body below.

for.body: %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] %a.07 = phi i32 [ 0, %entry ], [ %add, %for.body ] %arrayidx = getelementptr inbounds [5000 x i8], [5000 x i8]* @​arr, i64 0, i64 %indvars.iv %0 = load i8, i8* %arrayidx, align 1 %conv = zext i8 %0 to i32 %conv1 = and i32 %a.07, 255 %add = add nuw nsw i32 %conv, %conv1 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 %exitcond = icmp eq i64 %indvars.iv.next, 5000 br i1 %exitcond, label %for.cond.cleanup, label %for.body

Is there still interest in getting this to work? I've been thinking about how I can add support for this case, and it seems to me that modifying the reduction detection framework is probably the way to go.

Alternatively, evaluating the "add" at i8 would eliminate the "zext" and "and", but leave you with an arithmetic operation not in a native integer width. Any thoughts?

llvmbot avatar Aug 12 '15 23:08 llvmbot

In fact, this affects many benchmarks including for example 188.ammp, where it affects the innermost loop.

If we vectorized it, we could get around a 30% speedup on that loop.

jmolloy avatar Oct 24 '14 18:10 jmolloy