Skip to content

Commit a6eb4b4

Browse files
author
Zak Kent
committed
[TBD] Refactored SymbolSourceMap to be a typedef
1 parent 25f078a commit a6eb4b4

File tree

8 files changed

+37
-74
lines changed

8 files changed

+37
-74
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ SWIFT_TYPEID(FragileFunctionKind)
3737
SWIFT_TYPEID(PolymorphicEffectKind)
3838
SWIFT_TYPEID(PolymorphicEffectRequirementList)
3939
SWIFT_TYPEID(TangentPropertyInfo)
40-
SWIFT_TYPEID(SymbolSourceMap)
4140
SWIFT_TYPEID(Type)
4241
SWIFT_TYPEID(TypePair)
4342
SWIFT_TYPEID(TypeWitnessAndDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ struct FragileFunctionKind;
7070
enum class PolymorphicEffectKind : uint8_t;
7171
class PolymorphicEffectRequirementList;
7272
class SourceFile;
73-
class SymbolSourceMap;
7473
struct TangentPropertyInfo;
7574
class Type;
7675
class TypeAliasDecl;

include/swift/AST/TBDGenRequests.h

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -205,50 +205,12 @@ class SymbolSource {
205205
};
206206

207207
/// Maps a symbol back to its source for lazy compilation.
208-
class SymbolSourceMap {
209-
friend class SymbolSourceMapRequest;
210-
211-
using Storage = llvm::StringMap<SymbolSource>;
212-
const Storage *storage;
213-
214-
explicit SymbolSourceMap(const Storage *storage) : storage(storage) {
215-
assert(storage);
216-
}
217-
218-
public:
219-
llvm::Optional<SymbolSource> find(StringRef symbol) const {
220-
auto result = storage->find(symbol);
221-
if (result == storage->end())
222-
return llvm::None;
223-
return result->second;
224-
}
225-
226-
Storage::const_iterator begin() const {
227-
return storage->begin();
228-
}
229-
230-
Storage::const_iterator end() const {
231-
return storage->end();
232-
}
233-
234-
friend bool operator==(const SymbolSourceMap &lhs,
235-
const SymbolSourceMap &rhs) {
236-
return lhs.storage == rhs.storage;
237-
}
238-
friend bool operator!=(const SymbolSourceMap &lhs,
239-
const SymbolSourceMap &rhs) {
240-
return !(lhs == rhs);
241-
}
242-
243-
friend void simple_display(llvm::raw_ostream &out, const SymbolSourceMap &) {
244-
out << "(symbol storage map)";
245-
}
246-
};
208+
using SymbolSourceMap = llvm::StringMap<SymbolSource>;
247209

248210
/// Computes a map of symbols to their SymbolSource for a file or module.
249211
class SymbolSourceMapRequest
250212
: public SimpleRequest<SymbolSourceMapRequest,
251-
SymbolSourceMap(TBDGenDescriptor),
213+
const SymbolSourceMap *(TBDGenDescriptor),
252214
RequestFlags::Cached> {
253215
public:
254216
using SimpleRequest::SimpleRequest;
@@ -257,7 +219,8 @@ class SymbolSourceMapRequest
257219
friend SimpleRequest;
258220

259221
// Evaluation.
260-
SymbolSourceMap evaluate(Evaluator &evaluator, TBDGenDescriptor desc) const;
222+
const SymbolSourceMap *evaluate(Evaluator &evaluator,
223+
TBDGenDescriptor desc) const;
261224

262225
public:
263226
// Cached.

include/swift/AST/TBDGenTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SWIFT_REQUEST(TBDGen, PublicSymbolsRequest,
2020
std::vector<std::string>(TBDGenDescriptor),
2121
Uncached, NoLocationInfo)
2222
SWIFT_REQUEST(TBDGen, SymbolSourceMapRequest,
23-
SymbolSourceMap(TBDGenDescriptor),
23+
const SymbolSourceMap *(TBDGenDescriptor),
2424
Cached, NoLocationInfo)
2525
SWIFT_REQUEST(TBDGen, APIGenRequest, apigen::API(TBDGenDescriptor), Uncached,
2626
NoLocationInfo)

include/swift/Immediate/SwiftMaterializationUnit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ class LazySwiftMaterializationUnit : public llvm::orc::MaterializationUnit {
127127

128128
private:
129129
LazySwiftMaterializationUnit(SwiftJIT &JIT, CompilerInstance &CI,
130-
SymbolSourceMap Sources,
130+
const SymbolSourceMap *Sources,
131131
llvm::orc::SymbolFlagsMap Symbols);
132132
void materialize(
133133
std::unique_ptr<llvm::orc::MaterializationResponsibility> MR) override;
134134

135135
void discard(const llvm::orc::JITDylib &JD,
136136
const llvm::orc::SymbolStringPtr &Sym) override;
137137

138-
SymbolSourceMap Sources;
138+
const SymbolSourceMap *Sources;
139139
SwiftJIT &JIT;
140140
CompilerInstance &CI;
141141
};

lib/IRGen/IRGen.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,22 +1077,23 @@ getSymbolSourcesToEmit(const IRGenDescriptor &desc) {
10771077
auto &ctx = desc.getParentModule()->getASTContext();
10781078
auto tbdDesc = desc.getTBDGenDescriptor();
10791079
tbdDesc.getOptions().PublicSymbolsOnly = false;
1080-
auto symbolMap =
1080+
const auto *symbolMap =
10811081
llvm::cantFail(ctx.evaluator(SymbolSourceMapRequest{std::move(tbdDesc)}));
10821082

10831083
// Then split up the symbols so they can be emitted by the appropriate part
10841084
// of the pipeline.
10851085
SILRefsToEmit silRefsToEmit;
10861086
IREntitiesToEmit irEntitiesToEmit;
10871087
for (const auto &symbol : *desc.SymbolsToEmit) {
1088-
auto source = symbolMap.find(symbol);
1089-
assert(source && "Couldn't find symbol");
1090-
switch (source->kind) {
1088+
auto itr = symbolMap->find(symbol);
1089+
assert(itr != symbolMap->end() && "Couldn't find symbol");
1090+
const auto &source = itr->getValue();
1091+
switch (source.kind) {
10911092
case SymbolSource::Kind::SIL:
1092-
silRefsToEmit.push_back(source->getSILDeclRef());
1093+
silRefsToEmit.push_back(source.getSILDeclRef());
10931094
break;
10941095
case SymbolSource::Kind::IR:
1095-
irEntitiesToEmit.push_back(source->getIRLinkEntity());
1096+
irEntitiesToEmit.push_back(source.getIRLinkEntity());
10961097
break;
10971098
case SymbolSource::Kind::LinkerDirective:
10981099
case SymbolSource::Kind::Unknown:

lib/IRGen/TBDGen.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -855,24 +855,24 @@ void swift::writeAPIJSONFile(ModuleDecl *M, llvm::raw_ostream &os,
855855

856856
/// NOTE: This is part of an incomplete experimental feature. There are
857857
/// currently no clients that depend on its output.
858-
SymbolSourceMap SymbolSourceMapRequest::evaluate(Evaluator &evaluator,
859-
TBDGenDescriptor desc) const {
860-
using Map = SymbolSourceMap::Storage;
861-
Map symbolSources;
858+
const SymbolSourceMap *
859+
SymbolSourceMapRequest::evaluate(Evaluator &evaluator,
860+
TBDGenDescriptor desc) const {
862861

863-
auto addSymbol = [&](StringRef symbol, SymbolKind kind, SymbolSource source) {
864-
symbolSources.insert({symbol, source});
862+
// FIXME: Once the evaluator supports returning a reference to a cached value
863+
// in storage, this won't be necessary.
864+
auto &Ctx = desc.getParentModule()->getASTContext();
865+
auto *SymbolSources = Ctx.Allocate<SymbolSourceMap>();
866+
867+
auto addSymbol = [=](StringRef symbol, SymbolKind kind, SymbolSource source) {
868+
SymbolSources->insert({symbol, source});
865869
};
866870

867871
SimpleAPIRecorder recorder(addSymbol);
868872
TBDGenVisitor visitor(desc, recorder);
869873
visitor.visit(desc);
870874

871-
// FIXME: Once the evaluator supports returning a reference to a cached value
872-
// in storage, this won't be necessary.
873-
auto &ctx = desc.getParentModule()->getASTContext();
874-
auto *memory = ctx.Allocate<Map>();
875-
*memory = std::move(symbolSources);
876-
ctx.addCleanup([memory](){ memory->~Map(); });
877-
return SymbolSourceMap(memory);
875+
Ctx.addCleanup([SymbolSources]() { SymbolSources->~SymbolSourceMap(); });
876+
877+
return SymbolSources;
878878
}

lib/Immediate/SwiftMaterializationUnit.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ LazySwiftMaterializationUnit::Create(SwiftJIT &JIT, CompilerInstance &CI) {
293293
Opts.PublicSymbolsOnly = false;
294294
auto TBDDesc = TBDGenDescriptor::forModule(M, std::move(Opts));
295295
SymbolSourceMapRequest SourceReq{TBDDesc};
296-
auto Sources =
296+
const auto *Sources =
297297
llvm::cantFail(M->getASTContext().evaluator(std::move(SourceReq)));
298298
llvm::orc::SymbolFlagsMap PublicInterface;
299-
for (const auto &Entry : Sources) {
299+
for (const auto &Entry : *Sources) {
300300
const auto &Source = Entry.getValue();
301301
if (Source.kind != SymbolSource::Kind::SIL) {
302302
continue;
@@ -320,20 +320,21 @@ StringRef LazySwiftMaterializationUnit::getName() const {
320320
}
321321

322322
LazySwiftMaterializationUnit::LazySwiftMaterializationUnit(
323-
SwiftJIT &JIT, CompilerInstance &CI, SymbolSourceMap Sources,
323+
SwiftJIT &JIT, CompilerInstance &CI, const SymbolSourceMap *Sources,
324324
llvm::orc::SymbolFlagsMap Symbols)
325-
: MaterializationUnit({std::move(Symbols), nullptr}),
326-
Sources(std::move(Sources)), JIT(JIT), CI(CI) {}
325+
: MaterializationUnit({std::move(Symbols), nullptr}), Sources(Sources),
326+
JIT(JIT), CI(CI) {}
327327

328328
void LazySwiftMaterializationUnit::materialize(
329329
std::unique_ptr<llvm::orc::MaterializationResponsibility> MR) {
330330
SILRefsToEmit Refs;
331331
const auto &RS = MR->getRequestedSymbols();
332332
for (auto &Sym : RS) {
333333
auto Name = demangle(*Sym);
334-
auto Source = Sources.find(Name);
335-
assert(Source && "Requested symbol doesn't have source?");
336-
auto Ref = Source->getSILDeclRef();
334+
auto itr = Sources->find(Name);
335+
assert(itr != Sources->end() && "Requested symbol doesn't have source?");
336+
const auto &Source = itr->getValue();
337+
auto Ref = Source.getSILDeclRef();
337338
if (auto *AFD = Ref.getAbstractFunctionDecl()) {
338339
AFD->getTypecheckedBody();
339340
if (CI.getASTContext().hadError()) {
@@ -390,7 +391,7 @@ void LazySwiftMaterializationUnit::materialize(
390391
}
391392
}
392393
std::unique_ptr<MaterializationUnit> UnrequestedMU(
393-
new LazySwiftMaterializationUnit(JIT, CI, std::move(Sources),
394+
new LazySwiftMaterializationUnit(JIT, CI, Sources,
394395
std::move(UnrequestedSymbols)));
395396
if (auto Err = MR->replace(std::move(UnrequestedMU))) {
396397
logError(std::move(Err));

0 commit comments

Comments
 (0)