https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119781

            Bug ID: 119781
           Summary: Apple Rosetta 2 x86-64 emulator does not support
                    x86-64-v2 SAHF instruction
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dimitri.ledkov at surgut dot co.uk
  Target Milestone: ---

When running x86-64-v2 binaries, on an Apple Rosetta 2 emulator, the SAHF
instruction is not supported.

The simplest reproducer is to recompile cpython interpreter with
-march=x86-64-v2, then transfer that build and execute `python -c
"print((-0.01)**2);"`

The most common way this is discovered is via Docker Desktop VM installation on
Apple M hardware, as it creates a VM, but then sets up Rosetta passthrough to
enable Rosetta emulation of all binaries.

A minimal x86-64-v2 container with such a python build that exhibits this
problem is available publicly in:
cgr.dev/chainguard/python@sha256:8abba49840847fb6afc6157a9927318b9e681de4025ad51ed8950111e5ca6da2

E.g. docker run --rm -it
cgr.dev/chainguard/python@sha256:8abba49840847fb6afc6157a9927318b9e681de4025ad51ed8950111e5ca6da2
-c "print((-0.01)**2);"

A bigger developer container with package managers, shell, and binutils is
available from:
cgr.dev/chainguard/python@sha256:2b9b79fb611797941d79cf826f4793d89d4f0089e91b461b67bedc73b7d8fda8

There is not much runtime detection that can be done here, as the VM in
question has a patched buildkit based kernel that fakes the AUX vector. On the
Mac OS side of things, the system properties correctly report that SAHF
instruction is not supported for x86-64 emulation.

If there is a need to statically identify affected binaries, one can use
objdump to find if sahf instruction is present:

$ objdump --no-show-raw-insn -M intel -d /usr/lib/libpython3.13.so.1.0  | awk
'{if ($2 !~ ":" && $2 != "data32" && $2 != "file" && $2 != "of" && length($2) >
0) {print $2}}' | sort -u | grep sahf
sahf

Workarounds:
1) Recommend users to use –platform linux/arm64 containers on Mac OS X, instead
of foreign arch –platform linux/amd64
2) Compile binaries with `-march=x86-64-v2 -mno-sahf` such that all vector
instructions are assumed to be present, but compiler chooses to not generate
and use SAHF
3) When configuring toolchain itself, one can use –with-arch=x86-64-v2
–with-specs=-mno-sahf.

GCC actions:
1) Updating documentation for the -msahf option to mention that Apple Rosetta 2
does not support this instruction would be very helpful. I will send a patch
with a proposed documentation update.
2) Consider to bump sahf instruction from x86-64-v2 list to x86-64-v3, such
that by default x86-64-v2 binaries work on Rosetta 2 (roughly 20% market share
of new workstations sold). This may need coordination with the definition of
x86-64-v2 instruction sets. Note other compilers do not appear to generate sahf
instruction by default.

A report about this lack of feature has been filed with Apple, thus potentially
this might be fixed in the future updates of Rosetta 2.

Reply via email to