Teach the MLIR AsmPrinter to correctly escape asm names that use invalid characters.

Reviewers: rriddle!

Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D75919
This commit is contained in:
Chris Lattner
2020-03-10 06:24:11 -07:00
parent 1c9c23d60e
commit 89ecd8c149
2 changed files with 50 additions and 0 deletions

View File

@@ -760,7 +760,43 @@ void SSANameState::setValueName(Value value, StringRef name) {
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);

View File

@@ -10,3 +10,17 @@ func @custom_region_names() -> () {
// 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
}