[llvm] f906e3d - [RISCV] Fold processSingleLetterExtension/processMultiLetterExtension into RISCVISAInfo::parseArchString.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 15:04:26 PDT 2024


Author: Craig Topper
Date: 2024-06-27T15:04:17-07:00
New Revision: f906e3dd62b979814658e8610388117dd7db7bbf

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

LOG: [RISCV] Fold processSingleLetterExtension/processMultiLetterExtension into RISCVISAInfo::parseArchString.

The end of both functions was very similar. Merging reduces the
duplication.

I'm planning to make additional changes to this code soon.

Added: 
    

Modified: 
    llvm/lib/TargetParser/RISCVISAInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index 739a36477c287..8b3fba19fed38 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -521,87 +521,6 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
   return std::move(ISAInfo);
 }
 
-static Error processMultiLetterExtension(
-    StringRef RawExt,
-    MapVector<std::string, RISCVISAUtils::ExtensionVersion,
-              std::map<std::string, unsigned>> &SeenExtMap,
-    bool IgnoreUnknown, bool EnableExperimentalExtension,
-    bool ExperimentalExtensionVersionCheck) {
-  StringRef Type = getExtensionType(RawExt);
-  StringRef Desc = getExtensionTypeDesc(RawExt);
-  auto Pos = findLastNonVersionCharacter(RawExt) + 1;
-  StringRef Name(RawExt.substr(0, Pos));
-  StringRef Vers(RawExt.substr(Pos));
-
-  if (Type.empty()) {
-    if (IgnoreUnknown)
-      return Error::success();
-    return createStringError(errc::invalid_argument,
-                             "invalid extension prefix '" + RawExt + "'");
-  }
-
-  if (!IgnoreUnknown && Name.size() == Type.size())
-    return createStringError(errc::invalid_argument,
-                             Desc + " name missing after '" + Type + "'");
-
-  unsigned Major, Minor, ConsumeLength;
-  if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
-                                   EnableExperimentalExtension,
-                                   ExperimentalExtensionVersionCheck)) {
-    if (IgnoreUnknown) {
-      consumeError(std::move(E));
-      return Error::success();
-    }
-    return E;
-  }
-
-  // Check if duplicated extension.
-  if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
-    return createStringError(errc::invalid_argument,
-                             "duplicated " + Desc + " '" + Name + "'");
-
-  if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
-    return Error::success();
-
-  SeenExtMap[Name.str()] = {Major, Minor};
-  return Error::success();
-}
-
-static Error processSingleLetterExtension(
-    StringRef &RawExt,
-    MapVector<std::string, RISCVISAUtils::ExtensionVersion,
-              std::map<std::string, unsigned>> &SeenExtMap,
-    bool IgnoreUnknown, bool EnableExperimentalExtension,
-    bool ExperimentalExtensionVersionCheck) {
-  unsigned Major, Minor, ConsumeLength;
-  StringRef Name = RawExt.take_front(1);
-  RawExt.consume_front(Name);
-  if (auto E = getExtensionVersion(Name, RawExt, Major, Minor, ConsumeLength,
-                                   EnableExperimentalExtension,
-                                   ExperimentalExtensionVersionCheck)) {
-    if (IgnoreUnknown) {
-      consumeError(std::move(E));
-      RawExt = RawExt.substr(ConsumeLength);
-      return Error::success();
-    }
-    return E;
-  }
-
-  RawExt = RawExt.substr(ConsumeLength);
-
-  // Check if duplicated extension.
-  if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
-    return createStringError(errc::invalid_argument,
-                             "duplicated standard user-level extension '" +
-                                 Name + "'");
-
-  if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
-    return Error::success();
-
-  SeenExtMap[Name.str()] = {Major, Minor};
-  return Error::success();
-}
-
 llvm::Expected<std::unique_ptr<RISCVISAInfo>>
 RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
                               bool ExperimentalExtensionVersionCheck,
@@ -738,11 +657,12 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
     Exts = Exts.slice(Idx, StringRef::npos);
 
     do {
+      StringRef Name, Vers, Desc;
       if (RISCVISAUtils::AllStdExts.contains(Ext.front())) {
-        if (auto E = processSingleLetterExtension(
-                Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
-                ExperimentalExtensionVersionCheck))
-          return std::move(E);
+        Name = Ext.take_front(1);
+        Ext = Ext.drop_front();
+        Vers = Ext;
+        Desc = "standard user-level extension";
       } else if (Ext.front() == 'z' || Ext.front() == 's' ||
                  Ext.front() == 'x') {
         // Handle other types of extensions other than the standard
@@ -753,19 +673,49 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
         // These extensions start with 'z', 's', 'x' prefixes, might have a
         // version number (major, minor) and are separated by a single
         // underscore '_'. We do not enforce a canonical order for them.
-        if (auto E = processMultiLetterExtension(
-                Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
-                ExperimentalExtensionVersionCheck))
-          return std::move(E);
-        // Multi-letter extension must be separate following extension with
-        // underscore
-        break;
+        StringRef Type = getExtensionType(Ext);
+        Desc = getExtensionTypeDesc(Ext);
+        auto Pos = findLastNonVersionCharacter(Ext) + 1;
+        Name = Ext.substr(0, Pos);
+        Vers = Ext.substr(Pos);
+        Ext = StringRef();
+
+        assert(!Type.empty() && "Empty type?");
+        if (!IgnoreUnknown && Name.size() == Type.size())
+          return createStringError(errc::invalid_argument,
+                                   Desc + " name missing after '" + Type + "'");
       } else {
         // FIXME: Could it be ignored by IgnoreUnknown?
         return createStringError(errc::invalid_argument,
                                  "invalid standard user-level extension '" +
                                      Twine(Ext.front()) + "'");
       }
+
+      unsigned Major, Minor, ConsumeLength;
+      if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
+                                       EnableExperimentalExtension,
+                                       ExperimentalExtensionVersionCheck)) {
+        if (IgnoreUnknown) {
+          consumeError(std::move(E));
+          if (Name.size() == 1)
+            Ext = Ext.substr(ConsumeLength);
+          continue;
+        }
+        return E;
+      }
+
+      if (Name.size() == 1)
+        Ext = Ext.substr(ConsumeLength);
+
+      // Check if duplicated extension.
+      if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
+        return createStringError(errc::invalid_argument,
+                                 "duplicated " + Desc + " '" + Name + "'");
+
+      if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
+        continue;
+
+      SeenExtMap[Name.str()] = {Major, Minor};
     } while (!Ext.empty());
   }
 


        


More information about the llvm-commits mailing list