[llvm] [RISCV] Don't pre-split before the loop in parseNormalizedArchString. (PR #91684)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu May 9 17:07:19 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/91684

We can extract each extension as we process them without much complexity.

I changed the error message for cases where there are double underscores or a trailing underscore. I think this is an improvement over the previous error.

>From d4557e55065c789d3db7f9055b4b4f244a999199 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 9 May 2024 17:01:27 -0700
Subject: [PATCH] [RISCV] Don't pre-split before the loop in
 parseNormalizedArchString.

We can extract each extension as we process them without much
complexity.

I changed the error message for cases where there are double
underscores or a trailing underscore. I think this is an improvement
over the previous error.
---
 llvm/lib/TargetParser/RISCVISAInfo.cpp           | 15 ++++++++++++---
 llvm/unittests/TargetParser/RISCVISAInfoTest.cpp |  9 +++++++--
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index c553e330a878b..033df27c68781 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -451,9 +451,18 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
   // Each extension is of the form ${name}${major_version}p${minor_version}
   // and separated by _. Split by _ and then extract the name and version
   // information for each extension.
-  SmallVector<StringRef, 8> Split;
-  Arch.split(Split, '_');
-  for (StringRef Ext : Split) {
+  while (!Arch.empty()) {
+    if (Arch[0] == '_') {
+      if (Arch.size() == 1 || Arch[1] == '_')
+        return createStringError(errc::invalid_argument,
+                                 "extension name missing after separator '_'");
+      Arch = Arch.drop_front();
+    }
+
+    size_t Idx = Arch.find('_');
+    StringRef Ext = Arch.slice(0, Idx);
+    Arch = Arch.slice(Idx, StringRef::npos);
+
     StringRef Prefix, MinorVersionStr;
     std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
     if (MinorVersionStr.empty())
diff --git a/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp b/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
index f9e386a85fea8..7f2d1eb8c0170 100644
--- a/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
+++ b/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
@@ -38,12 +38,17 @@ TEST(ParseNormalizedArchString, RejectsInvalidBaseISA) {
 }
 
 TEST(ParseNormalizedArchString, RejectsMalformedInputs) {
-  for (StringRef Input :
-       {"rv64i2p0_", "rv32i2p0__a2p0", "rv64e2p", "rv32i", "rv64ip1"}) {
+  for (StringRef Input : {"rv64e2p", "rv32i", "rv64ip1"}) {
     EXPECT_EQ(
         toString(RISCVISAInfo::parseNormalizedArchString(Input).takeError()),
         "extension lacks version in expected format");
   }
+
+  for (StringRef Input : {"rv64i2p0_", "rv32i2p0__a2p0"}) {
+    EXPECT_EQ(
+        toString(RISCVISAInfo::parseNormalizedArchString(Input).takeError()),
+        "extension name missing after separator '_'");
+  }
 }
 
 TEST(ParseNormalizedArchString, RejectsOnlyVersion) {



More information about the llvm-commits mailing list