[llvm] e53f41c - [LoongArch] Support getHostCPUName and getHostCPUFeatures

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 1 00:38:39 PST 2023


Author: wanglei
Date: 2023-02-01T16:38:15+08:00
New Revision: e53f41c39f3eb5052965c720d2cb517d2945fd12

URL: https://github.com/llvm/llvm-project/commit/e53f41c39f3eb5052965c720d2cb517d2945fd12
DIFF: https://github.com/llvm/llvm-project/commit/e53f41c39f3eb5052965c720d2cb517d2945fd12.diff

LOG: [LoongArch] Support getHostCPUName and getHostCPUFeatures

Reviewed By: xen0n, MaskRay

Differential Revision: https://reviews.llvm.org/D142950

Added: 
    

Modified: 
    llvm/lib/TargetParser/Host.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index 80ebe0fa57d48..f6ea8d290030a 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -1448,6 +1448,20 @@ StringRef sys::getHostCPUName() {
     return "generic";
   }
 }
+#elif defined(__loongarch__)
+StringRef sys::getHostCPUName() {
+  // Use processor id to detect cpu name.
+  uint32_t processor_id;
+  __asm__("cpucfg %[prid], $zero\n\t" : [prid] "=r"(processor_id));
+  switch (processor_id & 0xff00) {
+  case 0xc000: // Loongson 64bit, 4-issue
+    return "la464";
+  // TODO: Others.
+  default:
+    break;
+  }
+  return "generic";
+}
 #elif defined(__riscv)
 StringRef sys::getHostCPUName() {
 #if defined(__linux__)
@@ -1842,6 +1856,23 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
 
   return true;
 }
+#elif defined(__linux__) && defined(__loongarch__)
+#include <sys/auxv.h>
+bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
+  unsigned long hwcap = getauxval(AT_HWCAP);
+  bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
+  uint32_t cpucfg2 = 0x2;
+  __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
+
+  Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
+  Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
+
+  Features["lsx"] = hwcap & (1UL << 4);  // HWCAP_LOONGARCH_LSX
+  Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
+  Features["lvz"] = hwcap & (1UL << 9);  // HWCAP_LOONGARCH_LVZ
+
+  return true;
+}
 #else
 bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
 #endif


        


More information about the llvm-commits mailing list