Skip to content

Fix ARM64/ARM64EC minmax_element() family vectorization for unsigned elements#6376

Open
StephanTLavavej wants to merge 2 commits into
microsoft:mainfrom
StephanTLavavej:fix-arm64-minmax_element-unsigned
Open

Fix ARM64/ARM64EC minmax_element() family vectorization for unsigned elements#6376
StephanTLavavej wants to merge 2 commits into
microsoft:mainfrom
StephanTLavavej:fix-arm64-minmax_element-unsigned

Conversation

@StephanTLavavej

Copy link
Copy Markdown
Member

Fixes #6373, a regression that was introduced by #5949 in the MSVC Build Tools 14.51. Thanks to @statementreply for helping to analyze this bug in the STL Discord. Thanks to randomized testing for finding this.

Our x64/x86 and ARM64/ARM64EC vectorized implementations of the minmax_element() family share common code, but there's a significant difference: x64/x86 vector instructions are always signed, while ARM64/ARM64EC has signed/unsigned vector instructions. Most of the code correctly handled this, but we weren't properly initializing the values and weren't properly comparing them later. For certain element patterns, this would cause incorrect results to be returned.

Disclosure: I used Opus 4.8 to fix this, then cleaned up its work and verified that it matched @statementreply's analysis and my understanding. I wrote the test cases from scratch, where the first one (for vector<uint8_t> and min_element()) is directly taken from the original randomized test failure, which is why it looks different.

I verified that all of the test cases, including the "extra" one that @statementreply correctly predicted, fail without the fix, except for the uint64_t cases because we don't vectorize those on ARM64/ARM64EC for performance reasons. (I included the 64-bit elements for completeness, and any possible changes in the future.)

The product code isn't heavily commented and generally assumes that the reader has a good understanding of the code. As the existing unsigned-handling code is generally not commented, I felt that it was best to be consistent and not add extra verbose comments for this new stuff, whose purpose is fairly clear in context (and of course enforced by the tests).

@StephanTLavavej
StephanTLavavej requested a review from a team as a code owner July 22, 2026 16:22
@StephanTLavavej StephanTLavavej added bug Something isn't working ARM64 Related to the ARM64 architecture ARM64EC I can't believe it's not x64! labels Jul 22, 2026
Copilot AI review requested due to automatic review settings July 22, 2026 16:22
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Jul 22, 2026
@azure-pipelines

This comment was marked as resolved.

This comment was marked as resolved.

Comment thread stl/src/vector_algorithms.cpp
Comment thread stl/src/vector_algorithms.cpp
Comment thread stl/src/vector_algorithms.cpp
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in STL Code Reviews Jul 22, 2026

@BillyONeal BillyONeal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems plausible (I am not familiar with this area though)

@StephanTLavavej StephanTLavavej moved this from Final Review to Ready To Merge in STL Code Reviews Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ARM64EC I can't believe it's not x64! ARM64 Related to the ARM64 architecture bug Something isn't working

Projects

Status: Ready To Merge

Development

Successfully merging this pull request may close these issues.

ARM64/ARM64EC vectorized minmax_element() family mishandles unsigned elements

5 participants