From 6cca6e8e9ad94e67c7527a9abb67255337ea8871 Mon Sep 17 00:00:00 2001 From: Christian Aurich Date: Thu, 10 Sep 2026 00:42:06 -0300 Subject: [PATCH] gh-157212: Fix _Py_ThreadId() on Windows ARM64 with MinGW The `__MINGW32__ && _M_ARM64` branch calls `__getReg(18)`, mirroring the MSVC branch above it. That intrinsic is not available there: for a `*-windows-gnu` target clang's `intrin.h` defers to mingw-w64's, which declares `__getReg` only behind `__MACHINEIA64`, and GCC provides no corresponding builtin. The branch is not GCC-specific either, since mingw-w64's `_mingw_mac.h` defines `_M_ARM64` whenever `__aarch64__` is defined. Falling through to the generic `__aarch64__` branch would not be correct: Windows ARM64 keeps the thread environment block in `x18`, while `tpidr_el0` is not the Windows thread pointer and reads as 0 on every thread, which would collide with `_Py_UNOWNED_TID`. Read `x18` directly, as mingw-w64's own `NtCurrentTeb()` does for aarch64. --- Include/cpython/object.h | 4 +++- .../Windows/2026-09-10-01-21-34.gh-issue-157212.2Zx7YD.rst | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Windows/2026-09-10-01-21-34.gh-issue-157212.2Zx7YD.rst diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 4c5a677e5543ece..976d1d8a4b1ff0a 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -523,7 +523,9 @@ _Py_ThreadId(void) #elif defined(__MINGW32__) && defined(_M_IX86) tid = __readfsdword(24); #elif defined(__MINGW32__) && defined(_M_ARM64) - tid = __getReg(18); + // Windows on ARM64 keeps the TEB in x18. The __getReg() intrinsic used by + // the MSVC branch above is not available for MinGW ARM64. + __asm__ ("mov %0, x18" : "=r" (tid)); #elif defined(__i386__) __asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS #elif defined(__MACH__) && defined(__x86_64__) diff --git a/Misc/NEWS.d/next/Windows/2026-09-10-01-21-34.gh-issue-157212.2Zx7YD.rst b/Misc/NEWS.d/next/Windows/2026-09-10-01-21-34.gh-issue-157212.2Zx7YD.rst new file mode 100644 index 000000000000000..1f011a5241dbb1b --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-09-10-01-21-34.gh-issue-157212.2Zx7YD.rst @@ -0,0 +1,4 @@ +Fix compilation of free-threaded builds and extension modules on Windows +ARM64 with MinGW toolchains. ``_Py_ThreadId()`` used ``__getReg()``, an +intrinsic that is not available for MinGW ARM64; the thread environment +block is now read from ``x18`` directly. Patch by Christian Aurich.