[PATCH] D119788: [AArch64] Add support for -march=native for Apple M1 CPU

Keith Smiley via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 14 15:14:38 PST 2022


keith created this revision.
Herald added subscribers: dexonsmith, pengfei, hiraditya, kristof.beyls.
keith requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

This improves the getHostCPUName check for Apple M1 <https://reviews.llvm.org/M1> CPUs, which
previously would always be considered cyclone instead. This also enables
`-march=native` support when building on M1 <https://reviews.llvm.org/M1> CPUs which would previously
fail. This isn't as sophisticated as the X86 CPU feature checking which
consults the CPU via getHostCPUFeatures, but this is still better than
before. This CPU selection could also be invalid if this was run on an
iOS device instead, ideally we can improve those cases as they come up.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119788

Files:
  clang/lib/Driver/ToolChains/Arch/AArch64.cpp
  llvm/lib/Support/Host.cpp


Index: llvm/lib/Support/Host.cpp
===================================================================
--- llvm/lib/Support/Host.cpp
+++ llvm/lib/Support/Host.cpp
@@ -1299,11 +1299,7 @@
   bool HaveVectorSupport = CVT[244] & 0x80;
   return getCPUNameFromS390Model(Id, HaveVectorSupport);
 }
-#elif defined(__APPLE__) && defined(__aarch64__)
-StringRef sys::getHostCPUName() {
-  return "cyclone";
-}
-#elif defined(__APPLE__) && defined(__arm__)
+#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
 StringRef sys::getHostCPUName() {
   host_basic_info_data_t hostInfo;
   mach_msg_type_number_t infoCount;
@@ -1314,15 +1310,23 @@
             &infoCount);
   mach_port_deallocate(mach_task_self(), hostPort);
 
-  if (hostInfo.cpu_type != CPU_TYPE_ARM) {
-    assert(false && "CPUType not equal to ARM should not be possible on ARM");
-    return "generic";
-  }
-  switch (hostInfo.cpu_subtype) {
+  if (hostInfo.cpu_type == CPU_TYPE_ARM) {
+    switch (hostInfo.cpu_subtype) {
     case CPU_SUBTYPE_ARM_V7S:
       return "swift";
-    default:;
+    default:
+      break;
     }
+  } else if (hostInfo.cpu_type == CPU_TYPE_ARM64) {
+    switch (hostInfo.cpu_subtype) {
+    case CPU_SUBTYPE_ARM64E:
+      return "apple-m1";
+    default:
+      return "cyclone";
+    }
+  } else {
+    assert(false && "CPUType not equal to ARM/ARM64 should not be possible");
+  }
 
   return "generic";
 }
Index: clang/lib/Driver/ToolChains/Arch/AArch64.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Arch/AArch64.cpp
+++ clang/lib/Driver/ToolChains/Arch/AArch64.cpp
@@ -151,6 +151,8 @@
   std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
 
   llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
+  if (Split.first == "native")
+    ArchKind = llvm::AArch64::getCPUArchKind(llvm::sys::getHostCPUName().str());
   if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
       !llvm::AArch64::getArchFeatures(ArchKind, Features))
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119788.408640.patch
Type: text/x-patch
Size: 2062 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220214/7de8ba52/attachment.bin>


More information about the cfe-commits mailing list