Skip to content

Disable constant folding in JIT resulting in NaN, infinities etc #10905

Closed

Description

@echesakov

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.

category:correctness
theme:expression-opts
skill-level:beginner
cost:small

Activity

  1. tannergooding commented on Aug 15, 2018

    @tannergooding
    Member

    @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).

  2. echesakov commented on Aug 24, 2018

    @echesakov
    ContributorAuthor

    @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.

  3. tannergooding commented on Aug 24, 2018

    @tannergooding
    Member

    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.

    CC. @CarolEidt, @RussKeldorph

  4. mikedn commented on Aug 24, 2018

    @mikedn
    Contributor

    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.

  5. tannergooding commented on Aug 24, 2018

    @tannergooding
    Member

    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).

  6. BruceForstall commented on Mar 13, 2019

    @BruceForstall
    Contributor

    @tannergooding @echesakovMSFT This doesn't seem like a 3.0 issue. Can we move it to Future?

  7. echesakov commented on Mar 13, 2019

    @echesakov
    ContributorAuthor

    Agree

  8. transferred this issue fromdotnet/coreclron Jan 31, 2020
  9. added this to the 5.0 milestone on Jan 31, 2020
  10. modified the milestones: 5.0, Future on Apr 28, 2020
  11. added
    JitUntriaged
    on Oct 28, 2020
  12. modified the milestones: Future, 6.0.0 on Nov 25, 2020
  13. removed
    JitUntriaged
    on Nov 25, 2020
  14. added
    needs-further-triage
    on Mar 23, 2021
  15. added
    Priority:3
    and removed
    needs-further-triage
    on Jun 3, 2021
  16. modified the milestones: 6.0.0, Future on Jun 3, 2021
  17. SingleAccretion commented on Aug 5, 2021

    @SingleAccretion
    Contributor

    This has been fixed in #53782.

  18. echesakov commented on Oct 15, 2021

    @echesakov
    ContributorAuthor

    Closed in #53782.

  19. ghost locked as resolved and limited conversation to collaborators on Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:3area-CodeGen-coreclrenhancementhelp wanted

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions