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

[LoopVectorization] Invalid code after interleaving add reduction

Open CongzheUalberta opened this issue 5 years ago • 4 comments

Bugzilla Link 48546
Resolution INVALID
Resolved on Jan 26, 2021 06:25
Version trunk
OS Linux
CC @DMG862

Extended Description

The following C code generate an issue as follows:

int printf(const char *, ...); int a, b, e; char c, d; int main() { d = 19; for (; d < 45; d++) { char *f = &c; *f &= 1; e = b== 0 ?:a%b; (*f)--; } printf("%d\n", c); }

This code compiled with the following flag causes a bug:

clang test.c -O2 && ./a.out Output: 254

The correct output is '0'.

Upon further investigation, the bug is caused by the interleaving of the Loop Vectorizer.

The follwing command produces correct code:

clang test.c -O2 -mllvm -force-vector-interleave=1 && ./a.out

The root cause is not clear yet, but it seems like there is an add reduction that the loop vectorization legalily determines that it is safe to vectorize, but in this case it generates an incorrect result.

The IVDescriptor.cpp:629, the function call to AddReductionVar for Add reduction finds this to be a signed i1 add reduction, which to me seems like an invalid type.

The test file reduction-small-size.ll is important for this because it has the same type if reduction, but the interleaving is forced to 1, if we force it to 2 we have an invalid code.

CongzheUalberta avatar Dec 17 '20 19:12 CongzheUalberta

Thanks very much for your reply! And apologies for not being able to get back to you sooner.

After further investigation we found that we had internal changes that caused the issue, hence the bug is not relevant to upstream trunk code and we decided to abandon our patch.

Thanks again!

CongzheUalberta avatar Jan 26 '21 14:01 CongzheUalberta

Hello

Trying the C code, I don't seem to see an error with the latest clang. It prints 0 for all the tests I tried. Do you have any more specific instructions for reproducing the issue?

Thanks

DMG862 avatar Jan 20 '21 07:01 DMG862

Suggested fix:

https://reviews.llvm.org/D93477

My apologies, the above one is a wrong link. This is our suggested fix: https://reviews.llvm.org/D93478

CongzheUalberta avatar Dec 17 '20 19:12 CongzheUalberta

Suggested fix:

https://reviews.llvm.org/D93477

CongzheUalberta avatar Dec 17 '20 19:12 CongzheUalberta