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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.)

tid = (uintptr_t)__builtin_thread_pointer();
#else
# error "define _Py_ThreadId for this platform"
#endif
Expand Down
41 changes: 41 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,23 @@ AS_VAR_IF([ac_cv_builtin_atomic], [yes], [
AC_DEFINE(HAVE_BUILTIN_ATOMIC, 1, [Has builtin __atomic_load_n() and __atomic_store_n() functions])
])

AC_CACHE_CHECK([for builtin __builtin_thread_pointer], [ac_cv_builtin_thread_pointer], [
AC_LINK_IFELSE(
[
AC_LANG_SOURCE([[
void *tp;
int main() {
tp = __builtin_thread_pointer();
return 0;
}
]])
],[ac_cv_builtin_thread_pointer=yes],[ac_cv_builtin_thread_pointer=no])
])

AS_VAR_IF([ac_cv_builtin_thread_pointer], [yes], [
AC_DEFINE(HAVE_BUILTIN_THREAD_POINTER, 1, [Has builtin __builtin_thread_pointer])
])

# --with-mimalloc
AC_MSG_CHECKING([for --with-mimalloc])
AC_ARG_WITH([mimalloc],
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
/* Has builtin __atomic_load_n() and __atomic_store_n() functions */
#undef HAVE_BUILTIN_ATOMIC

/* Has builtin __builtin_thread_pointer */
#undef HAVE_BUILTIN_THREAD_POINTER

/* Define to 1 if you have the <bzlib.h> header file. */
#undef HAVE_BZLIB_H

Expand Down