You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Disable constant folding in JIT resulting in NaN, infinities etc #10905
Priority:3Work that is nice to havearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
As it was discussed and agreed in dotnet/coreclr#19395 JIT should not statically evaluate expressions resulting in NaNs, infinities, or any cases left implementation-defined by IEEE754.
@echesakovMSFT, why infinities? Looking through the spec, I didn't see any implementation defined behavior here...
Also, we should consider the handling for quiet NaN. The spec, while recommending preservation of a quiet NaN payload (in order to "facilitate propagation of diagnostic information"), permits modification of the quiet NaN payload while still preserving the literal meaning: IEEE 754:2008 10.4: Literal meaning and value-changing optimizations specifies:
The following value-changing transformations, among others, preserve the literal meaning of the source code:
― Applying the identity property 0 + x when x is not zero and is not a signaling NaN and the result has the same exponent as x.
― Applying the identity property 1 × x when x is not a signaling NaN and the result has the same exponent as x.
― Changing the payload or sign bit of a quiet NaN.
― Changing the order in which different flags are raised.
― Changing the number of times a flag is raised when it is raised at least once.
With regards to signalling NaN, they only enable additional behavior when floating-point exception handling is enabled (something which CoreCLR doesn't currently support).
@tannergooding Sorry - I missed you message.. You might be right - expression resulting in "infinities" could be still be folded.
This issue was opened in context of https://github.com/dotnet/coreclr/issues/19370 - we had different binary output for x86_arm and arm_arm crossgen due to folding expressions to NaN in JIT - it turned arm and x86 hosts have different binary representation of NaN value - which resulted in one bit difference between their output images. As a workaround dotnet/coreclr#19395 was implemented - but it would be safer to not allow NaN to be "embedded" into final code completely.
but it would be safer to not allow NaN to be "embedded" into final code completely
I was indicating that, according to the IEEE rules, it should be completely safe for us to normalize these differences for the purposes of embedding/folding.
I was indicating that, according to the IEEE rules, it should be completely safe for us to normalize these differences for the purposes of embedding/folding.
It's rather common rule that if a compiler does constant folding then it should do it according to the target's machine rules, so that the expression evaluated at compile time produces the exact same result as if it was evaluated at runtime. It's not set in stone and I'm sure that there are compilers that deviate from it here and there, either accidentally or on purpose. But it's reasonable practice and arguing against is just missing the point.
I would generally agree, but NaN are a bit special, and the majority of people on .NET won't be checking the exact result/payload (this is fairly rare in native code, from what I have seen, as well).
Given that there are a number of other places (such as serialization) where we explicitly decided to not preserve the exact payload and to instead normalize to Double.NaN or Single.NaN, and that the IEEE spec allows it, I think, for .NET Core, it might make sense to just normalize all QNaN (and possibly SNaN, since we don't support IEEE exception handling) to Double.NaN and Single.NaN as well (with regards to constant folding or embedding values).
Priority:3Work that is nice to havearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
Activity
tannergooding commentedon Aug 15, 2018
@echesakovMSFT, why
infinities? Looking through the spec, I didn't see any implementation defined behavior here...Also, we should consider the handling for
quiet NaN. The spec, while recommending preservation of aquiet NaNpayload (in order to "facilitate propagation of diagnostic information"), permits modification of thequiet NaNpayload while still preserving the literal meaning:IEEE 754:2008 10.4: Literal meaning and value-changing optimizationsspecifies:With regards to
signalling NaN, they only enable additional behavior when floating-point exception handling is enabled (something which CoreCLR doesn't currently support).echesakov commentedon Aug 24, 2018
@tannergooding Sorry - I missed you message.. You might be right - expression resulting in "infinities" could be still be folded.
This issue was opened in context of https://github.com/dotnet/coreclr/issues/19370 - we had different binary output for x86_arm and arm_arm crossgen due to folding expressions to NaN in JIT - it turned arm and x86 hosts have different binary representation of NaN value - which resulted in one bit difference between their output images. As a workaround dotnet/coreclr#19395 was implemented - but it would be safer to not allow NaN to be "embedded" into final code completely.
tannergooding commentedon Aug 24, 2018
I was indicating that, according to the IEEE rules, it should be completely safe for us to normalize these differences for the purposes of embedding/folding.
CC. @CarolEidt, @RussKeldorph
mikedn commentedon Aug 24, 2018
It's rather common rule that if a compiler does constant folding then it should do it according to the target's machine rules, so that the expression evaluated at compile time produces the exact same result as if it was evaluated at runtime. It's not set in stone and I'm sure that there are compilers that deviate from it here and there, either accidentally or on purpose. But it's reasonable practice and arguing against is just missing the point.
tannergooding commentedon Aug 24, 2018
I would generally agree, but
NaNare a bit special, and the majority of people on .NET won't be checking the exact result/payload (this is fairly rare in native code, from what I have seen, as well).Given that there are a number of other places (such as serialization) where we explicitly decided to not preserve the exact payload and to instead normalize to
Double.NaNorSingle.NaN, and that the IEEE spec allows it, I think, for .NET Core, it might make sense to just normalize allQNaN(and possiblySNaN, since we don't support IEEE exception handling) toDouble.NaNandSingle.NaNas well (with regards to constant folding or embedding values).BruceForstall commentedon Mar 13, 2019
@tannergooding @echesakovMSFT This doesn't seem like a 3.0 issue. Can we move it to Future?
echesakov commentedon Mar 13, 2019
Agree
SingleAccretion commentedon Aug 5, 2021
This has been fixed in #53782.
echesakov commentedon Oct 15, 2021
Closed in #53782.