diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -420,8 +420,12 @@ // Consider the operations within this block, ignoring the terminator if // requested. + bool hasTerminator = + !block->empty() && block->back().hasTrait(); auto range = llvm::make_range( - block->begin(), std::prev(block->end(), printBlockTerminator ? 0 : 1)); + block->begin(), + std::prev(block->end(), + (!hasTerminator || printBlockTerminator) ? 0 : 1)); for (Operation &op : range) print(&op); } @@ -2604,8 +2608,12 @@ } currentIndent += indentWidth; + bool hasTerminator = + !block->empty() && block->back().hasTrait(); auto range = llvm::make_range( - block->begin(), std::prev(block->end(), printBlockTerminator ? 0 : 1)); + block->begin(), + std::prev(block->end(), + (!hasTerminator || printBlockTerminator) ? 0 : 1)); for (auto &op : range) { print(&op); os << newLine;