Skip to content
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

gh-110820: Make sure processor specific defines are correct for Universal 2 build on macOS #112828

Merged
merged 2 commits into from
Dec 8, 2023

Conversation

ronaldoussoren
Copy link
Contributor

@ronaldoussoren ronaldoussoren commented Dec 7, 2023

A number of processor specific defines are different for x86-64 and arm64, and need to be adjusted in pymacconfig.h.

… Universal 2 build on macOS

A number of processor specific defines are different for x86-64 and
arm64, and need to be adjusted in pymacconfig.h.
@@ -74,8 +77,14 @@
# define DOUBLE_IS_LITTLE_ENDIAN_IEEE754
#endif

#ifdef __i386__
#if defined(__i386__) || defined(__x86_64__)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added __x86_64__ to the check because the compiler included with Xcode 15.1 (and possibly earlier) no longer defines __i386__ when building for Intel.

@ronaldoussoren ronaldoussoren added the needs backport to 3.12 bug and security fixes label Dec 7, 2023
@ronaldoussoren
Copy link
Contributor Author

I've tested this on an M1 system by building a "universal2" build and then running test_capi for both architectures:

$ arch -arm64 ./python.exe -m test test_capi -v 
$ arch -x86_64 ./python.exe -m test test_capi -v 

Without this PR the second command crashes as described in the issue.

@ronaldoussoren ronaldoussoren requested a review from a team December 7, 2023 13:30
@ronaldoussoren
Copy link
Contributor Author

Longer term it might be more useful to simply pymacconfig.h using pre-defined macro's in clang and GCC, e.g.

#define ALIGNOF_MAX_ALIGN_T  __BIGGEST_ALIGNMENT__
#define SIZEOF_LONG_DOUBLE     __SIZEOF_LONG_DOUBLE__ 

That's for a different PR though, one that won't be back ported to older releases.

Include/pyport.h Outdated Show resolved Hide resolved
# define SIZEOF_LONG_DOUBLE 16
#else
# define ALIGNOF_MAX_ALIGN_T 8
# define SIZEOF_LONG_DOUBLE 8
Copy link
Member

Choose a reason for hiding this comment

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

A compatibility question: up until now, any universal2 executable built on an Intel Mac define 16 for both Intel and Apple Silicon Macs, while builds on Apple Silicon Macs (either single arch or universal2) use 8. With this PR going forward, that would change. Are there any compatibility implications for, say, existing third-party extension modules?

Copy link
Contributor Author

@ronaldoussoren ronaldoussoren Dec 7, 2023

Choose a reason for hiding this comment

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

Another issue is HAVE_GCC_ASM_FOR_X87, which was silently disabled on macOS for both universal2 and intel-only builds (at least with Xcode 15.1, I haven't checked older Apple compiler).

If I read the code correctly ALIGNOF_MAX_ALIGN_T is only used in typeobject.c in the definition of _align_up and that's to get the correct alignment for type data that's specified with a negative item size which is a feature new in 3.12. This likely only affects performance, although unless the subtype specific data contains vector types (AFAIK those have strict alignment requirements).

I'm not sure how to effectively search for external users of the macro.

My gut feeling is that this should be safe. Maybe ask Thomas about his opinion as RM?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW. I get no hits on SIZEOF_LONG_DOUBLE at all in 3.13, other than exporting it through pyconfig and sysconfig.

Copy link
Member

@ned-deily ned-deily Dec 7, 2023

Choose a reason for hiding this comment

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

My gut feeling is that this should be safe. Maybe ask Thomas about his opinion as RM?

@Yhg1s, @ambv Any opinions? We're early enough in the 3.13 dev cycle that it shouldn't be any issue changing this now regardless. But what about for 3.12 or even 3.11? Is there any reasonable risk that this change could affect existing extension modules (for example, in PyPI wheels) that were built with previous releases of 3.12.x (et al) when used with universal2 builds with this PR merged?

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, one reason for backporting is that, without it, tests fail when universal2 builds are built on Apple Silicon Macs and then run on Intel Macs.

Copy link
Member

Choose a reason for hiding this comment

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

Considering the defines are plain wrong, this feels like something that should be backported, yes. The compatibility impact scales with the bug impact: if nobody uses it, nobody should notice; if lots of people use it, they will want the fix. I think it's worth backporting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll back port to 3.12 and propose to skip the back port to 3.11 because the impact there is likely lower:

  • ALIGNOF_MAX_ALIGN_T isn't present in 3.11( added for a feature in 3.12)
  • SIZEOF_LONG_DOUBLE is present, but isn't used by CPython itself
  • HAVE_GCC_ASM_FOR_X64 is present, but isn't used by CPython itself
  • HAVE_GCC_ASM_FOR_X87 is present and used, but X87_DOUBLE_ROUNDING is not set

For the latter: I checked with the configure check for x87 double rounding that double rounding isn't detected when compiling for x86_64 using clang -arch x86_64.

cpython/configure.ac

Lines 5469 to 5492 in 1eac0aa

AC_CACHE_CHECK([for x87-style double rounding], [ac_cv_x87_double_rounding], [
# $BASECFLAGS may affect the result
ac_save_cc="$CC"
CC="$CC $BASECFLAGS"
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <stdlib.h>
#include <math.h>
int main(void) {
volatile double x, y, z;
/* 1./(1-2**-53) -> 1+2**-52 (correct), 1.0 (double rounding) */
x = 0.99999999999999989; /* 1-2**-53 */
y = 1./x;
if (y != 1.)
exit(0);
/* 1e16+2.99999 -> 1e16+2. (correct), 1e16+4. (double rounding) */
x = 1e16;
y = 2.99999;
z = x + y;
if (z != 1e16+4.)
exit(0);
/* both tests show evidence of double rounding */
exit(1);
}
]])],

Copy link
Member

@ned-deily ned-deily left a comment

Choose a reason for hiding this comment

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

LGTM. I did a quick test with a universal2 installer built on an Apple Silicon Mac and test_capi now passes on both archs.

@ronaldoussoren ronaldoussoren merged commit 15a80b1 into python:main Dec 8, 2023
34 checks passed
@miss-islington-app
Copy link

Thanks @ronaldoussoren for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@ronaldoussoren ronaldoussoren deleted the gh-110820 branch December 8, 2023 09:09
@miss-islington-app
Copy link

Sorry, @ronaldoussoren, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 15a80b15af9a0b0ebe6bd538a1919712ce7d4ef9 3.12

ronaldoussoren added a commit to ronaldoussoren/cpython that referenced this pull request Dec 8, 2023
… Universal 2 build on macOS (python#112828)

* pythongh-110820: Make sure processor specific defines are correct for Universal 2 build on macOS

A number of processor specific defines are different for x86-64 and
arm64, and need to be adjusted in pymacconfig.h.

* remove debug stuf

(cherry picked from commit 15a80b1)
@bedevere-app
Copy link

bedevere-app bot commented Dec 8, 2023

GH-112864 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Dec 8, 2023
ronaldoussoren added a commit that referenced this pull request Dec 9, 2023
…on macOS (GH-112834)

* gh-110820: Make sure processor specific defines are correct for Universal 2 build on macOS (#112828)

A number of processor specific defines are different for x86-64 and
arm64, and need to be adjusted in pymacconfig.h.

(cherry picked from commit 15a80b1)
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
… Universal 2 build on macOS (python#112828)

* pythongh-110820: Make sure processor specific defines are correct for Universal 2 build on macOS

A number of processor specific defines are different for x86-64 and
arm64, and need to be adjusted in pymacconfig.h.

* remove debug stuf
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
… Universal 2 build on macOS (python#112828)

* pythongh-110820: Make sure processor specific defines are correct for Universal 2 build on macOS

A number of processor specific defines are different for x86-64 and
arm64, and need to be adjusted in pymacconfig.h.

* remove debug stuf
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