@@ -543,6 +543,10 @@ Type Parser::codeCompleteDialectSymbol(const llvm::StringMap<Type> &aliases) {
543543// ===----------------------------------------------------------------------===//
544544
545545namespace {
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+
546550// / This class provides support for parsing operations and regions of
547551// / operations.
548552class OperationParser : public Parser {
@@ -618,7 +622,8 @@ class OperationParser : public Parser {
618622 ParseResult parseSuccessors (SmallVectorImpl<Block *> &destinations);
619623
620624 // / Parse an operation instance that is in the generic form.
621- Operation *parseGenericOperation ();
625+ Operation *parseGenericOperation (
626+ std::optional<ArrayRef<ResultRecord>> resultIDs = std::nullopt );
622627
623628 // / Parse different components, viz., use-info of operand(s), successor(s),
624629 // / region(s), attribute(s) and function-type, of the generic form of an
@@ -659,10 +664,6 @@ class OperationParser : public Parser {
659664 // / token is actually an alias, which means it must not contain a dot.
660665 ParseResult parseLocationAlias (LocationAttr &loc);
661666
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-
666667 // / Parse an operation instance that is in the op-defined custom form.
667668 // / resultInfo specifies information about the "%name =" specifiers.
668669 Operation *parseCustomOperation (ArrayRef<ResultRecord> resultIDs);
@@ -1238,7 +1239,7 @@ ParseResult OperationParser::parseOperation() {
12381239 if (nameTok.is (Token::bare_identifier) || nameTok.isKeyword ())
12391240 op = parseCustomOperation (resultIDs);
12401241 else if (nameTok.is (Token::string))
1241- op = parseGenericOperation ();
1242+ op = parseGenericOperation (resultIDs );
12421243 else if (nameTok.isCodeCompletionFor (Token::string))
12431244 return codeCompleteStringDialectOrOperationName (nameTok.getStringValue ());
12441245 else if (nameTok.isCodeCompletion ())
@@ -1344,6 +1345,38 @@ struct CleanupOpStateRegions {
13441345 }
13451346 OperationState &state;
13461347};
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+
13471380} // namespace
13481381
13491382ParseResult OperationParser::parseGenericOperationAfterOpName (
@@ -1457,7 +1490,8 @@ ParseResult OperationParser::parseGenericOperationAfterOpName(
14571490 return success ();
14581491}
14591492
1460- Operation *OperationParser::parseGenericOperation () {
1493+ Operation *OperationParser::parseGenericOperation (
1494+ std::optional<ArrayRef<ResultRecord>> maybeResultIDs) {
14611495 // Get location information for the operation.
14621496 auto srcLocation = getEncodedSourceLocation (getToken ().getLoc ());
14631497
@@ -1531,6 +1565,17 @@ Operation *OperationParser::parseGenericOperation() {
15311565
15321566 // Create the operation and try to parse a location for it.
15331567 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+ }
15341579 if (parseTrailingLocationSpecifier (op))
15351580 return nullptr ;
15361581
@@ -1571,7 +1616,7 @@ namespace {
15711616class CustomOpAsmParser : public AsmParserImpl <OpAsmParser> {
15721617public:
15731618 CustomOpAsmParser (
1574- SMLoc nameLoc, ArrayRef<OperationParser:: ResultRecord> resultIDs,
1619+ SMLoc nameLoc, ArrayRef<ResultRecord> resultIDs,
15751620 function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly,
15761621 bool isIsolatedFromAbove, StringRef opName, OperationParser &parser)
15771622 : AsmParserImpl<OpAsmParser>(nameLoc, parser), resultIDs(resultIDs),
@@ -1634,18 +1679,7 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
16341679 // / getResultName(3) == {"z", 0 }
16351680 std::pair<StringRef, unsigned >
16361681 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);
16491683 }
16501684
16511685 // / Return the number of declared SSA results. This returns 4 for the foo.op
@@ -1962,7 +1996,7 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
19621996
19631997private:
19641998 // / Information about the result name specifiers.
1965- ArrayRef<OperationParser:: ResultRecord> resultIDs;
1999+ ArrayRef<ResultRecord> resultIDs;
19662000
19672001 // / The abstract information of the operation.
19682002 function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly;
@@ -2093,6 +2127,18 @@ OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
20932127
20942128 // Otherwise, create the operation and try to parse a location for it.
20952129 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+
20962142 if (parseTrailingLocationSpecifier (op))
20972143 return nullptr ;
20982144
@@ -2235,6 +2281,11 @@ ParseResult OperationParser::parseRegionBody(Region ®ion, SMLoc startLoc,
22352281 Location loc = entryArg.sourceLoc .has_value ()
22362282 ? *entryArg.sourceLoc
22372283 : 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+ }
22382289 BlockArgument arg = block->addArgument (entryArg.type , loc);
22392290
22402291 // Add a definition of this arg to the assembly state if provided.
@@ -2415,6 +2466,11 @@ ParseResult OperationParser::parseOptionalBlockArgList(Block *owner) {
24152466 return emitError (" argument and block argument type mismatch" );
24162467 } else {
24172468 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+ }
24182474 arg = owner->addArgument (type, loc);
24192475 }
24202476
0 commit comments