[SLPVectorizer] Assertion "trying to erase instruction with users" in horizontal reduction at -O2; release builds segfault
https://godbolt.org/z/KsoKcqxEf (-O2, x86_64)
int a = 0, c = 0;
int d[8][8] = {0, 5, 10, 15, 19, 23, 27, 30, 1, 6, 11, 16, 20, 20,
20, 25, 29, 34, 34, 37, 16, 13, 0, 0, 0, 0, 0, 0};
int main(void)
{
int e;
for (int b = 0; b < 4; b++) {
for (e = 0; e < 8; e++)
d[b][e] = 100000 * b + 1000 + 100 * e;
d[7][2] += b;
d[7][1] += c;
d[7][0] += a + b + c;
for (e = 0; e < 8; e++)
d[6][e] -= d[7][e] - a;
}
a += d[2][7] - d[3][7] - d[4][7] - d[5][7] - d[6][7];
for (e = 0; e < 8; e++)
if (e != 1)
a += d[6][e] - d[2][e] - d[7][e] - d[3][e] - d[4][e] + d[2][0];
for (e = 0; e < 8; e++)
if (e != 0)
a += d[7][e] - d[5][e] - d[4][e] - d[0][e] + d[7][0];
for (e = 0; e < 8; e++)
if (e != 1)
a += d[7][e];
for (e = 0; e < 8; e++)
if (d[7][e] != 6)
a += d[7][e];
for (e = 0; e < 8; e++)
if (d[7][e] != 7)
a += d[7][e];
for (e = 0; e < 8; e++)
if (d[7][e] != 7)
a += d[7][e];
return a;
}
clang: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:2307: void llvm::slpvectorizer::BoUpSLP::removeInstructionsAndOperands(llvm::ArrayRef<BlockT*>, llvm::ArrayRef<{anonymous}::ReductionVectorPart>) [with T = llvm::Instruction]: Assertion `(I->use_empty() || all_of(I->uses(), [&](Use &U) { return isDeleted( cast<Instruction>(U.getUser())); })) && "trying to erase instruction with users."' failed.
#10 llvm::slpvectorizer::BoUpSLP::removeInstructionsAndOperands<llvm::Instruction>(...)
#11 llvm::slpvectorizer::BoUpSLP::vectorizeTree(...)
#12 (anonymous namespace)::HorizontalReduction::tryToReduce(...)
#14 llvm::SLPVectorizerPass::vectorizeHorReduction(...)
#15 llvm::SLPVectorizerPass::vectorizeRootInstruction(...)
#16 llvm::SLPVectorizerPass::vectorizeChainsInBlock(...)
Valid code, no diagnostics. -O1, -O3 and -O2 -march=x86-64-v3 compile it fine.
Release builds crash too: release trunk and 23.1.0 segfault in the same pass (Running pass "slp-vectorizer" on function "main"), in HorizontalReduction::matchAssociativeReduction -> generateKeySubkey. 22.1.0 compiles it (https://godbolt.org/z/csY59drdh).
IR reproducer: the module just before SLP, reduced to 76 lines. opt -passes=slp-vectorizer hits the same assertion on it, and the release opt segfaults the same way as the release clang (https://godbolt.org/z/K14W8o3d5). I can attach it as a file.
Found by a fuzzer (generated C program), reduced by hand.
Command: clang --target=x86_64-unknown-linux-gnu -O2 -c repro.c
Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): the assertion above. Release trunk f4085066128a: SIGSEGV in SLPVectorizer.
History: 21.1.0 and 22.1.0 (assertions builds) and 22.1.0 (release build) compile it. 23.1.0 hits the same assertion in the assertions build and segfaults in the release build, as trunk does. Not bisected.
Classification: crash on valid code. The program is UBSan-clean; clang 22.1 and GCC 16.2 both run it to exit status 188.
Related: #204814, #205886 and #218709 have the same assertion; all were closed as completed, on 2026-06-19, 2026-06-25 and 2026-08-25. This input still crashes trunk after those fixes, and #218709's reproducer does not crash 23.1.0. No open issue with this assertion found (searched "trying to erase instruction with users", removeInstructionsAndOperands, generateKeySubkey, matchAssociativeReduction).