[llvm] [RISCV] Use vlenb CSR to set Zvl*b feature getHostCPUFeatures. (PR #225486)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 13:50:21 PDT 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/225486

>From 88924330c727e810e7811bc85f1c0d1d3e651d0d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Mon, 21 Sep 2026 17:39:42 -0700
Subject: [PATCH 1/2] [RISCV] Use vlenb CSR to set Zvl*b feature
 getHostCPUFeatures.

Zvl*b features aren't exposed in HwProbe, probably because the
vlenb CSR exists.
---
 llvm/lib/TargetParser/Host.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index d89133d534b56d..dac9cb12036e20 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -2618,6 +2618,24 @@ StringMap<bool> sys::getHostCPUFeatures() {
       Query[2].Value == /*RISCV_HWPROBE_MISALIGNED_SCALAR_FAST=*/3)
     Features["unaligned-scalar-mem"] = true;
 
+  // Infer Zvl from vlenb CSR.
+  if (Features["v"] || Features["zve32x"]) {
+#if __riscv_xlen == 64
+    uint64_t VLen;
+#elif __riscv_xlen == 32
+    uint32_t VLen;
+#else
+#error "Unknown XLEN"
+#endif
+    // Use the raw CSR number in case assembler doesn't know vlenb.
+    __asm__ volatile("csrr %0, 0xc22" : "=r"(VLen));
+    if (VLen) {
+      VLen *= 8;
+      std::string ZvlFeature = (Twine("zvl") + Twine(VLen) + "b").str();
+      Features[ZvlFeature] = true;
+    }
+  }
+
   return Features;
 }
 #else

>From ad6dab05e3a9fc1d2c63046124ae29e6511a9d1d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 23 Sep 2026 13:49:54 -0700
Subject: [PATCH 2/2] fixup! remove check for vlen==0

---
 llvm/lib/TargetParser/Host.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index dac9cb12036e20..56f2757d3fd3a3 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -2629,11 +2629,9 @@ StringMap<bool> sys::getHostCPUFeatures() {
 #endif
     // Use the raw CSR number in case assembler doesn't know vlenb.
     __asm__ volatile("csrr %0, 0xc22" : "=r"(VLen));
-    if (VLen) {
-      VLen *= 8;
-      std::string ZvlFeature = (Twine("zvl") + Twine(VLen) + "b").str();
-      Features[ZvlFeature] = true;
-    }
+    VLen *= 8;
+    std::string ZvlFeature = (Twine("zvl") + Twine(VLen) + "b").str();
+    Features[ZvlFeature] = true;
   }
 
   return Features;



More information about the llvm-commits mailing list