@@ -73,7 +73,8 @@ OpAsmParser::~OpAsmParser() = default;
73
73
MLIRContext *AsmParser::getContext () const { return getBuilder ().getContext (); }
74
74
75
75
// / 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
77
78
ParseResult AsmParser::parseTypeList (SmallVectorImpl<Type> &result) {
78
79
return parseCommaSeparatedList (
79
80
[&]() { return parseType (result.emplace_back ()); });
@@ -195,6 +196,10 @@ struct AsmPrinterOptions {
195
196
" mlir-print-unique-ssa-ids" , llvm::cl::init (false ),
196
197
llvm::cl::desc (" Print unique SSA ID numbers for values, block arguments "
197
198
" 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" )};
198
203
};
199
204
} // namespace
200
205
@@ -212,7 +217,8 @@ OpPrintingFlags::OpPrintingFlags()
212
217
: printDebugInfoFlag(false ), printDebugInfoPrettyFormFlag(false ),
213
218
printGenericOpFormFlag(false ), skipRegionsFlag(false ),
214
219
assumeVerifiedFlag(false ), printLocalScope(false ),
215
- printValueUsersFlag(false ), printUniqueSSAIDsFlag(false ) {
220
+ printValueUsersFlag(false ), printUniqueSSAIDsFlag(false ),
221
+ useNameLocAsPrefix(false ) {
216
222
// Initialize based upon command line options, if they are available.
217
223
if (!clOptions.isConstructed ())
218
224
return ;
@@ -231,6 +237,7 @@ OpPrintingFlags::OpPrintingFlags()
231
237
skipRegionsFlag = clOptions->skipRegionsOpt ;
232
238
printValueUsersFlag = clOptions->printValueUsers ;
233
239
printUniqueSSAIDsFlag = clOptions->printUniqueSSAIDs ;
240
+ useNameLocAsPrefix = clOptions->useNameLocAsPrefix ;
234
241
}
235
242
236
243
// / Enable the elision of large elements attributes, by printing a '...'
@@ -362,6 +369,11 @@ bool OpPrintingFlags::shouldPrintUniqueSSAIDs() const {
362
369
return printUniqueSSAIDsFlag || shouldPrintGenericOpForm ();
363
370
}
364
371
372
+ // / Return if the printer should use NameLocs as prefixes when printing SSA IDs.
373
+ bool OpPrintingFlags::shouldUseNameLocAsPrefix () const {
374
+ return useNameLocAsPrefix;
375
+ }
376
+
365
377
// ===----------------------------------------------------------------------===//
366
378
// NewLineCounter
367
379
// ===----------------------------------------------------------------------===//
@@ -1506,11 +1518,22 @@ void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) {
1506
1518
}
1507
1519
}
1508
1520
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
+
1509
1530
void SSANameState::numberValuesInRegion (Region ®ion) {
1510
1531
auto setBlockArgNameFn = [&](Value arg, StringRef name) {
1511
1532
assert (!valueIDs.count (arg) && " arg numbered multiple times" );
1512
1533
assert (llvm::cast<BlockArgument>(arg).getOwner ()->getParent () == ®ion &&
1513
1534
" arg not defined in current region" );
1535
+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1536
+ name = maybeGetValueNameFromLoc (arg, name);
1514
1537
setValueName (arg, name);
1515
1538
};
1516
1539
@@ -1553,7 +1576,10 @@ void SSANameState::numberValuesInBlock(Block &block) {
1553
1576
specialNameBuffer.resize (strlen (" arg" ));
1554
1577
specialName << nextArgumentID++;
1555
1578
}
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);
1557
1583
}
1558
1584
1559
1585
// Number the operations in this block.
@@ -1567,6 +1593,8 @@ void SSANameState::numberValuesInOp(Operation &op) {
1567
1593
auto setResultNameFn = [&](Value result, StringRef name) {
1568
1594
assert (!valueIDs.count (result) && " result numbered multiple times" );
1569
1595
assert (result.getDefiningOp () == &op && " result not defined by 'op'" );
1596
+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1597
+ name = maybeGetValueNameFromLoc (result, name);
1570
1598
setValueName (result, name);
1571
1599
1572
1600
// Record the result number for groups not anchored at 0.
@@ -1607,6 +1635,12 @@ void SSANameState::numberValuesInOp(Operation &op) {
1607
1635
}
1608
1636
Value resultBegin = op.getResult (0 );
1609
1637
1638
+ if (printerFlags.shouldUseNameLocAsPrefix () && !valueIDs.count (resultBegin)) {
1639
+ if (auto nameLoc = resultBegin.getLoc ()->findInstanceOf <NameLoc>()) {
1640
+ setValueName (resultBegin, nameLoc.getName ());
1641
+ }
1642
+ }
1643
+
1610
1644
// If the first result wasn't numbered, give it a default number.
1611
1645
if (valueIDs.try_emplace (resultBegin, nextValueID).second )
1612
1646
++nextValueID;
0 commit comments