Skip to content

[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

Merged
merged 3 commits into from
Jun 16, 2025

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Jun 14, 2025

Update the guideline to reduce the chance of miscompilation/performance regression.

@dtcxzyw dtcxzyw marked this pull request as ready for review June 14, 2025 15:41

| Original Pattern | Canonical Form | Condition |
|------------------------------|--------------------|-------------------------------|
| `icmp spred X, Y` | `icmp upred X, Y` | `sign(X) == sign(Y)` |
Copy link
Contributor

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?

Comment on lines +564 to +565
Do not rely on fcmp's `nsz` flag to perform optimizations. It is meaningless for fcmp so it should not affect
the optimization.
Copy link
Contributor

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.?

Copy link
Member Author

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.

Copy link
Contributor

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;
  }
}

Copy link
Member Author

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.

Copy link
Contributor

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.

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.

LGTM

Comment on lines +564 to +565
Do not rely on fcmp's `nsz` flag to perform optimizations. It is meaningless for fcmp so it should not affect
the optimization.
Copy link
Contributor

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>
@dtcxzyw dtcxzyw merged commit 299a55a into llvm:main Jun 16, 2025
8 checks passed
@dtcxzyw dtcxzyw deleted the guideline-update branch June 16, 2025 10:07
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.

4 participants