<div dir="ltr">The review was done in <a href="https://reviews.llvm.org/D104854" target="_blank">https://reviews.llvm.org/D104854</a>. The design was discussed there, in <a href="https://lists.llvm.org/pipermail/llvm-dev/2021-August/152257.html">https://lists.llvm.org/pipermail/llvm-dev/2021-August/152257.html</a> and in this thread. No alternatives were proposed and no disagreement was expressed.<div><br><div>If somebody has concerns or objections, they can express them here or in
<a href="https://reviews.llvm.org/D104854" target="_blank">https://reviews.llvm.org/D104854</a> if they are about the implementation.</div><div><br clear="all"><div><div dir="ltr" data-smartmail="gmail_signature">Thanks,<br>--Serge<br></div></div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 22, 2021 at 3:19 AM Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com" target="_blank">lebedev.ri@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Given just how contentious all this has been so far,<br>
i would strongly suggest for that to go through review.<br>
<br>
Roman<br>
<br>
On Tue, Sep 21, 2021 at 8:39 PM Serge Pavlov via cfe-dev<br>
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> If nobody argues, in a couple of days I will put back the `llvm.isnan` implementation excluding the changes for fast-mode.<br>
><br>
> Thanks,<br>
> --Serge<br>
><br>
><br>
> On Fri, Sep 3, 2021 at 1:46 PM Serge Pavlov <<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>> wrote:<br>
>><br>
>> Thank you for summarizing.<br>
>><br>
>> I would prefer Option 2, a separate tool for separate tasks. It seems to me that a specialized function is easier to implement than a universal one. As implementation of llvm.isnan demonstrated, there may be various issues in implementing such functions for different targets. It is easier to provide optimized versions of small functions which are used more frequently than `fpclassify`. Besides, `fpclassify` may be used itself and a user may choose arbitrary constants, which complicates code generation. It seems that it is more acceptable to tolerate some inefficiency in `fpclassify` than in basic classification intrinsics. And yes, we should consider `signbit` with them.<br>
>><br>
>> Another consideration in favor of dedicated intrinsic rather than ordinary function. Compiler may do some optimizations when it knows the semantics of the function. In <a href="https://reviews.llvm.org/D104854" rel="noreferrer" target="_blank">https://reviews.llvm.org/D104854</a> this intrinsic was optimized out if its argument was provided by an operation with 'nnan' flag. We can also think about an optimization that determines basic blocks guarded by `llvm.isnan` and assign flag 'nnan' in them. It could make code faster in many practical cases, and make use of notorious -ffast-math less attractive.<br>
>><br>
>> Thanks,<br>
>> --Serge<br>
>><br>
>><br>
>> On Fri, Sep 3, 2021 at 12:22 PM James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>> wrote:<br>
>>><br>
>>> As you say, we don't strictly need a new intrinsic -- we can emit the code to do the correct integer-based bit-checking in the frontend -- as was done before. Yet, that is not ideal. The rationale to have an intrinsic instead of the frontend generating integer-bit-manipulation seem good, IMO.<br>
>>><br>
>>> So the question that arises: which new intrinsic(s) do we want to add? I list 3 options below. I think Option 1 should be discarded from consideration, and I'm not sure which of 2 or 3 is best. I'm leaning towards 3 -- it seems like it may be simpler -- although I'm not certain.<br>
>>><br>
>>> Option 1: we could theoretically address the problem with an llvm.experimental.constrained.superquiet_fcmp, which would be the same as fcmp, except it would not raise an fp exception even for an sNAN. This would be a straightforward substitution for fcmp in Clang's existing codegen, and it should work.<br>
>>><br>
>>> However, I don't think this would be a good idea, because it's a poor match for hardware. The "superquiet_fcmp" operation doesn't map to CPU functionality in any ISA i'm aware of. Generally, the implementation would have to be to filter out sNANs prior to using the hardware fp-compare instruction. And how you you detect an sNAN without raising an fp exceptoin? Fall down to integer ops. But by doing that, you've done work almost equivalent to what you needed for the actual classification function that you were trying to implement in the first place. Not real useful.<br>
>>><br>
>>> Option 2: We could add a whole family of classification intrinsics. I think that would be:<br>
>>><br>
>>> llvm.isnan<br>
>>> llvm.issignaling<br>
>>> llvm.isinf<br>
>>> llvm.isfinite<br>
>>> llvm.isnormal<br>
>>> llvm.issubnormal<br>
>>> llvm.iszero<br>
>>> (Note, some of these are missing corresponding __builtin_is* in Clang at the moment -- we have no __builtin_issignaling, __builtin_issubnormal, or __builtin_iszero. Probably ought to.)<br>
>>><br>
>>> We don't necessarily need an intrinsic for fpclassify if we have the above intrinsics, since it can be built with llvm.isnan, llvm.isinf, llvm.iszero, and llvm.isnormal.<br>
>>><br>
>>> Option 3: Add only an fpclassify intrinsic.<br>
>>><br>
>>> That is, something like:<br>
>>> i32 llvm.fpclassify.i32.f32(i32 %if_snan, i32 %if_qnan, i32 %if_infinite, i32 %if_normal, i32 %if_subnormal, i32 %if_zero, float %value)<br>
>>> which classifies the given value, returning the value of the argument corresponding to its categorization. We can say the %if_* args are required to be constant integers, if we like, for simplicity of implementation.<br>
>>><br>
>>> Thus, Clang would codegen __builtin_isnan/etc like:<br>
>>> %isnan = call i1 llvm.fpclassify.i1.f32(i1 1, i1 1, i1 0, i1 0, i1 0, i1 0, float %value)<br>
>>> And for fpclassify, we might generate something like:<br>
>>> %ret = call i32 llvm.fpclassify.i32.f32(i32 0, i32 0, i32 1, i32 4, i32 3, i32 2, float %value)<br>
>>><br>
>>> On most architectures, we'd expand this intrinsic into appropriate integer operations (skipping the parts of classification which are irrelevant for the given constant arguments), since there's no corresponding hardware instructions available for float classification. Or, for non-strictfp functions, we could continue to expand into an fcmp-based set of tests...although looking at the asm we currently generate, the integer versions may well be faster, other than isnan.<br>
>>><br>
>>> On SystemZ/s390x, this intrinsic would translate almost directly into the "test data class" instruction -- so long as the %if_* arguments are all 0/1. That's kinda nice. ("Test data class" takes a fp value and a bitmask, and returns true if a bit is set in the position corresponding to the classification of the fp value.)<br>
>>><br>
>>><br>
>>> Separately, we have the signbit operation. I think that's the only other operation that needs to be addressed related to this RFC. Currently, Clang always generates integer bit-ops for __builtin_signbit in the frontend. This is arguably OK as is. Yet, completing the set of IR fp classification intrinsics seems like it'd be a good idea. So, we could also (along with any of the above options) add:<br>
>>> i1 llvm.signbit.f32(float %value)<br>
>>><br>
>>><br>
>>> On Thu, Sep 2, 2021 at 8:33 AM Serge Pavlov via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>>><br>
>>>> Hi all,<br>
>>>><br>
>>>> Some time ago a new intrinsic `llvm.isnan` was introduced, which was intended to represent IEEE-754 operation `isNaN` as well as a family of C library functions `isnan*`. Then a concern was raised (see <a href="https://reviews.llvm.org/D104854" rel="noreferrer" target="_blank">https://reviews.llvm.org/D104854</a>) that this functionality should be removed. Discussion in the subsequent RFC (<a href="https://lists.llvm.org/pipermail/llvm-dev/2021-August/152257.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2021-August/152257.html</a>) came to consensus that such intrinsic is necessary. Nevertheless the patches related to the new intrinsic were reverted. I have to restart the discussion in hope to convince the community that this intrinsic and other classification functions are necessary.<br>
>>>><br>
>>>> There are two main reasons why this intrinsic is necessary:<br>
>>>> 1. It allows correct implementation of `isnan` if strict floating point semantics is in effect,<br>
>>>> 2. It allows preserving the check in -ffast-math compilation.<br>
>>>><br>
>>>> To facilitate the discussion let's concentrate on the first problem.<br>
>>>><br>
>>>> Previously the frontend intrinsic `__builtin_isnan` was converted into `cmp uno` during IR generation in clang codegen. This solution is not suitable if FP exceptions are not ignored, because compare instructions raise exceptions if its argument is signaling NaN. Both IEEE-754 (5.7.2) an C standard (<a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf" rel="noreferrer" target="_blank">http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf</a>, F.3p6) demand that this function does not raise floating point exceptions. There was no target-independent IR construct that could represent `isnan`.<br>
>>>><br>
>>>> This drawback was significant enough and some attempts to alleviate it were undertaken. In <a href="https://reviews.llvm.org/D95948" rel="noreferrer" target="_blank">https://reviews.llvm.org/D95948</a> `isnan` was implemented using integer operations in strictfp functions. It however is not suitable for targets where a more efficient way exists, like dedicated instruction. Another solution was implemented in <a href="https://reviews.llvm.org/D96568" rel="noreferrer" target="_blank">https://reviews.llvm.org/D96568</a>, where a hook `clang::TargetCodeGenInfo::testFPKind` was introduced, which injects target specific code into IR. Such a solution makes IR more target-dependent and prevents some IR-level optimizations.<br>
>>>><br>
>>>> To have a solution suitable for all cases, a new intrinsic function `llvm.isnan` was introduced (<a href="https://reviews.llvm.org/D104854" rel="noreferrer" target="_blank">https://reviews.llvm.org/D104854</a>). It protects the check from undesirable optimizations and preserves it till selector, where it can be lowered in optimal for a particular target way.<br>
>>>><br>
>>>> Other classification functions also need their own intrinsics. In strictfp mode even a check for zero (`iszero`) cannot be made by comparing a value against zero, - if the value is signaling NaN, FP exceptions would be raised. James Y Knight in the previous discussion (<a href="https://lists.llvm.org/pipermail/llvm-dev/2021-August/152282.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2021-August/152282.html</a>) listed such "non-computational" functions, which should not signal if provided with an sNAN argument.<br>
>>>><br>
>>>> It looks like new intrinsic is the only consistent and in target-agnostic way to implement these checks in all environments including the case when FP exceptions are not ignored.<br>
>>>><br>
>>>> Any feedback is welcome.<br>
>>><br>
>>><br>
>>><br>
><br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>