Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

vedantparanjape-amd
Copy link
Member

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.

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()))
Copy link
Collaborator

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.

@nikic
Copy link
Contributor

nikic commented Apr 5, 2024

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.

@vedantparanjape-amd
Copy link
Member Author

vedantparanjape-amd commented Apr 6, 2024

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.

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.

Maybe it's an acceptable workaround, but I'm not convinced it really addresses all issues that can arise from this.

I am not sure, what more issue can this cause ?

@nikic
Copy link
Contributor

nikic commented Apr 9, 2024

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.

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.

Yeah, this is not spelled out in LangRef, but it's verified here:

// Some attributes can apply to all "values" but there are no `void` values.
if (Ty->isVoidTy()) {
if (ASK & ASK_SAFE_TO_DROP)
Incompatible.addAttribute(Attribute::NoUndef);
}

@vedantparanjape-amd
Copy link
Member Author

vedantparanjape-amd commented Apr 10, 2024

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.

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.

Yeah, this is not spelled out in LangRef, but it's verified here:

// Some attributes can apply to all "values" but there are no `void` values.
if (Ty->isVoidTy()) {
if (ASK & ASK_SAFE_TO_DROP)
Incompatible.addAttribute(Attribute::NoUndef);
}

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

Value *SimpleV = simplifyInstruction(I, DL);

@vedantparanjape-amd
Copy link
Member Author

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.

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.

Yeah, this is not spelled out in LangRef, but it's verified here:

// Some attributes can apply to all "values" but there are no `void` values.
if (Ty->isVoidTy()) {
if (ASK & ASK_SAFE_TO_DROP)
Incompatible.addAttribute(Attribute::NoUndef);
}

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

Value *SimpleV = simplifyInstruction(I, DL);

@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 (

// Finally, clone over the terminator.
), and clones it into the new function which has NoUndef attribute, so ideally this clone is illegal.

It becomes especially tricky as to how to handle it, given this return void statement has no successor.

@vedantparanjape-amd
Copy link
Member Author

ping @nikic

@vedantparanjape-amd
Copy link
Member Author

@nikic ping!

@nikic
Copy link
Contributor

nikic commented May 20, 2024

@vedantparanjape-amd The issue should already be fixed by #90489 -- do you still see problems?

Copy link
Contributor

@nikic nikic left a 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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants