Issue 241484
Summary [SLPVectorizer] Disengaged optional dereference in canFormVector (stale pointer-diff cache)
Labels new issue
Assignees
Reporter arie2026
    `canFormVector` dereferences `*Diff` from `getPointersDiff(front, SI)` without a check. That relies on
`collectUserStores` having validated the same pair, but in the *other* order. With the pointer-distance cache, the two
orders disagree: `(%p.i → %p)` is cached as nullopt while the GEP index is still `or i64 0, 0` (SCEVUnknown). Then SLP's
horizontal-reduction code folds that `or` to `0` without clearing the cache. `(%p → %p.i)` is computed fresh as 0, and
the reverse entry isn't updated (`try_emplace`), so `canFormVector` gets the stale nullopt.

Reproducer (`opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=slp-vectorizer`; also reproduces on 5e457cda8d5a and f2808fc79):
```llvm
define void @f(ptr %p, ptr %q) {
  %off = or i64 0, 0
  %p.i = getelementptr i8, ptr %p, i64 %off
  %im = fadd double 0.000000e+00, 0.000000e+00
  %a = load double, ptr %p, align 8
  %m1 = fmul double 2.000000e+00, %a
  %b = load double, ptr %p.i, align 8
  %m2 = fmul double 3.000000e+00, %b
  %re = fsub reassoc nsz double %m1, %m2
  %sr = fsub double 1.000000e+00, %re
  store double %sr, ptr %p, align 8
  %hr = fadd double %re, 0.000000e+00
  store double %hr, ptr %q, align 8
  %hi = fadd double %im, 1.000000e+00
  store double %hi, ptr %p.i, align 8
  ret void
}
```

```
$ opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=slp-vectorizer -disable-output test.ll
.../include/c++/13/optional:479: _Tp &std::_Optional_base_impl<long, std::_Optional_base<long>>::_M_get() [_Tp = long, _Dp = std::_Optional_base<long>]: Assertion 'this->_M_is_engaged()' failed.
2.      Running pass "slp-vectorizer" on function "f"
#10 BoUpSLP::canFormVector(...) const
#11 BoUpSLP::findExternalStoreUsersReorderIndices(...) const
#12 BoUpSLP::reorderTopToBottom()
#13 llvm::SLPVectorizerPass::tryToVectorizeList(...)
#15 tryToVectorizeSequence<llvm::Value>(...)
#16 llvm::SLPVectorizerPass::vectorizeOnceUsedSeeds(...)
#17 llvm::SLPVectorizerPass::runImpl(...)
#18 llvm::SLPVectorizerPass::run(...)
```

The abort needs libstdc++ assertions (`_GLIBCXX_ASSERTIONS`). Without them this is a silent read of a disengaged optional (UB).

Expected: no crash and no UB. `canFormVector` should treat a failed distance as "cannot form a vector", and cached
distances shouldn't survive SLP's own IR changes outside `vectorizeTree()`.

Notes: I traced the sequence above in gdb. The cache was added in #232083 (not confirmed by building its parent).
`or disjoint`/`add` as the index, or a constant index, doesn't crash. cc @npanchen

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were
assisted by an LLM; verified the reproducer on 9dce18f0e.

Assisted-by: Claude (Anthropic)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to