Issue 241478
Summary [IndVarSimplify] `!NodePtr->isKnownSentinel()` in sinkUnusedInvariants: catchswitch exit block
Labels
Assignees
Reporter arie2026
    `sinkUnusedInvariants` moves a sinkable preheader instruction to `ExitBlock->getFirstInsertionPt()`. If the unique exit
block is a `catchswitch` block, that is `end()`, and `Instruction::moveBefore(BasicBlock::iterator)` dereferences the
sentinel.

Reproducer (`opt -passes=indvars`, no target flags; also reproduces on 5e457cda8d5a and f2808fc79):
```llvm
define i32 @f(i32 %a) personality ptr null {
entry:
  %inv = add i32 %a, 1
  br label %loop

loop:
  invoke void @g()
          to label %loop unwind label %cs

cs:
  %cs1 = catchswitch within none [label %handler] unwind to caller

handler:
  %cp = catchpad within %cs1 [ptr null, i32 0, ptr null]
  ret i32 %inv
}
declare void @g()
```

```
$ opt -passes=indvars -disable-output test.ll
opt: llvm/include/llvm/ADT/ilist_iterator.h:322: reference llvm::ilist_iterator_w_bits<...>::operator*() const [...]: Assertion `!NodePtr->isKnownSentinel()' failed.
2.      Running pass "loop(indvars)" on function "f"
#11 <unsymbolized; by address inside llvm::Instruction::moveBefore(BasicBlock::iterator)>
#12 (anonymous namespace)::IndVarSimplify::run(llvm::Loop*)      ; sinkUnusedInvariants inlined
#13 llvm::IndVarSimplifyPass::run(...)
#14 llvm::detail::PassModel<llvm::Loop, llvm::IndVarSimplifyPass, ...>::runImpl(...)
#15 llvm::PassManager<llvm::Loop, ...>::runSinglePass<...>(...)
#16 llvm::PassManager<llvm::Loop, ...>::runWithoutLoopNestPasses(...)
#17 llvm::PassManager<llvm::Loop, ...>::run(...)
#19 llvm::FunctionToLoopPassAdaptor::run(...)
```

Expected: no crash. Nothing should be sunk into a block that has no insertion point, so `%inv` stays in the preheader.

Notes: #135205 (7f107c301963) replaced `moveBefore(*ExitBlock, InsertPt)`, which accepts `end()`, with
`I.moveBefore(ExitBlock->getFirstInsertionPt())`. A possible fix is to bail out when
`ExitBlock->getFirstInsertionPt() == ExitBlock->end()`. Same assertion text as #63318, but a different site
(`WidenIV::widenIVUse` there). `-O1`/`-O2` on this input don't crash. cc @srpande

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