[PATCH] D109215: [RISCV] Fix arch string parsing for multi-character extensions

Yueh-Ting Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 23 03:46:26 PDT 2021


eopXD updated this revision to Diff 381720.
eopXD added a comment.

Address comment

- Add comment to explain patterns that the function match
- Avoid `Pos` from turning into negative number


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D109215

Files:
  llvm/lib/Support/RISCVISAInfo.cpp


Index: llvm/lib/Support/RISCVISAInfo.cpp
===================================================================
--- llvm/lib/Support/RISCVISAInfo.cpp
+++ llvm/lib/Support/RISCVISAInfo.cpp
@@ -71,6 +71,28 @@
   return Ext.consume_front("experimental-");
 }
 
+// This function to find the first character that doesn't belong to a version
+// (e.g. zbe0p93 is extension 'zbe' of version '0p93'). So the function will
+// consume [0-9]*p[0-9]* starting from the backward. An extension name will not
+// end with a digit or the letter 'p', so this function will parse correctly.
+// NOTE: This function is NOT able to take empty strings or strings that only have
+// version numbers and no extension name. It assumes the extension name will be at
+// least more than one character.
+static size_t findFirstNonVersionCharacter(const StringRef &Ext) {
+  if (Ext.size() == 0)
+    llvm_unreachable("Already guarded by if-statement in ::parseArchString");
+
+  int Pos = Ext.size() - 1;
+  while (Pos > 0 && isDigit(Ext[Pos]))
+    Pos--;
+  if (Pos > 0 && Ext[Pos] == 'p' && isDigit(Ext[Pos - 1])) {
+    Pos--;
+    while (Pos > 0 && isDigit(Ext[Pos]))
+      Pos--;
+  }
+  return Pos;
+}
+
 struct FindByName {
   FindByName(StringRef Ext) : Ext(Ext){};
   StringRef Ext;
@@ -637,7 +659,7 @@
 
     StringRef Type = getExtensionType(Ext);
     StringRef Desc = getExtensionTypeDesc(Ext);
-    auto Pos = Ext.find_if(isDigit);
+    size_t Pos = findFirstNonVersionCharacter(Ext) + 1;
     StringRef Name(Ext.substr(0, Pos));
     StringRef Vers(Ext.substr(Pos));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109215.381720.patch
Type: text/x-patch
Size: 1558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211023/09436794/attachment.bin>


More information about the llvm-commits mailing list