From edcf4831a01c2d6485b8e882078bfb175900d209 Mon Sep 17 00:00:00 2001 From: Ye Kuang Date: Wed, 6 May 2020 08:28:18 +0900 Subject: [PATCH] [ir][refactor] Move legacy frontend constructs to frontend.h/cpp --- taichi/ir/frontend.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ taichi/ir/frontend.h | 36 ++++++++++++++++++++++++ taichi/ir/ir.cpp | 61 ---------------------------------------- taichi/ir/ir.h | 32 --------------------- 4 files changed, 100 insertions(+), 93 deletions(-) diff --git a/taichi/ir/frontend.cpp b/taichi/ir/frontend.cpp index 3ac2097d12006..f3a61e47db293 100644 --- a/taichi/ir/frontend.cpp +++ b/taichi/ir/frontend.cpp @@ -21,4 +21,68 @@ Expr global_new(DataType dt, std::string name) { return Expr::make(dt, id_expr->id); } +// Begin: legacy frontend constructs + +If::If(const Expr &cond) { + auto stmt_tmp = std::make_unique(cond); + stmt = stmt_tmp.get(); + current_ast_builder().insert(std::move(stmt_tmp)); +} + +If::If(const Expr &cond, const std::function &func) : If(cond) { + Then(func); +} + +If &If::Then(const std::function &func) { + auto _ = current_ast_builder().create_scope(stmt->true_statements); + func(); + return *this; +} + +If &If::Else(const std::function &func) { + auto _ = current_ast_builder().create_scope(stmt->false_statements); + func(); + return *this; +} + +For::For(const Expr &s, const Expr &e, const std::function &func) { + auto i = Expr(std::make_shared()); + auto stmt_unique = std::make_unique(i, s, e); + auto stmt = stmt_unique.get(); + current_ast_builder().insert(std::move(stmt_unique)); + auto _ = current_ast_builder().create_scope(stmt->body); + func(i); +} + +For::For(const Expr &i, + const Expr &s, + const Expr &e, + const std::function &func) { + auto stmt_unique = std::make_unique(i, s, e); + auto stmt = stmt_unique.get(); + current_ast_builder().insert(std::move(stmt_unique)); + auto _ = current_ast_builder().create_scope(stmt->body); + func(); +} + +For::For(const ExprGroup &i, + const Expr &global, + const std::function &func) { + auto stmt_unique = std::make_unique(i, global); + auto stmt = stmt_unique.get(); + current_ast_builder().insert(std::move(stmt_unique)); + auto _ = current_ast_builder().create_scope(stmt->body); + func(); +} + +While::While(const Expr &cond, const std::function &func) { + auto while_stmt = std::make_unique(cond); + FrontendWhileStmt *ptr = while_stmt.get(); + current_ast_builder().insert(std::move(while_stmt)); + auto _ = current_ast_builder().create_scope(ptr->body); + func(); +} + +// End: legacy frontend constructs + TLANG_NAMESPACE_END diff --git a/taichi/ir/frontend.h b/taichi/ir/frontend.h index ac176463a07a2..8634652431228 100644 --- a/taichi/ir/frontend.h +++ b/taichi/ir/frontend.h @@ -187,6 +187,42 @@ inline Expr AssumeInRange(const Expr &expr, return Expr::make(expr, base, low, high); } +// Begin: legacy frontend constructs + +class If { + public: + FrontendIfStmt *stmt; + + explicit If(const Expr &cond); + + If(const Expr &cond, const std::function &func); + + If &Then(const std::function &func); + + If &Else(const std::function &func); +}; + +class For { + public: + For(const Expr &i, + const Expr &s, + const Expr &e, + const std::function &func); + + For(const ExprGroup &i, + const Expr &global, + const std::function &func); + + For(const Expr &s, const Expr &e, const std::function &func); +}; + +class While { + public: + While(const Expr &cond, const std::function &func); +}; + +// End: legacy frontend constructs + #define Kernel(x) auto &x = get_current_program().kernel(#x) #define Assert(x) InsertAssert(#x, (x)) diff --git a/taichi/ir/ir.cpp b/taichi/ir/ir.cpp index f7e1c05ff372c..6024d7248866c 100644 --- a/taichi/ir/ir.cpp +++ b/taichi/ir/ir.cpp @@ -7,7 +7,6 @@ #include #include "taichi/ir/frontend.h" -#include "taichi/ir/frontend_ir.h" #include "taichi/ir/statements.h" TLANG_NAMESPACE_BEGIN @@ -910,36 +909,6 @@ std::unique_ptr ConstStmt::copy() { return std::make_unique(val); } -For::For(const Expr &s, const Expr &e, const std::function &func) { - auto i = Expr(std::make_shared()); - auto stmt_unique = std::make_unique(i, s, e); - auto stmt = stmt_unique.get(); - current_ast_builder().insert(std::move(stmt_unique)); - auto _ = current_ast_builder().create_scope(stmt->body); - func(i); -} - -For::For(const Expr &i, - const Expr &s, - const Expr &e, - const std::function &func) { - auto stmt_unique = std::make_unique(i, s, e); - auto stmt = stmt_unique.get(); - current_ast_builder().insert(std::move(stmt_unique)); - auto _ = current_ast_builder().create_scope(stmt->body); - func(); -} - -For::For(const ExprGroup &i, - const Expr &global, - const std::function &func) { - auto stmt_unique = std::make_unique(i, global); - auto stmt = stmt_unique.get(); - current_ast_builder().insert(std::move(stmt_unique)); - auto _ = current_ast_builder().create_scope(stmt->body); - func(); -} - OffloadedStmt::OffloadedStmt(OffloadedStmt::TaskType task_type) : OffloadedStmt(task_type, nullptr) { } @@ -1005,34 +974,4 @@ bool ContinueStmt::as_return() const { return false; } -If::If(const Expr &cond) { - auto stmt_tmp = std::make_unique(cond); - stmt = stmt_tmp.get(); - current_ast_builder().insert(std::move(stmt_tmp)); -} - -If::If(const Expr &cond, const std::function &func) : If(cond) { - Then(func); -} - -If &If::Then(const std::function &func) { - auto _ = current_ast_builder().create_scope(stmt->true_statements); - func(); - return *this; -} - -If &If::Else(const std::function &func) { - auto _ = current_ast_builder().create_scope(stmt->false_statements); - func(); - return *this; -} - -While::While(const Expr &cond, const std::function &func) { - auto while_stmt = std::make_unique(cond); - FrontendWhileStmt *ptr = while_stmt.get(); - current_ast_builder().insert(std::move(while_stmt)); - auto _ = current_ast_builder().create_scope(ptr->body); - func(); -} - TLANG_NAMESPACE_END diff --git a/taichi/ir/ir.h b/taichi/ir/ir.h index e80f17fe6ad5e..c316ae416c877 100644 --- a/taichi/ir/ir.h +++ b/taichi/ir/ir.h @@ -1291,19 +1291,6 @@ class PrintStmt : public Stmt { DEFINE_ACCEPT }; -class If { - public: - FrontendIfStmt *stmt; - - explicit If(const Expr &cond); - - If(const Expr &cond, const std::function &func); - - If &Then(const std::function &func); - - If &Else(const std::function &func); -}; - class ConstStmt : public Stmt { public: LaneAttribute val; @@ -1504,25 +1491,6 @@ inline void SLP(int v) { current_ast_builder().insert(Stmt::make(v)); } -class For { - public: - For(const Expr &i, - const Expr &s, - const Expr &e, - const std::function &func); - - For(const ExprGroup &i, - const Expr &global, - const std::function &func); - - For(const Expr &s, const Expr &e, const std::function &func); -}; - -class While { - public: - While(const Expr &cond, const std::function &func); -}; - Expr Var(const Expr &x); class VectorElement {