diff --git a/taichi/ir/ir.cpp b/taichi/ir/ir.cpp
index 5fb2fdf644397..d30674c339a96 100644
--- a/taichi/ir/ir.cpp
+++ b/taichi/ir/ir.cpp
@@ -757,7 +757,7 @@ void Block::replace_with(Stmt *old_statement,
     }
   }
   TI_ASSERT(location != -1);
-  if (replace_usages)
+  if (replace_usages && !new_statements.stmts.empty())
     old_statement->replace_with(new_statements.back().get());
   trash_bin.push_back(std::move(statements[location]));
   if (new_statements.size() == 1) {