Skip to content

Commit 09bc359

Browse files
authored
Merge pull request swiftlang#32862 from hamishknight/linked-in
2 parents 8248f9c + c354b0f commit 09bc359

File tree

16 files changed

+128
-138
lines changed

16 files changed

+128
-138
lines changed

include/swift/AST/IRGenRequests.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace swift {
2727
class SourceFile;
2828
class IRGenOptions;
2929
class SILModule;
30+
struct TBDGenOptions;
3031

3132
namespace irgen {
3233
class IRGenModule;
@@ -113,15 +114,17 @@ class GeneratedModule final {
113114
};
114115

115116
struct IRGenDescriptor {
116-
const IRGenOptions &Opts;
117117
llvm::PointerUnion<ModuleDecl *, SourceFile *> Ctx;
118+
119+
const IRGenOptions &Opts;
120+
const TBDGenOptions &TBDOpts;
121+
118122
SILModule *SILMod;
119123
StringRef ModuleName;
120124
const PrimarySpecificPaths &PSPs;
121125
StringRef PrivateDiscriminator;
122126
ArrayRef<std::string> parallelOutputFilenames;
123127
llvm::GlobalVariable **outModuleHash;
124-
llvm::StringSet<> *LinkerDirectives;
125128

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

140143
public:
141144
static IRGenDescriptor
142-
forFile(const IRGenOptions &Opts, SourceFile &SF,
143-
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
144-
const PrimarySpecificPaths &PSPs, StringRef PrivateDiscriminator,
145-
llvm::GlobalVariable **outModuleHash,
146-
llvm::StringSet<> *LinkerDirectives) {
147-
return IRGenDescriptor{Opts,
148-
&SF,
145+
forFile(SourceFile &SF, const IRGenOptions &Opts,
146+
const TBDGenOptions &TBDOpts, std::unique_ptr<SILModule> &&SILMod,
147+
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
148+
StringRef PrivateDiscriminator,
149+
llvm::GlobalVariable **outModuleHash) {
150+
return IRGenDescriptor{&SF,
151+
Opts,
152+
TBDOpts,
149153
SILMod.release(),
150154
ModuleName,
151155
PSPs,
152156
PrivateDiscriminator,
153157
{},
154-
outModuleHash,
155-
LinkerDirectives};
158+
outModuleHash};
156159
}
157160

158161
static IRGenDescriptor
159-
forWholeModule(const IRGenOptions &Opts, swift::ModuleDecl *M,
162+
forWholeModule(ModuleDecl *M, const IRGenOptions &Opts,
163+
const TBDGenOptions &TBDOpts,
160164
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
161165
const PrimarySpecificPaths &PSPs,
162166
ArrayRef<std::string> parallelOutputFilenames,
163-
llvm::GlobalVariable **outModuleHash,
164-
llvm::StringSet<> *LinkerDirectives) {
165-
return IRGenDescriptor{Opts,
166-
M,
167+
llvm::GlobalVariable **outModuleHash) {
168+
return IRGenDescriptor{M,
169+
Opts,
170+
TBDOpts,
167171
SILMod.release(),
168172
ModuleName,
169173
PSPs,
170174
"",
171175
parallelOutputFilenames,
172-
outModuleHash,
173-
LinkerDirectives};
176+
outModuleHash};
174177
}
175178

176179
/// Retrieves the files to perform IR generation for.
@@ -179,6 +182,9 @@ struct IRGenDescriptor {
179182
/// For a single file, returns its parent module, otherwise returns the module
180183
/// itself.
181184
ModuleDecl *getParentModule() const;
185+
186+
/// Compute the linker directives to emit.
187+
std::vector<std::string> getLinkerDirectives() const;
182188
};
183189

184190
/// Report that a request of the given kind is being evaluated, so it

include/swift/AST/TBDGenRequests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void simple_display(llvm::raw_ostream &out, const TBDGenDescriptor &desc);
7878
SourceLoc extractNearestSourceLoc(const TBDGenDescriptor &desc);
7979

8080
using TBDFileAndSymbols =
81-
std::pair<llvm::MachO::InterfaceFile, llvm::StringSet<>>;
81+
std::pair<llvm::MachO::InterfaceFile, std::vector<std::string>>;
8282

8383
/// Computes the TBD file and public symbols for a given module or file.
8484
class GenerateTBDRequest

include/swift/Subsystems.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace swift {
6262
class SourceManager;
6363
class SyntaxParseActions;
6464
class SyntaxParsingCache;
65+
struct TBDGenOptions;
6566
class Token;
6667
class TopLevelContext;
6768
class TypeCheckerOptions;
@@ -207,23 +208,23 @@ namespace swift {
207208
/// and return the generated LLVM IR module.
208209
/// If you set an outModuleHash, then you need to call performLLVM.
209210
GeneratedModule
210-
performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
211+
performIRGeneration(ModuleDecl *M, const IRGenOptions &Opts,
212+
const TBDGenOptions &TBDOpts,
211213
std::unique_ptr<SILModule> SILMod,
212214
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
213215
ArrayRef<std::string> parallelOutputFilenames,
214-
llvm::GlobalVariable **outModuleHash = nullptr,
215-
llvm::StringSet<> *LinkerDirectives = nullptr);
216+
llvm::GlobalVariable **outModuleHash = nullptr);
216217

217218
/// Turn the given Swift module into either LLVM IR or native code
218219
/// and return the generated LLVM IR module.
219220
/// If you set an outModuleHash, then you need to call performLLVM.
220221
GeneratedModule
221-
performIRGeneration(const IRGenOptions &Opts, SourceFile &SF,
222+
performIRGeneration(SourceFile &SF, const IRGenOptions &Opts,
223+
const TBDGenOptions &TBDOpts,
222224
std::unique_ptr<SILModule> SILMod,
223225
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
224226
StringRef PrivateDiscriminator,
225-
llvm::GlobalVariable **outModuleHash = nullptr,
226-
llvm::StringSet<> *LinkerDirectives = nullptr);
227+
llvm::GlobalVariable **outModuleHash = nullptr);
227228

228229
/// Given an already created LLVM module, construct a pass pipeline and run
229230
/// the Swift LLVM Pipeline upon it. This does not cause the module to be

include/swift/TBDGen/TBDGen.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/ADT/Hashing.h"
1616
#include "llvm/ADT/StringSet.h"
17+
#include "swift/AST/TBDGenRequests.h"
1718
#include "swift/Basic/Version.h"
1819
#include <vector>
1920

@@ -88,10 +89,7 @@ struct TBDGenOptions {
8889
}
8990
};
9091

91-
void enumeratePublicSymbols(FileUnit *module, llvm::StringSet<> &symbols,
92-
const TBDGenOptions &opts);
93-
void enumeratePublicSymbols(ModuleDecl *module, llvm::StringSet<> &symbols,
94-
const TBDGenOptions &opts);
92+
std::vector<std::string> getPublicSymbols(TBDGenDescriptor desc);
9593

9694
void writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
9795
const TBDGenOptions &opts);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,9 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
10541054
}
10551055
auto tbdOpts = Invocation.getTBDGenOptions();
10561056
tbdOpts.LinkerDirectivesOnly = true;
1057-
llvm::StringSet<> ldSymbols;
10581057
auto *module = Instance.getMainModule();
1059-
enumeratePublicSymbols(module, ldSymbols, tbdOpts);
1058+
auto ldSymbols =
1059+
getPublicSymbols(TBDGenDescriptor::forModule(module, tbdOpts));
10601060
std::error_code EC;
10611061
llvm::raw_fd_ostream OS(Path, EC, llvm::sys::fs::F_None);
10621062
if (EC) {
@@ -1074,7 +1074,7 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
10741074
llvm::raw_svector_ostream NameOS(NameBuffer);
10751075
NameOS << "ldAdd_" << Idx;
10761076
OS << "extern const char " << NameOS.str() << " __asm(\"" <<
1077-
changeToLdAdd(S.getKey()) << "\");\n";
1077+
changeToLdAdd(S) << "\");\n";
10781078
OS << "const char " << NameOS.str() << " = 0;\n";
10791079
++ Idx;
10801080
}
@@ -1501,24 +1501,21 @@ static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,
15011501
}
15021502

15031503
static GeneratedModule
1504-
generateIR(const IRGenOptions &IRGenOpts,
1504+
generateIR(const IRGenOptions &IRGenOpts, const TBDGenOptions &TBDOpts,
15051505
std::unique_ptr<SILModule> SM,
15061506
const PrimarySpecificPaths &PSPs,
15071507
StringRef OutputFilename, ModuleOrSourceFile MSF,
15081508
llvm::GlobalVariable *&HashGlobal,
1509-
ArrayRef<std::string> parallelOutputFilenames,
1510-
llvm::StringSet<> &LinkerDirectives) {
1509+
ArrayRef<std::string> parallelOutputFilenames) {
15111510
if (auto *SF = MSF.dyn_cast<SourceFile *>()) {
1512-
return performIRGeneration(IRGenOpts, *SF,
1511+
return performIRGeneration(*SF, IRGenOpts, TBDOpts,
15131512
std::move(SM), OutputFilename, PSPs,
15141513
SF->getPrivateDiscriminator().str(),
1515-
&HashGlobal,
1516-
&LinkerDirectives);
1514+
&HashGlobal);
15171515
} else {
1518-
return performIRGeneration(IRGenOpts, MSF.get<ModuleDecl *>(),
1516+
return performIRGeneration(MSF.get<ModuleDecl *>(), IRGenOpts, TBDOpts,
15191517
std::move(SM), OutputFilename, PSPs,
1520-
parallelOutputFilenames,
1521-
&HashGlobal, &LinkerDirectives);
1518+
parallelOutputFilenames, &HashGlobal);
15221519
}
15231520
}
15241521

@@ -1663,17 +1660,6 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
16631660
OutputFilename, Instance.getStatsReporter());
16641661
}
16651662

1666-
static void collectLinkerDirectives(const CompilerInvocation &Invocation,
1667-
ModuleOrSourceFile MSF,
1668-
llvm::StringSet<> &Symbols) {
1669-
auto tbdOpts = Invocation.getTBDGenOptions();
1670-
tbdOpts.LinkerDirectivesOnly = true;
1671-
if (MSF.is<SourceFile*>())
1672-
enumeratePublicSymbols(MSF.get<SourceFile*>(), Symbols, tbdOpts);
1673-
else
1674-
enumeratePublicSymbols(MSF.get<ModuleDecl*>(), Symbols, tbdOpts);
1675-
}
1676-
16771663
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16781664
std::unique_ptr<SILModule> SM,
16791665
ModuleOrSourceFile MSF,
@@ -1781,18 +1767,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
17811767
return processCommandLineAndRunImmediately(
17821768
Instance, std::move(SM), MSF, observer, ReturnValue);
17831769

1784-
llvm::StringSet<> LinkerDirectives;
1785-
collectLinkerDirectives(Invocation, MSF, LinkerDirectives);
1786-
// Don't proceed to IRGen if collecting linker directives failed.
1787-
if (Context.hadError())
1788-
return true;
17891770
StringRef OutputFilename = PSPs.OutputFilename;
17901771
std::vector<std::string> ParallelOutputFilenames =
17911772
opts.InputsAndOutputs.copyOutputFilenames();
17921773
llvm::GlobalVariable *HashGlobal;
17931774
auto IRModule = generateIR(
1794-
IRGenOpts, std::move(SM), PSPs, OutputFilename, MSF, HashGlobal,
1795-
ParallelOutputFilenames, LinkerDirectives);
1775+
IRGenOpts, Invocation.getTBDGenOptions(), std::move(SM), PSPs,
1776+
OutputFilename, MSF, HashGlobal, ParallelOutputFilenames);
17961777

17971778
// Just because we had an AST error it doesn't mean we can't performLLVM.
17981779
bool HadError = Instance.getASTContext().hadError();

lib/FrontendTool/TBD.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ bool swift::inputFileKindCanHaveTBDValidated(InputFileKind kind) {
7373
llvm_unreachable("unhandled kind");
7474
}
7575

76-
static bool validateSymbolSet(DiagnosticEngine &diags,
77-
llvm::StringSet<> symbols,
78-
const llvm::Module &IRModule,
79-
bool diagnoseExtraSymbolsInTBD) {
76+
static bool validateSymbols(DiagnosticEngine &diags,
77+
const std::vector<std::string> &symbols,
78+
const llvm::Module &IRModule,
79+
bool diagnoseExtraSymbolsInTBD) {
80+
llvm::StringSet<> symbolSet;
81+
symbolSet.insert(symbols.begin(), symbols.end());
82+
8083
auto error = false;
8184

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

122125
if (diagnoseExtraSymbolsInTBD) {
123126
// Look for any extra symbols.
124-
for (auto &name : sortSymbols(symbols)) {
127+
for (auto &name : sortSymbols(symbolSet)) {
125128
diags.diagnose(SourceLoc(), diag::symbol_in_tbd_not_in_ir, name,
126129
Demangle::demangleSymbolAsString(name));
127130
error = true;
@@ -139,21 +142,16 @@ bool swift::validateTBD(ModuleDecl *M,
139142
const llvm::Module &IRModule,
140143
const TBDGenOptions &opts,
141144
bool diagnoseExtraSymbolsInTBD) {
142-
llvm::StringSet<> symbols;
143-
enumeratePublicSymbols(M, symbols, opts);
144-
145-
return validateSymbolSet(M->getASTContext().Diags, symbols, IRModule,
146-
diagnoseExtraSymbolsInTBD);
145+
auto symbols = getPublicSymbols(TBDGenDescriptor::forModule(M, opts));
146+
return validateSymbols(M->getASTContext().Diags, symbols, IRModule,
147+
diagnoseExtraSymbolsInTBD);
147148
}
148149

149150
bool swift::validateTBD(FileUnit *file,
150151
const llvm::Module &IRModule,
151152
const TBDGenOptions &opts,
152153
bool diagnoseExtraSymbolsInTBD) {
153-
llvm::StringSet<> symbols;
154-
enumeratePublicSymbols(file, symbols, opts);
155-
156-
return validateSymbolSet(file->getParentModule()->getASTContext().Diags,
157-
symbols, IRModule,
158-
diagnoseExtraSymbolsInTBD);
154+
auto symbols = getPublicSymbols(TBDGenDescriptor::forFile(file, opts));
155+
return validateSymbols(file->getParentModule()->getASTContext().Diags,
156+
symbols, IRModule, diagnoseExtraSymbolsInTBD);
159157
}

lib/IRGen/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ target_link_libraries(swiftIRGen PRIVATE
6969
swiftLLVMPasses
7070
swiftSIL
7171
swiftSILGen
72-
swiftSILOptimizer)
72+
swiftSILOptimizer
73+
swiftTBDGen)

lib/IRGen/GenDecl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,8 @@ static bool hasCodeCoverageInstrumentation(SILFunction &f, SILModule &m) {
10301030
return f.getProfiler() && m.getOptions().EmitProfileCoverageMapping;
10311031
}
10321032

1033-
void IRGenerator::emitGlobalTopLevel(llvm::StringSet<> *linkerDirectives) {
1033+
void IRGenerator::emitGlobalTopLevel(
1034+
const std::vector<std::string> &linkerDirectives) {
10341035
// Generate order numbers for the functions in the SIL module that
10351036
// correspond to definitions in the LLVM module.
10361037
unsigned nextOrderNumber = 0;
@@ -1050,10 +1051,8 @@ void IRGenerator::emitGlobalTopLevel(llvm::StringSet<> *linkerDirectives) {
10501051
CurrentIGMPtr IGM = getGenModule(wt.getProtocol()->getDeclContext());
10511052
ensureRelativeSymbolCollocation(wt);
10521053
}
1053-
if (linkerDirectives) {
1054-
for (auto &entry: *linkerDirectives) {
1055-
createLinkerDirectiveVariable(*PrimaryIGM, entry.getKey());
1056-
}
1054+
for (auto &directive: linkerDirectives) {
1055+
createLinkerDirectiveVariable(*PrimaryIGM, directive);
10571056
}
10581057
for (SILGlobalVariable &v : PrimaryIGM->getSILModule().getSILGlobals()) {
10591058
Decl *decl = v.getDecl();

0 commit comments

Comments
 (0)