forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opt] Add ImmediateIRModifier to provide amortized constant-time repl…
…ace_usages_with() (taichi-dev#7001) Issue: taichi-dev#6933 ### Brief Summary `ImmediateIRModifier` aims at replacing `Stmt::replace_usages_with`, which visits the whole tree for a single replacement. `ImmediateIRModifier` is currently associated with a pass, visits the whole tree once at the beginning of that pass, and performs a single replacement with amortized constant time. It is now used in two most recent passes, `eliminate_immutable_local_vars` and `scalarize`. More passes can be modified to leverage it in the future. After this PR, the profiling result of the script in taichi-dev#6933 shows that the time of `eliminate_immutable_local_vars` reduces from `0.956 s` to `0.162 s`, the time of `scalarize` reduces from `3.510 s` to `0.478 s`, and the total time of `compile` reduces from `8.550 s` to `4.696 s`. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c62414f
commit 26b81e7
Showing
6 changed files
with
143 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "taichi/ir/ir.h" | ||
#include "taichi/ir/statements.h" | ||
#include "taichi/ir/transforms.h" | ||
#include "taichi/ir/visitors.h" | ||
|
||
namespace taichi::lang { | ||
|
||
class GatherStatementUsages : public BasicStmtVisitor { | ||
private: | ||
using BasicStmtVisitor::visit; | ||
|
||
// maps a stmt to all its usages <stmt, operand> | ||
std::unordered_map<Stmt *, std::vector<std::pair<Stmt *, int>>> stmt_usages_; | ||
|
||
public: | ||
explicit GatherStatementUsages() { | ||
invoke_default_visitor = true; | ||
} | ||
|
||
void default_visit(Stmt *stmt) { | ||
auto ops = stmt->get_operands(); | ||
for (int i = 0; i < ops.size(); i++) { | ||
auto &op = ops[i]; | ||
if (op != nullptr) { | ||
stmt_usages_[op].push_back({stmt, i}); | ||
} | ||
} | ||
} | ||
|
||
void visit(Stmt *stmt) override { | ||
default_visit(stmt); | ||
} | ||
|
||
void preprocess_container_stmt(Stmt *stmt) override { | ||
default_visit(stmt); | ||
} | ||
|
||
static std::unordered_map<Stmt *, std::vector<std::pair<Stmt *, int>>> run( | ||
IRNode *node) { | ||
GatherStatementUsages pass; | ||
node->accept(&pass); | ||
return pass.stmt_usages_; | ||
} | ||
}; | ||
|
||
namespace irpass::analysis { | ||
|
||
std::unordered_map<Stmt *, std::vector<std::pair<Stmt *, int>>> | ||
gather_statement_usages(IRNode *root) { | ||
return GatherStatementUsages::run(root); | ||
} | ||
|
||
} // namespace irpass::analysis | ||
|
||
} // namespace taichi::lang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters