-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
[mlir][IR] Insert operations before SingleBlock
's terminator
#65959
Conversation
@llvm/pr-subscribers-mlir ChangesAvoid member functions duplication caused by
|
This causes build failures if the member functions are used. |
SingleBlockImplicitTerminator
member funionsSingleBlockImplicitTerminator
member functions
Change `SingleBlock::{insert,push_back}` to avoid inserting the argument operation after the block's terminator. This allows removing `SingleBlockImplicitTerminator`'s functions with the same name. Define `Block::hasTerminator` checking whether the block has a terminator operation. Signed-off-by: Victor Perez <victor.perez@codeplay.com>
8857ede
to
6229d53
Compare
SingleBlockImplicitTerminator
member functionsSingleBlock
's terminator
@matthias-springer updated according to Discourse conversation |
/// Check whether this block has a terminator. | ||
bool Block::hasTerminator() { | ||
return !empty() && back().mightHaveTrait<OpTrait::IsTerminator>(); | ||
} |
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.
The API is misnamed: this should be "mightHaveTerminator", as the trait check indicates.
Block *body = getBody(); | ||
// Insert op before the block's terminator if it has one | ||
if (insertPt == body->end() && body->hasTerminator()) | ||
insertPt = Block::iterator(body->getTerminator()); |
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.
This is not a safe: if there is an unregistered operation at the end of the current block it'll insert before.
I don't see a safe way of doing this actually.
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.
Previously, (before 0ac21e6, which made push_back
ambiguous) this was actually (somewhat?) safe. SingleBlockImplicitTerminator<OpTy>
must be instantiated with an op type, which means that the terminator op must be known (registered?).
I'm wondering if it's better to just always insert at the end of the block. It would simplify the API: no special handling for terminators, it behaves like a vector::push_back
.
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.
Just a nit: the terminator must be known when the verifier is passing, this may not be the case in the middle of a transformation or while building the operation.
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.
Changing the definition of hasTerminator
so that it checks the last operation is definitely a terminator and keeping current semantics should work, right? (of course reverting the change to the assertion in this PR)
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.
It's not clear to me that it'll fix it: if you can't be sure if the last op is a terminator or not, I don't quite see how you can decide to insert before or after it?
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.
My point is: if it is definitely a terminator, do not break the code and insert before it; keep vector::push_back
semantics otherwise
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.
This depends if the operation is registered in the MLIRContext, so we're creating a different behavior here, and avoiding this is the reason the "mighHaveTrait" exists.
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.
So do you guys think we should just be consistent with vector::push_back
semantics and just blame the API user if the code is broken? I'm fine with that, just wanted to keep former semantics.
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.
The push_back approach has the advantage of being consistent in terms of behavior (there is no "gotcha" involved). If there is an API misuse here it'll blow up immediately and consistently.
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.
Makes total sense. I'll push a follow-up PR.
…65959) Change `SingleBlock::{insert,push_back}` to avoid inserting the argument operation after the block's terminator. This allows removing `SingleBlockImplicitTerminator`'s functions with the same name. Define `Block::hasTerminator` checking whether the block has a terminator operation. Signed-off-by: Victor Perez <victor.perez@codeplay.com>
Change
SingleBlock::{insert,push_back}
to avoid inserting the argument operation after the block's terminator. This allows removingSingleBlockImplicitTerminator
's functions with the same name.Define
Block::hasTerminator
checking whether the block has a terminator operation.Signed-off-by: Victor Perez victor.perez@codeplay.com