<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63854>63854</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SLP][RISCV] Odd reordering of gather operands breaks stride load matching
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:SLPVectorizer
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
preames
</td>
</tr>
</table>
<pre>
Noticed some odd SLP behavior which is inhibiting strided load matching on RISCV.
`clang -march=rv64gcv -target riscv64-unknown-linux-gnu -S -O3 example.c`
`cat example.c`
```
typedef signed char int8_t;
// Signed subtract which is not UB on overflow
inline int8_t sub(int8_t a, int8_t b) {
return (int8_t)((int)a - (int)b);
}
inline int8_t myabs(int8_t c) {
// Note: Assumes C != INT_MIN
return (c < 0) ? sub(0,c) : c;
}
int sum_of_abs(int8_t* restrict a, int8_t* restrict b) {
int sum = 0;
for (unsigned i = 0; i < 8; i++)
sum += myabs(a[i*64]);
return sum;
}
```
Looking at the output of `-mllvm -print-after=slp-vectorizer` here, we see something odd. The indices of the gather have been reordered in the emitted gep. (Looking at the output before SLP shows this reordering is definitely being done by SLP.) The reordering is legal (as the reduction is associative), but unlikely to be profitable.
```
%0 = insertelement <8 x ptr> poison, ptr %a, i32 0
%1 = shufflevector <8 x ptr> %0, <8 x ptr> poison, <8 x i32> zeroinitializer
%2 = getelementptr i8, <8 x ptr> %1, <8 x i64> <i64 64, i64 0, i64 128, i64 192, i64 256, i64 320, i64 384, i64 448>
%3 = call <8 x i8> @llvm.masked.gather.v8i8.v8p0(<8 x ptr> %2, i32 1, <8 x i1> <i1 true, i1 true, i1 true, i1 true, i1 true, i1 true, i1 true, i1 true>, <8 x i8> poison), !tbaa !7
```
This has the look of a sort in place of a stable sort, or something analogous. I did a bit of digging through SLP, but didn't find anything obvious.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk1v2zgQ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