Skip to content

Bring back RC identity cache #2691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/swift/SILOptimizer/Analysis/RCIdentityAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@

namespace swift {

/// Limit the size of the rc identity cache. We keep a cache per function.
constexpr unsigned MaxRCIdentityCacheSize = 64;

class DominanceAnalysis;

/// This class is a simple wrapper around an identity cache.
class RCIdentityFunctionInfo {
llvm::DenseSet<SILArgument *> VisitedArgs;
// RC identity cache.
llvm::DenseMap<SILValue, SILValue> RCCache;
DominanceAnalysis *DA;

/// This number is arbitrary and conservative. At some point if compile time
Expand All @@ -50,6 +55,15 @@ class RCIdentityFunctionInfo {
/// unchecked_trivial_bit_cast.
void getRCUsers(SILValue V, llvm::SmallVectorImpl<SILInstruction *> &Users);

void handleDeleteNotification(ValueBase *V) {
// Check the cache. If we don't find it, there is nothing to do.
auto Iter = RCCache.find(SILValue(V));
if (Iter == RCCache.end())
return;
// Then erase Iter from the cache.
RCCache.erase(Iter);
}

private:
SILValue getRCIdentityRootInner(SILValue V, unsigned RecursionDepth);
SILValue stripRCIdentityPreservingOps(SILValue V, unsigned RecursionDepth);
Expand All @@ -70,6 +84,18 @@ class RCIdentityAnalysis : public FunctionAnalysisBase<RCIdentityFunctionInfo> {
RCIdentityAnalysis(const RCIdentityAnalysis &) = delete;
RCIdentityAnalysis &operator=(const RCIdentityAnalysis &) = delete;

virtual void handleDeleteNotification(ValueBase *V) override {
// If the parent function of this instruction was just turned into an
// external declaration, bail. This happens during SILFunction destruction.
SILFunction *F = V->getParentBB()->getParent();
if (F->isExternalDeclaration()) {
return;
}
get(F)->handleDeleteNotification(V);
}

virtual bool needsNotifications() override { return true; }

static bool classof(const SILAnalysis *S) {
return S->getKind() == AnalysisKind::RCIdentity;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/SILOptimizer/Analysis/RCIdentityAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,24 @@ SILValue RCIdentityFunctionInfo::getRCIdentityRootInner(SILValue V,
}

SILValue RCIdentityFunctionInfo::getRCIdentityRoot(SILValue V) {
// Do we have it in the RCCache ?
auto Iter = RCCache.find(V);
if (Iter != RCCache.end())
return Iter->second;

SILValue Root = getRCIdentityRootInner(V, 0);
VisitedArgs.clear();

// If we fail to find a root, return V.
if (!Root)
return V;

return Root;
// Make sure the cache does not grow too big.
if (RCCache.size() > MaxRCIdentityCacheSize)
RCCache.clear();

// Return and cache it.
return RCCache[V] = Root;
}

//===----------------------------------------------------------------------===//
Expand Down
9 changes: 1 addition & 8 deletions lib/SILOptimizer/Transforms/RetainReleaseCodeMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,9 @@ class CodeMotionContext {
/// we compute the genset and killset.
llvm::SmallPtrSet<SILBasicBlock *, 8> InterestBlocks;

/// An RC-identity cache.
/// TODO: this should be cached in RCIdentity analysis.
llvm::DenseMap<SILValue, SILValue> RCCache;

/// Return the rc-identity root of the SILValue.
SILValue getRCRoot(SILValue R) {
auto Iter = RCCache.find(R);
if (Iter != RCCache.end())
return Iter->second;
return RCCache[R] = RCFI->getRCIdentityRoot(R);
return RCFI->getRCIdentityRoot(R);
}

/// Return the rc-identity root of the RC instruction, i.e.
Expand Down