[PATCH] D152423: [RISCV] Add function that check extension name with version

Piyou Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 02:07:31 PDT 2023


BeMg updated this revision to Diff 529862.
BeMg added a comment.

rename findLastNonVersionCharacter with https://reviews.llvm.org/D152506


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152423/new/

https://reviews.llvm.org/D152423

Files:
  llvm/include/llvm/Support/RISCVISAInfo.h
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/unittests/Support/RISCVISAInfoTest.cpp


Index: llvm/unittests/Support/RISCVISAInfoTest.cpp
===================================================================
--- llvm/unittests/Support/RISCVISAInfoTest.cpp
+++ llvm/unittests/Support/RISCVISAInfoTest.cpp
@@ -485,3 +485,24 @@
               ElementsAre("i", "m", "l", "c", "y", "zicsr", "zmfoo", "zfinx",
                            "zzfoo", "sbar", "sfoo", "xbar", "xfoo"));
 }
+
+TEST(isSupportedExtensionWithVersion, AcceptsSingleExtensionWithVersion) {
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion("zbb1p0"), true);
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion("zbb"), false);
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion("zfoo1p0"), false);
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion("zfoo"), false);
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion(""), false);
+  EXPECT_EQ(RISCVISAInfo::isSupportedExtensionWithVersion("c2p0zbb1p0"), false);
+}
+
+TEST(getTargetFeatureFromOneExt, RetrieveTargetFeatureFromOneExt) {
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zbb"), "zbb");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zihintntl0p2"),
+            "experimental-zihintntl");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zihintntl"),
+            "experimental-zihintntl");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zihintntl1234p4321"), "");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zfoo"), "");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt(""), "");
+  EXPECT_EQ(RISCVISAInfo::getTargetFeatureFromOneExt("zbbzihintntl"), "");
+}
Index: llvm/lib/Support/RISCVISAInfo.cpp
===================================================================
--- llvm/lib/Support/RISCVISAInfo.cpp
+++ llvm/lib/Support/RISCVISAInfo.cpp
@@ -1192,3 +1192,41 @@
   }
   llvm_unreachable("Invalid XLEN");
 }
+
+bool RISCVISAInfo::isSupportedExtensionWithVersion(StringRef Ext) {
+  if (Ext.empty())
+    return false;
+
+  auto Pos = findLastNonVersionCharacter(Ext) + 1;
+  StringRef Name = Ext.substr(0, Pos);
+  StringRef Vers = Ext.substr(Pos);
+  if (Vers.empty())
+    return false;
+
+  unsigned Major, Minor, ConsumeLength;
+  if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
+                                   true, true)) {
+    consumeError(std::move(E));
+    return false;
+  }
+
+  return true;
+}
+
+std::string RISCVISAInfo::getTargetFeatureFromOneExt(StringRef Ext) {
+  if (Ext.empty())
+    return std::string();
+
+  auto Pos = findLastNonVersionCharacter(Ext) + 1;
+  StringRef Name = Ext.substr(0, Pos);
+  StringRef Vers = Ext.substr(Pos);
+
+  if (!Vers.empty() && !isSupportedExtensionWithVersion(Ext))
+    return std::string();
+
+  if (!isSupportedExtension(Name))
+    return std::string();
+
+  return isExperimentalExtension(Name) ? "experimental-" + Name.str()
+                                       : Name.str();
+}
Index: llvm/include/llvm/Support/RISCVISAInfo.h
===================================================================
--- llvm/include/llvm/Support/RISCVISAInfo.h
+++ llvm/include/llvm/Support/RISCVISAInfo.h
@@ -85,10 +85,12 @@
 
   static bool isSupportedExtensionFeature(StringRef Ext);
   static bool isSupportedExtension(StringRef Ext);
+  static bool isSupportedExtensionWithVersion(StringRef Ext);
   static bool isSupportedExtension(StringRef Ext, unsigned MajorVersion,
                                    unsigned MinorVersion);
   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
   postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
+  static std::string getTargetFeatureFromOneExt(StringRef Ext);
 
 private:
   RISCVISAInfo(unsigned XLen)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152423.529862.patch
Type: text/x-patch
Size: 3673 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230609/42d8c242/attachment.bin>


More information about the llvm-commits mailing list