diff --git a/mlir/lib/IR/Block.cpp b/mlir/lib/IR/Block.cpp --- a/mlir/lib/IR/Block.cpp +++ b/mlir/lib/IR/Block.cpp @@ -170,20 +170,21 @@ void Block::eraseArgument(unsigned index, bool updatePredTerms) { assert(index < arguments.size()); + // If requested, update predecessors. We do this first since this block might + // be a predecessor of itself and use this block argument as a successor + // operand. + if (updatePredTerms) { + // Erase this argument from each of the predecessor's terminator. + for (auto predIt = pred_begin(), predE = pred_end(); predIt != predE; + ++predIt) { + auto *predTerminator = (*predIt)->getTerminator(); + predTerminator->eraseSuccessorOperand(predIt.getSuccessorIndex(), index); + } + } + // Delete the argument. arguments[index].destroy(); arguments.erase(arguments.begin() + index); - - // If we aren't updating predecessors, there is nothing left to do. - if (!updatePredTerms) - return; - - // Erase this argument from each of the predecessor's terminator. - for (auto predIt = pred_begin(), predE = pred_end(); predIt != predE; - ++predIt) { - auto *predTerminator = (*predIt)->getTerminator(); - predTerminator->eraseSuccessorOperand(predIt.getSuccessorIndex(), index); - } } /// Insert one value to the given position of the argument list. The existing