Skip to content
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
42 changes: 24 additions & 18 deletions include/swift/AST/IRGenRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace swift {
class SourceFile;
class IRGenOptions;
class SILModule;
struct TBDGenOptions;

namespace irgen {
class IRGenModule;
Expand Down Expand Up @@ -113,15 +114,17 @@ class GeneratedModule final {
};

struct IRGenDescriptor {
const IRGenOptions &Opts;
llvm::PointerUnion<ModuleDecl *, SourceFile *> Ctx;

const IRGenOptions &Opts;
const TBDGenOptions &TBDOpts;

SILModule *SILMod;
StringRef ModuleName;
const PrimarySpecificPaths &PSPs;
StringRef PrivateDiscriminator;
ArrayRef<std::string> parallelOutputFilenames;
llvm::GlobalVariable **outModuleHash;
llvm::StringSet<> *LinkerDirectives;

friend llvm::hash_code hash_value(const IRGenDescriptor &owner) {
return llvm::hash_combine(owner.Ctx);
Expand All @@ -139,38 +142,38 @@ struct IRGenDescriptor {

public:
static IRGenDescriptor
forFile(const IRGenOptions &Opts, SourceFile &SF,
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
const PrimarySpecificPaths &PSPs, StringRef PrivateDiscriminator,
llvm::GlobalVariable **outModuleHash,
llvm::StringSet<> *LinkerDirectives) {
return IRGenDescriptor{Opts,
&SF,
forFile(SourceFile &SF, const IRGenOptions &Opts,
const TBDGenOptions &TBDOpts, std::unique_ptr<SILModule> &&SILMod,
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
StringRef PrivateDiscriminator,
llvm::GlobalVariable **outModuleHash) {
return IRGenDescriptor{&SF,
Opts,
TBDOpts,
SILMod.release(),
ModuleName,
PSPs,
PrivateDiscriminator,
{},
outModuleHash,
LinkerDirectives};
outModuleHash};
}

static IRGenDescriptor
forWholeModule(const IRGenOptions &Opts, swift::ModuleDecl *M,
forWholeModule(ModuleDecl *M, const IRGenOptions &Opts,
const TBDGenOptions &TBDOpts,
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
const PrimarySpecificPaths &PSPs,
ArrayRef<std::string> parallelOutputFilenames,
llvm::GlobalVariable **outModuleHash,
llvm::StringSet<> *LinkerDirectives) {
return IRGenDescriptor{Opts,
M,
llvm::GlobalVariable **outModuleHash) {
return IRGenDescriptor{M,
Opts,
TBDOpts,
SILMod.release(),
ModuleName,
PSPs,
"",
parallelOutputFilenames,
outModuleHash,
LinkerDirectives};
outModuleHash};
}

/// Retrieves the files to perform IR generation for.
Expand All @@ -179,6 +182,9 @@ struct IRGenDescriptor {
/// For a single file, returns its parent module, otherwise returns the module
/// itself.
ModuleDecl *getParentModule() const;

/// Compute the linker directives to emit.
std::vector<std::string> getLinkerDirectives() const;
};

/// Report that a request of the given kind is being evaluated, so it
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TBDGenRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void simple_display(llvm::raw_ostream &out, const TBDGenDescriptor &desc);
SourceLoc extractNearestSourceLoc(const TBDGenDescriptor &desc);

using TBDFileAndSymbols =
std::pair<llvm::MachO::InterfaceFile, llvm::StringSet<>>;
std::pair<llvm::MachO::InterfaceFile, std::vector<std::string>>;

/// Computes the TBD file and public symbols for a given module or file.
class GenerateTBDRequest
Expand Down
13 changes: 7 additions & 6 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace swift {
class SourceManager;
class SyntaxParseActions;
class SyntaxParsingCache;
struct TBDGenOptions;
class Token;
class TopLevelContext;
class TypeCheckerOptions;
Expand Down Expand Up @@ -207,23 +208,23 @@ namespace swift {
/// and return the generated LLVM IR module.
/// If you set an outModuleHash, then you need to call performLLVM.
GeneratedModule
performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
performIRGeneration(ModuleDecl *M, const IRGenOptions &Opts,
const TBDGenOptions &TBDOpts,
std::unique_ptr<SILModule> SILMod,
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
ArrayRef<std::string> parallelOutputFilenames,
llvm::GlobalVariable **outModuleHash = nullptr,
llvm::StringSet<> *LinkerDirectives = nullptr);
llvm::GlobalVariable **outModuleHash = nullptr);

/// Turn the given Swift module into either LLVM IR or native code
/// and return the generated LLVM IR module.
/// If you set an outModuleHash, then you need to call performLLVM.
GeneratedModule
performIRGeneration(const IRGenOptions &Opts, SourceFile &SF,
performIRGeneration(SourceFile &SF, const IRGenOptions &Opts,
const TBDGenOptions &TBDOpts,
std::unique_ptr<SILModule> SILMod,
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
StringRef PrivateDiscriminator,
llvm::GlobalVariable **outModuleHash = nullptr,
llvm::StringSet<> *LinkerDirectives = nullptr);
llvm::GlobalVariable **outModuleHash = nullptr);

/// Given an already created LLVM module, construct a pass pipeline and run
/// the Swift LLVM Pipeline upon it. This does not cause the module to be
Expand Down
6 changes: 2 additions & 4 deletions include/swift/TBDGen/TBDGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringSet.h"
#include "swift/AST/TBDGenRequests.h"
#include "swift/Basic/Version.h"
#include <vector>

Expand Down Expand Up @@ -88,10 +89,7 @@ struct TBDGenOptions {
}
};

void enumeratePublicSymbols(FileUnit *module, llvm::StringSet<> &symbols,
const TBDGenOptions &opts);
void enumeratePublicSymbols(ModuleDecl *module, llvm::StringSet<> &symbols,
const TBDGenOptions &opts);
std::vector<std::string> getPublicSymbols(TBDGenDescriptor desc);

void writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
const TBDGenOptions &opts);
Expand Down
41 changes: 11 additions & 30 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,9 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
}
auto tbdOpts = Invocation.getTBDGenOptions();
tbdOpts.LinkerDirectivesOnly = true;
llvm::StringSet<> ldSymbols;
auto *module = Instance.getMainModule();
enumeratePublicSymbols(module, ldSymbols, tbdOpts);
auto ldSymbols =
getPublicSymbols(TBDGenDescriptor::forModule(module, tbdOpts));
std::error_code EC;
llvm::raw_fd_ostream OS(Path, EC, llvm::sys::fs::F_None);
if (EC) {
Expand All @@ -1074,7 +1074,7 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
llvm::raw_svector_ostream NameOS(NameBuffer);
NameOS << "ldAdd_" << Idx;
OS << "extern const char " << NameOS.str() << " __asm(\"" <<
changeToLdAdd(S.getKey()) << "\");\n";
changeToLdAdd(S) << "\");\n";
OS << "const char " << NameOS.str() << " = 0;\n";
++ Idx;
}
Expand Down Expand Up @@ -1501,24 +1501,21 @@ static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,
}

static GeneratedModule
generateIR(const IRGenOptions &IRGenOpts,
generateIR(const IRGenOptions &IRGenOpts, const TBDGenOptions &TBDOpts,
std::unique_ptr<SILModule> SM,
const PrimarySpecificPaths &PSPs,
StringRef OutputFilename, ModuleOrSourceFile MSF,
llvm::GlobalVariable *&HashGlobal,
ArrayRef<std::string> parallelOutputFilenames,
llvm::StringSet<> &LinkerDirectives) {
ArrayRef<std::string> parallelOutputFilenames) {
if (auto *SF = MSF.dyn_cast<SourceFile *>()) {
return performIRGeneration(IRGenOpts, *SF,
return performIRGeneration(*SF, IRGenOpts, TBDOpts,
std::move(SM), OutputFilename, PSPs,
SF->getPrivateDiscriminator().str(),
&HashGlobal,
&LinkerDirectives);
&HashGlobal);
} else {
return performIRGeneration(IRGenOpts, MSF.get<ModuleDecl *>(),
return performIRGeneration(MSF.get<ModuleDecl *>(), IRGenOpts, TBDOpts,
std::move(SM), OutputFilename, PSPs,
parallelOutputFilenames,
&HashGlobal, &LinkerDirectives);
parallelOutputFilenames, &HashGlobal);
}
}

Expand Down Expand Up @@ -1663,17 +1660,6 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
OutputFilename, Instance.getStatsReporter());
}

static void collectLinkerDirectives(const CompilerInvocation &Invocation,
ModuleOrSourceFile MSF,
llvm::StringSet<> &Symbols) {
auto tbdOpts = Invocation.getTBDGenOptions();
tbdOpts.LinkerDirectivesOnly = true;
if (MSF.is<SourceFile*>())
enumeratePublicSymbols(MSF.get<SourceFile*>(), Symbols, tbdOpts);
else
enumeratePublicSymbols(MSF.get<ModuleDecl*>(), Symbols, tbdOpts);
}

static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
std::unique_ptr<SILModule> SM,
ModuleOrSourceFile MSF,
Expand Down Expand Up @@ -1781,18 +1767,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
return processCommandLineAndRunImmediately(
Instance, std::move(SM), MSF, observer, ReturnValue);

llvm::StringSet<> LinkerDirectives;
collectLinkerDirectives(Invocation, MSF, LinkerDirectives);
// Don't proceed to IRGen if collecting linker directives failed.
if (Context.hadError())
return true;
StringRef OutputFilename = PSPs.OutputFilename;
std::vector<std::string> ParallelOutputFilenames =
opts.InputsAndOutputs.copyOutputFilenames();
llvm::GlobalVariable *HashGlobal;
auto IRModule = generateIR(
IRGenOpts, std::move(SM), PSPs, OutputFilename, MSF, HashGlobal,
ParallelOutputFilenames, LinkerDirectives);
IRGenOpts, Invocation.getTBDGenOptions(), std::move(SM), PSPs,
OutputFilename, MSF, HashGlobal, ParallelOutputFilenames);

// Just because we had an AST error it doesn't mean we can't performLLVM.
bool HadError = Instance.getASTContext().hadError();
Expand Down
34 changes: 16 additions & 18 deletions lib/FrontendTool/TBD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ bool swift::inputFileKindCanHaveTBDValidated(InputFileKind kind) {
llvm_unreachable("unhandled kind");
}

static bool validateSymbolSet(DiagnosticEngine &diags,
llvm::StringSet<> symbols,
const llvm::Module &IRModule,
bool diagnoseExtraSymbolsInTBD) {
static bool validateSymbols(DiagnosticEngine &diags,
const std::vector<std::string> &symbols,
const llvm::Module &IRModule,
bool diagnoseExtraSymbolsInTBD) {
llvm::StringSet<> symbolSet;
symbolSet.insert(symbols.begin(), symbols.end());

auto error = false;

// Diff the two sets of symbols, flagging anything outside their intersection.
Expand All @@ -101,13 +104,13 @@ static bool validateSymbolSet(DiagnosticEngine &diags,
GV->hasExternalLinkage() && !GV->hasHiddenVisibility();
if (!GV->isDeclaration() && externallyVisible) {
// Is it listed?
if (!symbols.erase(name))
if (!symbolSet.erase(name))
// Note: Add the unmangled name to the irNotTBD list, which is owned
// by the IRModule, instead of the mangled name.
irNotTBD.push_back(unmangledName);
}
} else {
assert(symbols.find(name) == symbols.end() &&
assert(symbolSet.find(name) == symbolSet.end() &&
"non-global value in value symbol table");
}
}
Expand All @@ -121,7 +124,7 @@ static bool validateSymbolSet(DiagnosticEngine &diags,

if (diagnoseExtraSymbolsInTBD) {
// Look for any extra symbols.
for (auto &name : sortSymbols(symbols)) {
for (auto &name : sortSymbols(symbolSet)) {
diags.diagnose(SourceLoc(), diag::symbol_in_tbd_not_in_ir, name,
Demangle::demangleSymbolAsString(name));
error = true;
Expand All @@ -139,21 +142,16 @@ bool swift::validateTBD(ModuleDecl *M,
const llvm::Module &IRModule,
const TBDGenOptions &opts,
bool diagnoseExtraSymbolsInTBD) {
llvm::StringSet<> symbols;
enumeratePublicSymbols(M, symbols, opts);

return validateSymbolSet(M->getASTContext().Diags, symbols, IRModule,
diagnoseExtraSymbolsInTBD);
auto symbols = getPublicSymbols(TBDGenDescriptor::forModule(M, opts));
return validateSymbols(M->getASTContext().Diags, symbols, IRModule,
diagnoseExtraSymbolsInTBD);
}

bool swift::validateTBD(FileUnit *file,
const llvm::Module &IRModule,
const TBDGenOptions &opts,
bool diagnoseExtraSymbolsInTBD) {
llvm::StringSet<> symbols;
enumeratePublicSymbols(file, symbols, opts);

return validateSymbolSet(file->getParentModule()->getASTContext().Diags,
symbols, IRModule,
diagnoseExtraSymbolsInTBD);
auto symbols = getPublicSymbols(TBDGenDescriptor::forFile(file, opts));
return validateSymbols(file->getParentModule()->getASTContext().Diags,
symbols, IRModule, diagnoseExtraSymbolsInTBD);
}
3 changes: 2 additions & 1 deletion lib/IRGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ target_link_libraries(swiftIRGen PRIVATE
swiftLLVMPasses
swiftSIL
swiftSILGen
swiftSILOptimizer)
swiftSILOptimizer
swiftTBDGen)
9 changes: 4 additions & 5 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,8 @@ static bool hasCodeCoverageInstrumentation(SILFunction &f, SILModule &m) {
return f.getProfiler() && m.getOptions().EmitProfileCoverageMapping;
}

void IRGenerator::emitGlobalTopLevel(llvm::StringSet<> *linkerDirectives) {
void IRGenerator::emitGlobalTopLevel(
const std::vector<std::string> &linkerDirectives) {
// Generate order numbers for the functions in the SIL module that
// correspond to definitions in the LLVM module.
unsigned nextOrderNumber = 0;
Expand All @@ -1050,10 +1051,8 @@ void IRGenerator::emitGlobalTopLevel(llvm::StringSet<> *linkerDirectives) {
CurrentIGMPtr IGM = getGenModule(wt.getProtocol()->getDeclContext());
ensureRelativeSymbolCollocation(wt);
}
if (linkerDirectives) {
for (auto &entry: *linkerDirectives) {
createLinkerDirectiveVariable(*PrimaryIGM, entry.getKey());
}
for (auto &directive: linkerDirectives) {
createLinkerDirectiveVariable(*PrimaryIGM, directive);
}
for (SILGlobalVariable &v : PrimaryIGM->getSILModule().getSILGlobals()) {
Decl *decl = v.getDecl();
Expand Down
Loading