[PATCH] D60021: InstSimplify: Fold round intrinsics from sitofp

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 1 08:04:41 PDT 2019


spatel added reviewers: lebedev.ri, efriedma, nikic, hfinkel, scanon.
spatel added a comment.

In D60021#1449807 <https://reviews.llvm.org/D60021#1449807>, @lebedev.ri wrote:

> I'm still a bit unsure about this. I could be simply confused though.
>  I agree that it is fine for those integral types when `iN.SINT_MAX() <= float.MAX() && iN.SINT_MIN() >= float.MIN()`.
>  That means, we are good for up to i128 for `float` / i1024 for `double` or so.
>  But what about larger types? I don't think `[su]itofp` clamps?


It's a maze of docs, but this is well-defined:
https://llvm.org/docs/LangRef.html#sitofp-to-instruction
"If the value cannot be exactly represented, it is rounded using the default rounding mode."

And then we have:
https://llvm.org/docs/LangRef.html#floatenv 
"Results assume the round-to-nearest rounding mode."

So then we refer back to IEEE754 (section 7.4 Overflow):
"...if the destination format’s largest finite number is exceeded in magnitude by what would have been the rounded floating-point result (see 4) were the exponent range unbounded. The default result shall be determined by the rounding-direction attribute and the sign of the intermediate result as follows:
a) roundTiesToEven and roundTiesToAway carry all overflows to ∞ with the sign of the intermediate result."

So if the FP result is infinity, we then check the behavior of the LLVM intrinsics, and they all say, for example:
https://llvm.org/docs/LangRef.html#llvm-floor-intrinsic
"This function returns the same values as the libm floor functions would, and handles error conditions in the same way."

I'm not sure where or if there are official docs for libm, but:
https://en.cppreference.com/w/cpp/numeric/math/floor
"If arg is ±∞, it is returned, unmodified"

So that means the simplification is safe...obvious, right? :)

Note: There's a proposal to add intrinsic siblings for the other direction (fp --> int...which can produce poison) here:
D54749 <https://reviews.llvm.org/D54749>


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

https://reviews.llvm.org/D60021





More information about the llvm-commits mailing list