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

Loop vectorizer miscompile

Open topperc opened this issue 9 years ago • 12 comments

Bugzilla Link 32135
Resolution DUPLICATE
Resolved on May 23, 2017 14:29
Version trunk
OS All
Attachments Test case
CC @annamthomas,@hfinkel,@sanjoy

Extended Description

The attached test case produces different output when compiled at -O1 and -O2.

The power of 2 trip count of the first loop seems to be significant. Values of 64, 128, and 256 all expose the problem. 16 does not.

It's also significant that the index for the second loop is a smaller type than the first loop. If the same index type is used for both the loop is reduced to a single store before the loop vectorizer even runs.

topperc avatar Mar 03 '17 23:03 topperc

mentioned in issue llvm/llvm-bugzilla-archive#32396

llvmbot avatar Nov 26 '21 23:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#32317

llvmbot avatar Nov 26 '21 23:11 llvmbot

Hi Craig, Davide,

This should be fixed -- it's a duplicate of bug llvm/llvm-bugzilla-archive#32396 (had marked other bugs as duplicate of this).

I had checked in 2 changes: https://reviews.llvm.org/rL299985 https://reviews.llvm.org/rL300238

Wish there was an easier way of finding duplicates!

*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#32396 ***

annamthomas avatar May 23 '17 21:05 annamthomas

Bisect narrowed it to r299985 [LV] Avoid vectorizing first order recurrence when phi uses are outside loop.

topperc avatar May 23 '17 21:05 topperc

My money on Dimar's commit that fixed LCSSA handling in LV.

llvmbot avatar May 23 '17 19:05 llvmbot

I can't reproduce it either. I'll try to bisect and see when it might have gotten fixed.

topperc avatar May 23 '17 19:05 topperc

Created attachment 18052 [details] Test case

The attached test case produces different output when compiled at -O1 and -O2.

The power of 2 trip count of the first loop seems to be significant. Values of 64, 128, and 256 all expose the problem. 16 does not.

It's also significant that the index for the second loop is a smaller type than the first loop. If the same index type is used for both the loop is reduced to a single store before the loop vectorizer even runs.

Craig, I'm not able to reproduce this on trunk. Can you please try again?

commit ddde65713895962142cdb1cfa2b2e3bbc05b43dc Author: Stanislav Mekhanoshin [email protected] Date: Tue May 23 15:59:58 2017 +0000

[AMDGPU] Convert shl (add) into add (shl)

shl (or|add x, c2), c1 => or|add (shl x, c1), (c2 << c1)
This allows to fold a constant into an address in some cases as
well as to eliminate second shift if the expression is used as
an address and second shift is a result of a GEP.

Differential Revision: https://reviews.llvm.org/D33432

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303641 91177308-0d34-0410-b5e6-96231b3b80d8

llvmbot avatar May 23 '17 18:05 llvmbot

The "two step flow" is not a correct two step flow

clang -O0 vs -O2 changes the behavior of the IR generator in the frontend. Also -O0 still runs a subset of the optimization pass pipeline. For example it still runs the always inline pass, but marks everything that isn't always inline with "noinline"

The -disable-llvm-passes switch gets the frontend to use -O2 IR generation, but stop before any optimization passes run.

I bet you'll see differences if you compare the output of

clang -O0 test.c -emit-llvm -S

and

clang -O2 test.c -emit-llvm -S -Xclang -disable-llvm-passes

topperc avatar May 23 '17 18:05 topperc

IR Comparison was done b/w following two:

A/ Two step flow A.1/ clang -O0 test.c -emit-llvm -S A.2/ opt -print-after-all test.ll -o test_opt.ll -S -debug-pass=Structure

B/ Single step flow B.1/ clang -O2 -mllvm -print-after-all -mllvm -debug-pass=Structure -emit-llvm -S test.c

It showed that Instruction Combining kicking off after Loop Vectorization is optimising out extractelement instruction from basic block (middle.block) in single step (B) flow but not in two step flow (A).

llvmbot avatar May 23 '17 17:05 llvmbot

clang -O0 does not return the IR before optimizations are run.

You need to do clang -O2 -Xclang -disable-llvm-passes

topperc avatar May 23 '17 16:05 topperc

Some more analysis on this bug:

Problem does not occurs in two step flow where opt is run explicitly.

clang -O0 test.c -emit-llvm -S opt -O2 test.ll -o test_opt.ll -S lli test_opt.ll

But in single step flow problem occurs.

clang -O2 test.c -emit-llvm -S lli test.ll

IR comparison b/w single and two step flow after each transformation pass shows that Instruction Combining kicking off after Loop Vectorization is optimising out extractelement instruction from basic block (middle.block) in single step flow but not in two step flow.

llvmbot avatar May 23 '17 16:05 llvmbot

Some more analysis on this bug:

Problem does not occurs in two step flow where opt is run explicitly.

clang -O0 test.c -emit-llvm -S opt -O2 test.ll -o test_opt.ll -S lli test_opt.ll

But in single step flow problem occurs.

clang -O2 test.c -emit-llvm -S lli test.ll

IR comparison done after each transformation pass done in single and two step flow shows that Instruction Combining kicking off after Loop Vectorization is optimising out extractelement instruction from basic block (middle.block) in single step flow but not in single step flow.

llvmbot avatar May 23 '17 16:05 llvmbot