Skip to content

Is it okay that isInf() and isNan() can return false for infinities / nans? #2270

Closed

Description

@ben-clayton

Issue #2259 highlights the fact that some backends assume fast-math, and so compilers may perform transformations that result in the isInf() / isNan() builtins returning false for infinite / NaN values, even when loading these values directly from memory.

As such, the CTS tests for these builtins will pass if they always return false.

Is this something we're happy with? If not, should we consider removing these builtins from the WGSL spec until we can guarantee more strict behaviours?

Activity

  1. added
    wgsl
    on Nov 8, 2021
  2. dneto0 commented on Nov 8, 2021

    @dneto0
    Contributor

    isFinite(x) is false when x is infinity or NaN. An implementation can assume infinities and NaNs don't exist, so isFinite can be implemented as always returning false.

  3. RobinMorisset commented on Nov 16, 2021

    @RobinMorisset
    Contributor

    As long as the backends use -ffast-math, I don't think these functions make much sense (since the compiler can always assume that no value is inf/NaN).
    Since issue #2259 suggests that disabling -ffast-math would come from an extension, I would propose moving these functions to the same extension.

  4. kdashg commented on Nov 16, 2021

    @kdashg
    Contributor

    As long as the backends use -ffast-math, I don't think these functions make much sense (since the compiler can always assume that no value is inf/NaN). Since issue #2259 suggests that disabling -ffast-math would come from an extension, I would propose moving these functions to the same extension.

    I like this idea.

  5. kdashg commented on Nov 16, 2021

    @kdashg
    Contributor
    WGSL meeting minutes 2021-11-16
    • JG: (gives background). Fast math can make isnan() turn into always-false. (!)
    • JG: Like Robin’s idea of removing now, and later adding an extension that adds preservation of inf and nan and than adds the functions back again.
    • MM: Philosophically adding false information is worse than not adding information.
    • DM: Can we possibly detect these on fast-math?
    • BC: Even if you polyfill, the compiler can still assume it doesn’t happen, and will melt it away.
    • JG: I think that even “loading directly from memory” isn’t enough
    • MM: Yes, can still get false negatives.
    • DN: So when translating spir-v, when I see these opcodes, what do I do? Inject false? (JG: yes)
    • MM: I think it’s up to implementation. Like, you can (try to) check the bits, that’s up to you. But rather that we shouldn’t volunteer known-flakey builtins.
    • AB: Would we mind if multiple extensions added these same builtins?
    • MM: Which multiple extensions?
    • AB: I could see multiple precise-math approach extensions might both re-introduce these.
    • MM: It would be unfortunate if we ended up with e.g. three subtly different extensions for precise math.
    • AB: We should figure out what our requirements for releasing/exposing extensions are. (AB to file issue)
    • MM: Did I hear DN say we have is_normal?
    • DN: Yes, since zero is non-normal. And there are also denormals. Denormals are not normal.
    • JG: So don’t some systems maybe-flush subnormals?
    • AB: Yeah, though if you load from memory it probably is preserved, just not for arithmetic. Is_normal is basically “is exponent not one of the extremes”.
    • MM: So doesn’t that put us in the same spot as isinf?
    • JG: It sounds like it’s more reliable than isinf.
    • AB: Review of all platforms. Can flush demormals on arithmetic operations. Vulkan phrases is “these operations preserve denormals”.. HLSL says “data movement” preserves denorms. Metal may have similar language. The language on underlying implementation is “implementation may assume that nans and infinities do not occur”. And with that the compiler will affirmatively remove function calls like isnan, isinf. My testing shows that it’s actually sometimes reliable and not always.
    • MM to file issue on whether to keep isNormal
    • Needs-Spec for removing isInf/IsNan
  6. self-assigned this
    on Nov 16, 2021
  7. added a commit that references this issue on Nov 16, 2021
    6b5d2a1
  8. added a commit that references this issue on Nov 18, 2021
    871df53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

wgsl

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions