Issue 241474
Summary [LoopVectorize] `isa<> used on a null pointer` in narrowToSingleScalarRecipes
Labels new issue
Assignees
Reporter arie2026
    The absolute-difference partial-reduction transform rewrites `abs(sub(sext X, sext Y))` into new
`freeze`/`smax`/`smin`/`sub` widen recipes that have no underlying IR instruction. When `X`/`Y` are loop-invariant,
`narrowToSingleScalarRecipes` treats `freeze X` as single-scalar and calls `getUnderlyingInstr()` on it, which is
`cast<Instruction>(nullptr)`.

Reproducer (`opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=loop-vectorize`, `+avx` too; also reproduces on 5e457cda8d5a and f2808fc79):
```llvm
define i64 @f(i32 %x, i32 %y) {
entry:
  br label %loop

loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %sum = phi i64 [ 0, %entry ], [ %sum.next, %loop ]
  %ext.x = sext i32 %x to i64
  %ext.y = sext i32 %y to i64
  %sub = sub i64 %ext.x, %ext.y
  %abs = call i64 @llvm.abs.i64(i64 %sub, i1 false)
  %sum.next = add i64 %abs, %sum
  %iv.next = add i64 %iv, 1
  %ec = icmp eq i64 %iv, 8000
  br i1 %ec, label %exit, label %loop

exit:
  ret i64 %sum.next
}
declare i64 @llvm.abs.i64(i64, i1 immarg)
```

```
$ opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=loop-vectorize -disable-output test.ll
opt: llvm/include/llvm/Support/Casting.h:109: static bool llvm::isa_impl_cl<llvm::Instruction, const llvm::Value *>::doit(const From *) [To = llvm::Instruction, From = const llvm::Value *]: Assertion `Val && "isa<> used on a null pointer"' failed.
2.      Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "f"
#11 narrowToSingleScalarRecipes(llvm::VPlan&)
#12 llvm::VPlanTransforms::runPass<...>(...)
#13 llvm::VPlanTransforms::optimize(llvm::VPlan&)
#14 llvm::VPlanTransforms::runPass<...>(...)
#15 llvm::LoopVectorizationPlanner::buildVPlans(...)
#16 llvm::LoopVectorizationPlanner::plan(...)
#17 llvm::LoopVectorizePass::processLoop(llvm::Loop*)
#18 llvm::LoopVectorizePass::runImpl(llvm::Function&)
#19 llvm::LoopVectorizePass::run(...)
```

Expected: no crash. Either vectorize (with or without the partial reduction), or skip the narrowing for recipes that
have no underlying instruction.

Notes: `or`/`xor`/`and` reductions and `zext` crash too. Loading `x`/`y` in the loop avoids it, and so do
AArch64, no-AVX x86 and AVX-512. Not #213601: that one has the same assertion but crashes in
`preparePlanForEpilogueVectorLoop` and is closed. The operand-less recipes come from #188043 (not bisected further).

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