Skip to content

Commit 628f520

Browse files
committed
add cache to replacer
1 parent 6f57bc1 commit 628f520

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "mlir/IR/Location.h"
1414
#include "llvm/ADT/STLExtras.h"
1515
#include "llvm/ADT/ScopeExit.h"
16+
#include "llvm/ADT/SetOperations.h"
1617
#include "llvm/ADT/TypeSwitch.h"
1718
#include "llvm/BinaryFormat/Dwarf.h"
1819
#include "llvm/IR/Constants.h"
@@ -355,6 +356,14 @@ static Attribute replaceAndPruneRecursiveTypes(
355356
SmallVector<ReplacementState> workStack;
356357
workStack.push_back({nullptr, {baseNode}, {}, 0, false});
357358

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+
358367
DenseSet<DistinctAttr> openDecls;
359368
while (workStack.size() > 1 || workStack.back().attrIndex == 0) {
360369
ReplacementState &state = workStack.back();
@@ -370,6 +379,8 @@ static Attribute replaceAndPruneRecursiveTypes(
370379
if (DistinctAttr recId = recType.getRecId())
371380
openDecls.erase(recId);
372381

382+
replacementCache[state.node] = CacheWithContext{openDecls, result};
383+
373384
workStack.pop_back();
374385
ReplacementState &prevState = workStack.back();
375386
prevState.replaceCurrAttr(result);
@@ -378,6 +389,20 @@ static Attribute replaceAndPruneRecursiveTypes(
378389
}
379390

380391
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+
381406
if (auto recType = dyn_cast<DIRecursiveTypeAttrInterface>(node)) {
382407
if (DistinctAttr recId = recType.getRecId()) {
383408
if (recType.isRecSelf()) {

0 commit comments

Comments
 (0)