Files
swift-mirror/lib/Serialization/SerializedSILLoader.cpp
Michael Gottesman 872bf40e17 [sil-optimizer] Centralize how we send out serialization notifications.
Previously SILModule contained two different pathways for the deserializer to
send notifications that it had created functions:

1. A list of function pointers that were called when a function's body was
deserialized. This was added recently so that access enforcement elimination is
run on newly deserialized SIL code if we have already eliminated access
enforcement from the module.

2. SILModule::SerializationCallback. This is an implementation of the full
callback interface and is used by the SILModule to update linkage and other
sorts of book keeping.

To fix the pass manager notification infrastructure, I need to be able to send
notifications to a SILPassManager when deserializing. I also need to be able to
eliminate these callbacks when a SILPassManager is destroyed. These requirements
are incompatible with the current two implementations since: (2) is an
implementation detail of SILModule and (1) only notifies on function bodies
being deserialized instead of the creation of new declarations (what the caller
analysis wants).

Rather than adding a third group of callbacks, this commit refactors the
infrastructure in such a way that all of these use cases can use one
implementation. This is done by:

1. Lifting the interface of SerializedSILLoader::Callback into a base
notification protocol for deserialization called
DeserializationNotificationHandlerBase and its base no-op implementation into an
implementation of the aforementioned protocol:
DeserializationNotificationHandler.

2. Changing SILModule::SerializationCallback to implement
DeserializationNotificationHandler.

3. Creating a class called FunctionBodyDeserializationNotificationHandler that
takes in a function pointer and uses that to just override the
didDeserializeFunctionBody. This eliminates the need for the specific function
body deserialization list.

4. Replacing the state associated with the two other pathways with a single
DeserializationNotificationHandlerSet class that contains a set of
DeserializationNotificationHandler and chains notifications to them. This set
implements DeserializationNotificationHandlerBase so we know that its
implementation will always be in sync with DeserializationNotificationHandler.

rdar://42301529
2018-08-15 15:49:15 -07:00

184 lines
5.8 KiB
C++

//===--- SerializedSILLoader.cpp - A loader for SIL sections --------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "serialized-sil-loader"
#include "swift/Serialization/SerializedSILLoader.h"
#include "DeserializeSIL.h"
#include "swift/Serialization/ModuleFile.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/SIL/SILModule.h"
#include "llvm/Support/Debug.h"
using namespace swift;
SerializedSILLoader::SerializedSILLoader(
ASTContext &Ctx, SILModule *SILMod,
DeserializationNotificationHandlerSet *callbacks) {
// Get a list of SerializedModules from ASTContext.
// FIXME: Iterating over LoadedModules is not a good way to do this.
for (auto &Entry : Ctx.LoadedModules) {
for (auto File : Entry.second->getFiles()) {
if (auto LoadedAST = dyn_cast<SerializedASTFile>(File)) {
auto Des = new SILDeserializer(&LoadedAST->File, *SILMod, callbacks);
#ifndef NDEBUG
SILMod->verify();
#endif
LoadedSILSections.emplace_back(Des);
}
}
}
}
SerializedSILLoader::~SerializedSILLoader() {}
SILFunction *SerializedSILLoader::lookupSILFunction(SILFunction *Callee) {
// It is possible that one module has a declaration of a SILFunction, while
// another has the full definition.
SILFunction *retVal = nullptr;
for (auto &Des : LoadedSILSections) {
if (auto Func = Des->lookupSILFunction(Callee)) {
LLVM_DEBUG(llvm::dbgs() << "Deserialized " << Func->getName() << " from "
<< Des->getModuleIdentifier().str() << "\n");
if (!Func->empty())
return Func;
retVal = Func;
}
}
return retVal;
}
SILFunction *SerializedSILLoader::lookupSILFunction(StringRef Name,
bool declarationOnly,
Optional<SILLinkage> Linkage) {
// It is possible that one module has a declaration of a SILFunction, while
// another has the full definition.
SILFunction *retVal = nullptr;
for (auto &Des : LoadedSILSections) {
if (auto Func = Des->lookupSILFunction(Name, declarationOnly)) {
LLVM_DEBUG(llvm::dbgs() << "Deserialized " << Func->getName() << " from "
<< Des->getModuleIdentifier().str() << "\n");
if (Linkage) {
// This is not the linkage we are looking for.
if (Func->getLinkage() != *Linkage) {
LLVM_DEBUG(llvm::dbgs()
<< "Wrong linkage for Function: "
<< Func->getName() << " : "
<< (int)Func->getLinkage() << "\n");
Des->invalidateFunction(Func);
Func->getModule().eraseFunction(Func);
continue;
}
}
if (!Func->empty() || declarationOnly)
return Func;
retVal = Func;
}
}
return retVal;
}
bool SerializedSILLoader::hasSILFunction(StringRef Name,
Optional<SILLinkage> Linkage) {
// It is possible that one module has a declaration of a SILFunction, while
// another has the full definition.
SILFunction *retVal = nullptr;
for (auto &Des : LoadedSILSections) {
if (Des->hasSILFunction(Name, Linkage))
return true;
}
return retVal;
}
SILVTable *SerializedSILLoader::lookupVTable(Identifier Name) {
for (auto &Des : LoadedSILSections) {
if (auto VT = Des->lookupVTable(Name))
return VT;
}
return nullptr;
}
SILWitnessTable *SerializedSILLoader::lookupWitnessTable(SILWitnessTable *WT) {
for (auto &Des : LoadedSILSections)
if (auto wT = Des->lookupWitnessTable(WT))
return wT;
return nullptr;
}
SILDefaultWitnessTable *SerializedSILLoader::
lookupDefaultWitnessTable(SILDefaultWitnessTable *WT) {
for (auto &Des : LoadedSILSections)
if (auto wT = Des->lookupDefaultWitnessTable(WT))
return wT;
return nullptr;
}
void SerializedSILLoader::invalidateCaches() {
for (auto &Des : LoadedSILSections)
Des->invalidateFunctionCache();
}
bool SerializedSILLoader::invalidateFunction(SILFunction *F) {
for (auto &Des : LoadedSILSections)
if (Des->invalidateFunction(F))
return true;
return false;
}
void SerializedSILLoader::getAll() {
for (auto &Des : LoadedSILSections)
Des->getAll();
}
// FIXME: Not the best interface. We know exactly which FileUnits may have SIL
// those in the main module.
void SerializedSILLoader::getAllForModule(Identifier Mod,
FileUnit *PrimaryFile) {
for (auto &Des : LoadedSILSections) {
if (Des->getModuleIdentifier() == Mod) {
Des->getAll(PrimaryFile ?
Des->getFile() != PrimaryFile : false);
}
}
}
void SerializedSILLoader::getAllSILFunctions() {
for (auto &Des : LoadedSILSections)
Des->getAllSILFunctions();
}
/// Deserialize all VTables in all SILModules.
void SerializedSILLoader::getAllVTables() {
for (auto &Des : LoadedSILSections)
Des->getAllVTables();
}
/// Deserialize all WitnessTables in all SILModules.
void SerializedSILLoader::getAllWitnessTables() {
for (auto &Des : LoadedSILSections)
Des->getAllWitnessTables();
}
/// Deserialize all DefaultWitnessTables in all SILModules.
void SerializedSILLoader::getAllDefaultWitnessTables() {
for (auto &Des : LoadedSILSections)
Des->getAllDefaultWitnessTables();
}
/// Deserialize all Properties in all SILModules.
void SerializedSILLoader::getAllProperties() {
for (auto &Des : LoadedSILSections)
Des->getAllProperties();
}