-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ValueTracking] Fix "getOperand() out of range!" assertion crash #87482
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
Conversation
Return instruction can also return void in LLVM IR. In such cases the number of operands are zero. So, call to getOperand(0) will result in an assertion failure as well. Instead of using getOperand(0), we use getReturnValue() as it graciously return nullptr and prevents a crash. This bug was triggered in inliner, and this patch also adds a testcase. This patch fixes the issue llvm#87441.
@@ -7378,7 +7378,7 @@ static bool handleGuaranteedWellDefinedOps(const Instruction *I, | |||
} | |||
case Instruction::Ret: | |||
if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) && | |||
Handle(I->getOperand(0))) | |||
Handle(cast<ReturnInst>(I)->getReturnValue())) |
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.
Calling Handle with a nullptr seems surprising here. I'd add a clause to the if condition to avoid calling Handle instead.
Not sure this is really the right fix. If a function has a noundef attribute on the return value, then it cannot have a void return. This looks more like a context problem, in that we're dealing with two different functions during inlining. Maybe it's an acceptable workaround, but I'm not convinced it really addresses all issues that can arise from this. |
I suspected this, but the LangRef for noundef isn't really clear on this aspect: https://llvm.org/docs/LangRef.html. It's not mentioned anywhere that noundef attribute on the return value means it can't have a void return.
I am not sure, what more issue can this cause ? |
Yeah, this is not spelled out in LangRef, but it's verified here: llvm-project/llvm/lib/IR/Attributes.cpp Lines 2067 to 2071 in 4ae33c5
|
I see, this fix is just a workaround. noundef should not apply to the inlined function as such, as in atleast for ret instructions. It is mostly happening because, somehow we are not DCE'ing ret void instructions, and before that we call simplifyinstruction
|
@nikic okay I did some more analysis, so what is happening is. PruningFunctionCloner clones the basic blocks from function to be inlined into the caller function. While doing so, it handles terminator instructions like Switch/Branch, but it doesn't specially handle the Ret ones (
It becomes especially tricky as to how to handle it, given this return void statement has no successor. |
ping @nikic |
@nikic ping! |
@vedantparanjape-amd The issue should already be fixed by #90489 -- do you still see problems? |
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.
Already fixed by #90489, removing this from the review queue...
Return instruction can also return void in LLVM IR. In such cases the number of operands are zero. So, call to getOperand(0) will result in an assertion failure as well.
Instead of using getOperand(0), we use getReturnValue() as it graciously return nullptr and prevents a crash. This bug was triggered in inliner, and this patch also adds a testcase. This patch fixes the issue #87441.