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;


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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to