Skip to content

Commit c3dd066

Browse files
authored
Merge pull request #33456 from hamishknight/symlink
2 parents 6dddf96 + aa86808 commit c3dd066

File tree

10 files changed

+224
-49
lines changed

10 files changed

+224
-49
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ SWIFT_TYPEID(Requirement)
2828
SWIFT_TYPEID(ResilienceExpansion)
2929
SWIFT_TYPEID(FragileFunctionKind)
3030
SWIFT_TYPEID(TangentPropertyInfo)
31+
SWIFT_TYPEID(SymbolSourceMap)
3132
SWIFT_TYPEID(Type)
3233
SWIFT_TYPEID(TypePair)
3334
SWIFT_TYPEID(TypeWitnessAndDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Requirement;
6060
enum class ResilienceExpansion : unsigned;
6161
struct FragileFunctionKind;
6262
class SourceFile;
63+
class SymbolSourceMap;
6364
struct TangentPropertyInfo;
6465
class Type;
6566
class TypeAliasDecl;

include/swift/AST/TBDGenRequests.h

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "swift/AST/ASTTypeIDs.h"
2121
#include "swift/AST/SimpleRequest.h"
22+
#include "swift/IRGen/Linking.h"
23+
#include "swift/SIL/SILDeclRef.h"
24+
#include "swift/TBDGen/TBDGen.h"
2225

2326
namespace llvm {
2427

@@ -37,12 +40,11 @@ namespace swift {
3740

3841
class FileUnit;
3942
class ModuleDecl;
40-
struct TBDGenOptions;
4143

4244
class TBDGenDescriptor final {
4345
using FileOrModule = llvm::PointerUnion<FileUnit *, ModuleDecl *>;
4446
FileOrModule Input;
45-
const TBDGenOptions &Opts;
47+
TBDGenOptions Opts;
4648

4749
TBDGenDescriptor(FileOrModule input, const TBDGenOptions &opts)
4850
: Input(input), Opts(opts) {
@@ -62,6 +64,7 @@ class TBDGenDescriptor final {
6264

6365
/// Returns the TBDGen options.
6466
const TBDGenOptions &getOptions() const { return Opts; }
67+
TBDGenOptions &getOptions() { return Opts; }
6568

6669
const llvm::DataLayout &getDataLayout() const;
6770
const llvm::Triple &getTarget() const;
@@ -117,6 +120,123 @@ class PublicSymbolsRequest
117120
evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
118121
};
119122

123+
/// Describes the origin of a particular symbol, including the stage of
124+
/// compilation it is introduced, as well as information on what decl introduces
125+
/// it.
126+
class SymbolSource {
127+
public:
128+
enum class Kind {
129+
/// A symbol introduced when emitting a SIL decl.
130+
SIL,
131+
132+
/// A symbol introduced when emitting LLVM IR.
133+
IR,
134+
135+
/// A symbol used to customize linker behavior, introduced by TBDGen.
136+
LinkerDirective,
137+
138+
/// A symbol with an unknown origin.
139+
// FIXME: This should be eliminated.
140+
Unknown
141+
};
142+
Kind kind;
143+
144+
private:
145+
union {
146+
SILDeclRef silDeclRef;
147+
irgen::LinkEntity irEntity;
148+
};
149+
150+
explicit SymbolSource(SILDeclRef ref) : kind(Kind::SIL) {
151+
silDeclRef = ref;
152+
}
153+
explicit SymbolSource(irgen::LinkEntity entity) : kind(Kind::IR) {
154+
irEntity = entity;
155+
}
156+
explicit SymbolSource(Kind kind) : kind(kind) {
157+
assert(kind == Kind::LinkerDirective || kind == Kind::Unknown);
158+
}
159+
160+
public:
161+
static SymbolSource forSILDeclRef(SILDeclRef ref) {
162+
return SymbolSource{ref};
163+
}
164+
static SymbolSource forIRLinkEntity(irgen::LinkEntity entity) {
165+
return SymbolSource{entity};
166+
}
167+
static SymbolSource forLinkerDirective() {
168+
return SymbolSource{Kind::LinkerDirective};
169+
}
170+
static SymbolSource forUnknown() {
171+
return SymbolSource{Kind::Unknown};
172+
}
173+
174+
bool isLinkerDirective() const {
175+
return kind == Kind::LinkerDirective;
176+
}
177+
178+
SILDeclRef getSILDeclRef() const {
179+
assert(kind == Kind::SIL);
180+
return silDeclRef;
181+
}
182+
irgen::LinkEntity getIRLinkEntity() const {
183+
assert(kind == Kind::IR);
184+
return irEntity;
185+
}
186+
};
187+
188+
/// Maps a symbol back to its source for lazy compilation.
189+
class SymbolSourceMap {
190+
friend class SymbolSourceMapRequest;
191+
192+
using Storage = llvm::StringMap<SymbolSource>;
193+
const Storage *storage;
194+
195+
explicit SymbolSourceMap(const Storage *storage) : storage(storage) {
196+
assert(storage);
197+
}
198+
199+
public:
200+
Optional<SymbolSource> find(StringRef symbol) const {
201+
auto result = storage->find(symbol);
202+
if (result == storage->end())
203+
return None;
204+
return result->second;
205+
}
206+
207+
friend bool operator==(const SymbolSourceMap &lhs,
208+
const SymbolSourceMap &rhs) {
209+
return lhs.storage == rhs.storage;
210+
}
211+
friend bool operator!=(const SymbolSourceMap &lhs,
212+
const SymbolSourceMap &rhs) {
213+
return !(lhs == rhs);
214+
}
215+
216+
friend void simple_display(llvm::raw_ostream &out, const SymbolSourceMap &) {
217+
out << "(symbol storage map)";
218+
}
219+
};
220+
221+
/// Computes a map of symbols to their SymbolSource for a file or module.
222+
class SymbolSourceMapRequest
223+
: public SimpleRequest<SymbolSourceMapRequest,
224+
SymbolSourceMap(TBDGenDescriptor),
225+
RequestFlags::Cached> {
226+
public:
227+
using SimpleRequest::SimpleRequest;
228+
229+
private:
230+
friend SimpleRequest;
231+
232+
// Evaluation.
233+
SymbolSourceMap evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
234+
235+
public:
236+
// Cached.
237+
bool isCached() const { return true; }
238+
};
239+
120240
/// Report that a request of the given kind is being evaluated, so it
121241
/// can be recorded by the stats reporter.
122242
template <typename Request>

include/swift/AST/TBDGenTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ SWIFT_REQUEST(TBDGen, GenerateTBDRequest, TBDFile(TBDGenDescriptor),
1919
SWIFT_REQUEST(TBDGen, PublicSymbolsRequest,
2020
std::vector<std::string>(TBDGenDescriptor),
2121
Uncached, NoLocationInfo)
22+
SWIFT_REQUEST(TBDGen, SymbolSourceMapRequest,
23+
SymbolSourceMap(TBDGenDescriptor),
24+
Cached, NoLocationInfo)

include/swift/TBDGen/TBDGen.h

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

1515
#include "llvm/ADT/Hashing.h"
1616
#include "llvm/ADT/StringSet.h"
17-
#include "swift/AST/TBDGenRequests.h"
1817
#include "swift/Basic/Version.h"
1918
#include <vector>
2019

@@ -25,6 +24,7 @@ class raw_ostream;
2524
namespace swift {
2625
class FileUnit;
2726
class ModuleDecl;
27+
class TBDGenDescriptor;
2828

2929
/// Options for controlling the exact set of symbols included in the TBD
3030
/// output.
@@ -38,6 +38,9 @@ struct TBDGenOptions {
3838
/// Only collect linker directive symbols.
3939
bool LinkerDirectivesOnly = false;
4040

41+
/// Whether to include only symbols with public linkage.
42+
bool PublicSymbolsOnly = true;
43+
4144
/// The install_name to use in the TBD file.
4245
std::string InstallName;
4346

@@ -66,6 +69,7 @@ struct TBDGenOptions {
6669
return lhs.HasMultipleIGMs == rhs.HasMultipleIGMs &&
6770
lhs.IsInstallAPI == rhs.IsInstallAPI &&
6871
lhs.LinkerDirectivesOnly == rhs.LinkerDirectivesOnly &&
72+
lhs.PublicSymbolsOnly == rhs.PublicSymbolsOnly &&
6973
lhs.InstallName == rhs.InstallName &&
7074
lhs.ModuleLinkName == rhs.ModuleLinkName &&
7175
lhs.CurrentVersion == rhs.CurrentVersion &&
@@ -82,8 +86,9 @@ struct TBDGenOptions {
8286
using namespace llvm;
8387
return hash_combine(
8488
opts.HasMultipleIGMs, opts.IsInstallAPI, opts.LinkerDirectivesOnly,
85-
opts.InstallName, opts.ModuleLinkName, opts.CurrentVersion,
86-
opts.CompatibilityVersion, opts.ModuleInstallNameMapPath,
89+
opts.PublicSymbolsOnly, opts.InstallName, opts.ModuleLinkName,
90+
opts.CurrentVersion, opts.CompatibilityVersion,
91+
opts.ModuleInstallNameMapPath,
8792
hash_combine_range(opts.embedSymbolsFromModules.begin(),
8893
opts.embedSymbolsFromModules.end()));
8994
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/AST/IRGenRequests.h"
3636
#include "swift/AST/NameLookup.h"
3737
#include "swift/AST/ASTMangler.h"
38+
#include "swift/AST/TBDGenRequests.h"
3839
#include "swift/AST/TypeRefinementContext.h"
3940
#include "swift/Basic/Dwarf.h"
4041
#include "swift/Basic/Edit.h"

lib/FrontendTool/TBD.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/DiagnosticsFrontend.h"
1818
#include "swift/AST/FileUnit.h"
1919
#include "swift/AST/Module.h"
20+
#include "swift/AST/TBDGenRequests.h"
2021
#include "swift/Basic/LLVM.h"
2122
#include "swift/Demangling/Demangle.h"
2223
#include "swift/Frontend/FrontendOptions.h"

lib/IRGen/IRGenRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "swift/AST/Module.h"
1717
#include "swift/AST/SourceFile.h"
1818
#include "swift/SIL/SILModule.h"
19+
#include "swift/AST/TBDGenRequests.h"
1920
#include "swift/Subsystems.h"
20-
#include "swift/TBDGen/TBDGen.h"
2121
#include "llvm/IR/Module.h"
2222
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
2323

0 commit comments

Comments
 (0)