[SLPVectorizer] Assertion `B must be in the tree` in getTreeCost: PHI incoming from unreachable block
When getTreeCost picks the block for extracting an externally used scalar, a loop PHI user's site is the
incoming block of the edge that carries the scalar. Only the PHI's own block is checked for reachability. If the
incoming block is unreachable (no DomTree node), DT->findNearestCommonDominator is called with it and asserts.
Reproducer (opt -mtriple=x86_64-unknown-linux-gnu -passes=slp-vectorizer; aarch64 too; also reproduces on 5e457cda8d5a and f2808fc79):
define void @f(float %x, float %y, i1 %c, ptr %out) {
entry:
br label %loop
dead: ; no predecessors
br label %loop
loop:
%p = phi float [ 0.0, %entry ], [ 0.0, %dead ], [ %b, %loop ]
%r = phi float [ 0.0, %entry ], [ %b, %dead ], [ 0.0, %loop ]
%a = fadd float %x, 1.0
%b = fadd float %y, 2.0
br i1 %c, label %loop, label %exit
exit:
%m = phi float [ %a, %loop ]
%n = phi float [ %b, %loop ]
store float %m, ptr %out
%g = getelementptr inbounds float, ptr %out, i64 1
store float %n, ptr %g
ret void
}
$ opt -mtriple=x86_64-unknown-linux-gnu -passes=slp-vectorizer -disable-output test.ll
opt: llvm/include/llvm/Support/GenericDomTree.h:531: NodeT *llvm::DominatorTreeBase<llvm::BasicBlock, false>::findNearestCommonDominator(NodeT *, NodeT *) const [...]: Assertion `NodeB && "B must be in the tree"' failed.
2. Running pass "slp-vectorizer" on function "f"
#11 <unsymbolized: DominatorTreeBase::findNearestCommonDominator>
#12 BoUpSLP::getTreeCost(...)
#13 llvm::SLPVectorizerPass::vectorizeStoreChainImpl(...)
#14 llvm::SLPVectorizerPass::vectorizeStoreChain(...)
#15 llvm::SLPVectorizerPass::vectorizeStores(...)
#18 llvm::SLPVectorizerPass::vectorizeStoreChains(...)
#19 llvm::SLPVectorizerPass::runImpl(...)
#20 llvm::SLPVectorizerPass::run(...)
If the loop has only the single PHI %r = phi float [ 0.0, %entry ], [ %b, %dead ], [ %b, %loop ], the assertion is
NodeA && "A must be in the tree" instead (same stack).
Expected: no crash. An incoming edge from an unreachable block needs no extract and should be skipped.
Notes: in a no-assertions build getNode() returns null and is then dereferenced, so release builds probably crash too
(not verified). The pre-pass computing ScalarToExtractBlock comes from #199962. Possible fix: continue on
!DT->isReachableFromEntry(InBB) in the incoming-block loop. cc @alexey-bataev
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)