Repository navigation
Vectorized minmax_element: fix AVX2 tail optimization - #6521
Merged
Stephan T. Lavavej (StephanTLavavej) merged 8 commits intoNov 21, 2026
Merged
Stephan T. Lavavej (StephanTLavavej) merged 8 commits into
Stephan T. Lavavej (StephanTLavavej) merged 8 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Stephan T. Lavavej (StephanTLavavej)
approved these changes
Nov 17, 2026
Member
|
I'm mirroring this to the MSVC-internal repo. Please notify me if any further changes are pushed, otherwise no action is required. |
Stephan T. Lavavej (StephanTLavavej)
merged commit Nov 21, 2026
27178e7
into
microsoft:main
48 checks passed
Member
📉 📈 🛠️ |
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.
Fixes #6520
This position-based algorithm needs to store element indices as vector element. As a result, for types smaller than 32 bits on x86 and types smaller than 64 bits on 64-bit targets we can't store full element index for arbitrarily large array. The solution is to split the array into portions. See
_Has_portion_maxand_Portion_max. The_Portion_maxvalues defined in traits are to the exact point where index is still fit; larger_Portion_maxwould have wrapped around.For AVX2 we also try process the final non-complete vector, using masked load, and applying mask to the data. This is similar to what we do in other (simpler) algorithms. See
_Avx2_tail_mask_32usages. For this particular algorithm though, this part costs some unusual complexity.Here's what went wrong: the tail is done past the stopping point determined by
_Portion_max. In the case of specific array size which is a multiply of_Portion_max, plus some small value less than vector size but large enough for masked processing to engage, the element index overflows back to zero, and the result is off.The bug was introduced in #4917 where we started using masked tail.
A conservative fix proposed in this PR detects the index overflow condition and skips masked tail if the overflow happens.
Another way to fix this bug could be to stop using masked tail for this algorithm, effectively reverting
minmax_elementpart of #4917, or even the whole #4917 altogether.