@@ -73,7 +73,8 @@ OpAsmParser::~OpAsmParser() = default;
7373MLIRContext *AsmParser::getContext () const { return getBuilder ().getContext (); }
7474
7575// / Parse a type list.
76- // / This is out-of-line to work-around https://github.com/llvm/llvm-project/issues/62918
76+ // / This is out-of-line to work-around
77+ // / https://github.com/llvm/llvm-project/issues/62918
7778ParseResult AsmParser::parseTypeList (SmallVectorImpl<Type> &result) {
7879 return parseCommaSeparatedList (
7980 [&]() { return parseType (result.emplace_back ()); });
@@ -195,6 +196,10 @@ struct AsmPrinterOptions {
195196 " mlir-print-unique-ssa-ids" , llvm::cl::init (false ),
196197 llvm::cl::desc (" Print unique SSA ID numbers for values, block arguments "
197198 " and naming conflicts across all regions" )};
199+
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" )};
198203};
199204} // namespace
200205
@@ -212,7 +217,8 @@ OpPrintingFlags::OpPrintingFlags()
212217 : printDebugInfoFlag(false ), printDebugInfoPrettyFormFlag(false ),
213218 printGenericOpFormFlag(false ), skipRegionsFlag(false ),
214219 assumeVerifiedFlag(false ), printLocalScope(false ),
215- printValueUsersFlag(false ), printUniqueSSAIDsFlag(false ) {
220+ printValueUsersFlag(false ), printUniqueSSAIDsFlag(false ),
221+ useNameLocAsPrefix(false ) {
216222 // Initialize based upon command line options, if they are available.
217223 if (!clOptions.isConstructed ())
218224 return ;
@@ -231,6 +237,7 @@ OpPrintingFlags::OpPrintingFlags()
231237 skipRegionsFlag = clOptions->skipRegionsOpt ;
232238 printValueUsersFlag = clOptions->printValueUsers ;
233239 printUniqueSSAIDsFlag = clOptions->printUniqueSSAIDs ;
240+ useNameLocAsPrefix = clOptions->useNameLocAsPrefix ;
234241}
235242
236243// / Enable the elision of large elements attributes, by printing a '...'
@@ -362,6 +369,11 @@ bool OpPrintingFlags::shouldPrintUniqueSSAIDs() const {
362369 return printUniqueSSAIDsFlag || shouldPrintGenericOpForm ();
363370}
364371
372+ // / Return if the printer should use NameLocs as prefixes when printing SSA IDs.
373+ bool OpPrintingFlags::shouldUseNameLocAsPrefix () const {
374+ return useNameLocAsPrefix;
375+ }
376+
365377// ===----------------------------------------------------------------------===//
366378// NewLineCounter
367379// ===----------------------------------------------------------------------===//
@@ -1506,11 +1518,22 @@ void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) {
15061518 }
15071519}
15081520
1521+ namespace {
1522+ // / Try to get value name from value's location, fallback to `name`.
1523+ StringRef maybeGetValueNameFromLoc (Value value, StringRef name) {
1524+ if (auto maybeNameLoc = value.getLoc ()->findInstanceOf <NameLoc>())
1525+ return maybeNameLoc.getName ();
1526+ return name;
1527+ }
1528+ } // namespace
1529+
15091530void SSANameState::numberValuesInRegion (Region ®ion) {
15101531 auto setBlockArgNameFn = [&](Value arg, StringRef name) {
15111532 assert (!valueIDs.count (arg) && " arg numbered multiple times" );
15121533 assert (llvm::cast<BlockArgument>(arg).getOwner ()->getParent () == ®ion &&
15131534 " arg not defined in current region" );
1535+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1536+ name = maybeGetValueNameFromLoc (arg, name);
15141537 setValueName (arg, name);
15151538 };
15161539
@@ -1553,7 +1576,10 @@ void SSANameState::numberValuesInBlock(Block &block) {
15531576 specialNameBuffer.resize (strlen (" arg" ));
15541577 specialName << nextArgumentID++;
15551578 }
1556- setValueName (arg, specialName.str ());
1579+ StringRef specialNameStr = specialName.str ();
1580+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1581+ specialNameStr = maybeGetValueNameFromLoc (arg, specialNameStr);
1582+ setValueName (arg, specialNameStr);
15571583 }
15581584
15591585 // Number the operations in this block.
@@ -1567,6 +1593,8 @@ void SSANameState::numberValuesInOp(Operation &op) {
15671593 auto setResultNameFn = [&](Value result, StringRef name) {
15681594 assert (!valueIDs.count (result) && " result numbered multiple times" );
15691595 assert (result.getDefiningOp () == &op && " result not defined by 'op'" );
1596+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1597+ name = maybeGetValueNameFromLoc (result, name);
15701598 setValueName (result, name);
15711599
15721600 // Record the result number for groups not anchored at 0.
@@ -1607,6 +1635,12 @@ void SSANameState::numberValuesInOp(Operation &op) {
16071635 }
16081636 Value resultBegin = op.getResult (0 );
16091637
1638+ if (printerFlags.shouldUseNameLocAsPrefix () && !valueIDs.count (resultBegin)) {
1639+ if (auto nameLoc = resultBegin.getLoc ()->findInstanceOf <NameLoc>()) {
1640+ setValueName (resultBegin, nameLoc.getName ());
1641+ }
1642+ }
1643+
16101644 // If the first result wasn't numbered, give it a default number.
16111645 if (valueIDs.try_emplace (resultBegin, nextValueID).second )
16121646 ++nextValueID;
0 commit comments