Skip to content

[RISC-V] NaN propagation should occur where feasible #119897

@tannergooding

Description

@tannergooding

Summary

The IEEE 754 specification recommends, but does not require, that operations propagate the input back as the output if it is a NaN, or one of the NaNs if the operation supports multiple.

This is something that most hardware follows and that a large amount of software depends on, particularly as it pertains to certain web scenarios where "NaN boxing" is fairly prevalent. For example, this is utilized by WebKit and called out by the WASM specs. It is also extensively used by SIMD algorithms due to the floating-point and integral SIMD registers being one and the same, so it is typical to produce some Vector128<float> that is AllBitsSet (a qNaN with a full payload) and then use that in a subsequent instruction.

.NET has correspondingly done NaN propagation historically and made efforts to ensure this operates as expected for users in the face of constant folding and other scenarios.

Issue

RISC-V differs in that they explicitly chose to not propagate and to always normalize, outside of a few operations that are strictly documented to operate on raw-bits. While this is "technically allowed", it creates a division in customer expectations for .NET and is likely to break real world apps.

This came up, for example, in #118589 when looking at the logic generated for the Min/Max APIs, since they just returned "some NaN".

The RISC-V codegen should ideally be updated to follow the same practices as our other hardware so that apps and user expectations are not subtly broken when running on such hardware. This will represent a perf penalty in some cases, but we correspondingly have APIs such as MinNative/MaxNative which are allowed to differ on such edges for places where it is irrelevant but the performance is necessary.

Fixing this is inline with other cross-platform determinism fixes we've done over past releases, including such as ensuring (int)someFloat saturates on overflow, which included minor perf hits to platforms that are currently more common for users to have.

Metadata

Metadata

Assignees

Labels

arch-riscvRelated to the RISC-V architecturearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions