[Mesa-dev] [PATCH] nir: fix divide by zero crash during constant folding

Jason Ekstrand jason at jlekstrand.net
Wed Feb 28 03:33:00 UTC 2018


On Tue, Feb 27, 2018 at 7:20 PM, Timothy Arceri <tarceri at itsqueeze.com>
wrote:

> On 28/02/18 14:13, Jason Ekstrand wrote:
>
>> On February 27, 2018 19:11:49 Jason Ekstrand <jason at jlekstrand.net>
>> wrote:
>>
>> On February 27, 2018 19:07:43 Timothy Arceri <tarceri at itsqueeze.com>
>>> wrote:
>>>
>>> From the GLSL 4.60 spec Section 5.9 (Expressions):
>>>>
>>>>    "Dividing by zero does not cause an exception but does result in
>>>>     an unspecified value."
>>>>
>>>> Fixes: 89285e4d47a6 "nir: add new constant folding infrastructure"
>>>>
>>>> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=105271
>>>> ---
>>>>  src/compiler/nir/nir_opcodes.py | 6 +++---
>>>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/src/compiler/nir/nir_opcodes.py
>>>> b/src/compiler/nir/nir_opcodes.py
>>>> index 278562b2bd..dcc5b07d05 100644
>>>> --- a/src/compiler/nir/nir_opcodes.py
>>>> +++ b/src/compiler/nir/nir_opcodes.py
>>>> @@ -403,9 +403,9 @@ binop("imul_high", tint32, commutative,
>>>>  binop("umul_high", tuint32, commutative,
>>>>        "(uint32_t)(((uint64_t) src0 * (uint64_t) src1) >> 32)")
>>>>
>>>> -binop("fdiv", tfloat, "", "src0 / src1")
>>>> -binop("idiv", tint, "", "src0 / src1")
>>>> -binop("udiv", tuint, "", "src0 / src1")
>>>> +binop("fdiv", tfloat, "", "src1 == 0 ? 0 : (src0 / src1)")
>>>>
>>>
>>> Should this return inf?
>>>
>>
>> Or maybe an unsignaling NaN?  I'm not really sure.
>>
>
> I'm not sure. GLSL IR doesn't even try to catch this currently.
>

According to wikipedia, it's supposed to result in +-inf:

https://en.wikipedia.org/wiki/IEEE_754#Exception_handling

The D3D10 docs say:

"Divide by 0 produces +/- INF, except 0/0 which results in NaN."

https://msdn.microsoft.com/en-us/library/windows/desktop/cc308050(v=vs.85).aspx

Another option would be to temporarily disable FP exceptions during
constant folding.


>
>> +binop("idiv", tint, "", "src1 == 0 ? 0 : (src0 / src1)")
>>>> +binop("udiv", tuint, "", "src1 == 0 ? 0 : (src0 / src1)")
>>>>
>>>>  # returns a boolean representing the carry resulting from the addition
>>>> of
>>>>  # the two unsigned arguments.
>>>> --
>>>> 2.14.3
>>>>
>>>> _______________________________________________
>>>> mesa-dev mailing list
>>>> mesa-dev at lists.freedesktop.org
>>>> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>>>>
>>>
>>>
>>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180227/f78a3c68/attachment.html>


More information about the mesa-dev mailing list