Skip to content

Commit

Permalink
[ir] [refactor] BasicStmtVisitor includes Frontend sstatements (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate authored Apr 9, 2020
1 parent 69bf35f commit b5283a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
21 changes: 7 additions & 14 deletions taichi/analysis/detect_for_with_break.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class DetectForWithBreak : public BasicStmtVisitor {
DetectForWithBreak(IRNode *root) : root(root) {
}

void visit(FrontendBreakStmt *stmt) override {
TI_ASSERT_INFO(loop_stack.size() != 0, "break statement out of loop scope");
auto loop = loop_stack.back();
if (loop->is<FrontendForStmt>())
fors_with_break.insert(loop);
}

void visit(FrontendWhileStmt *stmt) override {
loop_stack.push_back(stmt);
stmt->body->accept(this);
Expand All @@ -27,20 +34,6 @@ class DetectForWithBreak : public BasicStmtVisitor {
loop_stack.pop_back();
}

void visit(FrontendIfStmt *stmt) override {
if (stmt->true_statements)
stmt->true_statements->accept(this);
if (stmt->false_statements)
stmt->false_statements->accept(this);
}

void visit(FrontendBreakStmt *stmt) override {
TI_ASSERT_INFO(loop_stack.size() != 0, "break statement out of loop scope");
auto loop = loop_stack.back();
if (loop->is<FrontendForStmt>())
fors_with_break.insert(loop);
}

std::unordered_set<Stmt *> run() {
root->accept(this);
return fors_with_break;
Expand Down
15 changes: 15 additions & 0 deletions taichi/ir/basic_stmt_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ void BasicStmtVisitor::visit(OffloadedStmt *stmt) {
stmt->body->accept(this);
}

void BasicStmtVisitor::visit(FrontendWhileStmt *stmt) {
stmt->body->accept(this);
}

void BasicStmtVisitor::visit(FrontendForStmt *stmt) {
stmt->body->accept(this);
}

void BasicStmtVisitor::visit(FrontendIfStmt *stmt) {
if (stmt->true_statements)
stmt->true_statements->accept(this);
if (stmt->false_statements)
stmt->false_statements->accept(this);
}

TLANG_NAMESPACE_END
6 changes: 6 additions & 0 deletions taichi/ir/visitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class BasicStmtVisitor : public IRVisitor {
void visit(StructForStmt *for_stmt) override;

void visit(OffloadedStmt *stmt) override;

void visit(FrontendWhileStmt *stmt) override;

void visit(FrontendForStmt *stmt) override;

void visit(FrontendIfStmt *stmt) override;
};

TLANG_NAMESPACE_END

0 comments on commit b5283a9

Please sign in to comment.