Skip to content

Commit 960b5a2

Browse files
authored
Merge pull request swiftlang#29629 from CodaFi/big-generator
Add SILGenSourceFileRequest
2 parents ff9d3ef + 4570ba8 commit 960b5a2

File tree

5 files changed

+113
-92
lines changed

5 files changed

+113
-92
lines changed

include/swift/AST/SILGenRequests.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ struct SILGenDescriptor {
7474
}
7575
};
7676

77-
class GenerateSILRequest :
78-
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,
7983
std::unique_ptr<SILModule>(SILGenDescriptor),
8084
CacheKind::Uncached> {
8185
public:
@@ -92,9 +96,23 @@ class GenerateSILRequest :
9296
bool isCached() const { return true; }
9397
};
9498

95-
void simple_display(llvm::raw_ostream &out, const SILGenDescriptor &d);
99+
class SILGenWholeModuleRequest :
100+
public SimpleRequest<SILGenWholeModuleRequest,
101+
std::unique_ptr<SILModule>(SILGenDescriptor),
102+
CacheKind::Uncached> {
103+
public:
104+
using SimpleRequest::SimpleRequest;
96105

97-
SourceLoc extractNearestSourceLoc(const SILGenDescriptor &desc);
106+
private:
107+
friend SimpleRequest;
108+
109+
// Evaluation.
110+
llvm::Expected<std::unique_ptr<SILModule>>
111+
evaluate(Evaluator &evaluator, SILGenDescriptor desc) const;
112+
113+
public:
114+
bool isCached() const { return true; }
115+
};
98116

99117
/// The zone number for SILGen.
100118
#define SWIFT_TYPEID_ZONE SILGen

include/swift/AST/SILGenTypeIDZone.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
SWIFT_REQUEST(SILGen, GenerateSILRequest,
17+
SWIFT_REQUEST(SILGen, SILGenSourceFileRequest,
18+
std::unique_ptr<SILModule>(SILGenDescriptor),
19+
Uncached, NoLocationInfo)
20+
SWIFT_REQUEST(SILGen, SILGenWholeModuleRequest,
1821
std::unique_ptr<SILModule>(SILGenDescriptor),
1922
Uncached, NoLocationInfo)

include/swift/SIL/SILModule.h

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

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

lib/SILGen/SILGen.cpp

Lines changed: 85 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,111 +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-
void SILGenModule::emitSourceFile(SourceFile *sf) {
1673-
SourceFileScope scope(*this, sf);
1674-
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-file", sf);
1675-
for (Decl *D : sf->getTopLevelDecls()) {
1676-
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-decl", D);
1677-
visit(D);
1678-
}
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+
}
16791683

1680-
for (TypeDecl *TD : sf->LocalTypeDecls) {
1681-
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-tydecl", TD);
1682-
// FIXME: Delayed parsing would prevent these types from being added to the
1683-
// module in the first place.
1684-
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
1685-
continue;
1686-
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+
}
16871693
}
1688-
}
16891694

1690-
//===----------------------------------------------------------------------===//
1691-
// SILModule::constructSIL method implementation
1692-
//===----------------------------------------------------------------------===//
1693-
1694-
std::unique_ptr<SILModule>
1695-
SILModule::constructSIL(ModuleDecl *mod, TypeConverter &tc,
1696-
const SILOptions &options, FileUnit *SF) {
1697-
FrontendStatsTracer tracer(mod->getASTContext().Stats, "SILGen");
1698-
const DeclContext *DC;
1699-
if (SF) {
1700-
DC = SF;
1701-
} else {
1702-
DC = mod;
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+
}
17031713
}
1714+
};
1715+
} // end anonymous namespace
17041716

1705-
std::unique_ptr<SILModule> M(
1706-
new SILModule(mod, tc, options, DC, /*wholeModule*/ SF == nullptr));
1707-
SILGenModule SGM(*M, 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);
1731+
}
17081732

1709-
if (SF) {
1710-
if (auto *file = dyn_cast<SourceFile>(SF)) {
1711-
SGM.emitSourceFile(file);
1712-
} else if (auto *file = dyn_cast<SerializedASTFile>(SF)) {
1713-
if (file->isSIB())
1714-
M->getSILLoader()->getAllForModule(mod->getName(), file);
1715-
}
1716-
} else {
1717-
for (auto file : mod->getFiles()) {
1718-
auto nextSF = dyn_cast<SourceFile>(file);
1719-
if (!nextSF || nextSF->ASTStage != SourceFile::TypeChecked)
1720-
continue;
1721-
SGM.emitSourceFile(nextSF);
1722-
}
1733+
return std::move(M);
1734+
}
17231735

1724-
// Also make sure to process any intermediate files that may contain SIL
1725-
bool hasSIB = std::any_of(mod->getFiles().begin(),
1726-
mod->getFiles().end(),
1727-
[](const FileUnit *File) -> bool {
1728-
auto *SASTF = dyn_cast<SerializedASTFile>(File);
1729-
return SASTF && SASTF->isSIB();
1730-
});
1731-
if (hasSIB)
1732-
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);
17331749
}
17341750

1735-
// Emit any delayed definitions that were forced.
1736-
// Emitting these may in turn force more definitions, so we have to take care
1737-
// to keep pumping the queues.
1738-
while (!SGM.forcedFunctions.empty()
1739-
|| !SGM.pendingConformances.empty()) {
1740-
while (!SGM.forcedFunctions.empty()) {
1741-
auto &front = SGM.forcedFunctions.front();
1742-
front.second.emitter(SGM.getFunction(front.first, ForDefinition));
1743-
SGM.forcedFunctions.pop_front();
1744-
}
1745-
while (!SGM.pendingConformances.empty()) {
1746-
SGM.getWitnessTable(SGM.pendingConformances.front());
1747-
SGM.pendingConformances.pop_front();
1748-
}
1749-
}
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);
17501760

1751-
return M;
1761+
return std::move(M);
17521762
}
17531763

17541764
std::unique_ptr<SILModule>
17551765
swift::performSILGeneration(ModuleDecl *mod, Lowering::TypeConverter &tc,
17561766
const SILOptions &options) {
17571767
auto desc = SILGenDescriptor::forWholeModule(mod, tc, options);
1758-
return llvm::cantFail(mod->getASTContext().evaluator(GenerateSILRequest{desc}));
1768+
return llvm::cantFail(
1769+
mod->getASTContext().evaluator(SILGenWholeModuleRequest{desc}));
17591770
}
17601771

17611772
std::unique_ptr<SILModule>
17621773
swift::performSILGeneration(FileUnit &sf, Lowering::TypeConverter &tc,
17631774
const SILOptions &options) {
17641775
auto desc = SILGenDescriptor::forFile(sf, tc, options);
1765-
return llvm::cantFail(sf.getASTContext().evaluator(GenerateSILRequest{desc}));
1766-
}
1767-
1768-
llvm::Expected<std::unique_ptr<SILModule>>
1769-
GenerateSILRequest::evaluate(Evaluator &evaluator, SILGenDescriptor sgd) const {
1770-
if (auto *MD = sgd.context.dyn_cast<ModuleDecl *>()) {
1771-
return SILModule::constructSIL(MD, sgd.conv, sgd.opts, nullptr);
1772-
} else {
1773-
auto *SF = sgd.context.get<FileUnit *>();
1774-
return SILModule::constructSIL(SF->getParentModule(),
1775-
sgd.conv, sgd.opts, SF);
1776-
}
1776+
return llvm::cantFail(
1777+
sf.getASTContext().evaluator(SILGenSourceFileRequest{desc}));
17771778
}

lib/SILGen/SILGen.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
208208

209209
void emitAbstractFuncDecl(AbstractFunctionDecl *AFD);
210210

211-
/// Generate code for a source file of the module.
212-
void emitSourceFile(SourceFile *sf);
213-
214211
/// Generates code for the given FuncDecl and adds the
215212
/// SILFunction to the current SILModule under the name SILDeclRef(decl). For
216213
/// curried functions, curried entry point Functions are also generated and

0 commit comments

Comments
 (0)