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

SLP vectorizer produces bad shuffles

Open wjschmidt opened this issue 2 years ago • 2 comments

slp-shuffle-bug.ll.txt slp-shuffle-output.ll.txt

opt -passes=slp-vectorizer slp-shuffle-bug.ll -o slp-shuffle-output.ll

The attached reduced test case slp-shuffle-bug.ll.txt contains a number of scalar load-multiply-store chains that SLP vectorizes. However, something goes wrong with generating two of the shuffles in the vectorized sequence, shown in slp-shuffle-output.ll.

The shuffle

%14 = shufflevector <16 x float> %13, <16 x float> %12, <16 x i32> <i32 1, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0>

produces wrong code, as all of the 0 indices should have referenced indices from the second operand %12 instead. Also, the shuffle

%12 = shufflevector <2 x float> %11, <2 x float> %8, <16 x i32> <i32 poison, i32 0, i32 2, i32 1, i32 0, i32 3, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>

is suspect, as the first four indices are never used and the fifth entry is incorrect (0 instead of 2).

The test case loads from two areas of array GLOB, performs multiplications, and stores the result in a third area of GLOB. The expected behavior is:

[2928:2956] = [1612] * [1208:1236] [2960:2996] = [1616] * [1240:1276] [3000:3036] = [1620] * [1280:1316] [3040:3052] = [1624] * [1320:1332]

where [X] references dereference of the location at offset X of GLOB, and [X:Y] references a sequence of such dereferences. The vectorized code produces correct values for locations [2928:2996], but due to the bad shuffles the rest of the values are:

[3000:3004] = [1612] * [1280:1284] [3008] = [1612] * [1620] [3012:3052] = [1612] * [1292:1332]

wjschmidt avatar Nov 21 '24 14:11 wjschmidt

Tagging @alexey-bataev for information.

wjschmidt avatar Nov 21 '24 14:11 wjschmidt

Thank you for the quick fix!

wjschmidt avatar Nov 21 '24 21:11 wjschmidt