Skip to content

Commit 46e41c8

Browse files
dtzSiFivedtzWill
andauthored
[mlir] Sanitize identifiers with leading symbol. (#94795)
Presently, if name starts with a symbol it's converted to hex which may cause the result to be invalid by starting with a digit. Address this and add a small test. Co-authored-by: Will Dietz <w@wdtz.org>
1 parent 48aebd4 commit 46e41c8

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,13 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
999999
bool allowTrailingDigit = true) {
10001000
assert(!name.empty() && "Shouldn't have an empty name here");
10011001

1002+
auto validChar = [&](char ch) {
1003+
return llvm::isAlnum(ch) || allowedPunctChars.contains(ch);
1004+
};
1005+
10021006
auto copyNameToBuffer = [&] {
10031007
for (char ch : name) {
1004-
if (llvm::isAlnum(ch) || allowedPunctChars.contains(ch))
1008+
if (validChar(ch))
10051009
buffer.push_back(ch);
10061010
else if (ch == ' ')
10071011
buffer.push_back('_');
@@ -1013,7 +1017,7 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
10131017
// Check to see if this name is valid. If it starts with a digit, then it
10141018
// could conflict with the autogenerated numeric ID's, so add an underscore
10151019
// prefix to avoid problems.
1016-
if (isdigit(name[0])) {
1020+
if (isdigit(name[0]) || (!validChar(name[0]) && name[0] != ' ')) {
10171021
buffer.push_back('_');
10181022
copyNameToBuffer();
10191023
return buffer;
@@ -1029,7 +1033,7 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
10291033

10301034
// Check to see that the name consists of only valid identifier characters.
10311035
for (char ch : name) {
1032-
if (!llvm::isAlnum(ch) && !allowedPunctChars.contains(ch)) {
1036+
if (!validChar(ch)) {
10331037
copyNameToBuffer();
10341038
return buffer;
10351039
}

mlir/test/IR/print-attr-type-aliases.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// CHECK-DAG: #_0_test_alias = "alias_test:prefixed_digit"
1212
"test.op"() {alias_test = "alias_test:prefixed_digit"} : () -> ()
1313

14+
// CHECK-DAG: #_25test = "alias_test:prefixed_symbol"
15+
"test.op"() {alias_test = "alias_test:prefixed_symbol"} : () -> ()
16+
1417
// CHECK-DAG: #test_alias_conflict0_ = "alias_test:sanitize_conflict_a"
1518
// CHECK-DAG: #test_alias_conflict0_1 = "alias_test:sanitize_conflict_b"
1619
"test.op"() {alias_test = ["alias_test:sanitize_conflict_a", "alias_test:sanitize_conflict_b"]} : () -> ()

mlir/test/lib/Dialect/Test/TestDialectInterfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ struct TestOpAsmInterface : public OpAsmDialectInterface {
188188
.Case("alias_test:dot_in_name", StringRef("test.alias"))
189189
.Case("alias_test:trailing_digit", StringRef("test_alias0"))
190190
.Case("alias_test:prefixed_digit", StringRef("0_test_alias"))
191+
.Case("alias_test:prefixed_symbol", StringRef("%test"))
191192
.Case("alias_test:sanitize_conflict_a",
192193
StringRef("test_alias_conflict0"))
193194
.Case("alias_test:sanitize_conflict_b",

0 commit comments

Comments
 (0)