@@ -543,6 +543,10 @@ Type Parser::codeCompleteDialectSymbol(const llvm::StringMap<Type> &aliases) {
543
543
// ===----------------------------------------------------------------------===//
544
544
545
545
namespace {
546
+ // / This is the structure of a result specifier in the assembly syntax,
547
+ // / including the name, number of results, and location.
548
+ using ResultRecord = std::tuple<StringRef, unsigned , SMLoc>;
549
+
546
550
// / This class provides support for parsing operations and regions of
547
551
// / operations.
548
552
class OperationParser : public Parser {
@@ -618,7 +622,8 @@ class OperationParser : public Parser {
618
622
ParseResult parseSuccessors (SmallVectorImpl<Block *> &destinations);
619
623
620
624
// / Parse an operation instance that is in the generic form.
621
- Operation *parseGenericOperation ();
625
+ Operation *parseGenericOperation (
626
+ std::optional<ArrayRef<ResultRecord>> resultIDs = std::nullopt);
622
627
623
628
// / Parse different components, viz., use-info of operand(s), successor(s),
624
629
// / region(s), attribute(s) and function-type, of the generic form of an
@@ -659,10 +664,6 @@ class OperationParser : public Parser {
659
664
// / token is actually an alias, which means it must not contain a dot.
660
665
ParseResult parseLocationAlias (LocationAttr &loc);
661
666
662
- // / This is the structure of a result specifier in the assembly syntax,
663
- // / including the name, number of results, and location.
664
- using ResultRecord = std::tuple<StringRef, unsigned , SMLoc>;
665
-
666
667
// / Parse an operation instance that is in the op-defined custom form.
667
668
// / resultInfo specifies information about the "%name =" specifiers.
668
669
Operation *parseCustomOperation (ArrayRef<ResultRecord> resultIDs);
@@ -1238,7 +1239,7 @@ ParseResult OperationParser::parseOperation() {
1238
1239
if (nameTok.is (Token::bare_identifier) || nameTok.isKeyword ())
1239
1240
op = parseCustomOperation (resultIDs);
1240
1241
else if (nameTok.is (Token::string))
1241
- op = parseGenericOperation ();
1242
+ op = parseGenericOperation (resultIDs );
1242
1243
else if (nameTok.isCodeCompletionFor (Token::string))
1243
1244
return codeCompleteStringDialectOrOperationName (nameTok.getStringValue ());
1244
1245
else if (nameTok.isCodeCompletion ())
@@ -1344,6 +1345,38 @@ struct CleanupOpStateRegions {
1344
1345
}
1345
1346
OperationState &state;
1346
1347
};
1348
+
1349
+ std::pair<StringRef, unsigned > getResultName (ArrayRef<ResultRecord> resultIDs,
1350
+ unsigned resultNo) {
1351
+ // Scan for the resultID that contains this result number.
1352
+ for (const auto &entry : resultIDs) {
1353
+ if (resultNo < std::get<1 >(entry)) {
1354
+ // Don't pass on the leading %.
1355
+ StringRef name = std::get<0 >(entry).drop_front ();
1356
+ return {name, resultNo};
1357
+ }
1358
+ resultNo -= std::get<1 >(entry);
1359
+ }
1360
+
1361
+ // Invalid result number.
1362
+ return {" " , ~0U };
1363
+ }
1364
+
1365
+ std::pair<SMLoc, unsigned > getResultLoc (ArrayRef<ResultRecord> resultIDs,
1366
+ unsigned resultNo) {
1367
+ // Scan for the resultID that contains this result number.
1368
+ for (const auto &entry : resultIDs) {
1369
+ if (resultNo < std::get<1 >(entry)) {
1370
+ SMLoc loc = std::get<2 >(entry);
1371
+ return {loc, resultNo};
1372
+ }
1373
+ resultNo -= std::get<1 >(entry);
1374
+ }
1375
+
1376
+ // Invalid result number.
1377
+ return {SMLoc{}, ~0U };
1378
+ }
1379
+
1347
1380
} // namespace
1348
1381
1349
1382
ParseResult OperationParser::parseGenericOperationAfterOpName (
@@ -1457,7 +1490,8 @@ ParseResult OperationParser::parseGenericOperationAfterOpName(
1457
1490
return success ();
1458
1491
}
1459
1492
1460
- Operation *OperationParser::parseGenericOperation () {
1493
+ Operation *OperationParser::parseGenericOperation (
1494
+ std::optional<ArrayRef<ResultRecord>> maybeResultIDs) {
1461
1495
// Get location information for the operation.
1462
1496
auto srcLocation = getEncodedSourceLocation (getToken ().getLoc ());
1463
1497
@@ -1531,6 +1565,17 @@ Operation *OperationParser::parseGenericOperation() {
1531
1565
1532
1566
// Create the operation and try to parse a location for it.
1533
1567
Operation *op = opBuilder.create (result);
1568
+ if (state.config .shouldRetainIdentifierNames () && maybeResultIDs) {
1569
+ for (OpResult opResult : op->getResults ()) {
1570
+ unsigned resultNum = opResult.getResultNumber ();
1571
+ Location resultLoc = getEncodedSourceLocation (
1572
+ getResultLoc (*maybeResultIDs, resultNum).first );
1573
+ opResult.setLoc (NameLoc::get (
1574
+ StringAttr::get (state.config .getContext (),
1575
+ getResultName (*maybeResultIDs, resultNum).first ),
1576
+ resultLoc));
1577
+ }
1578
+ }
1534
1579
if (parseTrailingLocationSpecifier (op))
1535
1580
return nullptr ;
1536
1581
@@ -1571,7 +1616,7 @@ namespace {
1571
1616
class CustomOpAsmParser : public AsmParserImpl <OpAsmParser> {
1572
1617
public:
1573
1618
CustomOpAsmParser (
1574
- SMLoc nameLoc, ArrayRef<OperationParser:: ResultRecord> resultIDs,
1619
+ SMLoc nameLoc, ArrayRef<ResultRecord> resultIDs,
1575
1620
function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly,
1576
1621
bool isIsolatedFromAbove, StringRef opName, OperationParser &parser)
1577
1622
: AsmParserImpl<OpAsmParser>(nameLoc, parser), resultIDs(resultIDs),
@@ -1634,18 +1679,7 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
1634
1679
// / getResultName(3) == {"z", 0 }
1635
1680
std::pair<StringRef, unsigned >
1636
1681
getResultName (unsigned resultNo) const override {
1637
- // Scan for the resultID that contains this result number.
1638
- for (const auto &entry : resultIDs) {
1639
- if (resultNo < std::get<1 >(entry)) {
1640
- // Don't pass on the leading %.
1641
- StringRef name = std::get<0 >(entry).drop_front ();
1642
- return {name, resultNo};
1643
- }
1644
- resultNo -= std::get<1 >(entry);
1645
- }
1646
-
1647
- // Invalid result number.
1648
- return {" " , ~0U };
1682
+ return ::getResultName (resultIDs, resultNo);
1649
1683
}
1650
1684
1651
1685
// / Return the number of declared SSA results. This returns 4 for the foo.op
@@ -1962,7 +1996,7 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
1962
1996
1963
1997
private:
1964
1998
// / Information about the result name specifiers.
1965
- ArrayRef<OperationParser:: ResultRecord> resultIDs;
1999
+ ArrayRef<ResultRecord> resultIDs;
1966
2000
1967
2001
// / The abstract information of the operation.
1968
2002
function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly;
@@ -2093,6 +2127,18 @@ OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
2093
2127
2094
2128
// Otherwise, create the operation and try to parse a location for it.
2095
2129
Operation *op = opBuilder.create (opState);
2130
+
2131
+ if (state.config .shouldRetainIdentifierNames ()) {
2132
+ for (OpResult opResult : op->getResults ()) {
2133
+ unsigned resultNum = opResult.getResultNumber ();
2134
+ Location resultLoc =
2135
+ getEncodedSourceLocation (getResultLoc (resultIDs, resultNum).first );
2136
+ StringRef resName = opAsmParser.getResultName (resultNum).first ;
2137
+ opResult.setLoc (NameLoc::get (
2138
+ StringAttr::get (state.config .getContext (), resName), resultLoc));
2139
+ }
2140
+ }
2141
+
2096
2142
if (parseTrailingLocationSpecifier (op))
2097
2143
return nullptr ;
2098
2144
@@ -2235,6 +2281,11 @@ ParseResult OperationParser::parseRegionBody(Region ®ion, SMLoc startLoc,
2235
2281
Location loc = entryArg.sourceLoc .has_value ()
2236
2282
? *entryArg.sourceLoc
2237
2283
: getEncodedSourceLocation (argInfo.location );
2284
+ if (state.config .shouldRetainIdentifierNames ()) {
2285
+ loc = NameLoc::get (StringAttr::get (state.config .getContext (),
2286
+ entryArg.ssaName .name .drop_front (1 )),
2287
+ loc);
2288
+ }
2238
2289
BlockArgument arg = block->addArgument (entryArg.type , loc);
2239
2290
2240
2291
// Add a definition of this arg to the assembly state if provided.
@@ -2415,6 +2466,11 @@ ParseResult OperationParser::parseOptionalBlockArgList(Block *owner) {
2415
2466
return emitError (" argument and block argument type mismatch" );
2416
2467
} else {
2417
2468
auto loc = getEncodedSourceLocation (useInfo.location );
2469
+ if (state.config .shouldRetainIdentifierNames ()) {
2470
+ loc = NameLoc::get (StringAttr::get (state.config .getContext (),
2471
+ useInfo.name .drop_front (1 )),
2472
+ loc);
2473
+ }
2418
2474
arg = owner->addArgument (type, loc);
2419
2475
}
2420
2476
0 commit comments