@@ -197,9 +197,9 @@ struct AsmPrinterOptions {
197197 llvm::cl::desc (" Print unique SSA ID numbers for values, block arguments "
198198 " and naming conflicts across all regions" )};
199199
200- llvm::cl::opt<bool > useNameLocAsPrefix{" mlir-use-nameloc-as-prefix " ,
201- llvm::cl::init (false ),
202- llvm::cl::desc (" TODO " )};
200+ llvm::cl::opt<bool > useNameLocAsPrefix{
201+ " mlir-use-nameloc-as-prefix " , llvm::cl::init (false ),
202+ llvm::cl::desc (" Print SSA IDs using NameLocs as prefixes " )};
203203};
204204} // namespace
205205
@@ -369,7 +369,7 @@ bool OpPrintingFlags::shouldPrintUniqueSSAIDs() const {
369369 return printUniqueSSAIDsFlag || shouldPrintGenericOpForm ();
370370}
371371
372- // / TODO
372+ // / Return if the printer should use NameLocs as prefixes when printing SSA IDs
373373bool OpPrintingFlags::shouldUseNameLocAsPrefix () const {
374374 return useNameLocAsPrefix;
375375}
@@ -1523,25 +1523,18 @@ void SSANameState::numberValuesInRegion(Region ®ion) {
15231523 assert (!valueIDs.count (arg) && " arg numbered multiple times" );
15241524 assert (llvm::cast<BlockArgument>(arg).getOwner ()->getParent () == ®ion &&
15251525 " arg not defined in current region" );
1526- setValueName (arg, name);
1526+ if (printerFlags.shouldUseNameLocAsPrefix () && isa<NameLoc>(arg.getLoc ())) {
1527+ auto nameLoc = cast<NameLoc>(arg.getLoc ());
1528+ setValueName (arg, nameLoc.getName ());
1529+ } else {
1530+ setValueName (arg, name);
1531+ }
15271532 };
15281533
1529- bool alreadySetNames = false ;
15301534 if (!printerFlags.shouldPrintGenericOpForm ()) {
15311535 if (Operation *op = region.getParentOp ()) {
1532- if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op)) {
1536+ if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op))
15331537 asmInterface.getAsmBlockArgumentNames (region, setBlockArgNameFn);
1534- alreadySetNames = true ;
1535- }
1536- }
1537- }
1538-
1539- if (printerFlags.shouldUseNameLocAsPrefix () && !alreadySetNames) {
1540- for (BlockArgument arg : region.getArguments ()) {
1541- if (isa<NameLoc>(arg.getLoc ())) {
1542- auto nameLoc = cast<NameLoc>(arg.getLoc ());
1543- setBlockArgNameFn (arg, nameLoc.getName ());
1544- }
15451538 }
15461539 }
15471540
@@ -1596,7 +1589,13 @@ void SSANameState::numberValuesInOp(Operation &op) {
15961589 auto setResultNameFn = [&](Value result, StringRef name) {
15971590 assert (!valueIDs.count (result) && " result numbered multiple times" );
15981591 assert (result.getDefiningOp () == &op && " result not defined by 'op'" );
1599- setValueName (result, name);
1592+ if (printerFlags.shouldUseNameLocAsPrefix () &&
1593+ isa<NameLoc>(result.getLoc ())) {
1594+ auto nameLoc = cast<NameLoc>(result.getLoc ());
1595+ setValueName (result, nameLoc.getName ());
1596+ } else {
1597+ setValueName (result, name);
1598+ }
16001599
16011600 // Record the result number for groups not anchored at 0.
16021601 if (int resultNo = llvm::cast<OpResult>(result).getResultNumber ())
@@ -1618,25 +1617,14 @@ void SSANameState::numberValuesInOp(Operation &op) {
16181617 blockNames[block] = {-1 , name};
16191618 };
16201619
1621- bool alreadySetNames = false ;
16221620 if (!printerFlags.shouldPrintGenericOpForm ()) {
16231621 if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
16241622 asmInterface.getAsmBlockNames (setBlockNameFn);
16251623 asmInterface.getAsmResultNames (setResultNameFn);
1626- alreadySetNames = true ;
16271624 }
16281625 }
16291626
16301627 unsigned numResults = op.getNumResults ();
1631- if (printerFlags.shouldUseNameLocAsPrefix () && !alreadySetNames &&
1632- numResults > 0 ) {
1633- Value resultBegin = op.getResult (0 );
1634- if (isa<NameLoc>(resultBegin.getLoc ())) {
1635- auto nameLoc = cast<NameLoc>(resultBegin.getLoc ());
1636- setResultNameFn (resultBegin, nameLoc.getName ());
1637- }
1638- }
1639-
16401628 if (numResults == 0 ) {
16411629 // If value users should be printed, operations with no result need an id.
16421630 if (printerFlags.shouldPrintValueUsers ()) {
@@ -1647,6 +1635,13 @@ void SSANameState::numberValuesInOp(Operation &op) {
16471635 }
16481636 Value resultBegin = op.getResult (0 );
16491637
1638+ if (printerFlags.shouldUseNameLocAsPrefix () && !valueIDs.count (resultBegin)) {
1639+ if (isa<NameLoc>(resultBegin.getLoc ())) {
1640+ auto nameLoc = cast<NameLoc>(resultBegin.getLoc ());
1641+ setResultNameFn (resultBegin, nameLoc.getName ());
1642+ }
1643+ }
1644+
16501645 // If the first result wasn't numbered, give it a default number.
16511646 if (valueIDs.try_emplace (resultBegin, nextValueID).second )
16521647 ++nextValueID;
0 commit comments