-
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
[ir] Add RAII guards to IR Builder #2242
Conversation
taichi/ir/ir_builder.cpp
Outdated
if (location_ >= 0 && location_ < loop_->parent->size() && | ||
loop_->parent->statements[location_].get() == loop_) { | ||
// faster than set_insertion_point_to_after() | ||
builder_.set_insertion_point({loop_->parent, location_ + 1}); |
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.
At a high level, what does this condition mean? Something like "the number of statements before the loop statements stays the same"? I'm wondering in what scenario the number of statements changes - it sounds to me that when using the guard the users are only supposed to modify the statements within the guarded scope.
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.
Something like "the number of statements before the loop statements stays the same"?
Yes, exactly. This condition is to prevent some random things like
auto *loop = builder.create_range_for(zero, ten);
{
IRBuilder::LoopGuard _(builder, loop);
builder.create_add(...);
builder.set_insertion_point_to(some_random_insertion_point);
builder.create_add(...); // if some_random_insertion_point is in the same block but before the loop, it may cause trouble...
} // here I set the insertion point to a *defined* place -- the point after the loop.
or
auto *loop = builder.create_range_for(zero, ten);
builder.create_add(...); // this is inserted to the place after the loop so we can't locate the loop in O(1) then
{
IRBuilder::LoopGuard _(builder, loop);
builder.create_add(...);
}
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.
OK sounds good! Let's add these comments to the code and this PR is ready to merge!
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.
I set the insertion point to a defined place -- the point after the loop.
Could you add this comment to the header ? It describes the API's behavior.
Co-authored-by: Yuanming Hu <yuanming-hu@users.noreply.github.com>
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.
LGTM now, thanks!
taichi/ir/ir_builder.cpp
Outdated
if (location_ >= 0 && location_ < loop_->parent->size() && | ||
loop_->parent->statements[location_].get() == loop_) { | ||
// faster than set_insertion_point_to_after() | ||
builder_.set_insertion_point({loop_->parent, location_ + 1}); |
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.
OK sounds good! Let's add these comments to the code and this PR is ready to merge!
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.
Thanks! I left a few nits
taichi/ir/ir_builder.cpp
Outdated
if (location_ >= 0 && location_ < loop_->parent->size() && | ||
loop_->parent->statements[location_].get() == loop_) { | ||
// faster than set_insertion_point_to_after() | ||
builder_.set_insertion_point({loop_->parent, location_ + 1}); |
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.
I set the insertion point to a defined place -- the point after the loop.
Could you add this comment to the header ? It describes the API's behavior.
Related issue = #2193
This PR adds
IRBuilder::LoopGuard
andIRBuilder::IfGuard
to make writing control flows with IR Builder easier.These classes' objects automatically set the insertion point in O(1) time if the insertion point is never manually set in their lifetime by other functions.
[Click here for the format server]