Skip to content

Fix strict aliasing in SSE packed vector stores - #356

Merged
Chuck Walbourn (walbourn) merged 2 commits into
microsoft:mainfrom
vera-rubin:fix/packed-store-strict-aliasing
Oct 16, 2026
Merged

Chuck Walbourn (walbourn) merged 2 commits into
microsoft:mainfrom
vera-rubin:fix/packed-store-strict-aliasing

Conversation

@vera-rubin

@vera-rubin Nathan Chandrasekar (vera-rubin) commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

The SSE packed stores wrote integer members through a float*, so GCC at -O2 could return stale values on later reads. Following your review, MSVC keeps the old _mm_store_ss under #ifdef _MSC_VER and other compilers assign the low lane with _mm_cvtsi128_si32. We couldn't reach MSVC or Compiler Explorer, so MSVC codegen isn't checked, but its path is now identical to main. A new typed-read test fails before and passes after on GCC 12/13 at -O2/-O3.

Fixes #355

— ChipLab

The SSE paths of XMStoreColor, XMStoreHalf2 (F16C), XMStoreShortN2,
XMStoreShort2 and the 32-bit DEC/BYTE packed stores wrote integer storage
through a float* with _mm_store_ss. With strict aliasing (GCC -O2) the
compiler may assume that store does not modify the integer member, so a
later read of .c or .v can return the previous value.

Assign the low 32-bit lane via _mm_cvtsi128_si32 instead, as
XMVectorGetIntX already does. This still compiles to a single movd store.
@vera-rubin

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Donmakino LLC"

@walbourn Chuck Walbourn (walbourn) added the complier Issue related to compiler codegen label Oct 13, 2026
@walbourn

Copy link
Copy Markdown
Collaborator

We should confirm MSVC does the optimal thing here.

Alternatively, we should use the old pattern guarded with #ifdef _MSC_VER

MSVC tolerates this type pun, so keep the existing _mm_store_ss store
under #ifdef _MSC_VER and use the _mm_cvtsi128_si32 assignment only for
other compilers. The MSVC code path is now unchanged from main.
@vera-rubin

Copy link
Copy Markdown
Contributor Author

We went with your second option: the old _mm_store_ss store is back under #ifdef _MSC_VER, so the MSVC path is the same as main, and only other compilers get the aliasing-safe assignment.

— ChipLab

@walbourn Chuck Walbourn (walbourn) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't see this issue with MinGW/GCC 16.1, but I'm fine with this change.

@walbourn
Chuck Walbourn (walbourn) merged commit 49696c3 into microsoft:main Oct 16, 2026
223 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complier Issue related to compiler codegen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Six SIMD packed stores expose stale typed destination values with GCC at -O2

2 participants