llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[KnownFPClass] Support rounding-modes for `fadd`/`fsub`/`fmul`/`fdiv`/`fma`/`fmuladd` and `ldexp`

Open ZERICO2005 opened this issue 2 months ago • 2 comments

Currently, fadd/fsub/fmul/fdiv/fma/fmuladd (and ldexp) assume round-to-nearest with ties-to-even. We also support experimental_constrained_* versions of these operations which take a rounding mode, so it would probably be good to add a rounding mode parameter for these primitive IEEE operations. frem is always exact and does not need a rounding mode.

The easiest way to implement the rounding-mode for each function would be:

  • Implement the function assuming RoundingMode::Dynamic. It is possible that we may add a RoundingMode::AwayZero (not the same as RoundingMode::NearestTiesToAway) rounding mode in the future, so we should assume that RoundingMode::Dynamic also contains RoundingMode::AwayZero.
  • Then add back in deductions specific to RoundingMode::NearestTiesToEven (e.g. ruling out -0.0 for fadd(x, +0.0))

Afterwards, we can add specific deductions for a particular rounding mode. For example, ruling out +0.0 for fadd(x, -0.0) under RoundingMode::TowardNegative.

experimental_constrained_sqrt is already handled in ValueTracking.cpp, so adding support for the experimental_constrained_* intrinsics shouldn't be too difficult. https://github.com/llvm/llvm-project/blob/9f70ca880b37eba68a171f36272e06d8d2d371f3/llvm/lib/Analysis/ValueTracking.cpp#L5463-L5464

Progress

  • [x] Support experimental_constrained_fadd/experimental_constrained_fsub https://github.com/llvm/llvm-project/pull/241060
  • [ ] Support experimental_constrained_fmul
  • [ ] Support experimental_constrained_fdiv
  • [x] Support experimental_constrained_frem https://github.com/llvm/llvm-project/pull/239882
  • [ ] Support experimental_constrained_fma
  • [ ] Support experimental_constrained_fmuladd
  • [ ] Support experimental_constrained_ldexp

ZERICO2005 avatar Oct 05 '26 05:10 ZERICO2005

hey, i would like to work on this

Vara715 avatar Oct 05 '26 06:10 Vara715

hey, i would like to work on this

I am sorry, but I have already started working on this issue.

ZERICO2005 avatar Oct 05 '26 13:10 ZERICO2005