Skip to content

ProtocolConformanceAnalysis #17177

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 1 commit into from
Jun 19, 2018
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
1 change: 1 addition & 0 deletions include/swift/SILOptimizer/Analysis/Analysis.def
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ANALYSIS(LoopRegion)
ANALYSIS(OptimizerStats)
ANALYSIS(PostDominance)
ANALYSIS(PostOrder)
ANALYSIS(ProtocolConformance)
ANALYSIS(RCIdentity)
ANALYSIS(SideEffect)
ANALYSIS(TypeExpansion)
Expand Down
87 changes: 87 additions & 0 deletions include/swift/SILOptimizer/Analysis/ProtocolConformanceAnalysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//===--- ProtocolConformanceAnalysis.h - Protocol Conformance ---*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This analysis collects a set of nominal types (classes, structs, and enums)
// that conform to a protocol during whole module compilation. We only track
// protocols that are non-public.

#ifndef SWIFT_SILOPTIMIZER_ANALYSIS_PROTOCOLCONFORMANCE_H
#define SWIFT_SILOPTIMIZER_ANALYSIS_PROTOCOLCONFORMANCE_H

#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/Analysis.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"

namespace swift {

class SILModule;
class NominalTypeDecl;
class ProtocolDecl;

class ProtocolConformanceAnalysis : public SILAnalysis {
public:
typedef SmallVector<NominalTypeDecl *, 8> NominalTypeList;
typedef llvm::DenseMap<ProtocolDecl *, NominalTypeList>
ProtocolConformanceMap;

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

~ProtocolConformanceAnalysis();

static bool classof(const SILAnalysis *S) {
return S->getKind() == AnalysisKind::ProtocolConformance;
}

/// Invalidate all information in this analysis.
virtual void invalidate() override {}

/// Invalidate all of the information for a specific function.
virtual void invalidate(SILFunction *F, InvalidationKind K) override {}

/// Notify the analysis about a newly created function.
virtual void notifyAddFunction(SILFunction *F) override {}

/// Notify the analysis about a function which will be deleted from the
/// module.
virtual void notifyDeleteFunction(SILFunction *F) override {}

/// Notify the analysis about changed witness or vtables.
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 *>(ConformsListIt->second.begin(),
ConformsListIt->second.end())
: ArrayRef<NominalTypeDecl *>();
}

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

/// The module.
SILModule *M;

/// A cache that maps a protocol to its conformances
ProtocolConformanceMap ProtocolConformanceCache;
};

} // namespace swift
#endif
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Analysis/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/SILOptimizer/Analysis/IVAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
#include "swift/SILOptimizer/Analysis/ProtocolConformanceAnalysis.h"
#include "swift/AST/Module.h"
#include "swift/AST/SILOptions.h"
#include "swift/SIL/SILModule.h"
Expand Down Expand Up @@ -55,3 +56,7 @@ SILAnalysis *swift::createClassHierarchyAnalysis(SILModule *M) {
SILAnalysis *swift::createBasicCalleeAnalysis(SILModule *M) {
return new BasicCalleeAnalysis(M);
}

SILAnalysis *swift::createProtocolConformanceAnalysis(SILModule *M) {
return new ProtocolConformanceAnalysis(M);
}
1 change: 1 addition & 0 deletions lib/SILOptimizer/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(ANALYSIS_SOURCES
Analysis/LoopAnalysis.cpp
Analysis/LoopRegionAnalysis.cpp
Analysis/MemoryBehavior.cpp
Analysis/ProtocolConformanceAnalysis.cpp
Analysis/RCIdentityAnalysis.cpp
Analysis/SideEffectAnalysis.cpp
Analysis/SimplifyInstruction.cpp
Expand Down
70 changes: 70 additions & 0 deletions lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===-- ProtocolConformanceAnalysis.cpp - Protocol Conformance Analysis ---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This analysis collects a set of nominal types (classes, structs, and enums)
// that conform to a protocol during whole module compilation. We only track
// protocols that are non-public.

#include "swift/SILOptimizer/Analysis/ProtocolConformanceAnalysis.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Module.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILValue.h"

using namespace swift;

namespace {
/// A helper class to collect all nominal type declarations.
class NominalTypeWalker : public ASTWalker {
ProtocolConformanceAnalysis::ProtocolConformanceMap &ProtocolConformanceCache;

public:
NominalTypeWalker(ProtocolConformanceAnalysis::ProtocolConformanceMap
&ProtocolConformanceCache)
: ProtocolConformanceCache(ProtocolConformanceCache) {}

bool walkToDeclPre(Decl *D) override {
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
auto Protocols = NTD->getAllProtocols();
for (auto &Protocol : Protocols) {
if (Protocol->getEffectiveAccess() <= AccessLevel::Internal) {
ProtocolConformanceCache[Protocol].push_back(NTD);
}
}
}
return true;
}
};
} // end anonymous namespace

void ProtocolConformanceAnalysis::init() {

// We only do this in Whole-Module compilation mode.
if (!M->isWholeModule())
return;

// Process all types implementing protocols.
SmallVector<Decl *, 32> Decls;

// Find all top level declarations.
M->getSwiftModule()->getTopLevelDecls(Decls);

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

ProtocolConformanceAnalysis::~ProtocolConformanceAnalysis() {}