Skip to content

Vectorized minmax_element: fix AVX2 tail optimization - #6521

Merged
Stephan T. Lavavej (StephanTLavavej) merged 8 commits into
microsoft:mainfrom
AlexGuteniev:not-your-tail
Nov 21, 2026
Merged

Stephan T. Lavavej (StephanTLavavej) merged 8 commits into
microsoft:mainfrom
AlexGuteniev:not-your-tail

Conversation

@AlexGuteniev

Copy link
Copy Markdown
Contributor

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_max and _Portion_max. The _Portion_max values defined in traits are to the exact point where index is still fit; larger _Portion_max would 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_32 usages. 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_element part of #4917, or even the whole #4917 altogether.

@AlexGuteniev
AlexGuteniev requested a review from a team as a code owner November 14, 2026 19:39
Copilot AI balanced review requested due to automatic review settings November 14, 2026 19:39
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Nov 14, 2026
@azure-pipelines

This comment was marked as resolved.

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 14, 2026 19:46

This comment was marked as resolved.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added the bug Something isn't working label Nov 14, 2026
Copilot AI balanced review requested due to automatic review settings November 14, 2026 20:35

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 14, 2026 20:57

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 04:53

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 13:59

This comment was marked as resolved.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Initial Review to Ready To Merge in STL Code Reviews Nov 17, 2026
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Ready To Merge to Merging in STL Code Reviews Nov 20, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo. Please notify me if any further changes are pushed, otherwise no action is required.

@StephanTLavavej
Stephan T. Lavavej (StephanTLavavej) merged commit 27178e7 into microsoft:main Nov 21, 2026
48 checks passed
@github-project-automation github-project-automation Bot moved this from Merging to Done in STL Code Reviews Nov 21, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

📉 📈 🛠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

<algorithm>: x64 AVX2 vectorized minmax_element() family returns incorrect results

3 participants