From 2dc4b5047db794fa9acc9229910cbd57bfadd00d Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Fri, 29 May 2020 10:46:21 +0800 Subject: [PATCH 1/4] [skip ci] [bug] [ir] fix replace_with segf when stmts.size() == 0 --- taichi/transforms/lower_ast.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/taichi/transforms/lower_ast.cpp b/taichi/transforms/lower_ast.cpp index c066f9a5fe1fe..8dfc87d054fdd 100644 --- a/taichi/transforms/lower_ast.cpp +++ b/taichi/transforms/lower_ast.cpp @@ -331,7 +331,10 @@ class LowerAST : public IRVisitor { auto fctx = make_flatten_ctx(); expr->flatten(&fctx); stmt->eval_expr.cast()->stmt_ptr = stmt->expr->stmt; - stmt->parent->replace_with(stmt, std::move(fctx.stmts)); + if (fctx.stmts.stmts.size()) + stmt->parent->replace_with(stmt, std::move(fctx.stmts)); + else + stmt->parent->erase(stmt); throw IRModified(); } From 251225787f6281b93adcdbfaa5a6a8d2ae05ddd6 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Fri, 29 May 2020 10:55:11 +0800 Subject: [PATCH 2/4] [skip ci] wewewe --- taichi/transforms/lower_ast.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/taichi/transforms/lower_ast.cpp b/taichi/transforms/lower_ast.cpp index 8dfc87d054fdd..cbac235a253a4 100644 --- a/taichi/transforms/lower_ast.cpp +++ b/taichi/transforms/lower_ast.cpp @@ -331,10 +331,7 @@ class LowerAST : public IRVisitor { auto fctx = make_flatten_ctx(); expr->flatten(&fctx); stmt->eval_expr.cast()->stmt_ptr = stmt->expr->stmt; - if (fctx.stmts.stmts.size()) - stmt->parent->replace_with(stmt, std::move(fctx.stmts)); - else - stmt->parent->erase(stmt); + stmt->parent->replace_with(stmt, std::move(fctx.stmts), false); throw IRModified(); } From 01f52d5fc33369a75896c3884c6c8c3e0f29e00f Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sat, 30 May 2020 16:19:01 +0800 Subject: [PATCH 3/4] Apply @xumingkuan's review --- taichi/ir/ir.cpp | 2 +- taichi/transforms/lower_ast.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/taichi/ir/ir.cpp b/taichi/ir/ir.cpp index 5fb2fdf644397..8a9f3ef9e2e58 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.empty()) old_statement->replace_with(new_statements.back().get()); trash_bin.push_back(std::move(statements[location])); if (new_statements.size() == 1) { diff --git a/taichi/transforms/lower_ast.cpp b/taichi/transforms/lower_ast.cpp index cbac235a253a4..c066f9a5fe1fe 100644 --- a/taichi/transforms/lower_ast.cpp +++ b/taichi/transforms/lower_ast.cpp @@ -331,7 +331,7 @@ class LowerAST : public IRVisitor { auto fctx = make_flatten_ctx(); expr->flatten(&fctx); stmt->eval_expr.cast()->stmt_ptr = stmt->expr->stmt; - stmt->parent->replace_with(stmt, std::move(fctx.stmts), false); + stmt->parent->replace_with(stmt, std::move(fctx.stmts)); throw IRModified(); } From 3d25ae204b238cb132e42159a1648ffac40ed361 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sat, 30 May 2020 16:25:54 +0800 Subject: [PATCH 4/4] [skip ci] fix --- taichi/ir/ir.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taichi/ir/ir.cpp b/taichi/ir/ir.cpp index 8a9f3ef9e2e58..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 && !new_statements.empty()) + 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) {