Skip to content

Conversation

@tannergooding
Copy link
Member

@tannergooding tannergooding commented Feb 6, 2024

Backport of #98001 to release/8.0-staging

/cc @tannergooding

Customer Impact

  • Customer reported
  • Found internally

Developers who are shifting by a constant amount while using the platform specific APIs may see incorrect results.

Regression

  • Yes
  • No

Support for constant folding some primitive SIMD intrinsics was added in .NET 8. For the most part, the behavior of these intrinsics is consistent across platforms and consistent with the underlying scalar APIs users will typically consume from C#. However, "overshifting" is not well-defined behavior and can differ across platforms. Overshifting, in particular, is when you provide a shift amount more than the number of bits available in the underlying data type. For example, given int x, doing x << 32 is not "well-defined" across languages and platforms.

IL leaves this as strictly undefined behavior and leaves it to the underlying platform, while C# makes it well defined and prescribes that it is actually x << (32 % (sizeof(int) * 8)), which is equivalent to it wrapping around and thus is the same as x << 0. This matches what quite a bit of hardware does, such as x86/x64 for their scalar shift instructions. However, the vector shift instructions for x86/x64 differ and instead treat it as actually shifting by 1, 32 times. Thus it produces a value that is all zero.

While we have tests covering overshifting, we missed a test covering the combination of overshifting by a constant amount and thus the edge case that can occur in some kinds of user code.

Testing

A regression test covering the scenario was added as well as other potentially interesting combinations.

Risk

Low. The issue is well understood, the other constant folding support added has been checked for potential edge cases, and the functionality has also been checked against Arm64.

…ect behavior on overshift (dotnet#98001)

* Ensure that constant folding for SIMD shifts on xarch follow the correct behavior on overshift

* Ensure we test Sse2.IsSupported
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 6, 2024
@ghost ghost assigned tannergooding Feb 6, 2024
@ghost
Copy link

ghost commented Feb 6, 2024

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Backport of #98001 to release/8.0-staging

/cc @tannergooding

Customer Impact

This was a customer reported issue and was a regression from .NET 7.

The impact is that users may see incorrect results in release code.

Analysis

Support for constant folding some primitive SIMD intrinsics was added in .NET 8. For the most part, the behavior of these intrinsics is consistent across platforms and consistent with the underlying scalar APIs users will typically consume from C#. However, "overshifting" is not well-defined behavior and can differ across platforms. Overshifting, in particular, is when you provide a shift amount more than the number of bits available in the underlying data type. For example, given int x, doing x << 32 is not "well-defined" across languages and platforms.

IL leaves this as strictly undefined behavior and leaves it to the underlying platform, while C# makes it well defined and prescribes that it is actually x << (32 % (sizeof(int) * 8)), which is equivalent to it wrapping around and thus is the same as x << 0. This matches what quite a bit of hardware does, such as x86/x64 for their scalar shift instructions. However, the vector shift instructions for x86/x64 differ and instead treat it as actually shifting by 1, 32 times. Thus it produces a value that is all zero.

While we have tests covering overshifting, we missed a test covering the combination of overshifting by a constant amount and thus the edge case that can occur in some kinds of user code.

Testing

A regression test covering the scenario was added as well as other potentially interesting combinations.

Risk

Low. The issue is well understood, the other constant folding support added has been checked for potential edge cases, and the functionality has also been checked against Arm64.

Author: tannergooding
Assignees: tannergooding
Labels:

area-CodeGen-coreclr

Milestone: -

@tannergooding
Copy link
Member Author

CC. @EgorBo, could you provide sign-off on this as well so it can get marked servicing-consider.

@EgorBo
Copy link
Member

EgorBo commented Feb 6, 2024

I thought that was a theoretical corner-case, does it meet the bar for backporting?
AFAIR, @saucecontrol found it when they were trying to create a CQ repro against JIT's CSE for const vectors.

@tannergooding
Copy link
Member Author

While it is effectively an edge case in an already niche scenario, it has been hit by at least two independent users in unrelated workloads that I'm aware of.

Its notably also a net new regression, in a pre-existing API in a recently released LTS, so I believe it meets all the qualifications making it appropriate to backport.

@tannergooding tannergooding added the Servicing-consider Issue for next servicing release review label Feb 7, 2024
Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved. we will take for consideration in 8.0.x

@rbhanda rbhanda added this to the 8.0.3 milestone Feb 8, 2024
@rbhanda rbhanda added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Feb 8, 2024
@tannergooding
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@tannergooding
Copy link
Member Author

Failures are for runtime-extra-platforms for Mono and are unrelated. The CI for these was already in an unclean state.

@tannergooding
Copy link
Member Author

CC. @EgorBo, @dotnet/jit-contrib for merging. Monday February 12th is the Code Complete deadline for the March Release, so it needs to be merged by then (Julie asked that I let one of the JIT reviewers do the actual merge here).

The extra-platform failures are unrelated and occuring in regular outerloop (had run them to validate the same bug wasn't there on Mono).

@EgorBo EgorBo merged commit f4075f3 into dotnet:release/8.0-staging Feb 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 12, 2024
@tannergooding tannergooding deleted the backport-85b5eab branch July 1, 2025 14:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants