[llvm] [RISCV] Get host CPU name via hwprobe (PR #142745)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 11 04:45:58 PDT 2025


================
@@ -1672,8 +1673,31 @@ StringRef sys::getHostCPUName() {
   return "generic";
 }
 #elif defined(__riscv)
+#if defined(__linux__)
+// struct riscv_hwprobe
+struct RISCVHwProbe {
+  int64_t Key;
+  uint64_t Value;
+};
+#endif
+
 StringRef sys::getHostCPUName() {
 #if defined(__linux__)
+  // Try the hwprobe way first.
+  RISCVHwProbe Query[]{{/*RISCV_HWPROBE_KEY_MVENDORID=*/0, 0},
+                       {/*RISCV_HWPROBE_KEY_MARCHID=*/1, 0},
+                       {/*RISCV_HWPROBE_KEY_MIMPID=*/2, 0}};
+  int Ret = syscall(/*__NR_riscv_hwprobe=*/258, /*pairs=*/Query,
+                    /*pair_count=*/std::size(Query), /*cpu_count=*/0,
+                    /*cpus=*/0, /*flags=*/0);
+  if (Ret == 0) {
+    RISCV::CPUModel Model{Query[0].Value, Query[1].Value, Query[2].Value};
----------------
lukel97 wrote:

I get a build error when compiling with clang:

```
llvm/lib/TargetParser/Host.cpp:1694:27: error: non-constant-expression cannot be narrowed from type 'uint64_t' (aka 'unsigned long') to 'uint32_t' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]
 1694 |     RISCV::CPUModel Model{Query[0].Value, Query[1].Value, Query[2].Value};
```

I think we need to insert a cast for mvendorid?

https://github.com/llvm/llvm-project/pull/142745


More information about the llvm-commits mailing list