Replace ExtractMostSignificantBits+BitOp patterns with Vector helper methods#126841
Closed
Copilot wants to merge 11 commits into
Closed
Replace ExtractMostSignificantBits+BitOp patterns with Vector helper methods#126841Copilot wants to merge 11 commits into
Copilot wants to merge 11 commits into
Conversation
Replace patterns of ExtractMostSignificantBits() followed by PopCount/TrailingZeroCount/LeadingZeroCount with the optimized Vector helpers: CountWhereAllBitsSet, IndexOfWhereAllBitsSet, and LastIndexOfWhereAllBitsSet. Remove AdvSimd special paths from Vector64/Vector128 internal helpers (CountMatches, IndexOfFirstMatch, IndexOfLastMatch) and the now-unused AdvSimdExtractBitMask/AdvSimdFixupBitCount methods. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/fcd7f0bb-7e64-41fa-8773-089705b9a737 Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
EgorBo
April 13, 2026 16:54
View session
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
…ctly Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/fd3915a5-bbf3-4530-8406-f3983be66ffb Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/527f6141-5f1f-41b3-bb40-336677a9c787 Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
…ression Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/83aad277-6910-4c67-8aac-88c2e15047b3 Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/83aad277-6910-4c67-8aac-88c2e15047b3 Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
This was referenced Apr 23, 2026
This was referenced Apr 29, 2026
Member
|
@copilot resolve the merge conflicts in this pull request |
…ct-msb-with-vector-functions # Conflicts: # src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.IndexOfMax.cs Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Contributor
Author
Comment on lines
+134
to
+138
| private static unsafe int IndexOfMinMaxCore<T, TIndexOfMinMax>(ReadOnlySpan<T> x) | ||
| where T : INumber<T> | ||
| where TIndexOfMinMax : struct, IIndexOfOperator<T> | ||
| { | ||
| if (x.IsEmpty) |
Comment on lines
27
to
33
| public static int IndexOfMax<T>(ReadOnlySpan<T> x) | ||
| where T : INumber<T> => | ||
| IndexOfMinMaxCore<T, IndexOfMaxOperator<T>>(x); | ||
|
|
||
| /// <summary>Returns the index of MathF.Max(x, y)</summary> | ||
| internal readonly struct IndexOfMaxOperator<T> : IIndexOfMinMaxOperator<T> where T : INumber<T> | ||
| internal readonly struct IndexOfMaxOperator<T> : IIndexOfOperator<T> where T : INumber<T> | ||
| { |
Member
|
@copilot please completely revert all changes in TensorPrimitives.IndexOfMax.cs |
Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Contributor
Author
Done — |
This was referenced Jun 25, 2026
Open
Member
|
@tannergooding feel free to pick it up, but looks like #129688 will optimize all uses. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace usages of
ExtractMostSignificantBits()followed byPopCount/TrailingZeroCount/LeadingZeroCountwith the recently optimized[Intrinsic]vector helpers:CountMatches,IndexOfFirstMatch,IndexOfLastMatch, andIndexOfWhereAllBitsSet.Description
Pattern replacements
BitOperations.PopCount(v.ExtractMostSignificantBits())→VectorN.CountMatches(v)(within CoreLib, using internal helper to avoid x64 regression)BitOperations.TrailingZeroCount(v.ExtractMostSignificantBits())→VectorN.IndexOfFirstMatch(v)(within CoreLib) orVectorN.IndexOfWhereAllBitsSet(v)(in Tensors, using public API)Count - 1 - BitOperations.LeadingZeroCount(v.ExtractMostSignificantBits())→VectorN.IndexOfLastMatch(v)(within CoreLib)Files changed
CountValueTypeloop bodies using the internalCountMatcheshelper directly; replacedComputeFirstIndex(3 overloads,EMSB+TZC→IndexOfFirstMatch) andComputeLastIndex(3 overloads,EMSB+LZC→IndexOfLastMatch)EMSB+TZC→IndexOfFirstMatch), keeping the vector comparison result and comparing againstVectorN<byte>.Zeroinstead of extracting to a bitmaskVectorN.IndexOfWhereAllBitsSetdirectly at all 9 call sites (previously consumed the sharedIndexOfFirstMatchhelpers)Not changed
CountMatches,IndexOfFirstMatch, etc.) that the other callsites consumeCountWhereAllBitsSetwould introduce an x64 regression, and the internalCountMatcheshelper is not accessible from the Tensors assemblyResetLowestSetBit/FlipBitdon't map to single-match helpersTZC(~matches)for first difference, not first matchTesting
All relevant tests pass: