Skip to content

Commit 233a987

Browse files
committed
Added system to handle when we use default names
1 parent fb557df commit 233a987

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,8 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
981981
/// store the new copy,
982982
static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
983983
StringRef allowedPunctChars = "$._-",
984-
bool allowTrailingDigit = true) {
984+
bool allowTrailingDigit = true,
985+
bool allowNumeric = false) {
985986
assert(!name.empty() && "Shouldn't have an empty name here");
986987

987988
auto copyNameToBuffer = [&] {
@@ -997,16 +998,17 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
997998

998999
// Check to see if this name is valid. If it starts with a digit, then it
9991000
// could conflict with the autogenerated numeric ID's, so add an underscore
1000-
// prefix to avoid problems.
1001-
if (isdigit(name[0])) {
1001+
// prefix to avoid problems. This can be overridden by setting allowNumeric.
1002+
if (isdigit(name[0]) && !allowNumeric) {
10021003
buffer.push_back('_');
10031004
copyNameToBuffer();
10041005
return buffer;
10051006
}
10061007

10071008
// If the name ends with a trailing digit, add a '_' to avoid potential
1008-
// conflicts with autogenerated ID's.
1009-
if (!allowTrailingDigit && isdigit(name.back())) {
1009+
// conflicts with autogenerated ID's. This can be overridden by setting
1010+
// allowNumeric.
1011+
if (!allowTrailingDigit && isdigit(name.back()) && !allowNumeric) {
10101012
copyNameToBuffer();
10111013
buffer.push_back('_');
10121014
return buffer;
@@ -1292,11 +1294,11 @@ class SSANameState {
12921294
std::optional<int> &lookupResultNo) const;
12931295

12941296
/// Set a special value name for the given value.
1295-
void setValueName(Value value, StringRef name);
1297+
void setValueName(Value value, StringRef name, bool allowNumeric = false);
12961298

12971299
/// Uniques the given value name within the printer. If the given name
12981300
/// conflicts, it is automatically renamed.
1299-
StringRef uniqueValueName(StringRef name);
1301+
StringRef uniqueValueName(StringRef name, bool allowNumeric = false);
13001302

13011303
/// Set the original identifier names if available. Used in debugging with
13021304
/// `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
@@ -1541,7 +1543,10 @@ void SSANameState::numberValuesInOp(Operation &op) {
15411543
// Function used to set the special result names for the operation.
15421544
SmallVector<int, 2> resultGroups(/*Size=*/1, /*Value=*/0);
15431545
auto setResultNameFn = [&](Value result, StringRef name) {
1544-
assert(!valueIDs.count(result) && "result numbered multiple times");
1546+
// Case where the result has already been named
1547+
if (valueIDs.count(result))
1548+
return;
1549+
// assert(!valueIDs.count(result) && "result numbered multiple times");
15451550
assert(result.getDefiningOp() == &op && "result not defined by 'op'");
15461551
setValueName(result, name);
15471552

@@ -1565,17 +1570,17 @@ void SSANameState::numberValuesInOp(Operation &op) {
15651570
blockNames[block] = {-1, name};
15661571
};
15671572

1573+
// Set the original identifier names if available. Used in debugging with
1574+
// `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
1575+
setRetainedIdentifierNames(op, resultGroups);
1576+
15681577
if (!printerFlags.shouldPrintGenericOpForm()) {
15691578
if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
15701579
asmInterface.getAsmBlockNames(setBlockNameFn);
15711580
asmInterface.getAsmResultNames(setResultNameFn);
15721581
}
15731582
}
15741583

1575-
// Set the original identifier names if available. Used in debugging with
1576-
// `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
1577-
setRetainedIdentifierNames(op, resultGroups);
1578-
15791584
unsigned numResults = op.getNumResults();
15801585
if (numResults == 0) {
15811586
// If value users should be printed, operations with no result need an id.
@@ -1610,7 +1615,7 @@ void SSANameState::setRetainedIdentifierNames(
16101615
auto resultName = resultNames[i].cast<StringAttr>().strref();
16111616
if (!resultName.empty()) {
16121617
if (!usedNames.count(resultName))
1613-
setValueName(results[i], resultName);
1618+
setValueName(results[i], resultName, /*allowNumeric=*/true);
16141619
// If a result has a name, it is the start of a result group.
16151620
if (i > 0)
16161621
resultGroups.push_back(i);
@@ -1628,7 +1633,7 @@ void SSANameState::setRetainedIdentifierNames(
16281633
for (size_t i = 0; i < opArgs.size() && i < opArgNames.size(); ++i) {
16291634
auto opArgName = opArgNames[i].cast<StringAttr>().strref();
16301635
if (!usedNames.count(opArgName))
1631-
setValueName(opArgs[i], opArgName);
1636+
setValueName(opArgs[i], opArgName, /*allowNumeric=*/true);
16321637
}
16331638
op.removeDiscardableAttr("mlir.opArgNames");
16341639
}
@@ -1649,7 +1654,7 @@ void SSANameState::setRetainedIdentifierNames(
16491654
for (size_t i = 0; i < blockArgs.size() && i < blockArgNames.size(); ++i) {
16501655
auto blockArgName = blockArgNames[i].cast<StringAttr>().strref();
16511656
if (!usedNames.count(blockArgName))
1652-
setValueName(blockArgs[i], blockArgName);
1657+
setValueName(blockArgs[i], blockArgName, /*allowNumeric=*/true);
16531658
}
16541659
op.removeDiscardableAttr("mlir.blockArgNames");
16551660
}
@@ -1695,20 +1700,22 @@ void SSANameState::getResultIDAndNumber(
16951700
lookupValue = owner->getResult(groupResultNo);
16961701
}
16971702

1698-
void SSANameState::setValueName(Value value, StringRef name) {
1703+
void SSANameState::setValueName(Value value, StringRef name,
1704+
bool allowNumeric) {
16991705
// If the name is empty, the value uses the default numbering.
17001706
if (name.empty()) {
17011707
valueIDs[value] = nextValueID++;
17021708
return;
17031709
}
17041710

17051711
valueIDs[value] = NameSentinel;
1706-
valueNames[value] = uniqueValueName(name);
1712+
valueNames[value] = uniqueValueName(name, allowNumeric);
17071713
}
17081714

1709-
StringRef SSANameState::uniqueValueName(StringRef name) {
1715+
StringRef SSANameState::uniqueValueName(StringRef name, bool allowNumeric) {
17101716
SmallString<16> tmpBuffer;
1711-
name = sanitizeIdentifier(name, tmpBuffer);
1717+
name = sanitizeIdentifier(name, tmpBuffer, /*allowedPunctChars=*/"$._-",
1718+
/*allowTrailingDigit=*/true, allowNumeric);
17121719

17131720
// Check to see if this name is already unique.
17141721
if (!usedNames.count(name)) {

0 commit comments

Comments
 (0)