Repository navigation
gh-157212: Fix _Py_ThreadId() on Windows ARM64 with MinGW - #157253
Conversation
The __MINGW32__ && _M_ARM64 branch calls __getReg(18), mirroring the MSVC branch above it. The intrinsic is not available for MinGW ARM64: mingw-w64 declares __getReg only for Itanium, and GCC does not provide a corresponding builtin. The branch is reached by AArch64 MinGW toolchains because mingw-w64 defines _M_ARM64 when __aarch64__ is defined. Falling through to the generic __aarch64__ branch would not provide the Windows thread pointer. The Windows ARM64 ABI reserves x18 as the platform register pointing to the TEB, so read x18 directly.
|
I think |
corona10
left a comment
There was a problem hiding this comment.
lgtm!
https://godbolt.org/z/sj97f6TEr
I've double checked with godbolt code gen :)
|
Thanks @christianaurichzm for the PR, and @corona10 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
Sorry, @christianaurichzm and @corona10, I could not cleanly backport this to |
|
Sorry, @christianaurichzm and @corona10, I could not cleanly backport this to |
|
GH-158164 is a backport of this pull request to the 3.15 branch. |
|
GH-162051 is a backport of this pull request to the 3.14 branch. |
Fixes #157212
_Py_ThreadId()currently uses__getReg(18)for the MinGW ARM64 branch:That intrinsic is not available for MinGW ARM64. For a
*-windows-gnutarget, Clang'sintrin.hdefers to mingw-w64's header, which declares__getRegonly for Itanium. GCC does not provide a corresponding builtin either.The branch is reached by AArch64 MinGW toolchains because mingw-w64 defines
_M_ARM64when__aarch64__is defined.Falling through to the generic
__aarch64__implementation is not a valid alternative. The Windows ARM64 ABI reservesx18as the platform register pointing to the Thread Environment Block (TEB). In the system tested in gh-157212,x18also matchedNtCurrentTeb()for each tested thread, whiletpidr_el0was observed as zero.This change therefore reads the TEB address directly from
x18for MinGWARM64:
The MSVC branch is unchanged.
Verification
I reproduced the original compile failure with
zig cc/Clang targetingaarch64-windows-gnu:With this change, the same target compiles successfully and at
-O2emits the expected register read:The change is limited to the MinGW ARM64 branch.
AI tools were used to help organize and edit the wording of this PR description.