-
Notifications
You must be signed in to change notification settings - Fork 170
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
Move insert point after PHI nodes. #794
Conversation
@@ -0,0 +1,46 @@ | |||
; RUN: %opt -load %pass -souper -S -stats -o - %s 2>&1 | %FileCheck %s |
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 was having a bit of a hard time coming up with good tests. If I could tell souper to just run once, I think I could create a better test (that checks we replace a PHI instruction with an add, for example).
Other than that, I can't come up with an input program where the PHI node is replaced with a non-phi node, and the whole thing isn't optimized away at some point later.
396ffed
to
4fc7c64
Compare
ahhh... thanks for looking into this! we just haven't had time. I'll look at the code tomorrow, but I think for now it's sufficient testing to include the original bug trigger as a regression test. we'll get plenty more testing using our existing fuzzers. |
@zhengyangl do you have time to review this request? |
lib/Codegen/Codegen.cpp
Outdated
while (!InsertPoint->isTerminator() && | ||
isa<PHINode>(InsertPoint->getNextNode())) | ||
InsertPoint = InsertPoint->getNextNode(); | ||
if (InsertPoint->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.
Hi @zoecarver ,
Thanks for the patch! One concern: as stated in [1], after calling IRBuilder.SetInsertPoint(Instruction *I), the new instructions will be inserted before instruction I. In this case, InsertPoint should be the instruction just next to the last phi in that basic block, right? However, this code would make InsertPoint the last PHI and Instruction could still possibly be inserted between PHI nodes.
[1] https://llvm.org/doxygen/classllvm_1_1IRBuilderBase.html#ae72654261eee259da46249dc919b029c
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.
You're right. Fixed.
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.
Since IR builder is inserting before the ReplacedInst, it should work even when ReplacedInst is a terminator, right? Therefore, checking the terminator is not necessary here.
And Souper won't create Phis, the check I->K != Inst::Kind::Phi
is not necessary here.
I think
if (ReplacedInst) {
Instruction *InsertPoint = ReplacedInst;
while (isa<PHINode>(InsertPoint) {
InsertPoint = InsertPoint->getNextNode();
}
Builder.SetInsertPoint(InsertPoint);
}
should just do the work.
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.
Good point. I'll update it with your suggestion. I think it would be good to keep the isa<PHINode>(ReplacedInst->getNextNode())
check so that we don't unnecessarily update the InsertPoint
, though.
4fc7c64
to
8fff97f
Compare
If replacing a PHI node with another instruction, make sure the insertion point is after the last PHI node in that block. This prevents the assertion described in google#779.
8fff97f
to
e52fa7b
Compare
Hi @zoecarver, one final note: if some replacement has Phi for ReplacedInst and a non-phi for the replacedInst->getNextNode, then we'll never reach the true branch, and by the definition of setInsertPoint() the new instruction would be inserted before the ReplaceInst (a phi node), which will cause trouble since we're trying to insert a non-phi instruction in the middle of the phi chunk.
|
Sorry for the delayed response. My reasoning is that |
hey @zhengyangl are you OK with this patch now? it looks fine to me |
LGTM |
thanks for the bugfix! |
Of course. Thanks (both of you) for reviewing/merging! |
If replacing a PHI node with another instruction, make sure the insertion point is after the last PHI node in that block. This prevents the assertion described in google#779.
If replacing a PHI node with another instruction, make sure the insertion point is after the last PHI node in that block. This prevents the assertion described in #779.