Serge Pavlov via llvm-dev
2020-Mar-02 16:10 UTC
[llvm-dev] 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> wrote:> Serge Pavlov <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/6fef3cf6/attachment.html>
Kaylor, Andrew via llvm-dev
2020-Mar-02 17:56 UTC
[llvm-dev] Should rint and nearbyint be always constrained?
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>
Kaylor, Andrew via llvm-dev
2020-Mar-02 22:59 UTC
[llvm-dev] Should rint and nearbyint be always constrained?
Some clarification after getting feedback from Craig Topper…. It’s probably best to say in the documentation that the llvm.nearbyint and llvm.rint functions “assume the default rounding mode, roundToNearest”. This will allow the optimizer to transform them as if they were rounding to nearest without requiring backends to use an encoding that enforces roundToNearest as the rounding mode for these operations. On modern x86 targets we can encode it either way, but it seems more consistent to continue using the current encoding which tells the processor to use the current rounding mode. For other targets (including cases where x86 is forced to use x87 instructions), it may be much easier to leave this at the discretion of the backend. Also, we should take care to document the non-constrained forms of these intrinsics in a way that makes clear that we are “assuming” and not requiring that the operation has no side effects. For the constrained version of nearbyint, we will require that the inexact exception is not raised (to be consistent with iEEE 754-2019’s roundToIntegral operations) and for the constrained version of rint we will require that the inexact exception is raised (to be consistent with iEEE 754-2019’s roundToIntegralExact operation), but for the non-constrained forms it should be clear that the backend is free to implement this in the most efficient way possible, without regard to FP exception behavior. Finally, I see now the problem with documenting these in terms of the IEEE operations, given that IEEE 754-2019 doesn’t describe an operation that uses the current rounding mode without knowing what that is. I see this as a problem of documentation rather than one that presents any difficulty for the implementation. Here are some suggested wordings for the “Semantics” section of the langref for these functions: llvm.nearbyint::semantics This function returns the same value as one of the IEEE 754-2019 roundToIntegral operations using the current rounding mode. The optimizer may assume that actual rounding mode is roundToNearest (IEEE 754: roundTiesToEven), but backends may encode this operation either using that rounding mode explicitly or using the dynamic rounding mode from the floating point environment. The optimizer may assume that the operation has no side effects and raises no FP exceptions, but backends may encode this operation using either instructions that raise exceptions or instructions that do not. The FP exceptions are assumed to be ignored. llvm.rint (delete, or identical semantics to llvm.nearbyint) llvm.experimental.constrained.nearbyint::semantics This function returns the same value as one of the IEEE 754-2019 roundToIntegral operations. If the roundingMode argument is fpround.dynamic, the behavior corresponds to whichever of the roundToIntegral operations matches the dynamic rounding mode when the operation is executed. The optimizer may not assume any rounding mode in this case, and backends must encode the operation in a way that uses the dynamic rounding mode. Otherwise, the rounding mode may be assumed to be that described by the roundingMode argument and backends may either use instructions that encode that rounding mode explicitly or use the current rounding mode from the FP environment. The optimizer may assume that this operation does not raise the inexact exception when the return value differs from the input value, and if the exceptionBehavior argument is not fpexcept.ignore, the backend must encode this operation using instructions that guarantee that the inexact exception is not raised. If the exceptionBehavior argument is fpexcept.ignore, backends may encode this operation using either instructions that raise exceptions or instructions that do not. llvm.experimental.constrained.rint::semantics This function returns the same value as the IEEE 754-2019 roundToIntegralExact operation. If the roundingMode argument is fpround.dynamic, the behavior uses to the dynamic rounding mode when the operation is executed. The optimizer may not assume any rounding mode in this case, and backends must encode the operation in a way that uses the dynamic rounding mode. Otherwise, the rounding mode may be assumed to be that described by the roundingMode argument and backends may either use instructions that encode that rounding mode explicitly or use the current rounding mode from the FP environment. If the exceptionBehavior argument is not fpexcept.ignore, the optimizer must assume that this operation will raise the inexact exception when the return value differs from the input value and the backend must encode this operation using instructions that guarantee that the inexact exception is raised in that case. If the exceptionBehavior argument is fpexcept.ignore, backends may encode this operation using either instructions that raise exceptions or instructions that do not. I’d like to also say that these intrinsics can be lowered to the corresponding libm functions, but I’m not sure all libm implementations meet the requirements above. -Andy From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Kaylor, Andrew via llvm-dev Sent: Monday, March 02, 2020 9:56 AM To: Serge Pavlov <sepavloff at gmail.com>; Ulrich Weigand <Ulrich.Weigand at de.ibm.com> Cc: LLVM Developers <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] Should rint and nearbyint be always constrained? 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<mailto:sepavloff at gmail.com>> Sent: Monday, March 02, 2020 8:10 AM To: Ulrich Weigand <Ulrich.Weigand at de.ibm.com<mailto:Ulrich.Weigand at de.ibm.com>> Cc: Kaylor, Andrew <andrew.kaylor at intel.com<mailto:andrew.kaylor at intel.com>>; Cameron McInally <cameron.mcinally at nyu.edu<mailto:cameron.mcinally at nyu.edu>>; Kevin Neal <kevin.neal at sas.com<mailto:kevin.neal at sas.com>>; LLVM Developers <llvm-dev at lists.llvm.org<mailto: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/96423ce9/attachment.html>