<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/95086>95086</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang][LoopAccessAnalysis] Stride might be wrongly replaced by 1 in certain situation.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Yunzezhu94
</td>
</tr>
</table>
<pre>
Recently when I'm trying loop vectorize on llvm 17.x version, in a certain case I found when storing value into array sometimes it stored into wrong position. Here is a case reduced from the case I met with the problem:
```
extern int a[];
extern bool b[];
template <typename c> c d(c, c);
void e(long f, long long g[][1][1][1]) {
if (d(g[4][4][4][0], (long long)e))
for (int h = 0; h < 1; h += (int)f + 7)
a[h * 3] = b[h];
}
```
When compile with flag: -std=c++11 --target=riscv64-unknown-linux -march=rv64imafdcv -fPIC -w -fopenmp-simd -O3
there is
```
%add = shl i64 %f, 32
%sext = add i64 %add, 30064771072
%1 = ashr exact i64 %sext, 32
```
and %1 is the stride equal to (int)f + 7 .
When turns on debug flag there is debug log:
`LAA: Replacing SCEV: {@b,+,%1}<nw><%for.body> by: {@b,+,1}<nw><%for.body>`
and in vplan there is
`vp<%4> = SCALAR-STEPS vp<%2>, ir<1>`
That is the stride is wrongly replaced by 1.
In my opinion such versioning should be applied on the strides of symbolically striding memory
accesses. So I checked the source code and found the symbolic stride is collected in getStrideFromPointer:
```
const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
if (!U) {
const auto *C = dyn_cast<SCEVIntegralCastExpr>(V);
if (!C)
return nullptr;
U = dyn_cast<SCEVUnknown>(C->getOperand());
if (!U)
return nullptr;
// Match legacy behavior - this is not needed for correctness
if (!getUniqueCastUse(U->getValue(), Lp, V->getType()))
return nullptr;
}
return V;
```
The value V is regard as symbolic value when dyn_cast<SCEVUnknown>(V) returns a SCEVUnknown. However, in the case I post the stride has pattern:
```
X = Shl A, n
Y = Add X, c
Z = AShr Y, m
n, c and m are constants.
```
which is not supported in createSCEV on branch 17.x, and createSCEV return getUnknown when processing 1%, making 1% regard as symbolic stride value and replaced by 1 later.
I checked source code on main branch and found the ashr pattern is supported, so the case I post will not report error. But still in any case dyn_cast<SCEVUnknown>(V) returns a SCEVUnknown will return V as symbolic value, and this might be a risk.
I think returning a SCEVUnknown is not equal to regarding V as symbolic value because unsupported patterns in createSCEV will also return a SCEVUnknown, and in my opinion there should be an extra check to stop these unsupported pattern value being regard as symbolic value.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycV19v4zgO_zTKC5HAlvP3IQ-J22ILzGEH02lv514OsszYusqSV5KTZj79gbLTJJ30dnFF4STm_x8pkhLeq8ogrtlsy2Z3I9GF2rr1j878xJ91t5qOClse199Qogn6CIcaDTwyvmgguKMyFWhrW9ijDNapnwjWgNb7BtLF5A326LyyhvEclAEBEl0QyoAUHuERdrYzZa_Sk7ipYC90h6BMsCCcE0fwtsGgGvSgQuTCsicfnDUVtNaroKyZwG_oEJQnK6TdYdlJLGHnbAOhxpPNBgMcVKjju9bZQmPDsg1L7liyYfNk-I8_8S2gM2QORI8Py7ZXpMJaDcUHWsCm1SIgsCwPxxaNaBAky-5BQsn4UhIekvHVu8TeqhKQ8aWmmHZEj9_ioxrUz7bprx98BWwxaAFQO2B8STZIaNrzXX8kUSqHkzF6ML5CcoevTooAdtYRD8VeA8vuIGHZNn7NIR2-8i0Rei7GV2R8C4srNRCRI94NZGx2F1URYPUlYmxxdzMB_6TSkLZplcY-azstKpZtYOxDybI7ST7wbZrCeByEqzCw7M4pL_fz6bgzr8YezFgr073BuBFO1kTez6eqEbtS7mG8-_qYA4wPMN7ZFk3Tjr1qShj_ng25rPuyOkX0wUHGZ6IsY1C-1qDmU2B8FhOY8XcZPvP4FiIXcQ9coiwjX5LMp4tFmiwuBdKe29cO8E3IcBIiRZfaP_gjTNlLKx8L3AenSgT8sxMagv0lVzC5ADp0zng6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