Skip to content

[InstCombine] Simplify and correct folding fcmps with the same children - #9120

Open
Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-instcombine-fcmp-ord
Open

Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-instcombine-fcmp-ord

Conversation

@s-perron

Copy link
Copy Markdown
Collaborator

getFCmpCode encoded FCMP_ORD as 0, causing FoldOrOfFCmps to miscompile
(fcmp ord x, y) | (fcmp oeq x, y) into (fcmp oeq x, y). Encode fcmp
predicates directly using their 4-bit ULGE mask so FoldAndOfFCmps and
FoldOrOfFCmps fold bitwise combinations of predicates soundly, including
constant true and false results.

Backports llvm/llvm-project@aec68b263dc1 (D21775).

getFCmpCode encoded FCMP_ORD as 0, causing FoldOrOfFCmps to miscompile
(fcmp ord x, y) | (fcmp oeq x, y) into (fcmp oeq x, y). Encode fcmp
predicates directly using their 4-bit ULGE mask so FoldAndOfFCmps and
FoldOrOfFCmps fold bitwise combinations of predicates soundly, including
constant true and false results.

Backports llvm/llvm-project@aec68b263dc1 (D21775).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The user-visible floating-point comparison miscompile fix is missing the required entry under Upcoming Release → Bug Fixes in docs/ReleaseNotes.md.

1 open issue
What changed in this PR

Backports LLVM D21775 to fix incorrect InstCombine folding of floating-point comparisons with matching or swapped operands, including the ord | oeq miscompile.

Changes:

  • Uses four-bit predicate masks to simplify AND/OR folding and produce constant results where appropriate.
  • Adds five regression tests covering matching operands, swapped operands, and constant results.
  • Documents InstCombine’s dependency on floating-point predicate bit patterns.
File Description
tools/​clang/​test/​DXC/​Passes/​InstructionCombining/​and-or-fcmp.ll Adds regression coverage for comparison folding.
lib/​Transforms/​InstCombine/​InstCombineAndOrXor.cpp Replaces special-case folding with four-bit predicate operations.
include/​llvm/​IR/​InstrTypes.h Documents the dependency on predicate enum bit patterns.

Review effort: Balanced

Comment on lines +2023 to +2025
if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS,
Builder);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

2 participants