Issue 240038
Summary [KnownFPClass] Add magnitude refinement to KnownFPClass (to distinguish `|x|>1.0`, `|x|=1.0`, and `|x|<1.0`)
Labels floating-point
Assignees ZERICO2005
Reporter ZERICO2005
    Being able to distinguish between `|x|>1.0`, `|x|=1.0`, and `|x|<1.0` would be very useful for `KnownFPClass`. Some examples include:
- `fmul(x, <1.0)` and `fdiv(x, >1.0)` never overflow to infinity.
- `fmul(x, >1.0)` and `fdiv(x, <1.0)` never underflow to zero.
- `asin(x)` and `acos(x)` are not NaN if `-1.0 <= x <= +1.0`.
- See more examples here https://github.com/llvm/llvm-project/issues/190809

However, some work needs to be done before we can consider expanding the set of values `KnownFPClass` can represent. This issue serves as a tracking issue for the work that needs to be done.

***

Background:

Recently, `KnownFPClass` was refactored from `{FPClassTest, SignBit}` to `{FPClassMask}` so it can properly represent the sign bit of NaN as part of https://github.com/llvm/llvm-project/issues/217072.

Expanding the set of values `KnownFPClass` can represent is likely to decrease stability and increase the surface for bugs. So we should ensure that all other parts of the codebase that use `KnownFPClass`/`FPClassTest` are stable and *mostly* bug-free before we introduce additional complexity.

Potential issues that we may encounter:
- `FPClassTest` will represent a stable/frozen fixed set of classes, while the set of classes `KnownFPClass`/`FPClassMask` can represent will increase over time.

***
Required:
- [ ] Properly handle fast-math flags that ***cannot*** be ignored (`ninf` and `nnan` are safe to ignore for `computeKnownFPClass`, as in no miscompilations will occur by ignoring them):
  - [ ] `nsz`: Can probably be handled similarly to denormal mode flushing.
  - [ ] `arcp`: Can we assume that `fdiv(1.0, 1.0)` is `1.0` under `arcp`?
  - [ ] `afn`: See https://discourse.llvm.org/t/do-knownfpclass-deductions-still-hold-for-afn-functions/92463
 - [ ] `contract`
  - [ ] `reassoc`
- [ ] denormal modes:
  There is not just one flavor of "output subnormals flush to zero". We should also consider what happens when:
  - Subnormals are flushed before rounding (tiny before rounding).
  - Subnormals are flushed after rounding (tiny after rounding).
  - Does DAPZ (denormals are positive zero) treat negative underflow differently from a negative subnormal result? Does one become -0.0 while the other becomes +0.0? Does our analysis correctly handle this?
- [ ] Make `SimplifyDemandedUseFPClass` more stable and mostly "bug-free". While similar to `computeKnownFPClass`, `SimplifyDemandedUseFPClass` has to be much more careful to avoid miscompilations.

Optional:
- [ ] Replace `FPClassTest InterestedClasses` with `KnownFPClass/FPClassMask InterestedClasses` in `computeKnownFPClass`.
  Some risks include missed optimizations and complexity increases:
  - Setting `InterestedClasses` to `fcNone` will weaken the analysis, which may lead to missed optimizations (harmless).
  - Setting `InterestedClasses` to `fcAllFlags` will strengthen the analysis, but may increase compile time.
- [ ] Replace `FPClassTest DemandedMask` with `KnownFPClass/FPClassMask DemandedMask` in `simplifyDemandedFPClassResult`
- [ ] Replace `FPClassTest Mask` with `KnownFPClass/FPClassMask Mask` in `getFPClassConstant`, this currently will help with +-qNaN and +-sNaN constants which may be generated from `fabs` or `copysign`.






_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to