From fd2e75d07c2aad1f1f4fb62396153367c7e8f4b6 Mon Sep 17 00:00:00 2001 From: Francois-Xavier Coudert Date: Tue, 31 Oct 2023 23:29:46 +0100 Subject: [PATCH] aarch64, Darwin: Add support for -mcpu=native. This addresses issue #120. --- gcc/config/aarch64/driver-aarch64.cc | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/gcc/config/aarch64/driver-aarch64.cc b/gcc/config/aarch64/driver-aarch64.cc index 5c6ee60a266e..d646a7ce2bf0 100644 --- a/gcc/config/aarch64/driver-aarch64.cc +++ b/gcc/config/aarch64/driver-aarch64.cc @@ -28,6 +28,74 @@ #include "aarch64-protos.h" #include "aarch64-feature-deps.h" +#if TARGET_MACHO +# include +# include +#endif + + +#if TARGET_MACHO + +/* Default architecture to use if -mcpu=native did not detect a known CPU. */ +#define DEFAULT_ARCH "apple-m1" + +/* macOS does not have /proc/cpuinfo and needs a different approach, + based on sysctl. It is much simpler. */ + +const char * +host_detect_local_cpu (ATTRIBUTE_UNUSED int argc, ATTRIBUTE_UNUSED const char **argv) +{ + bool arch = false; + bool tune = false; + bool cpu = false; + const char *res = NULL; + uint32_t family; + size_t len = sizeof(family); + + gcc_assert (argc); + if (!argv[0]) + return NULL; + + /* Are we processing -march, mtune or mcpu? */ + arch = strcmp (argv[0], "arch") == 0; + if (!arch) + tune = strcmp (argv[0], "tune") == 0; + if (!arch && !tune) + cpu = strcmp (argv[0], "cpu") == 0; + if (!arch && !tune && !cpu) + return NULL; + + sysctlbyname("hw.cpufamily", &family, &len, NULL, 0); + + switch (family) + { + case 0x07d34b9f: // Vortex, Tempest + res = "apple-a12"; + break; + case 0x573b5eec: + case 0x1b588bb3: // Firestorm, Icestorm + res = "apple-m1"; + break; + case 0xda33d83d: // Blizzard, Avalanche + res = "apple-m2"; + break; + case 0xfa33415e: // Ibiza (M3) + case 0x5f4dea93: // Lobos (M3 Pro) + case 0x72015832: // Palma (M3 Max) + res = "apple-m3"; + break; + default: + res = DEFAULT_ARCH; + } + + if (res) + return concat ("-m", argv[0], "=", res, NULL); + else + return NULL; +} + +#else + struct aarch64_arch_extension { const char *ext; @@ -477,3 +545,4 @@ host_detect_local_cpu (int argc, const char **argv) } } +#endif