[PATCH] D75919: Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.

Chris Lattner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 06:25:05 PDT 2020


lattner created this revision.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a reviewer: rriddle.
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75919

Files:
  mlir/lib/IR/AsmPrinter.cpp
  mlir/test/IR/pretty-region-args.mlir


Index: mlir/test/IR/pretty-region-args.mlir
===================================================================
--- mlir/test/IR/pretty-region-args.mlir
+++ mlir/test/IR/pretty-region-args.mlir
@@ -10,3 +10,17 @@
   // CHECK-NEXT: ^bb{{.*}}(%i: index, %j: index, %k: index):
   return
 }
+
+// CHECK-LABEL: func @weird_names
+// Make sure the asmprinter handles weird names correctly.
+func @weird_names() -> () {
+  "test.polyfor"() ( {
+  ^bb0(%arg0: i32, %arg1: i32, %arg2: index):
+    "foo"() : () -> i32
+  }) { arg_names = ["a .^x", "0"] } : () -> ()
+  // CHECK: test.polyfor
+  // CHECK-NEXT: ^bb{{.*}}(%a_.5Ex: i32, %_0: i32, %arg0: index):
+  // CHECK-NEXT: %0 = "foo"()
+  return
+}
+
Index: mlir/lib/IR/AsmPrinter.cpp
===================================================================
--- mlir/lib/IR/AsmPrinter.cpp
+++ mlir/lib/IR/AsmPrinter.cpp
@@ -760,7 +760,43 @@
   valueNames[value] = uniqueValueName(name);
 }
 
+// Returns true if 'c' is an allowable punctuation character: [$._-]
+// Returns false otherwise.
+static bool isPunct(char c) {
+  return c == '$' || c == '.' || c == '_' || c == '-';
+}
+  
 StringRef SSANameState::uniqueValueName(StringRef name) {
+  assert(!name.empty() && "Shouldn't have an empty name here");
+  
+  // Check to see if this name is valid.  If it starts with a digit, then it
+  // could conflict with the autogenerated numeric ID's (we unique them in a
+  // different map), so add an underscore prefix to avoid problems.
+  if (isdigit(name[0])) {
+    SmallString<16> tmpName("_");
+    tmpName += name;
+    return uniqueValueName(tmpName);
+  }
+  
+  // Check to see if the name consists of all-valid identifiers.  If not, we
+  // need to escape them.
+  for (auto ch : name) {
+    if (isalpha(ch) || isPunct(ch) || isdigit(ch))
+      continue;
+    
+    SmallString<16> tmpName;
+    for (auto ch : name) {
+      if (isalpha(ch) || isPunct(ch) || isdigit(ch))
+        tmpName += ch;
+      else if (ch == ' ')
+        tmpName += '_';
+      else {
+        tmpName += llvm::utohexstr((unsigned char)ch);
+      }
+    }
+    return uniqueValueName(tmpName);
+  }
+  
   // Check to see if this name is already unique.
   if (!usedNames.count(name)) {
     name = name.copy(usedNameAllocator);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75919.249351.patch
Type: text/x-patch
Size: 2263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200310/ac2cafb0/attachment.bin>


More information about the llvm-commits mailing list