@@ -1303,7 +1303,9 @@ class SSANameState {
13031303 // / Set the original identifier names if available. Used in debugging with
13041304 // / `--retain-identifier-names`/`shouldRetainIdentifierNames` in ParserConfig
13051305 void setRetainedIdentifierNames (Operation &op,
1306- SmallVector<int , 2 > &resultGroups);
1306+ SmallVector<int , 2 > &resultGroups,
1307+ bool hasRegion = false );
1308+ void setRetainedIdentifierNames (Region ®ion);
13071309
13081310 // / This is the value ID for each SSA value. If this returns NameSentinel,
13091311 // / then the valueID has an entry in valueNames.
@@ -1492,6 +1494,9 @@ void SSANameState::numberValuesInRegion(Region ®ion) {
14921494 setValueName (arg, name);
14931495 };
14941496
1497+ // Use manually specified region arg names if available
1498+ setRetainedIdentifierNames (region);
1499+
14951500 if (!printerFlags.shouldPrintGenericOpForm ()) {
14961501 if (Operation *op = region.getParentOp ()) {
14971502 if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op))
@@ -1603,64 +1608,75 @@ void SSANameState::numberValuesInOp(Operation &op) {
16031608 }
16041609}
16051610
1606- void SSANameState::setRetainedIdentifierNames (
1607- Operation &op, SmallVector<int , 2 > &resultGroups) {
1608- // Get the original names for the results if available
1609- if (ArrayAttr resultNamesAttr =
1610- op.getAttrOfType <ArrayAttr>(" mlir.resultNames" )) {
1611- auto resultNames = resultNamesAttr.getValue ();
1612- auto results = op.getResults ();
1613- // Conservative in the case that the #results has changed
1614- for (size_t i = 0 ; i < results.size () && i < resultNames.size (); ++i) {
1615- auto resultName = resultNames[i].cast <StringAttr>().strref ();
1616- if (!resultName.empty ()) {
1617- if (!usedNames.count (resultName))
1618- setValueName (results[i], resultName, /* allowNumeric=*/ true );
1619- // If a result has a name, it is the start of a result group.
1620- if (i > 0 )
1621- resultGroups.push_back (i);
1622- }
1623- }
1624- op.removeDiscardableAttr (" mlir.resultNames" );
1625- }
1626-
1627- // Get the original name for the op args if available
1628- if (ArrayAttr opArgNamesAttr =
1629- op.getAttrOfType <ArrayAttr>(" mlir.opArgNames" )) {
1630- auto opArgNames = opArgNamesAttr.getValue ();
1631- auto opArgs = op.getOperands ();
1632- // Conservative in the case that the #operands has changed
1633- for (size_t i = 0 ; i < opArgs.size () && i < opArgNames.size (); ++i) {
1634- auto opArgName = opArgNames[i].cast <StringAttr>().strref ();
1635- if (!usedNames.count (opArgName))
1636- setValueName (opArgs[i], opArgName, /* allowNumeric=*/ true );
1611+ void SSANameState::setRetainedIdentifierNames (Operation &op,
1612+ SmallVector<int , 2 > &resultGroups,
1613+ bool hasRegion) {
1614+
1615+ // Lambda which fetches the list of relevant attributes (e.g.,
1616+ // mlir.resultNames) and associates them with the relevant values
1617+ auto handleNamedAttributes =
1618+ [this ](Operation &op, const Twine &attrName, auto getValuesFunc,
1619+ std::optional<std::function<void (int )>> customAction =
1620+ std::nullopt ) {
1621+ if (ArrayAttr namesAttr = op.getAttrOfType <ArrayAttr>(attrName.str ())) {
1622+ auto names = namesAttr.getValue ();
1623+ auto values = getValuesFunc ();
1624+ // Conservative in case the number of values has changed
1625+ for (size_t i = 0 ; i < values.size () && i < names.size (); ++i) {
1626+ auto name = names[i].cast <StringAttr>().strref ();
1627+ if (!name.empty ()) {
1628+ if (!this ->usedNames .count (name))
1629+ this ->setValueName (values[i], name, true );
1630+ if (customAction.has_value ())
1631+ customAction.value ()(i);
1632+ }
1633+ }
1634+ op.removeDiscardableAttr (attrName.str ());
1635+ }
1636+ };
1637+
1638+ if (hasRegion) {
1639+ // Get the original name(s) for the region arg(s) if available (e.g., for
1640+ // FuncOp args). Requires hasRegion flag to ensure scoping is correct
1641+ if (hasRegion && op.getNumRegions () > 0 &&
1642+ op.getRegion (0 ).getNumArguments () > 0 ) {
1643+ handleNamedAttributes (op, " mlir.regionArgNames" ,
1644+ [&]() { return op.getRegion (0 ).getArguments (); });
16371645 }
1638- op.removeDiscardableAttr (" mlir.opArgNames" );
1639- }
1640-
1641- // Get the original name for the block if available
1642- if (StringAttr blockNameAttr =
1643- op.getAttrOfType <StringAttr>(" mlir.blockName" )) {
1644- blockNames[op.getBlock ()] = {-1 , blockNameAttr.strref ()};
1645- op.removeDiscardableAttr (" mlir.blockName" );
1646- }
1647-
1648- // Get the original name for the block args if available
1649- if (ArrayAttr blockArgNamesAttr =
1650- op.getAttrOfType <ArrayAttr>(" mlir.blockArgNames" )) {
1651- auto blockArgNames = blockArgNamesAttr.getValue ();
1652- auto blockArgs = op.getBlock ()->getArguments ();
1653- // Conservative in the case that the #args has changed
1654- for (size_t i = 0 ; i < blockArgs.size () && i < blockArgNames.size (); ++i) {
1655- auto blockArgName = blockArgNames[i].cast <StringAttr>().strref ();
1656- if (!usedNames.count (blockArgName))
1657- setValueName (blockArgs[i], blockArgName, /* allowNumeric=*/ true );
1646+ } else {
1647+ // Get the original names for the results if available
1648+ handleNamedAttributes (
1649+ op, " mlir.resultNames" , [&]() { return op.getResults (); },
1650+ [&resultGroups](int i) { /* handles result groups*/
1651+ if (i > 0 )
1652+ resultGroups.push_back (i);
1653+ });
1654+
1655+ // Get the original name for the op args if available
1656+ handleNamedAttributes (op, " mlir.opArgNames" ,
1657+ [&]() { return op.getOperands (); });
1658+
1659+ // Get the original name for the block if available
1660+ if (StringAttr blockNameAttr =
1661+ op.getAttrOfType <StringAttr>(" mlir.blockName" )) {
1662+ blockNames[op.getBlock ()] = {-1 , blockNameAttr.strref ()};
1663+ op.removeDiscardableAttr (" mlir.blockName" );
16581664 }
1659- op.removeDiscardableAttr (" mlir.blockArgNames" );
1665+
1666+ // Get the original name(s) for the block arg(s) if available
1667+ handleNamedAttributes (op, " mlir.blockArgNames" ,
1668+ [&]() { return op.getBlock ()->getArguments (); });
16601669 }
16611670 return ;
16621671}
16631672
1673+ void SSANameState::setRetainedIdentifierNames (Region ®ion) {
1674+ if (Operation *op = region.getParentOp ()) {
1675+ SmallVector<int , 2 > resultGroups;
1676+ setRetainedIdentifierNames (*op, resultGroups, true );
1677+ }
1678+ }
1679+
16641680void SSANameState::getResultIDAndNumber (
16651681 OpResult result, Value &lookupValue,
16661682 std::optional<int > &lookupResultNo) const {
0 commit comments