Skip to content

Commit 447bce7

Browse files
committed
[mlir] retain identifier names
1 parent 60325ab commit 447bce7

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,10 @@ class OpPrintingFlags {
12211221
/// Return if printer should use unique SSA IDs.
12221222
bool shouldPrintUniqueSSAIDs() const;
12231223

1224+
/// Returns if the printer should retain identifier names collected using
1225+
/// parsing.
1226+
bool shouldUseNameLocAsPrefix() const;
1227+
12241228
private:
12251229
/// Elide large elements attributes if the number of elements is larger than
12261230
/// the upper limit.
@@ -1254,6 +1258,9 @@ class OpPrintingFlags {
12541258

12551259
/// Print unique SSA IDs for values, block arguments and naming conflicts
12561260
bool printUniqueSSAIDsFlag : 1;
1261+
1262+
/// Print the retained original names of identifiers
1263+
bool useNameLocAsPrefix : 1;
12571264
};
12581265

12591266
//===----------------------------------------------------------------------===//

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ struct AsmPrinterOptions {
195195
"mlir-print-unique-ssa-ids", llvm::cl::init(false),
196196
llvm::cl::desc("Print unique SSA ID numbers for values, block arguments "
197197
"and naming conflicts across all regions")};
198+
199+
llvm::cl::opt<bool> useNameLocAsPrefix{
200+
"mlir-use-nameloc-as-prefix", llvm::cl::init(false),
201+
llvm::cl::desc("TODO")};
198202
};
199203
} // namespace
200204

@@ -212,7 +216,8 @@ OpPrintingFlags::OpPrintingFlags()
212216
: printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false),
213217
printGenericOpFormFlag(false), skipRegionsFlag(false),
214218
assumeVerifiedFlag(false), printLocalScope(false),
215-
printValueUsersFlag(false), printUniqueSSAIDsFlag(false) {
219+
printValueUsersFlag(false), printUniqueSSAIDsFlag(false),
220+
useNameLocAsPrefix(false) {
216221
// Initialize based upon command line options, if they are available.
217222
if (!clOptions.isConstructed())
218223
return;
@@ -231,6 +236,7 @@ OpPrintingFlags::OpPrintingFlags()
231236
skipRegionsFlag = clOptions->skipRegionsOpt;
232237
printValueUsersFlag = clOptions->printValueUsers;
233238
printUniqueSSAIDsFlag = clOptions->printUniqueSSAIDs;
239+
useNameLocAsPrefix = clOptions->useNameLocAsPrefix;
234240
}
235241

236242
/// Enable the elision of large elements attributes, by printing a '...'
@@ -362,6 +368,11 @@ bool OpPrintingFlags::shouldPrintUniqueSSAIDs() const {
362368
return printUniqueSSAIDsFlag || shouldPrintGenericOpForm();
363369
}
364370

371+
/// TODO
372+
bool OpPrintingFlags::shouldUseNameLocAsPrefix() const {
373+
return useNameLocAsPrefix;
374+
}
375+
365376
//===----------------------------------------------------------------------===//
366377
// NewLineCounter
367378
//===----------------------------------------------------------------------===//
@@ -1514,10 +1525,22 @@ void SSANameState::numberValuesInRegion(Region &region) {
15141525
setValueName(arg, name);
15151526
};
15161527

1528+
bool alreadySetNames = false;
15171529
if (!printerFlags.shouldPrintGenericOpForm()) {
15181530
if (Operation *op = region.getParentOp()) {
1519-
if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op))
1531+
if (auto asmInterface = dyn_cast<OpAsmOpInterface>(op)) {
15201532
asmInterface.getAsmBlockArgumentNames(region, setBlockArgNameFn);
1533+
alreadySetNames = true;
1534+
}
1535+
}
1536+
}
1537+
1538+
if (printerFlags.shouldUseNameLocAsPrefix() && !alreadySetNames) {
1539+
for (BlockArgument arg : region.getArguments()) {
1540+
if (isa<NameLoc>(arg.getLoc())) {
1541+
auto nameLoc = cast<NameLoc>(arg.getLoc());
1542+
setBlockArgNameFn(arg, nameLoc.getName());
1543+
}
15211544
}
15221545
}
15231546

@@ -1553,7 +1576,12 @@ void SSANameState::numberValuesInBlock(Block &block) {
15531576
specialNameBuffer.resize(strlen("arg"));
15541577
specialName << nextArgumentID++;
15551578
}
1556-
setValueName(arg, specialName.str());
1579+
if (printerFlags.shouldUseNameLocAsPrefix() && isa<NameLoc>(arg.getLoc())) {
1580+
auto nameLoc = cast<NameLoc>(arg.getLoc());
1581+
setValueName(arg, nameLoc.getName());
1582+
} else {
1583+
setValueName(arg, specialName.str());
1584+
}
15571585
}
15581586

15591587
// Number the operations in this block.
@@ -1589,10 +1617,21 @@ void SSANameState::numberValuesInOp(Operation &op) {
15891617
blockNames[block] = {-1, name};
15901618
};
15911619

1620+
bool alreadySetNames = false;
15921621
if (!printerFlags.shouldPrintGenericOpForm()) {
15931622
if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
15941623
asmInterface.getAsmBlockNames(setBlockNameFn);
15951624
asmInterface.getAsmResultNames(setResultNameFn);
1625+
alreadySetNames = true;
1626+
}
1627+
}
1628+
1629+
if (printerFlags.shouldUseNameLocAsPrefix() && !alreadySetNames) {
1630+
for (Value opResult : op.getResults()) {
1631+
if (isa<NameLoc>(opResult.getLoc())) {
1632+
auto nameLoc = cast<NameLoc>(opResult.getLoc());
1633+
setResultNameFn(opResult, nameLoc.getName());
1634+
}
15961635
}
15971636
}
15981637

0 commit comments

Comments
 (0)