ym1813382441 created this revision.
Herald added subscribers: luke, VincentWu, vkmr, frasercrmck, evandro, 
luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, 
PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, 
shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, 
hiraditya, arichardson.
Herald added a project: All.
ym1813382441 requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, pcwang-thead, eopXD, 
MaskRay.
Herald added projects: clang, LLVM.

This helps to support multiple version number extensions later.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144696

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  llvm/include/llvm/Support/RISCVISAInfo.h
  llvm/lib/Support/RISCVISAInfo.cpp

Index: llvm/lib/Support/RISCVISAInfo.cpp
===================================================================
--- llvm/lib/Support/RISCVISAInfo.cpp
+++ llvm/lib/Support/RISCVISAInfo.cpp
@@ -23,11 +23,6 @@
 using namespace llvm;
 
 namespace {
-/// Represents the major and version number components of a RISC-V extension
-struct RISCVExtensionVersion {
-  unsigned Major;
-  unsigned Minor;
-};
 
 struct RISCVSupportedExtension {
   const char *Name;
@@ -190,13 +185,10 @@
   return std::nullopt;
 }
 
-void RISCVISAInfo::addExtension(StringRef ExtName, unsigned MajorVersion,
-                                unsigned MinorVersion) {
-  RISCVExtensionInfo Ext;
-  Ext.ExtName = ExtName.str();
-  Ext.MajorVersion = MajorVersion;
-  Ext.MinorVersion = MinorVersion;
-  Exts[ExtName.str()] = Ext;
+void RISCVISAInfo::addExtension(StringRef ExtName,
+                                RISCVExtensionVersion Version) {
+  assert(!Exts.count(ExtName.str()) && "Extension already exists");
+  Exts[ExtName.str()] = Version;
 }
 
 static StringRef getExtensionTypeDesc(StringRef Ext) {
@@ -247,11 +239,10 @@
          llvm::any_of(SupportedExperimentalExtensions, FindByName(Ext));
 }
 
-bool RISCVISAInfo::isSupportedExtension(StringRef Ext, unsigned MajorVersion,
-                                        unsigned MinorVersion) {
+bool RISCVISAInfo::isSupportedExtension(StringRef Ext,
+                                        RISCVExtensionVersion Version) {
   auto FindByNameAndVersion = [=](const RISCVSupportedExtension &ExtInfo) {
-    return ExtInfo.Name == Ext && (MajorVersion == ExtInfo.Version.Major) &&
-           (MinorVersion == ExtInfo.Version.Minor);
+    return ExtInfo.Name == Ext && (Version == ExtInfo.Version);
   };
   return llvm::any_of(SupportedExtensions, FindByNameAndVersion) ||
          llvm::any_of(SupportedExperimentalExtensions, FindByNameAndVersion);
@@ -381,13 +372,14 @@
 // Version number is divided into major and minor version numbers,
 // separated by a 'p'. If the minor version is 0 then 'p0' can be
 // omitted from the version string. E.g., rv32i2p0, rv32i2, rv32i2p1.
-static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
-                                 unsigned &Minor, unsigned &ConsumeLength,
+static Error getExtensionVersion(StringRef Ext, StringRef In,
+                                 RISCVExtensionVersion &Version,
+                                 unsigned &ConsumeLength,
                                  bool EnableExperimentalExtension,
                                  bool ExperimentalExtensionVersionCheck) {
   StringRef MajorStr, MinorStr;
-  Major = 0;
-  Minor = 0;
+  Version.Major = 0;
+  Version.Minor = 0;
   ConsumeLength = 0;
   MajorStr = In.take_while(isDigit);
   In = In.substr(MajorStr.size());
@@ -404,12 +396,12 @@
     }
   }
 
-  if (!MajorStr.empty() && MajorStr.getAsInteger(10, Major))
+  if (!MajorStr.empty() && MajorStr.getAsInteger(10, Version.Major))
     return createStringError(
         errc::invalid_argument,
         "Failed to parse major version number for extension '" + Ext + "'");
 
-  if (!MinorStr.empty() && MinorStr.getAsInteger(10, Minor))
+  if (!MinorStr.empty() && MinorStr.getAsInteger(10, Version.Minor))
     return createStringError(
         errc::invalid_argument,
         "Failed to parse minor version number for extension '" + Ext + "'");
@@ -446,8 +438,7 @@
     }
 
     auto SupportedVers = *ExperimentalExtension;
-    if (ExperimentalExtensionVersionCheck &&
-        (Major != SupportedVers.Major || Minor != SupportedVers.Minor)) {
+    if (ExperimentalExtensionVersionCheck && (Version != SupportedVers)) {
       std::string Error = "unsupported version number " + MajorStr.str();
       if (!MinorStr.empty())
         Error += "." + MinorStr.str();
@@ -466,15 +457,14 @@
 
   if (MajorStr.empty() && MinorStr.empty()) {
     if (auto DefaultVersion = findDefaultVersion(Ext)) {
-      Major = DefaultVersion->Major;
-      Minor = DefaultVersion->Minor;
+      Version = *DefaultVersion;
     }
     // No matter found or not, return success, assume other place will
     // verify.
     return Error::success();
   }
 
-  if (RISCVISAInfo::isSupportedExtension(Ext, Major, Minor))
+  if (RISCVISAInfo::isSupportedExtension(Ext, Version))
     return Error::success();
 
   std::string Error = "unsupported version number " + std::string(MajorStr);
@@ -509,8 +499,7 @@
       continue;
 
     if (Add)
-      ISAInfo->addExtension(ExtName, ExtensionInfoIterator->Version.Major,
-                            ExtensionInfoIterator->Version.Minor);
+      ISAInfo->addExtension(ExtName, ExtensionInfoIterator->Version);
     else
       ISAInfo->Exts.erase(ExtName.str());
   }
@@ -578,8 +567,9 @@
     Exts = Exts.substr(0, Pos);
   }
 
-  unsigned Major, Minor, ConsumeLength;
-  if (auto E = getExtensionVersion(std::string(1, Baseline), Exts, Major, Minor,
+  RISCVExtensionVersion Version;
+  unsigned ConsumeLength;
+  if (auto E = getExtensionVersion(std::string(1, Baseline), Exts, Version,
                                    ConsumeLength, EnableExperimentalExtension,
                                    ExperimentalExtensionVersionCheck))
     return std::move(E);
@@ -589,13 +579,13 @@
     // version since the we don't have clear version scheme for that on
     // ISA spec.
     for (const auto *Ext : {"i", "m", "a", "f", "d"})
-      if (auto Version = findDefaultVersion(Ext))
-        ISAInfo->addExtension(Ext, Version->Major, Version->Minor);
+      if (auto Vers = findDefaultVersion(Ext))
+        ISAInfo->addExtension(Ext, *Vers);
       else
         llvm_unreachable("Default extension version not found?");
   } else
     // Baseline is `i` or `e`
-    ISAInfo->addExtension(std::string(1, Baseline), Major, Minor);
+    ISAInfo->addExtension(std::string(1, Baseline), Version);
 
   // Consume the base ISA version number and any '_' between rvxxx and the
   // first extension
@@ -636,10 +626,11 @@
     ++StdExtsItr;
 
     std::string Next;
-    unsigned Major, Minor, ConsumeLength;
+    RISCVExtensionVersion Version;
+    unsigned ConsumeLength;
     if (std::next(I) != E)
       Next = std::string(std::next(I), E);
-    if (auto E = getExtensionVersion(std::string(1, C), Next, Major, Minor,
+    if (auto E = getExtensionVersion(std::string(1, C), Next, Version,
                                      ConsumeLength, EnableExperimentalExtension,
                                      ExperimentalExtensionVersionCheck)) {
       if (IgnoreUnknown) {
@@ -662,7 +653,7 @@
                                "unsupported standard user-level extension '%c'",
                                C);
     }
-    ISAInfo->addExtension(std::string(1, C), Major, Minor);
+    ISAInfo->addExtension(std::string(1, C), Version);
 
     // Consume full extension name and version, including any optional '_'
     // between this extension and the next
@@ -725,8 +716,9 @@
                                  Desc.str().c_str(), Type.str().c_str());
       }
 
-      unsigned Major, Minor, ConsumeLength;
-      if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
+      RISCVExtensionVersion Version;
+      unsigned ConsumeLength;
+      if (auto E = getExtensionVersion(Name, Vers, Version, ConsumeLength,
                                        EnableExperimentalExtension,
                                        ExperimentalExtensionVersionCheck)) {
         if (IgnoreUnknown) {
@@ -742,7 +734,7 @@
                                  Desc.str().c_str(), Name.str().c_str());
       }
 
-      ISAInfo->addExtension(Name, Major, Minor);
+      ISAInfo->addExtension(Name, Version);
       // Extension format is correct, keep parsing the extensions.
       // TODO: Save Type, Name, Major, Minor to avoid parsing them later.
       AllExts.push_back(Name);
@@ -897,7 +889,7 @@
   // implied
   if (!HasE && !HasI) {
     auto Version = findDefaultVersion("i");
-    addExtension("i", Version->Major, Version->Minor);
+    addExtension("i", *Version);
   }
 
   assert(llvm::is_sorted(ImpliedExts) && "Table not sorted by Name");
@@ -918,7 +910,7 @@
         if (Exts.count(ImpliedExt))
           continue;
         auto Version = findDefaultVersion(ImpliedExt);
-        addExtension(ImpliedExt, Version->Major, Version->Minor);
+        addExtension(ImpliedExt, *Version);
         WorkList.insert(ImpliedExt);
       }
     }
@@ -950,7 +942,7 @@
         IsAllRequiredFeatureExist &= hasExtension(Ext);
       if (IsAllRequiredFeatureExist) {
         auto Version = findDefaultVersion(CombineExt);
-        addExtension(CombineExt, Version->Major, Version->Minor);
+        addExtension(CombineExt, *Version);
         IsNewCombine = true;
       }
     }
@@ -1005,9 +997,9 @@
   ListSeparator LS("_");
   for (auto const &Ext : Exts) {
     StringRef ExtName = Ext.first;
-    auto ExtInfo = Ext.second;
+    auto Version = Ext.second;
     Arch << LS << ExtName;
-    Arch << ExtInfo.MajorVersion << "p" << ExtInfo.MinorVersion;
+    Arch << Version.Major << "p" << Version.Minor;
   }
 
   return Arch.str();
Index: llvm/include/llvm/Support/RISCVISAInfo.h
===================================================================
--- llvm/include/llvm/Support/RISCVISAInfo.h
+++ llvm/include/llvm/Support/RISCVISAInfo.h
@@ -17,10 +17,16 @@
 #include <vector>
 
 namespace llvm {
-struct RISCVExtensionInfo {
-  std::string ExtName;
-  unsigned MajorVersion;
-  unsigned MinorVersion;
+/// Represents the major and version number components of a RISC-V extension
+struct RISCVExtensionVersion {
+  unsigned Major;
+  unsigned Minor;
+  bool operator==(const RISCVExtensionVersion &Vers) const {
+    return this->Major == Vers.Major && this->Minor == Vers.Minor;
+  }
+  bool operator!=(const RISCVExtensionVersion &Vers) const {
+    return !(*this == Vers);
+  }
 };
 
 class RISCVISAInfo {
@@ -39,7 +45,7 @@
 
   /// OrderedExtensionMap is std::map, it's specialized to keep entries
   /// in canonical order of extension.
-  typedef std::map<std::string, RISCVExtensionInfo, ExtensionComparator>
+  typedef std::map<std::string, RISCVExtensionVersion, ExtensionComparator>
       OrderedExtensionMap;
 
   RISCVISAInfo(unsigned XLen, OrderedExtensionMap &Exts)
@@ -76,8 +82,8 @@
 
   static bool isSupportedExtensionFeature(StringRef Ext);
   static bool isSupportedExtension(StringRef Ext);
-  static bool isSupportedExtension(StringRef Ext, unsigned MajorVersion,
-                                   unsigned MinorVersion);
+  static bool isSupportedExtension(StringRef Ext,
+                                   RISCVExtensionVersion Version);
   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
   postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
 
@@ -92,8 +98,7 @@
 
   OrderedExtensionMap Exts;
 
-  void addExtension(StringRef ExtName, unsigned MajorVersion,
-                    unsigned MinorVersion);
+  void addExtension(StringRef ExtName, RISCVExtensionVersion Version);
 
   Error checkDependency();
 
Index: clang/lib/Basic/Targets/RISCV.cpp
===================================================================
--- clang/lib/Basic/Targets/RISCV.cpp
+++ clang/lib/Basic/Targets/RISCV.cpp
@@ -156,11 +156,11 @@
 
   for (auto &Extension : ISAInfo->getExtensions()) {
     auto ExtName = Extension.first;
-    auto ExtInfo = Extension.second;
+    auto Version = Extension.second;
 
     Builder.defineMacro(
         Twine("__riscv_", ExtName),
-        Twine(getVersionValue(ExtInfo.MajorVersion, ExtInfo.MinorVersion)));
+        Twine(getVersionValue(Version.Major, Version.Minor)));
   }
 
   if (ISAInfo->hasExtension("m") || ISAInfo->hasExtension("zmmul"))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to