Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Opt] Make irpass::replace_all_usages_with bottom-up #1789

Merged
merged 3 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions taichi/ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ Stmt *Stmt::insert_after_me(std::unique_ptr<Stmt> &&new_stmt) {
}

void Stmt::replace_with(Stmt *new_stmt) {
auto root = get_ir_root();
irpass::replace_all_usages_with(root, this, new_stmt);
irpass::replace_all_usages_with(nullptr, this, new_stmt);
}

void Stmt::replace_with(VecStatement &&new_statements, bool replace_usages) {
Expand Down
7 changes: 7 additions & 0 deletions taichi/transforms/ir_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class IRPrinter : public IRVisitor {
}

static void run(IRNode *node, std::string *output) {
if (node == nullptr) {
TI_WARN("IRPrinter: Printing nullptr.");
if (output) {
*output = std::string();
}
return;
}
auto p = IRPrinter(output);
p.print("kernel {{");
node->accept(&p);
Expand Down
1 change: 1 addition & 0 deletions taichi/transforms/offload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Offloader {

for (int i = 0; i < (int)root_statements.size(); i++) {
auto &stmt = root_statements[i];
// Note that stmt->parent is root_block, which doesn't contain stmt now.
if (auto s = stmt->cast<RangeForStmt>(); s && !s->strictly_serialized) {
assemble_serial_statements();
auto offloaded =
Expand Down
41 changes: 30 additions & 11 deletions taichi/transforms/statement_usage_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ TLANG_NAMESPACE_BEGIN
// Replace all usages statement A with a new statement B.
// Note that the original statement A is NOT replaced.
class StatementUsageReplace : public IRVisitor {
// The reason why we don't use BasicStmtVisitor is we don't want to go into
// FrontendForStmt.
public:
Stmt *old_stmt, *new_stmt;

Expand All @@ -16,17 +18,16 @@ class StatementUsageReplace : public IRVisitor {
invoke_default_visitor = true;
}

void visit(Stmt *stmt) {
// if (stmt->may_have_operand(old_stmt))
void visit(Stmt *stmt) override {
stmt->replace_operand_with(old_stmt, new_stmt);
}

void visit(WhileStmt *stmt) {
void visit(WhileStmt *stmt) override {
stmt->replace_operand_with(old_stmt, new_stmt);
stmt->body->accept(this);
}

void visit(IfStmt *if_stmt) {
void visit(IfStmt *if_stmt) override {
if_stmt->replace_operand_with(old_stmt, new_stmt);
if (if_stmt->true_statements)
if_stmt->true_statements->accept(this);
Expand All @@ -35,34 +36,52 @@ class StatementUsageReplace : public IRVisitor {
}
}

void visit(Block *stmt_list) {
void visit(Block *stmt_list) override {
for (auto &stmt : stmt_list->statements) {
stmt->accept(this);
}
}

void visit(RangeForStmt *stmt) {
void visit(RangeForStmt *stmt) override {
stmt->replace_operand_with(old_stmt, new_stmt);
stmt->body->accept(this);
}

void visit(StructForStmt *stmt) {
void visit(StructForStmt *stmt) override {
stmt->body->accept(this);
}

void visit(OffloadedStmt *stmt) {
void visit(OffloadedStmt *stmt) override {
stmt->all_blocks_accept(this);
}

static void run(IRNode *node, Stmt *old_stmt, Stmt *new_stmt) {
StatementUsageReplace inst(old_stmt, new_stmt);
node->accept(&inst);
static void run(IRNode *root, Stmt *old_stmt, Stmt *new_stmt) {
StatementUsageReplace replacer(old_stmt, new_stmt);
if (root != nullptr) {
// If root is specified, simply traverse the root.
root->accept(&replacer);
return;
}

// statements inside old_stmt->parent
TI_ASSERT(old_stmt->parent != nullptr);
old_stmt->parent->accept(&replacer);
auto current_block = old_stmt->parent->parent;

// statements outside old_stmt->parent: bottom-up
while (current_block != nullptr) {
for (auto &stmt : current_block->statements) {
stmt->replace_operand_with(old_stmt, new_stmt);
}
current_block = current_block->parent;
}
}
};

namespace irpass {

void replace_all_usages_with(IRNode *root, Stmt *old_stmt, Stmt *new_stmt) {
TI_AUTO_PROF;
StatementUsageReplace::run(root, old_stmt, new_stmt);
}

Expand Down