[llvm] [RISCV] Merge RISCVISAInfo::updateFLen/MinVLen/MaxELen into a single function. (PR #90665)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 19:45:51 PDT 2024


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

>From 97d54ce20e32784ded476d5a82e3d9af41678bca Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 30 Apr 2024 13:55:23 -0700
Subject: [PATCH 1/2] [RISCV] Merge RISCVISAInfo::updateFLen/MinVLen/MaxELen
 into a single function.

This simplifies the callers.
---
 llvm/include/llvm/TargetParser/RISCVISAInfo.h | 15 ++++----
 llvm/lib/TargetParser/RISCVISAInfo.cpp        | 37 ++++++++-----------
 2 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/llvm/include/llvm/TargetParser/RISCVISAInfo.h b/llvm/include/llvm/TargetParser/RISCVISAInfo.h
index 0d5637155daa96..36617a9b625972 100644
--- a/llvm/include/llvm/TargetParser/RISCVISAInfo.h
+++ b/llvm/include/llvm/TargetParser/RISCVISAInfo.h
@@ -78,13 +78,12 @@ class RISCVISAInfo {
   static std::string getTargetFeatureForExtension(StringRef Ext);
 
 private:
-  RISCVISAInfo(unsigned XLen)
-      : XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0) {}
+  RISCVISAInfo(unsigned XLen) : XLen(XLen) {}
 
   unsigned XLen;
-  unsigned FLen;
-  unsigned MinVLen;
-  unsigned MaxELen, MaxELenFp;
+  unsigned FLen = 0;
+  unsigned MinVLen = 0;
+  unsigned MaxELen = 0, MaxELenFp = 0;
 
   RISCVISAUtils::OrderedExtensionMap Exts;
 
@@ -94,9 +93,9 @@ class RISCVISAInfo {
 
   void updateImplication();
   void updateCombination();
-  void updateFLen();
-  void updateMinVLen();
-  void updateMaxELen();
+
+  /// Update FLen, MinVLen, MaxELen, and MaxELenFp.
+  void updateImpliedLengths();
 };
 
 } // namespace llvm
diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index 3b0cf8fab25f46..517c141c922e7e 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -478,9 +478,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
                                "failed to parse major version number");
     ISAInfo->addExtension(ExtName, {MajorVersion, MinorVersion});
   }
-  ISAInfo->updateFLen();
-  ISAInfo->updateMinVLen();
-  ISAInfo->updateMaxELen();
+  ISAInfo->updateImpliedLengths();
   return std::move(ISAInfo);
 }
 
@@ -907,28 +905,14 @@ void RISCVISAInfo::updateCombination() {
   } while (MadeChange);
 }
 
-void RISCVISAInfo::updateFLen() {
-  FLen = 0;
+void RISCVISAInfo::updateImpliedLengths() {
+  assert(FLen == 0);
   // TODO: Handle q extension.
   if (Exts.count("d"))
     FLen = 64;
   else if (Exts.count("f"))
     FLen = 32;
-}
-
-void RISCVISAInfo::updateMinVLen() {
-  for (auto const &Ext : Exts) {
-    StringRef ExtName = Ext.first;
-    bool IsZvlExt = ExtName.consume_front("zvl") && ExtName.consume_back("b");
-    if (IsZvlExt) {
-      unsigned ZvlLen;
-      if (!ExtName.getAsInteger(10, ZvlLen))
-        MinVLen = std::max(MinVLen, ZvlLen);
-    }
-  }
-}
 
-void RISCVISAInfo::updateMaxELen() {
   assert(MaxELenFp == 0 && MaxELen == 0);
   if (Exts.count("v")) {
     MaxELenFp = std::max(MaxELenFp, 64u);
@@ -953,6 +937,17 @@ void RISCVISAInfo::updateMaxELen() {
         MaxELen = std::max(MaxELen, ZveELen);
     }
   }
+
+  // FIXME: Merge these loops.
+  for (auto const &Ext : Exts) {
+    StringRef ExtName = Ext.first;
+    bool IsZvlExt = ExtName.consume_front("zvl") && ExtName.consume_back("b");
+    if (IsZvlExt) {
+      unsigned ZvlLen;
+      if (!ExtName.getAsInteger(10, ZvlLen))
+        MinVLen = std::max(MinVLen, ZvlLen);
+    }
+  }
 }
 
 std::string RISCVISAInfo::toString() const {
@@ -976,9 +971,7 @@ llvm::Expected<std::unique_ptr<RISCVISAInfo>>
 RISCVISAInfo::postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo) {
   ISAInfo->updateImplication();
   ISAInfo->updateCombination();
-  ISAInfo->updateFLen();
-  ISAInfo->updateMinVLen();
-  ISAInfo->updateMaxELen();
+  ISAInfo->updateImpliedLengths();
 
   if (Error Result = ISAInfo->checkDependency())
     return std::move(Result);

>From f3845137dda09b62869b65bb7c6d1ae634c2cffd Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 30 Apr 2024 19:30:39 -0700
Subject: [PATCH 2/2] fixup! Merge loops

---
 llvm/lib/TargetParser/RISCVISAInfo.cpp | 44 ++++++++++++++------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index 517c141c922e7e..9198b36c23b610 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -906,46 +906,50 @@ void RISCVISAInfo::updateCombination() {
 }
 
 void RISCVISAInfo::updateImpliedLengths() {
-  assert(FLen == 0);
+  assert(FLen == 0 && MaxELenFp == 0 && MaxELen == 0 && MinVLen == 0 &&
+         "Expected lengths to be initialied to zero");
+
   // TODO: Handle q extension.
   if (Exts.count("d"))
     FLen = 64;
   else if (Exts.count("f"))
     FLen = 32;
 
-  assert(MaxELenFp == 0 && MaxELen == 0);
   if (Exts.count("v")) {
     MaxELenFp = std::max(MaxELenFp, 64u);
     MaxELen = std::max(MaxELen, 64u);
   }
 
-  // handles EEW restriction by sub-extension zve
   for (auto const &Ext : Exts) {
     StringRef ExtName = Ext.first;
-    bool IsZveExt = ExtName.consume_front("zve");
-    if (IsZveExt) {
-      if (ExtName.back() == 'f')
+    // Infer MaxELen and MaxELenFp from Zve(32/64)(x/f/d)
+    if (ExtName.consume_front("zve")) {
+      unsigned ZveELen;
+      if (ExtName.consumeInteger(10, ZveELen))
+        continue;
+
+      if (ExtName == "f")
         MaxELenFp = std::max(MaxELenFp, 32u);
-      else if (ExtName.back() == 'd')
+      else if (ExtName == "d")
         MaxELenFp = std::max(MaxELenFp, 64u);
-      else if (ExtName.back() != 'x')
+      else if (ExtName != "x")
         continue;
 
-      ExtName = ExtName.drop_back();
-      unsigned ZveELen;
-      if (!ExtName.getAsInteger(10, ZveELen))
-        MaxELen = std::max(MaxELen, ZveELen);
+      MaxELen = std::max(MaxELen, ZveELen);
+      continue;
     }
-  }
 
-  // FIXME: Merge these loops.
-  for (auto const &Ext : Exts) {
-    StringRef ExtName = Ext.first;
-    bool IsZvlExt = ExtName.consume_front("zvl") && ExtName.consume_back("b");
-    if (IsZvlExt) {
+    // Infer MinVLen from zvl*b.
+    if (ExtName.consume_front("zvl")) {
       unsigned ZvlLen;
-      if (!ExtName.getAsInteger(10, ZvlLen))
-        MinVLen = std::max(MinVLen, ZvlLen);
+      if (ExtName.consumeInteger(10, ZvlLen))
+        continue;
+
+      if (ExtName != "b")
+        continue;
+
+      MinVLen = std::max(MinVLen, ZvlLen);
+      continue;
     }
   }
 }



More information about the llvm-commits mailing list