-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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] Simplify replace_statements and improve demote_dense_struct_fors #2335
Merged
+124
−184
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "taichi/ir/transforms.h" | ||
|
||
TLANG_NAMESPACE_BEGIN | ||
|
||
namespace irpass { | ||
|
||
bool replace_and_insert_statements( | ||
IRNode *root, | ||
std::function<bool(Stmt *)> filter, | ||
std::function<std::unique_ptr<Stmt>(Stmt *)> generator) { | ||
return transform_statements(root, std::move(filter), | ||
[&](Stmt *stmt, DelayedIRModifier *modifier) { | ||
modifier->replace_with(stmt, generator(stmt)); | ||
}); | ||
} | ||
|
||
bool replace_statements(IRNode *root, | ||
std::function<bool(Stmt *)> filter, | ||
std::function<Stmt *(Stmt *)> finder) { | ||
return transform_statements( | ||
root, std::move(filter), [&](Stmt *stmt, DelayedIRModifier *modifier) { | ||
auto existing_new_stmt = finder(stmt); | ||
irpass::replace_all_usages_with(root, stmt, existing_new_stmt); | ||
modifier->erase(stmt); | ||
}); | ||
} | ||
|
||
} // namespace irpass | ||
|
||
TLANG_NAMESPACE_END |
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
#include "taichi/ir/ir.h" | ||
#include "taichi/ir/statements.h" | ||
#include "taichi/ir/transforms.h" | ||
#include "taichi/ir/visitors.h" | ||
|
||
TLANG_NAMESPACE_BEGIN | ||
|
||
// Transform each filtered statement | ||
class StatementsTransformer : public BasicStmtVisitor { | ||
public: | ||
using BasicStmtVisitor::visit; | ||
|
||
StatementsTransformer( | ||
std::function<bool(Stmt *)> filter, | ||
std::function<void(Stmt *, DelayedIRModifier *)> transformer) | ||
: filter_(std::move(filter)), transformer_(std::move(transformer)) { | ||
allow_undefined_visitor = true; | ||
invoke_default_visitor = true; | ||
} | ||
|
||
void maybe_transform(Stmt *stmt) { | ||
if (filter_(stmt)) { | ||
transformer_(stmt, &modifier_); | ||
} | ||
} | ||
|
||
void preprocess_container_stmt(Stmt *stmt) override { | ||
maybe_transform(stmt); | ||
} | ||
|
||
void visit(Stmt *stmt) override { | ||
maybe_transform(stmt); | ||
} | ||
|
||
static bool run(IRNode *root, | ||
std::function<bool(Stmt *)> filter, | ||
std::function<void(Stmt *, DelayedIRModifier *)> replacer) { | ||
StatementsTransformer transformer(std::move(filter), std::move(replacer)); | ||
root->accept(&transformer); | ||
return transformer.modifier_.modify_ir(); | ||
} | ||
|
||
private: | ||
std::function<bool(Stmt *)> filter_; | ||
std::function<void(Stmt *, DelayedIRModifier *)> transformer_; | ||
DelayedIRModifier modifier_; | ||
}; | ||
|
||
namespace irpass { | ||
|
||
bool transform_statements( | ||
IRNode *root, | ||
std::function<bool(Stmt *)> filter, | ||
std::function<void(Stmt *, DelayedIRModifier *)> transformer) { | ||
return StatementsTransformer::run(root, std::move(filter), | ||
std::move(transformer)); | ||
} | ||
|
||
} // namespace irpass | ||
|
||
TLANG_NAMESPACE_END |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the above comment, how is |s|'s defining stmt handled in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|s|'s defining stmt is erased here.