Skip to content

[InstCombine] Do not rely on the nsz flag on fcmp #140992

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

Open
dtcxzyw opened this issue May 22, 2025 · 0 comments
Open

[InstCombine] Do not rely on the nsz flag on fcmp #140992

dtcxzyw opened this issue May 22, 2025 · 0 comments
Assignees
Labels
floating-point Floating-point math llvm:instcombine metabug Issue to collect references to a group of similar or related issues. miscompilation

Comments

@dtcxzyw
Copy link
Member

dtcxzyw commented May 22, 2025

As discussed in https://discourse.llvm.org/t/rfc-clarify-the-behavior-of-fp-operations-on-bit-strings-with-nsz-flag/85981, forbidding nsz flags in bitwise FP operations (select/phi/copysign/fabs/fneg) is infeasible as it blocks some common optimizations (e.g., (X < 0.0) ? -X : X -> fabs(X)). I am working on a better formal definition for the nsz flag (and other rewrite-based flags).

However, the nsz flag on fcmp is meaningless since fcmp treats both positive and negative zero as equal. It allows us to freely add nsz: https://alive2.llvm.org/ce/z/x2iNNo
Unfortunately, I notice that some existing optimizations in InstCombine rely on the nsz flag on fcmp. It is a strange behavior and causes some miscompilations (e.g., https://alive2.llvm.org/ce/z/J9ZBXX).

I have created a tool for metamorphic testing, to ensure that the nsz flag on fcmp doesn't affect the optimized IR.

@dtcxzyw dtcxzyw self-assigned this May 22, 2025
@dtcxzyw dtcxzyw added metabug Issue to collect references to a group of similar or related issues. miscompilation llvm:instcombine floating-point Floating-point math labels May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
floating-point Floating-point math llvm:instcombine metabug Issue to collect references to a group of similar or related issues. miscompilation
Projects
None yet
Development

No branches or pull requests

1 participant