[PATCH] D154678: [InstCombine] Fold IEEE `fmul`/`fdiv` by Pow2 to `add`/`sub` of exp

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 7 13:16:26 PDT 2023


goldstein.w.n added a comment.

In D154678#4481669 <https://reviews.llvm.org/D154678#4481669>, @arsenm wrote:

> In D154678#4481361 <https://reviews.llvm.org/D154678#4481361>, @goldstein.w.n wrote:
>
>> Working on adding `ldexp` to backend, but notice that we lose the important information that `exp <= Type->getScalarSizeInBits()`
>
> The useful exponent range is limited by ldexp. The only useful values are between -exp_min - mantissa size to exp_max, outside of that it saturates to 0/inf.
>
>> We can't propagate an assume to the backend either.
>> Think to do this, need to add a flag to `ldexp` if `exp` is log of some sort (allows us to reason about whether it may cause denorm/etc).
>
> The exp is interpreted as the log value.

Yeah, but that log value can be anything. We where only able to bound
it before because we actually took the log of a value that maxed at Type->getScalarSizeInBits().
In the backend, it can be anything no?
I.e if we have:

  %pow = shl i64 1, %cnt
  %pow_fp = uitofp i64 to double %pow
  %mul = fmul double %d, %pow_fp

We convert to:

  %mul = ldexp(%d, %cnt)

But when we see this in the backend we have no way to bounding `cnt`.

>> What do you think?
>>
>> Edit:
>> We lose also information about whether this is for `fdiv` or `fmul` which makes the bounds more constrained, although this is probably less important.
>
> Does that matter? Can we unconditionally turn fdiv into fmul if the divisor is known to be an exact power of 2?
>
> I do think fmul with power of 2 constant in the general case should be more canonical than ldexp. We should turn ldexp with constant exponent into fmuls, and back into ldexp if the target wants.

The thing is the power of not constant.
The fold is:
`(fmul FP_Constant, (uitofp Pow2_Int_Variable))`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154678/new/

https://reviews.llvm.org/D154678



More information about the llvm-commits mailing list