[llvm-dev] Should rint and nearbyint be always constrained?

Kaylor, Andrew via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 2 09:56:21 PST 2020


I agree with Ulrich. The default behavior of LLVM IR is to assume that the roundToNearest is the current rounding mode everywhere. This corresponds to the C standard, which says that the user may only modify the floating point environment if fenv access is enabled. In the latest version of the C standard, pragmas are added which can change the rounding mode for a region, and if these are implemented in clang the constrained versions of all FP operations should be used. However, outside of regions where fenv access is enabled either by pragma or command line option, we are free to assume that the current rounding mode is the default rounding mode.

So, llvm.rint and llvm.nearbyint (the non-constrained versions) can be specifically documented as performing their operation according to roundToNearest and clang can use them in the default case for the corresponding libm functions, and llvm.experimental.constrained.rint and llvm.experimental.constrained.nearbyint can be documented as using the current rounding mode.

The only issue I see is that since we also assume FP operations have no side effects by default there is no difference between llvm.rint and llvm.nearbyint. I wouldn’t have a problem with dropping llvm.rint completely.

As for the target-specific intrinsics, you are correct that we need a plan for that. I have given it some thought, but nothing is currently implemented. My suggestion would be that we should set the strictfp attribute on these intrinsics and provide the rounding mode and exception behavior arguments using an operand bundle. We do still need some way to handle the side effects. My suggestion here is to add some new attribute that means “no side effects” in the absence of the strictfp attribute and something similar to “inaccessibleMemOnly” in the presence of strictfp.

We could make the new attribute less restrictive than inaccessibleMemOnly in that it only really needs to act as a barrier relative to other things that are accessing the fp environment. I believe Ulrich suggested this to me at the last LLVM Developer Meeting.

-Andy

From: Serge Pavlov <sepavloff at gmail.com>
Sent: Monday, March 02, 2020 8:10 AM
To: Ulrich Weigand <Ulrich.Weigand at de.ibm.com>
Cc: Kaylor, Andrew <andrew.kaylor at intel.com>; Cameron McInally <cameron.mcinally at nyu.edu>; Kevin Neal <kevin.neal at sas.com>; LLVM Developers <llvm-dev at lists.llvm.org>
Subject: Re: Should rint and nearbyint be always constrained?

I'm not sure why this is an issue.  Yes, these two intrinsics depend
on the current rounding mode according to the C standard, and yes,
LLVM in default mode assumes that the current rounding mode is the
default rounding mode.  But the same holds true for many other
intrinsics and even the arithmetic IR operations like add.

Any other intrinsic, like `floor`, `round` etc has meaning at default rounding mode. But use of `rint` or `nearbyint` in default FP environment is strange, `roundeven` can be used instead. We could use more general intrinsics in all cases, as the special case of default environment is not of practical interest.

There is another reason for special handling. Set of intrinsics includes things like `x86_sse_cvtss2si`. It is unlikely that all of them eventually get constrained counterpart. It looks more natural that such intrinsics are defined as accessing FP environment and can be optimized if the latter is default. These two intrinsics could be a good model for such cases. IIUC, splitting entities into constrained or non-constrained is a temporary solution, ideally they will merge into one entity. We could do it for some intrinsics now.

Thanks,
--Serge


On Mon, Mar 2, 2020 at 8:58 PM Ulrich Weigand <Ulrich.Weigand at de.ibm.com<mailto:Ulrich.Weigand at de.ibm.com>> wrote:

Serge Pavlov <sepavloff at gmail.com<mailto:sepavloff at gmail.com>> wrote on 02.03.2020 14:38:48:

> This approach has issues when applied to the intrinsics `rint` and
> `nearbyint`. Value returned by either of these intrinsics depends on
> current rounding mode. If they are considered as operation in
> default environment, they would round only to nearest. It is by far
> not the meaning of the standard C functions that these intrinsics represent.

I'm not sure why this is an issue.  Yes, these two intrinsics depend
on the current rounding mode according to the C standard, and yes,
LLVM in default mode assumes that the current rounding mode is the
default rounding mode.  But the same holds true for many other
intrinsics and even the arithmetic IR operations like add.

If you want to stop clang from making the default rounding mode
assumption, you need to use the -frounding-math option (or one
of its equivalents), which will cause clang to emit the corresponding
constrained intrinsics instead, for those two as well all other
affected intrinsics.

I don't see why it would make sense to add another special case
just for those two intrinsics ...


Bye,
Ulrich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200302/e804bae2/attachment.html>


More information about the llvm-dev mailing list