diff --git a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp --- a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp +++ b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp @@ -25,6 +25,16 @@ return Triple.isOSDarwin(); } +static bool isPreA12AppleCPU(StringRef CPU) { + return llvm::StringSwitch(CPU) + .Case("apple-a11", true) + .Case("apple-a10", true) + .Case("apple-a9", true) + .Case("apple-a8", true) + .Case("apple-a7", true) + .Default(false); +}; + /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is /// provided, or to nullptr otherwise. @@ -40,7 +50,18 @@ // Handle CPU name is 'native'. if (CPU == "native") return std::string(llvm::sys::getHostCPUName()); - else if (CPU.size()) + + if (Triple.isTargetMachineMac() && + Triple.getArch() == llvm::Triple::aarch64) { + // Honor -mcpu as long it doesn't specify an older CPU than "apple-a12". + if (CPU.size() && !isPreA12AppleCPU(CPU)) + return CPU; + + // Apple Silicon macs default to A12 CPUs. + return "apple-a12"; + } + + if (CPU.size()) return CPU; // Make sure we pick the appropriate Apple CPU if -arch is used or when diff --git a/clang/test/Driver/aarch64-mac-cpus.c b/clang/test/Driver/aarch64-mac-cpus.c new file mode 100644 --- /dev/null +++ b/clang/test/Driver/aarch64-mac-cpus.c @@ -0,0 +1,24 @@ +// arm64 Mac-based targets default to Apple A12. + +// RUN: %clang -target arm64-apple-macos -### -c %s 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios-macabi -### -c %s 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios-simulator -### -c %s 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-watchos-simulator -### -c %s 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-tvos-simulator -### -c %s 2>&1 | FileCheck %s + +// RUN: %clang -target arm64-apple-macos -arch arm64 -### -c %s 2>&1 | FileCheck %s + +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a11 -### -c %s 2>&1 | FileCheck --check-prefix=INFER-A12 %s +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a10 -### -c %s 2>&1 | FileCheck --check-prefix=INFER-A12 %s +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a9 -### -c %s 2>&1 | FileCheck --check-prefix=INFER-A12 %s +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a8 -### -c %s 2>&1 | FileCheck --check-prefix=INFER-A12 %s +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a7 -### -c %s 2>&1 | FileCheck --check-prefix=INFER-A12 %s + +// RUN: %clang -target arm64-apple-macos -mcpu=apple-a13 -### -c %s 2>&1 | FileCheck --check-prefix=EXPLICT-A13 %s + +// CHECK: "-cc1"{{.*}} "-triple" "arm64{{.*}}" "-target-cpu" "apple-a12" +// CHECK-SAME: "-target-feature" "+v8.3a" + +// INFER-A12: "-cc1"{{.*}} "-triple" "arm64{{.*}}" "-target-cpu" "apple-a12" + +// EXPLICT-A13: "-cc1"{{.*}} "-triple" "arm64{{.*}}" "-target-cpu" "apple-a13" diff --git a/llvm/include/llvm/ADT/Triple.h b/llvm/include/llvm/ADT/Triple.h --- a/llvm/include/llvm/ADT/Triple.h +++ b/llvm/include/llvm/ADT/Triple.h @@ -484,6 +484,12 @@ return getEnvironment() == Triple::MacABI; } + /// Returns true for targets that run on a macOS machine. + bool isTargetMachineMac() const { + return isMacOSX() || (isOSDarwin() && (isSimulatorEnvironment() || + isMacCatalystEnvironment())); + } + bool isOSNetBSD() const { return getOS() == Triple::NetBSD; }