[llvm] 63c81b2 - [RISCV] Support getHostCpuName for sifive-u74

via llvm-commits llvm-commits at lists.llvm.org
Mon May 16 23:37:13 PDT 2022


Author: luxufan
Date: 2022-05-17T14:06:59+08:00
New Revision: 63c81b23bebed8ca39cf73715952fd40947b5b02

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

LOG: [RISCV] Support getHostCpuName for sifive-u74

Reviewed By: kito-cheng

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

Added: 
    

Modified: 
    llvm/include/llvm/Support/Host.h
    llvm/lib/Support/Host.cpp
    llvm/unittests/Support/Host.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Host.h b/llvm/include/llvm/Support/Host.h
index b3c15f0683b9d..f683371ad1d36 100644
--- a/llvm/include/llvm/Support/Host.h
+++ b/llvm/include/llvm/Support/Host.h
@@ -64,6 +64,7 @@ namespace sys {
   StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent);
   StringRef getHostCPUNameForARM(StringRef ProcCpuinfoContent);
   StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent);
+  StringRef getHostCPUNameForRISCV(StringRef ProcCpuinfoContent);
   StringRef getHostCPUNameForBPF();
 
   /// Helper functions to extract CPU details from CPUID on x86.

diff  --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp
index 976599f199f0f..08e3a27e0173a 100644
--- a/llvm/lib/Support/Host.cpp
+++ b/llvm/lib/Support/Host.cpp
@@ -386,6 +386,26 @@ StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) {
   return "generic";
 }
 
+StringRef sys::detail::getHostCPUNameForRISCV(StringRef ProcCpuinfoContent) {
+  // There are 24 lines in /proc/cpuinfo
+  SmallVector<StringRef> Lines;
+  ProcCpuinfoContent.split(Lines, "\n");
+
+  // Look for uarch line to determine cpu name
+  StringRef UArch;
+  for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
+    if (Lines[I].startswith("uarch")) {
+      UArch = Lines[I].substr(5).ltrim("\t :");
+      break;
+    }
+  }
+
+  return StringSwitch<const char *>(UArch)
+      .Case("sifive,u74-mc", "sifive-u74")
+      .Case("sifive,bullet0", "sifive-u74")
+      .Default("generic");
+}
+
 StringRef sys::detail::getHostCPUNameForBPF() {
 #if !defined(__linux__) || !defined(__x86_64__)
   return "generic";
@@ -1379,6 +1399,11 @@ StringRef sys::getHostCPUName() {
 }
 #elif defined(__riscv)
 StringRef sys::getHostCPUName() {
+#if defined(__linux__)
+  std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
+  StringRef Content = P ? P->getBuffer() : "";
+  return detail::getHostCPUNameForRISCV(Content);
+#else
 #if __riscv_xlen == 64
   return "generic-rv64";
 #elif __riscv_xlen == 32
@@ -1386,6 +1411,7 @@ StringRef sys::getHostCPUName() {
 #else
 #error "Unhandled value of __riscv_xlen"
 #endif
+#endif
 }
 #else
 StringRef sys::getHostCPUName() { return "generic"; }

diff  --git a/llvm/unittests/Support/Host.cpp b/llvm/unittests/Support/Host.cpp
index 590d2d73b791b..6351903ecc895 100644
--- a/llvm/unittests/Support/Host.cpp
+++ b/llvm/unittests/Support/Host.cpp
@@ -378,6 +378,21 @@ TEST(getLinuxHostCPUName, s390x) {
   }
 }
 
+TEST(getLinuxHostCPUName, RISCV) {
+  const StringRef SifiveU74MCProcCPUInfo = R"(
+processor       : 0
+hart            : 2
+isa             : rv64imafdc
+mmu             : sv39
+uarch           : sifive,u74-mc
+)";
+  EXPECT_EQ(sys::detail::getHostCPUNameForRISCV(SifiveU74MCProcCPUInfo),
+            "sifive-u74");
+  EXPECT_EQ(
+      sys::detail::getHostCPUNameForRISCV("uarch           : sifive,bullet0\n"),
+      "sifive-u74");
+}
+
 static bool runAndGetCommandOutput(
     const char *ExePath, ArrayRef<llvm::StringRef> argv,
     std::unique_ptr<char[]> &Buffer, off_t &Size) {


        


More information about the llvm-commits mailing list