Skip to content

SILOptimizer: Make some analysis caches lazy #68233

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 3 commits into from
Aug 31, 2023
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
24 changes: 11 additions & 13 deletions include/swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ class ClassDecl;
class ClassHierarchyAnalysis : public SILAnalysis {
public:
typedef SmallVector<ClassDecl *, 8> ClassList;
typedef SmallPtrSet<ClassDecl *, 32> ClassSet;
typedef SmallVector<NominalTypeDecl *, 8> NominalTypeList;
typedef llvm::DenseMap<ProtocolDecl *, NominalTypeList>
ProtocolImplementations;
typedef llvm::DenseMap<ClassDecl *, ClassList> ClassListMap;

ClassHierarchyAnalysis(SILModule *Mod)
: SILAnalysis(SILAnalysisKind::ClassHierarchy), M(Mod) {
init();
}
: SILAnalysis(SILAnalysisKind::ClassHierarchy), M(Mod) {}

~ClassHierarchyAnalysis();

Expand Down Expand Up @@ -65,7 +60,8 @@ class ClassHierarchyAnalysis : public SILAnalysis {
/// Returns a list of the known direct subclasses of a class \p C in
/// the current module.
const ClassList &getDirectSubClasses(ClassDecl *C) {
return DirectSubclassesCache[C];
populateDirectSubclassesCacheIfNecessary();
return (*DirectSubclassesCache)[C];
}

/// Returns a list of the known indirect subclasses of a class \p C in
Expand All @@ -81,7 +77,8 @@ class ClassHierarchyAnalysis : public SILAnalysis {

/// Returns true if the class is inherited by another class in this module.
bool hasKnownDirectSubclasses(ClassDecl *C) {
return DirectSubclassesCache.count(C);
populateDirectSubclassesCacheIfNecessary();
return DirectSubclassesCache->count(C);
}

/// Returns true if the class is indirectly inherited by another class
Expand All @@ -92,18 +89,19 @@ class ClassHierarchyAnalysis : public SILAnalysis {
}

private:
/// Compute inheritance properties.
void init();
void getIndirectSubClasses(ClassDecl *Base,
ClassList &IndirectSubs);
/// The module
SILModule *M;

/// A cache that maps a class to all of its known direct subclasses.
llvm::DenseMap<ClassDecl*, ClassList> DirectSubclassesCache;
llvm::Optional<ClassListMap> DirectSubclassesCache;

/// A cache that maps a class to all of its known indirect subclasses.
llvm::DenseMap<ClassDecl*, ClassList> IndirectSubclassesCache;
ClassListMap IndirectSubclassesCache;

/// Populates `DirectSubclassesCache` if necessary.
void populateDirectSubclassesCacheIfNecessary();
};

}
Expand Down
21 changes: 10 additions & 11 deletions include/swift/SILOptimizer/Analysis/ProtocolConformanceAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class ProtocolConformanceAnalysis : public SILAnalysis {
SoleConformingTypeMap;

ProtocolConformanceAnalysis(SILModule *Mod)
: SILAnalysis(SILAnalysisKind::ProtocolConformance), M(Mod) {
init();
}
: SILAnalysis(SILAnalysisKind::ProtocolConformance), M(Mod) {}

~ProtocolConformanceAnalysis();

Expand All @@ -66,14 +64,15 @@ class ProtocolConformanceAnalysis : public SILAnalysis {
virtual void invalidateFunctionTables() override {}

/// Get the nominal types that implement a protocol.
ArrayRef<NominalTypeDecl *> getConformances(const ProtocolDecl *P) const {
auto ConformsListIt = ProtocolConformanceCache.find(P);
return ConformsListIt != ProtocolConformanceCache.end()
ArrayRef<NominalTypeDecl *> getConformances(const ProtocolDecl *P) {
populateConformanceCacheIfNecessary();
auto ConformsListIt = ProtocolConformanceCache->find(P);
return ConformsListIt != ProtocolConformanceCache->end()
? ArrayRef<NominalTypeDecl *>(ConformsListIt->second.begin(),
ConformsListIt->second.end())
: ArrayRef<NominalTypeDecl *>();
}

/// Traverse ProtocolConformanceMapCache recursively to determine sole
/// conforming concrete type.
NominalTypeDecl *findSoleConformingType(ProtocolDecl *Protocol);
Expand All @@ -83,17 +82,17 @@ class ProtocolConformanceAnalysis : public SILAnalysis {
bool getSoleConformingType(ProtocolDecl *Protocol, ClassHierarchyAnalysis *CHA, CanType &ConcreteType);

private:
/// Compute inheritance properties.
void init();

/// The module.
SILModule *M;

/// A cache that maps a protocol to its conformances.
ProtocolConformanceMap ProtocolConformanceCache;
llvm::Optional<ProtocolConformanceMap> ProtocolConformanceCache;

/// A cache that holds SoleConformingType for protocols.
SoleConformingTypeMap SoleConformingTypeCache;

/// Populates `ProtocolConformanceCache` if necessary.
void populateConformanceCacheIfNecessary();
};

} // namespace swift
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/FrontendSourceFileDepGraphFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ static std::string identifierForContext(const DeclContext *DC) {

const auto *ext = cast<ExtensionDecl>(DC);
auto fp = ext->getBodyFingerprint().value_or(Fingerprint::ZERO());
auto typeStr = Mangler.mangleTypeAsContextUSR(ext->getExtendedNominal());
const auto *nominal = ext->getExtendedNominal();
auto typeStr = nominal ? Mangler.mangleTypeAsContextUSR(nominal) : "";
return (typeStr + "@" + fp.getRawValue()).str();
}

Expand Down
10 changes: 8 additions & 2 deletions lib/SILOptimizer/Analysis/ClassHierarchyAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

using namespace swift;

void ClassHierarchyAnalysis::init() {
// FIXME: This could be implemented as a request.
void ClassHierarchyAnalysis::populateDirectSubclassesCacheIfNecessary() {
if (DirectSubclassesCache.has_value())
return;

DirectSubclassesCache.emplace();

auto module = M->getSwiftModule();

// For each class declaration in our V-table list:
Expand All @@ -42,7 +48,7 @@ void ClassHierarchyAnalysis::init() {
// Find the superclass's list of direct subclasses. If it's non-empty,
// we've previously walked up to the class, so there's no reason to keep
// walking from this point.
auto &list = DirectSubclassesCache[super];
auto &list = (*DirectSubclassesCache)[super];
bool shouldVisitSuper = list.empty();

// Check whether C is already in the list, which can happen
Expand Down
9 changes: 7 additions & 2 deletions lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class NominalTypeWalker : public ASTWalker {
};
} // end anonymous namespace

void ProtocolConformanceAnalysis::init() {
// FIXME: This could be implemented as a request.
void ProtocolConformanceAnalysis::populateConformanceCacheIfNecessary() {
if (ProtocolConformanceCache.has_value())
return;

ProtocolConformanceCache.emplace();

// We only do this in Whole-Module compilation mode.
if (!M->isWholeModule())
Expand All @@ -84,7 +89,7 @@ void ProtocolConformanceAnalysis::init() {

/// This operation is quadratic and should only be performed
/// in whole module compilation!
NominalTypeWalker Walker(ProtocolConformanceCache);
NominalTypeWalker Walker(*ProtocolConformanceCache);
for (auto *D : Decls) {
D->walk(Walker);
}
Expand Down