Skip to content

Fix cross-compiling librt by enabling x86_64 optimisations with pragmas#20815

Merged
ilevkivskyi merged 1 commit intopython:masterfrom
chewi:librt-opt-pragmas
Feb 16, 2026
Merged

Fix cross-compiling librt by enabling x86_64 optimisations with pragmas#20815
ilevkivskyi merged 1 commit intopython:masterfrom
chewi:librt-opt-pragmas

Conversation

@chewi
Copy link
Contributor

@chewi chewi commented Feb 14, 2026

Although Python still lacks a supported method to cross-compile packages, many downstreams do it anyway, so librt should allow this.

The current approach of enabling x86_64 optimisations after checking platform.machine() is broken. When building on x86_64 for another architecture, the build fails because the optimisations get enabled when they shouldn't. Conversely, when building on another architecture for x86_64, the build fails because the optimisations don't get enabled when they should.

GCC supports enabling an optimisation with a pragma from that line onwards. Clang requires the optimisation to be pushed on and popped off the stack. Unfortunately, MSVC does not have an equivalent feature, but it is unlikely anyone would cross-compile to x86_64 with that.

The remaining logic for MSVC could be simplified, but it looks like other compilers like Borland are potentially supported?

I considered checking for x86_64 using CCompiler's preprocess() instead, but this seemed awkward.

This has been tested with GCC and Clang, including with AVX512 enabled.

Supersedes mypyc/librt#32.

@chewi
Copy link
Contributor Author

chewi commented Feb 14, 2026

Okay, so it isn't normally built with -Wall -Werror, but that's a problem in CI. -Wno-unknown-pragmas might be the answer, but I'll see if there's a better way.

@mr-c
Copy link
Contributor

mr-c commented Feb 15, 2026

Okay, so it isn't normally built with -Wall -Werror, but that's a problem in CI. -Wno-unknown-pragmas might be the answer, but I'll see if there's a better way.

I suggest surrounding the pramas with checks for the appropriate compiler

Although Python still lacks a supported method to cross-compile
packages, many downstreams do it anyway, so librt should allow this.

The current approach of enabling x86_64 optimisations after checking
platform.machine() is broken. When building on x86_64 for another
architecture, the build fails because the optimisations get enabled when
they shouldn't. Conversely, when building on another architecture for
x86_64, the build fails because the optimisations don't get enabled when
they should.

GCC supports enabling an optimisation with a pragma from that line
onwards. Clang requires the optimisation to be pushed on and popped off
the stack. Unfortunately, MSVC does not have an equivalent feature, but
it is unlikely anyone would cross-compile to x86_64 with that.

The remaining logic for MSVC could be simplified, but it looks like
other compilers like Borland are potentially supported?

I considered checking for x86_64 using CCompiler's preprocess() instead,
but this seemed awkward.

This has been tested with GCC and Clang, including with AVX512 enabled.
@chewi chewi force-pushed the librt-opt-pragmas branch from b9a9e73 to d0bf832 Compare February 15, 2026 22:59
@chewi
Copy link
Contributor Author

chewi commented Feb 15, 2026

Okay, done that.

Copy link
Contributor

@mr-c mr-c left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @chewi !

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

OK, let's try this!

@ilevkivskyi ilevkivskyi merged commit df6922d into python:master Feb 16, 2026
17 checks passed
@chewi chewi deleted the librt-opt-pragmas branch February 16, 2026 21:23
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