-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[InstCombine][Docs] Update InstCombine contributor guide #144228
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
|
||
| Original Pattern | Canonical Form | Condition | | ||
|------------------------------|--------------------|-------------------------------| | ||
| `icmp spred X, Y` | `icmp upred X, Y` | `sign(X) == sign(Y)` | |
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 thought the motivation for the samesign effort was to avoid canonicalizations like this, so that the back-end can choose?
Do not rely on fcmp's `nsz` flag to perform optimizations. It is meaningless for fcmp so it should not affect | ||
the optimization. |
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.
Is nsz meaningless for fcmp? I thought there are some optimizations already that take nsz on fcmp into account, with the property that -0. == +0.?
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.
0.0 and -0.0 are considered equal, even if nsz is absent.
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.
Perhaps I'm confused about something? See CmpInst::isEquivalence, which is used in some places:
// Returns true if either operand of CmpInst is a provably non-zero
// floating-point constant.
static bool hasNonZeroFPOperands(const CmpInst *Cmp) {
auto *LHS = dyn_cast<Constant>(Cmp->getOperand(0));
auto *RHS = dyn_cast<Constant>(Cmp->getOperand(1));
if (auto *Const = LHS ? LHS : RHS) {
using namespace llvm::PatternMatch;
return match(Const, m_NonZeroNotDenormalFP());
}
return false;
}
// Floating-point equality is not an equivalence when comparing +0.0 with
// -0.0, when comparing NaN with another value, or when flushing
// denormals-to-zero.
bool CmpInst::isEquivalence(bool Invert) const {
switch (Invert ? getInversePredicate() : getPredicate()) {
case CmpInst::Predicate::ICMP_EQ:
return true;
case CmpInst::Predicate::FCMP_UEQ:
if (!hasNoNaNs())
return false;
[[fallthrough]];
case CmpInst::Predicate::FCMP_OEQ:
return hasNonZeroFPOperands(this);
default:
return false;
}
}
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.
equal
is not equivalence
. The latter one means one of the operands can be replaced by the other.
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.
@artagnon Note that this does not actually checks the nsz flag, it checks whether the operands are non-zero. That's fine.
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
Do not rely on fcmp's `nsz` flag to perform optimizations. It is meaningless for fcmp so it should not affect | ||
the optimization. |
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.
@artagnon Note that this does not actually checks the nsz flag, it checks whether the operands are non-zero. That's fine.
Co-authored-by: Nikita Popov <github@npopov.com> Co-authored-by: Antonio Frighetto <me@antoniofrighetto.com>
Update the guideline to reduce the chance of miscompilation/performance regression.