Skip to content

Commit 4570ba8

Browse files
committed
Refactor GenerateSILRequest into WMO and per-SF SILGen Requests
1 parent 6764366 commit 4570ba8

File tree

4 files changed

+101
-109
lines changed

4 files changed

+101
-109
lines changed

include/swift/AST/SILGenRequests.h

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class SourceFile;
3131

3232
namespace Lowering {
3333
class TypeConverter;
34-
class SILGenModule;
3534
}
3635

3736
/// Report that a request of the given kind is being evaluated, so it
@@ -75,8 +74,12 @@ struct SILGenDescriptor {
7574
}
7675
};
7776

78-
class GenerateSILRequest :
79-
public SimpleRequest<GenerateSILRequest,
77+
void simple_display(llvm::raw_ostream &out, const SILGenDescriptor &d);
78+
79+
SourceLoc extractNearestSourceLoc(const SILGenDescriptor &desc);
80+
81+
class SILGenSourceFileRequest :
82+
public SimpleRequest<SILGenSourceFileRequest,
8083
std::unique_ptr<SILModule>(SILGenDescriptor),
8184
CacheKind::Uncached> {
8285
public:
@@ -93,13 +96,9 @@ class GenerateSILRequest :
9396
bool isCached() const { return true; }
9497
};
9598

96-
void simple_display(llvm::raw_ostream &out, const SILGenDescriptor &d);
97-
98-
SourceLoc extractNearestSourceLoc(const SILGenDescriptor &desc);
99-
100-
class SILGenSourceFileRequest :
101-
public SimpleRequest<SILGenSourceFileRequest,
102-
bool(Lowering::SILGenModule *, SourceFile *),
99+
class SILGenWholeModuleRequest :
100+
public SimpleRequest<SILGenWholeModuleRequest,
101+
std::unique_ptr<SILModule>(SILGenDescriptor),
103102
CacheKind::Uncached> {
104103
public:
105104
using SimpleRequest::SimpleRequest;
@@ -108,19 +107,13 @@ class SILGenSourceFileRequest :
108107
friend SimpleRequest;
109108

110109
// Evaluation.
111-
llvm::Expected<bool>
112-
evaluate(Evaluator &evaluator,
113-
Lowering::SILGenModule *SGM, SourceFile *SF) const;
110+
llvm::Expected<std::unique_ptr<SILModule>>
111+
evaluate(Evaluator &evaluator, SILGenDescriptor desc) const;
114112

115113
public:
116114
bool isCached() const { return true; }
117115
};
118116

119-
inline void
120-
simple_display(llvm::raw_ostream &out, const Lowering::SILGenModule *SGM) {
121-
// No-op
122-
}
123-
124117
/// The zone number for SILGen.
125118
#define SWIFT_TYPEID_ZONE SILGen
126119
#define SWIFT_TYPEID_HEADER "swift/AST/SILGenTypeIDZone.def"

include/swift/AST/SILGenTypeIDZone.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
SWIFT_REQUEST(SILGen, GenerateSILRequest,
17+
SWIFT_REQUEST(SILGen, SILGenSourceFileRequest,
1818
std::unique_ptr<SILModule>(SILGenDescriptor),
1919
Uncached, NoLocationInfo)
20-
SWIFT_REQUEST(SILGen, SILGenSourceFileRequest,
21-
bool(Lowering::SILGenModule *, SourceFile *),
20+
SWIFT_REQUEST(SILGen, SILGenWholeModuleRequest,
21+
std::unique_ptr<SILModule>(SILGenDescriptor),
2222
Uncached, NoLocationInfo)

include/swift/SIL/SILModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ enum class SILStage {
105105
/// when a Swift compilation context is lowered to SIL.
106106
class SILModule {
107107
friend class SILFunctionBuilder;
108+
friend class SILGenSourceFileRequest;
109+
friend class SILGenWholeModuleRequest;
108110

109111
public:
110112
using FunctionListType = llvm::ilist<SILFunction>;

lib/SILGen/SILGen.cpp

Lines changed: 85 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,115 +1667,112 @@ class SourceFileScope {
16671667
}
16681668
};
16691669

1670-
} // end anonymous namespace
1670+
// An RAII object that constructs a \c SILGenModule instance.
1671+
// On destruction, delayed definitions are automatically emitted.
1672+
class SILGenModuleRAII {
1673+
SILGenModule SGM;
16711674

1672-
llvm::Expected<bool>
1673-
SILGenSourceFileRequest::evaluate(Evaluator &evaluator,
1674-
SILGenModule *SGM, SourceFile *sf) const {
1675-
SourceFileScope scope(*SGM, sf);
1676-
for (Decl *D : sf->getTopLevelDecls()) {
1677-
FrontendStatsTracer StatsTracer(SGM->getASTContext().Stats, "SILgen-decl", D);
1678-
SGM->visit(D);
1679-
}
1675+
public:
1676+
void emitSourceFile(SourceFile *sf) {
1677+
SourceFileScope scope(SGM, sf);
1678+
for (Decl *D : sf->getTopLevelDecls()) {
1679+
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
1680+
"SILgen-decl", D);
1681+
SGM.visit(D);
1682+
}
16801683

1681-
for (TypeDecl *TD : sf->LocalTypeDecls) {
1682-
FrontendStatsTracer StatsTracer(SGM->getASTContext().Stats, "SILgen-tydecl", TD);
1683-
// FIXME: Delayed parsing would prevent these types from being added to the
1684-
// module in the first place.
1685-
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
1686-
continue;
1687-
SGM->visit(TD);
1684+
for (TypeDecl *TD : sf->LocalTypeDecls) {
1685+
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
1686+
"SILgen-tydecl", TD);
1687+
// FIXME: Delayed parsing would prevent these types from being added to
1688+
// the module in the first place.
1689+
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
1690+
continue;
1691+
SGM.visit(TD);
1692+
}
16881693
}
1689-
return true;
1690-
}
16911694

1692-
//===----------------------------------------------------------------------===//
1693-
// SILModule::constructSIL method implementation
1694-
//===----------------------------------------------------------------------===//
1695+
SILGenModuleRAII(SILModule &M, ModuleDecl *SM) : SGM{M, SM} {}
1696+
1697+
~SILGenModuleRAII() {
1698+
// Emit any delayed definitions that were forced.
1699+
// Emitting these may in turn force more definitions, so we have to take
1700+
// care to keep pumping the queues.
1701+
while (!SGM.forcedFunctions.empty()
1702+
|| !SGM.pendingConformances.empty()) {
1703+
while (!SGM.forcedFunctions.empty()) {
1704+
auto &front = SGM.forcedFunctions.front();
1705+
front.second.emitter(SGM.getFunction(front.first, ForDefinition));
1706+
SGM.forcedFunctions.pop_front();
1707+
}
1708+
while (!SGM.pendingConformances.empty()) {
1709+
SGM.getWitnessTable(SGM.pendingConformances.front());
1710+
SGM.pendingConformances.pop_front();
1711+
}
1712+
}
1713+
}
1714+
};
1715+
} // end anonymous namespace
16951716

1696-
std::unique_ptr<SILModule>
1697-
SILModule::constructSIL(ModuleDecl *mod, TypeConverter &tc,
1698-
const SILOptions &options, FileUnit *SF) {
1699-
FrontendStatsTracer tracer(mod->getASTContext().Stats, "SILGen");
1700-
const DeclContext *DC;
1701-
if (SF) {
1702-
DC = SF;
1703-
} else {
1704-
DC = mod;
1717+
llvm::Expected<std::unique_ptr<SILModule>>
1718+
SILGenSourceFileRequest::evaluate(Evaluator &evaluator,
1719+
SILGenDescriptor desc) const {
1720+
auto *unit = desc.context.get<FileUnit *>();
1721+
auto *mod = unit->getParentModule();
1722+
auto M = std::unique_ptr<SILModule>(
1723+
new SILModule(mod, desc.conv, desc.opts, unit, /*wholeModule*/ false));
1724+
SILGenModuleRAII scope(*M, mod);
1725+
1726+
if (auto *file = dyn_cast<SourceFile>(unit)) {
1727+
scope.emitSourceFile(file);
1728+
} else if (auto *file = dyn_cast<SerializedASTFile>(unit)) {
1729+
if (file->isSIB())
1730+
M->getSILLoader()->getAllForModule(mod->getName(), file);
17051731
}
17061732

1707-
std::unique_ptr<SILModule> M(
1708-
new SILModule(mod, tc, options, DC, /*wholeModule*/ SF == nullptr));
1709-
SILGenModule SGM(*M, mod);
1710-
1711-
if (SF) {
1712-
if (auto *file = dyn_cast<SourceFile>(SF)) {
1713-
(void)evaluateOrDefault(file->getASTContext().evaluator,
1714-
SILGenSourceFileRequest{&SGM, file}, false);
1715-
} else if (auto *file = dyn_cast<SerializedASTFile>(SF)) {
1716-
if (file->isSIB())
1717-
M->getSILLoader()->getAllForModule(mod->getName(), file);
1718-
}
1719-
} else {
1720-
for (auto file : mod->getFiles()) {
1721-
auto nextSF = dyn_cast<SourceFile>(file);
1722-
if (!nextSF || nextSF->ASTStage != SourceFile::TypeChecked)
1723-
continue;
1724-
(void)evaluateOrDefault(nextSF->getASTContext().evaluator,
1725-
SILGenSourceFileRequest{&SGM, nextSF}, false);
1726-
}
1733+
return std::move(M);
1734+
}
17271735

1728-
// Also make sure to process any intermediate files that may contain SIL
1729-
bool hasSIB = std::any_of(mod->getFiles().begin(),
1730-
mod->getFiles().end(),
1731-
[](const FileUnit *File) -> bool {
1732-
auto *SASTF = dyn_cast<SerializedASTFile>(File);
1733-
return SASTF && SASTF->isSIB();
1734-
});
1735-
if (hasSIB)
1736-
M->getSILLoader()->getAllForModule(mod->getName(), nullptr);
1736+
llvm::Expected<std::unique_ptr<SILModule>>
1737+
SILGenWholeModuleRequest::evaluate(Evaluator &evaluator,
1738+
SILGenDescriptor desc) const {
1739+
auto *mod = desc.context.get<ModuleDecl *>();
1740+
auto M = std::unique_ptr<SILModule>(
1741+
new SILModule(mod, desc.conv, desc.opts, mod, /*wholeModule*/ true));
1742+
SILGenModuleRAII scope(*M, mod);
1743+
1744+
for (auto file : mod->getFiles()) {
1745+
auto nextSF = dyn_cast<SourceFile>(file);
1746+
if (!nextSF || nextSF->ASTStage != SourceFile::TypeChecked)
1747+
continue;
1748+
scope.emitSourceFile(nextSF);
17371749
}
17381750

1739-
// Emit any delayed definitions that were forced.
1740-
// Emitting these may in turn force more definitions, so we have to take care
1741-
// to keep pumping the queues.
1742-
while (!SGM.forcedFunctions.empty()
1743-
|| !SGM.pendingConformances.empty()) {
1744-
while (!SGM.forcedFunctions.empty()) {
1745-
auto &front = SGM.forcedFunctions.front();
1746-
front.second.emitter(SGM.getFunction(front.first, ForDefinition));
1747-
SGM.forcedFunctions.pop_front();
1748-
}
1749-
while (!SGM.pendingConformances.empty()) {
1750-
SGM.getWitnessTable(SGM.pendingConformances.front());
1751-
SGM.pendingConformances.pop_front();
1752-
}
1753-
}
1751+
// Also make sure to process any intermediate files that may contain SIL
1752+
bool hasSIB = std::any_of(mod->getFiles().begin(),
1753+
mod->getFiles().end(),
1754+
[](const FileUnit *File) -> bool {
1755+
auto *SASTF = dyn_cast<SerializedASTFile>(File);
1756+
return SASTF && SASTF->isSIB();
1757+
});
1758+
if (hasSIB)
1759+
M->getSILLoader()->getAllForModule(mod->getName(), nullptr);
17541760

1755-
return M;
1761+
return std::move(M);
17561762
}
17571763

17581764
std::unique_ptr<SILModule>
17591765
swift::performSILGeneration(ModuleDecl *mod, Lowering::TypeConverter &tc,
17601766
const SILOptions &options) {
17611767
auto desc = SILGenDescriptor::forWholeModule(mod, tc, options);
1762-
return llvm::cantFail(mod->getASTContext().evaluator(GenerateSILRequest{desc}));
1768+
return llvm::cantFail(
1769+
mod->getASTContext().evaluator(SILGenWholeModuleRequest{desc}));
17631770
}
17641771

17651772
std::unique_ptr<SILModule>
17661773
swift::performSILGeneration(FileUnit &sf, Lowering::TypeConverter &tc,
17671774
const SILOptions &options) {
17681775
auto desc = SILGenDescriptor::forFile(sf, tc, options);
1769-
return llvm::cantFail(sf.getASTContext().evaluator(GenerateSILRequest{desc}));
1770-
}
1771-
1772-
llvm::Expected<std::unique_ptr<SILModule>>
1773-
GenerateSILRequest::evaluate(Evaluator &evaluator, SILGenDescriptor sgd) const {
1774-
if (auto *MD = sgd.context.dyn_cast<ModuleDecl *>()) {
1775-
return SILModule::constructSIL(MD, sgd.conv, sgd.opts, nullptr);
1776-
} else {
1777-
auto *SF = sgd.context.get<FileUnit *>();
1778-
return SILModule::constructSIL(SF->getParentModule(),
1779-
sgd.conv, sgd.opts, SF);
1780-
}
1776+
return llvm::cantFail(
1777+
sf.getASTContext().evaluator(SILGenSourceFileRequest{desc}));
17811778
}

0 commit comments

Comments
 (0)