@@ -981,7 +981,8 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
981
981
// / store the new copy,
982
982
static StringRef sanitizeIdentifier (StringRef name, SmallString<16 > &buffer,
983
983
StringRef allowedPunctChars = " $._-" ,
984
- bool allowTrailingDigit = true ) {
984
+ bool allowTrailingDigit = true ,
985
+ bool allowNumeric = false ) {
985
986
assert (!name.empty () && " Shouldn't have an empty name here" );
986
987
987
988
auto copyNameToBuffer = [&] {
@@ -997,16 +998,17 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
997
998
998
999
// Check to see if this name is valid. If it starts with a digit, then it
999
1000
// 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 ) {
1002
1003
buffer.push_back (' _' );
1003
1004
copyNameToBuffer ();
1004
1005
return buffer;
1005
1006
}
1006
1007
1007
1008
// 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) {
1010
1012
copyNameToBuffer ();
1011
1013
buffer.push_back (' _' );
1012
1014
return buffer;
@@ -1292,11 +1294,11 @@ class SSANameState {
1292
1294
std::optional<int > &lookupResultNo) const ;
1293
1295
1294
1296
// / 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 );
1296
1298
1297
1299
// / Uniques the given value name within the printer. If the given name
1298
1300
// / conflicts, it is automatically renamed.
1299
- StringRef uniqueValueName (StringRef name);
1301
+ StringRef uniqueValueName (StringRef name, bool allowNumeric = false );
1300
1302
1301
1303
// / Set the original identifier names if available. Used in debugging with
1302
1304
// / `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
@@ -1541,7 +1543,10 @@ void SSANameState::numberValuesInOp(Operation &op) {
1541
1543
// Function used to set the special result names for the operation.
1542
1544
SmallVector<int , 2 > resultGroups (/* Size=*/ 1 , /* Value=*/ 0 );
1543
1545
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");
1545
1550
assert (result.getDefiningOp () == &op && " result not defined by 'op'" );
1546
1551
setValueName (result, name);
1547
1552
@@ -1565,17 +1570,17 @@ void SSANameState::numberValuesInOp(Operation &op) {
1565
1570
blockNames[block] = {-1 , name};
1566
1571
};
1567
1572
1573
+ // Set the original identifier names if available. Used in debugging with
1574
+ // `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
1575
+ setRetainedIdentifierNames (op, resultGroups);
1576
+
1568
1577
if (!printerFlags.shouldPrintGenericOpForm ()) {
1569
1578
if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
1570
1579
asmInterface.getAsmBlockNames (setBlockNameFn);
1571
1580
asmInterface.getAsmResultNames (setResultNameFn);
1572
1581
}
1573
1582
}
1574
1583
1575
- // Set the original identifier names if available. Used in debugging with
1576
- // `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
1577
- setRetainedIdentifierNames (op, resultGroups);
1578
-
1579
1584
unsigned numResults = op.getNumResults ();
1580
1585
if (numResults == 0 ) {
1581
1586
// If value users should be printed, operations with no result need an id.
@@ -1610,7 +1615,7 @@ void SSANameState::setRetainedIdentifierNames(
1610
1615
auto resultName = resultNames[i].cast <StringAttr>().strref ();
1611
1616
if (!resultName.empty ()) {
1612
1617
if (!usedNames.count (resultName))
1613
- setValueName (results[i], resultName);
1618
+ setValueName (results[i], resultName, /* allowNumeric= */ true );
1614
1619
// If a result has a name, it is the start of a result group.
1615
1620
if (i > 0 )
1616
1621
resultGroups.push_back (i);
@@ -1628,7 +1633,7 @@ void SSANameState::setRetainedIdentifierNames(
1628
1633
for (size_t i = 0 ; i < opArgs.size () && i < opArgNames.size (); ++i) {
1629
1634
auto opArgName = opArgNames[i].cast <StringAttr>().strref ();
1630
1635
if (!usedNames.count (opArgName))
1631
- setValueName (opArgs[i], opArgName);
1636
+ setValueName (opArgs[i], opArgName, /* allowNumeric= */ true );
1632
1637
}
1633
1638
op.removeDiscardableAttr (" mlir.opArgNames" );
1634
1639
}
@@ -1649,7 +1654,7 @@ void SSANameState::setRetainedIdentifierNames(
1649
1654
for (size_t i = 0 ; i < blockArgs.size () && i < blockArgNames.size (); ++i) {
1650
1655
auto blockArgName = blockArgNames[i].cast <StringAttr>().strref ();
1651
1656
if (!usedNames.count (blockArgName))
1652
- setValueName (blockArgs[i], blockArgName);
1657
+ setValueName (blockArgs[i], blockArgName, /* allowNumeric= */ true );
1653
1658
}
1654
1659
op.removeDiscardableAttr (" mlir.blockArgNames" );
1655
1660
}
@@ -1695,20 +1700,22 @@ void SSANameState::getResultIDAndNumber(
1695
1700
lookupValue = owner->getResult (groupResultNo);
1696
1701
}
1697
1702
1698
- void SSANameState::setValueName (Value value, StringRef name) {
1703
+ void SSANameState::setValueName (Value value, StringRef name,
1704
+ bool allowNumeric) {
1699
1705
// If the name is empty, the value uses the default numbering.
1700
1706
if (name.empty ()) {
1701
1707
valueIDs[value] = nextValueID++;
1702
1708
return ;
1703
1709
}
1704
1710
1705
1711
valueIDs[value] = NameSentinel;
1706
- valueNames[value] = uniqueValueName (name);
1712
+ valueNames[value] = uniqueValueName (name, allowNumeric );
1707
1713
}
1708
1714
1709
- StringRef SSANameState::uniqueValueName (StringRef name) {
1715
+ StringRef SSANameState::uniqueValueName (StringRef name, bool allowNumeric ) {
1710
1716
SmallString<16 > tmpBuffer;
1711
- name = sanitizeIdentifier (name, tmpBuffer);
1717
+ name = sanitizeIdentifier (name, tmpBuffer, /* allowedPunctChars=*/ " $._-" ,
1718
+ /* allowTrailingDigit=*/ true , allowNumeric);
1712
1719
1713
1720
// Check to see if this name is already unique.
1714
1721
if (!usedNames.count (name)) {
0 commit comments