Skip to content

Conversation

@stephentoub
Copy link
Member

No description provided.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extends existing TensorPrimitive vectorization optimizations to cover additional integer types (e.g., nint/nuint) by replacing direct typeof(T) checks with new helper methods (IsInt32Like, IsUInt32Like, IsInt64Like, IsUInt64Like). Key changes include:

  • Updated Sign, Divide, ConvertToInteger, and ConvertToIntegerNative classes to use IsXXXLike helpers in both Vectorizable properties and Invoke overloads.
  • Unified type checks for 32/64-bit signed and unsigned integers to include native-sized integer types.
  • Removed some conditional else wrappers in Sign.cs to streamline early returns.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.Sign.cs Swapped typeof(T)==typeof(int/uint) for IsInt32Like<T>()/IsUInt32Like<T>() in vector methods.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.Divide.cs Changed the integer branch in Vectorizable to IsInt32Like<T>().
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ConvertToIntegerNative.cs Replaced each typeof(TTo) check with corresponding IsIntXxxLike<TTo>() in vector conversions.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ConvertToInteger.cs Mirrored the native conversion changes for non-native paths with IsIntXxxLike<TTo>().
Comments suppressed due to low confidence (4)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.Divide.cs:73

  • Consider adding unit tests for nint and nuint types to cover the new vectorized code path introduced by IsInt32Like<T>() in the Vectorizable property.
                                            || (Vector256.IsHardwareAccelerated && IsInt32Like<T>());

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ConvertToIntegerNative.cs:35

  • Add tests to validate conversion to native integer types including nint and nuint for both Invoke(Vector128<TFrom>) and Invoke(Vector256<TFrom>) paths.
                (typeof(TFrom) == typeof(float) && IsInt32Like<TTo>()) ||

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ConvertToInteger.cs:35

  • Include unit tests for ConvertToInteger<TFrom, TTo> with nint and nuint to ensure the helper methods cover these types correctly.
                (typeof(TFrom) == typeof(float) && IsInt32Like<TTo>()) ||

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.Sign.cs:119

  • The removal of the final closing brace appears to unbalance braces in this file and will cause a compilation error. Please ensure the correct number of closing braces are present.
}

Comment on lines 44 to 45
if (IsInt32Like<TTo>()) return Vector128.ConvertToInt32(x.AsSingle()).As<int, TTo>();
if (IsUInt32Like<TTo>()) return Vector128.ConvertToUInt32(x.AsSingle()).As<uint, TTo>();
Copy link
Member

Choose a reason for hiding this comment

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

Given then Vectorizable check, this could be (with optional asserts):

if (TTo.IsNegative(TTo.MinValue))
{
    return Vector128.ConvertToInt32(x.AsSingle()).As<int, TTo>();
}
return Vector128.ConvertToUInt32(x.AsSingle()).As<uint, TTo>();

It leads to much smaller IL that the JIT can trivially see is constant, without depending on the inliner

Copy link
Member Author

Choose a reason for hiding this comment

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

if (TTo.IsNegative(TTo.MinValue))

The public API only constrains TTo to IBinaryInteger<TTo>, which does not have MinValue.

Copy link
Member

Choose a reason for hiding this comment

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

You could do Vector128.IsNegative<TTo>(Vector128<T>.AllBitsSet) != Vector128<TTo>.Zero. This works since we know tto is an integer and AllBitsSet will either be -1 or MaxValue.

-- It's even simpler in .NET 10, but general intent is to try and avoid needing a second call to determine if a branch is dead or not, since that will impact inline profitability for these core helpers and we've already seen a few cases where the JIT gives up around that due to the size of the methods they're being inlined into.

Copy link
Member Author

Choose a reason for hiding this comment

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

You could do Vector128.IsNegative(Vector128.AllBitsSet) != Vector128.Zero

And this is guaranteed to be constant folded down such that the expression and one or the other branches is eliminated?

@stephentoub stephentoub merged commit 5c295b4 into dotnet:main Jun 25, 2025
80 of 86 checks passed
@stephentoub stephentoub deleted the tpnint branch June 25, 2025 00:44
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants