Skip to content

gh-112535: Update _Py_ThreadId() to support PowerPC, IBM Z, etc #112553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Nov 30, 2023

@@ -261,6 +261,8 @@ _Py_ThreadId(void)
__asm__ ("mrs %0, tpidrro_el0" : "=r" (tid));
#elif defined(__aarch64__)
__asm__ ("mrs %0, tpidr_el0" : "=r" (tid));
#elif define(HAVE_BUILTIN_THREAD_POINTER)
Copy link
Member

Choose a reason for hiding this comment

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

This line of the public C API which can be used by a C compiler different than the one used to build Python. I don't think that if define(HAVE_BUILTIN_THREAD_POINTER) check is reliable, according to what @colesbury wrote in the issue.

Copy link
Member

Choose a reason for hiding this comment

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

I wrote "C compiler", but the C API can be parsed by parsers which are not C compiler, but written in any programming language. Moreover, the C API is used in C++.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah got it, let me check :)

Copy link
Member Author

@corona10 corona10 Nov 30, 2023

Choose a reason for hiding this comment

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

. I don't think that if define(HAVE_BUILTIN_THREAD_POINTER) check is reliable
but the C API can be parsed by parsers which are not C compiler

Yeah, I thought that configuring time checking would be enough but it looks like we should consider 3rd party case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'm concerned that the compiler for CPython may be not exactly the same as the compiler for third-party extensions. For example, if we want to use __builtin_thread_pointer() for x86-64, it's available for GCC 11.1+, but an extension might be compiling with an older version of GCC that doesn't support it for that architecture.

Additionally, one thing to be careful of, is that macOS arm64 will successfully compile and run __builtin_thread_pointer(), but it returns the wrong value (CPU id).

PowerPC on GCC still requires a "register variable" https://github.com/bminor/glibc/blob/master/sysdeps/powerpc/nptl/thread_pointer.h (or it can fallback to a portable thread-local implementation, but that would be slower.)

@vstinner
Copy link
Member

Maybe we should have a more global configuration: "if the compiler doesn't support _Py_ThreadId(), use opaque function calls for Py_INCREF/Py_DECREF". Maybe even give a way to opt-out from inline assembly code.

@colesbury
Copy link
Contributor

@vstinner, that might be a better strategy. It just seems more difficult than making _Py_ThreadId() an opaque function if it's not supported.

@corona10 corona10 closed this Dec 5, 2023
@vstinner
Copy link
Member

vstinner commented Dec 5, 2023

@vstinner, that might be a better strategy. It just seems more difficult than making _Py_ThreadId() an opaque function if it's not supported.

One step forward: with PR #112747, it becomes possible to use the limited C API on a free threading build. It's implemented with opaque function calls for functions which use too "low-level" functions such at the private _Py_atomic API or _Py_ThreadId().

Later, we can modify the #if checks to also use this code path for platforms which don't support _Py_ThreadId(). A "platform" can also be a combination of {C/C++ compiler, C/C++ compiler options, operating system name/version, CPU architecture}. I'm not sure that inline assembly code is allowed by all C/C++ compiler configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants