mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Add a SILLinkage mode "Deserialized" to make sure IRGen will emit hidden symbols for deserialized SILFunction. Inside SIL linker, set Linkage to external if we only have a declaration for a callee function. Both sil block and decl block in a module can emit an array of substitutions. To share the serialization between SILSerializer and Serializer, we modify the interface to pass in the abbreviation codes to write functions and to pass in a cursor to read functions. We now correctly handle the serialization of Substitutions in SpecializeInst. For a deserialized SILFunction, we now temporarily set its SILLocation and DebugScope to an empty FileLocation. Once mandatory inliner sets the SILLocation to the location of ApplyInst, a null SILLocation and a null DebugScope may work for a deserialized SILFunction. Update testing cases to reflect that we are now inlining transparent functions from modules, or to disable SILDeserializer for now (I am not sure how to update those testing cases). Swift SVN r8582
87 lines
3.1 KiB
C++
87 lines
3.1 KiB
C++
//===--- Link.cpp - Link in transparent SILFunctions from module ----------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "link"
|
|
#include "swift/AST/Module.h"
|
|
#include "swift/Serialization/SerializedSILLoader.h"
|
|
#include "swift/SIL/SILModule.h"
|
|
#include "swift/Subsystems.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
using namespace swift;
|
|
|
|
// To help testing serialization, deserialization, we turn on sil-link-all.
|
|
static llvm::cl::opt<bool>
|
|
EnableLinkAll("sil-link-all", llvm::cl::Hidden, llvm::cl::init(false));
|
|
static llvm::cl::opt<bool>
|
|
EnableLinking("enable-sil-linking", llvm::cl::Hidden, llvm::cl::init(true));
|
|
|
|
STATISTIC(NumFuncLinked, "Number of SIL functions linked");
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Top Level Driver
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void swift::performSILLinking(SILModule *M) {
|
|
if (!EnableLinking && !EnableLinkAll)
|
|
return;
|
|
|
|
SerializedSILLoader *SILLoader = SerializedSILLoader::create(
|
|
M->getASTContext(), M);
|
|
SmallVector<SILFunction*, 128> Worklist;
|
|
for (auto &Fn : *M)
|
|
Worklist.push_back(&Fn);
|
|
|
|
while (!Worklist.empty()) {
|
|
auto Fn = Worklist.pop_back_val();
|
|
|
|
for (auto &BB : *Fn) {
|
|
for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
|
|
SILFunction *CalleeFunction = nullptr;
|
|
bool TryLinking = false;
|
|
if (ApplyInst *AI = dyn_cast<ApplyInst>(I)) {
|
|
SILValue Callee = AI->getCallee();
|
|
// Handles FunctionRefInst only.
|
|
if (FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(Callee.getDef()))
|
|
CalleeFunction = FRI->getFunction();
|
|
|
|
// When EnableLinkAll is true, we always link the Callee.
|
|
TryLinking = EnableLinkAll ? true : AI->isTransparent();
|
|
}
|
|
else if (SpecializeInst *SI = dyn_cast<SpecializeInst>(I)) {
|
|
SILValue Callee = SI->getOperand();
|
|
// Handles FunctionRefInst only.
|
|
if (FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(Callee.getDef()))
|
|
CalleeFunction = FRI->getFunction();
|
|
}
|
|
if (!CalleeFunction)
|
|
continue;
|
|
|
|
if (CalleeFunction->empty()) {
|
|
// Try to find the definition in a serialized module when callee is
|
|
// currently empty.
|
|
if (TryLinking) {
|
|
if (auto NewFn = SILLoader->lookupSILFunction(CalleeFunction)) {
|
|
Worklist.push_back(NewFn);
|
|
++NumFuncLinked;
|
|
continue;
|
|
}
|
|
}
|
|
// FIXME: Make sure the declaration has external linkage?
|
|
CalleeFunction->setLinkage(SILLinkage::External);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|