Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector128.ShuffleUnsafe #88559

Closed
wants to merge 4 commits into from
Closed

Conversation

AlexRadch
Copy link
Contributor

Close #81609 issue.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jul 9, 2023
@ghost
Copy link

ghost commented Jul 9, 2023

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

Issue Details

Close #81609 issue.

Author: AlexRadch
Assignees: -
Labels:

area-System.Runtime.Intrinsics, new-api-needs-documentation

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jul 10, 2023

Test failures look related

Comment on lines +2377 to +2390
if (AdvSimd.Arm64.IsSupported)
{
(Vector128<byte>, Vector128<byte>) table = (vector.GetLower(), vector.GetUpper());
return Vector256.Create(
AdvSimd.Arm64.VectorTableLookup(table, indices.GetLower()),
AdvSimd.Arm64.VectorTableLookup(table, indices.GetUpper()));
}

if (PackedSimd.IsSupported)
{
return Vector256.Create(
PackedSimd.Shuffle(vector.GetLower(), vector.GetUpper(), indices.GetLower()),
PackedSimd.Shuffle(vector.GetLower(), vector.GetUpper(), indices.GetUpper()));
}
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't need this additional handling for V256/V512. It isn't expected that users are calling these, let alone expecting good performance, when IsHardwareAccelerated reports false.

It's just causing the JIT to spend more time doing dead code elimination, managing the inliner budget, etc.

{
if (Avx2.IsSupported)
{
return Avx2.Shuffle(vector, indices);
Copy link
Member

Choose a reason for hiding this comment

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

These hardware intrinsics have different semantics than Vector256/512 Shuffles.

Avx2.Shuffle is effectively two Vector128.Shuffles on each half. The indices are still [0, 15]. Similar for Avx512.

Vector256.Shuffle on the other hand works with the whole vector and allows you to pick and index [0, 31].

Comment on lines +2361 to +2364
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 31].
/// On hardware with <see cref="Avx2"/> support, indices are treated as modulo 32, and if the high bit is set, the result will be set to 0 for that element.
/// On hardware with <see cref="AdvSimd.Arm64"/> support, this method behaves the same as Shuffle.</remarks>
/// On hardware with <see cref="PackedSimd"/> support, indices are treated as modulo 32.
Copy link
Member

Choose a reason for hiding this comment

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

nit: This remarks is confusing. Shuffle doesn't ensure the indices are normalized. It simply treats all out of range indices as setting the result to 0.

Unsafe simply changes how out of range indices are handled and allows it to differ based on the target hardware. In the case of xarch most out of range indices are handled as mod 32. But indices with the MSB set are set to 0 instead.

We can probably exclude the Arm64/Wasm callouts for V256/V512 as they aren't important. They can be combined into a general statement that other platforms will treat this the same as Shuffle

@@ -2384,7 +2384,7 @@ public static Vector128<sbyte> Shuffle(Vector128<sbyte> vector, Vector128<sbyte>
[CompExactlyDependsOn(typeof(AdvSimd))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
internal static Vector128<byte> ShuffleUnsafe(Vector128<byte> vector, Vector128<byte> indices)
public static Vector128<byte> ShuffleUnsafe(Vector128<byte> vector, Vector128<byte> indices)
Copy link
Member

Choose a reason for hiding this comment

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

Is there anywhere else this should be consumed in dotnet/runtime as part of making it public?

Copy link
Member

Choose a reason for hiding this comment

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

I believe I updated all such places when adding the internal helper as part of #80963

Copy link
Member

Choose a reason for hiding this comment

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

Ok. I know you updated places in corelib. I was wondering if there were places outside of corelib you couldn't update. Sounds like no.

Copy link
Member

Choose a reason for hiding this comment

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

There are two places I saw where we still use the platform-specific instructions directly (BitArray and OptimizedInboxTextEncoder), but both of those are in larger platform-specific blocks of logic, so there was no reason to replace them.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

Plenty of related tests are failing. Example:

Test System.Runtime.Intrinsics.Tests.Vectors.Vector256Tests.Vector256ByteShuffleOneInputWithLocalIndicesTest has failed

Please address the comments posted by other reviewers and fix the tests.

Thank you for your contribution!

@ghost ghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label Oct 19, 2023
@adamsitnik adamsitnik self-assigned this Oct 20, 2023
@ghost ghost added the no-recent-activity label Nov 3, 2023
@ghost
Copy link

ghost commented Nov 3, 2023

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost
Copy link

ghost commented Nov 18, 2023

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@ghost ghost closed this Nov 18, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 18, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.Intrinsics community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. new-api-needs-documentation no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Vector128.ShuffleUnsafe
6 participants