Skip to content

atomic: Fix memory barriers on MSVC ARM64#15447

Merged
cgutman merged 1 commit intolibsdl-org:mainfrom
cgutman:msvc_arm64_barriers
Apr 25, 2026
Merged

atomic: Fix memory barriers on MSVC ARM64#15447
cgutman merged 1 commit intolibsdl-org:mainfrom
cgutman:msvc_arm64_barriers

Conversation

@cgutman
Copy link
Copy Markdown
Collaborator

@cgutman cgutman commented Apr 24, 2026

  • I confirm that I am the author of this code and release it to the SDL project under the Zlib license. This contribution does not contain code from other sources, including code generated by a Large Language Model ("AI").

Description

MSVC ARM64 lacked an implementation of SDL_MemoryBarrierAcquire() and SDL_MemoryBarrierRelease(), so it fell back to SDL_CompilerBarrier() (_ReadWriteBarrier()) which isn't sufficient on ARM64.

Existing Issue(s)

Comment thread include/SDL3/SDL_atomic.h
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ishld" : : : "memory")
#elif defined(_MSC_VER) && defined(_M_ARM64)
#include <arm64intr.h>
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I included arm64intr.h here for the definitions of _ARM64_BARRIER_ISH and _ARM64_BARRIER_ISHLD. Another option would be to hardcode those constants and avoid having to pull in that header.

Anyone have strong opinions either way?

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.

Including that header seems fine as long as it's available for all the supported build environments.

Comment thread include/SDL3/SDL_atomic.h Outdated
#elif defined(__GNUC__) && defined(__aarch64__)
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ishld" : : : "memory")
#elif defined(_MSC_VER) && defined(_M_ARM64)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would ARM64EC toolchains need this as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I wasn't able to find any specific guidance on memory order guarantees for ARM64EC code, but it seems logical that we should use this for ARM64EC too. I'd imagine there's a DSB executed during transitions from ARM64EC to x64, but I doubt there's anything for ARM64EC code touching memory used by other ARM64EC code.

On that note, our spinlock code also needs an ARM64EC check too.

@cgutman cgutman force-pushed the msvc_arm64_barriers branch from 965d059 to 53aa470 Compare April 24, 2026 23:19
@cgutman cgutman merged commit 2c4c817 into libsdl-org:main Apr 25, 2026
45 of 46 checks passed
@cgutman cgutman added this to the 3.4.6 milestone Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants