@@ -245,6 +245,15 @@ static Symbol parseExtendedSymbol(Parser &p, AsmParserState *asmState,
245245 return nullptr ;
246246 }
247247
248+ if constexpr (std::is_same_v<Symbol, Attribute>) {
249+ auto &cache = p.getState ().symbols .attributesCache ;
250+ auto cacheIt = cache.find (symbolData);
251+ // Skip cached attribute if it has type.
252+ if (cacheIt != cache.end () && !p.getToken ().is (Token::colon))
253+ return cacheIt->second ;
254+
255+ return cache[symbolData] = createSymbol (dialectName, symbolData, loc);
256+ }
248257 return createSymbol (dialectName, symbolData, loc);
249258}
250259
@@ -337,6 +346,7 @@ Type Parser::parseExtendedType() {
337346template <typename T, typename ParserFn>
338347static T parseSymbol (StringRef inputStr, MLIRContext *context,
339348 size_t *numReadOut, bool isKnownNullTerminated,
349+ llvm::StringMap<Attribute> *attributesCache,
340350 ParserFn &&parserFn) {
341351 // Set the buffer name to the string being parsed, so that it appears in error
342352 // diagnostics.
@@ -348,6 +358,9 @@ static T parseSymbol(StringRef inputStr, MLIRContext *context,
348358 SourceMgr sourceMgr;
349359 sourceMgr.AddNewSourceBuffer (std::move (memBuffer), SMLoc ());
350360 SymbolState aliasState;
361+ if (attributesCache)
362+ aliasState.attributesCache = *attributesCache;
363+
351364 ParserConfig config (context);
352365 ParserState state (sourceMgr, config, aliasState, /* asmState=*/ nullptr ,
353366 /* codeCompleteContext=*/ nullptr );
@@ -358,6 +371,11 @@ static T parseSymbol(StringRef inputStr, MLIRContext *context,
358371 if (!symbol)
359372 return T ();
360373
374+ if constexpr (std::is_same_v<T, Attribute>) {
375+ if (attributesCache)
376+ *attributesCache = state.symbols .attributesCache ;
377+ }
378+
361379 // Provide the number of bytes that were read.
362380 Token endTok = parser.getToken ();
363381 size_t numRead =
@@ -374,13 +392,15 @@ static T parseSymbol(StringRef inputStr, MLIRContext *context,
374392
375393Attribute mlir::parseAttribute (StringRef attrStr, MLIRContext *context,
376394 Type type, size_t *numRead,
377- bool isKnownNullTerminated) {
395+ bool isKnownNullTerminated,
396+ llvm::StringMap<Attribute> *attributesCache) {
378397 return parseSymbol<Attribute>(
379- attrStr, context, numRead, isKnownNullTerminated,
398+ attrStr, context, numRead, isKnownNullTerminated, attributesCache,
380399 [type](Parser &parser) { return parser.parseAttribute (type); });
381400}
382401Type mlir::parseType (StringRef typeStr, MLIRContext *context, size_t *numRead,
383402 bool isKnownNullTerminated) {
384403 return parseSymbol<Type>(typeStr, context, numRead, isKnownNullTerminated,
404+ /* attributesCache=*/ nullptr ,
385405 [](Parser &parser) { return parser.parseType (); });
386406}
0 commit comments