From f0d4e70f26d38108c77e2a72ab3f6bc355d6e026 Mon Sep 17 00:00:00 2001 From: Uday Bondhugula Date: Fri, 1 Feb 2019 13:18:20 -0800 Subject: [PATCH] Fix Block::getNumSuccessors() - getTerminator() on a block can return nullptr; moreover, blocks that are improperly constructed/transformed by utilities/passes may not have terminators even for the top-level blocks PiperOrigin-RevId: 232025963 --- mlir/lib/IR/Block.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mlir/lib/IR/Block.cpp b/mlir/lib/IR/Block.cpp index 24fa2038c97d..f523f66565da 100644 --- a/mlir/lib/IR/Block.cpp +++ b/mlir/lib/IR/Block.cpp @@ -189,7 +189,12 @@ bool Block::hasNoPredecessors() const { return pred_begin() == pred_end(); } // Indexed successor access. unsigned Block::getNumSuccessors() const { - return getTerminator()->getNumSuccessors(); + if (auto *terminator = getTerminator()) { + return terminator->getNumSuccessors(); + } + assert(getParent() && "top-level block with no terminator"); + // Blocks inside 'for'/'if' instructions don't have successors. + return 0; } Block *Block::getSuccessor(unsigned i) {