13
13
#include " mlir/IR/Location.h"
14
14
#include " llvm/ADT/STLExtras.h"
15
15
#include " llvm/ADT/ScopeExit.h"
16
+ #include " llvm/ADT/SetOperations.h"
16
17
#include " llvm/ADT/TypeSwitch.h"
17
18
#include " llvm/BinaryFormat/Dwarf.h"
18
19
#include " llvm/IR/Constants.h"
@@ -355,6 +356,14 @@ static Attribute replaceAndPruneRecursiveTypes(
355
356
SmallVector<ReplacementState> workStack;
356
357
workStack.push_back ({nullptr , {baseNode}, {}, 0 , false });
357
358
359
+ // Replacement cache that remembers the context in which the cache is valid.
360
+ // All unboundRecIds must be in openDecls at time of lookup.
361
+ struct CacheWithContext {
362
+ DenseSet<DistinctAttr> unboundRecIds;
363
+ Attribute entry;
364
+ };
365
+ DenseMap<Attribute, CacheWithContext> replacementCache;
366
+
358
367
DenseSet<DistinctAttr> openDecls;
359
368
while (workStack.size () > 1 || workStack.back ().attrIndex == 0 ) {
360
369
ReplacementState &state = workStack.back ();
@@ -370,6 +379,8 @@ static Attribute replaceAndPruneRecursiveTypes(
370
379
if (DistinctAttr recId = recType.getRecId ())
371
380
openDecls.erase (recId);
372
381
382
+ replacementCache[state.node ] = CacheWithContext{openDecls, result};
383
+
373
384
workStack.pop_back ();
374
385
ReplacementState &prevState = workStack.back ();
375
386
prevState.replaceCurrAttr (result);
@@ -378,6 +389,20 @@ static Attribute replaceAndPruneRecursiveTypes(
378
389
}
379
390
380
391
Attribute node = state.attrs [state.attrIndex ];
392
+
393
+ // Lookup in cache first.
394
+ if (auto it = replacementCache.find (node); it != replacementCache.end ()) {
395
+ // If all the requried recIds are open decls, use cache.
396
+ if (llvm::set_is_subset (it->second .unboundRecIds , openDecls)) {
397
+ state.replaceCurrAttr (it->second .entry );
398
+ ++state.attrIndex ;
399
+ continue ;
400
+ }
401
+
402
+ // Otherwise, the cache entry is stale and can be removed now.
403
+ replacementCache.erase (it);
404
+ }
405
+
381
406
if (auto recType = dyn_cast<DIRecursiveTypeAttrInterface>(node)) {
382
407
if (DistinctAttr recId = recType.getRecId ()) {
383
408
if (recType.isRecSelf ()) {
0 commit comments