Skip to content

Commit 9f47a28

Browse files
authored
Merge pull request swiftlang#33159 from hamishknight/back-to-the-future
Introduce LoweredSILRequest
2 parents 5919697 + a99f8e9 commit 9f47a28

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

include/swift/AST/IRGenRequests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ struct IRGenDescriptor {
131131
const SILOptions &SILOpts;
132132

133133
Lowering::TypeConverter &Conv;
134+
135+
/// The SILModule to emit. If \c nullptr, a fresh SILModule will be requested
136+
/// during IRGen (which will eventually become the default behavior).
134137
SILModule *SILMod;
135138

136139
StringRef ModuleName;

include/swift/AST/SILOptimizerRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/AST/ASTTypeIDs.h"
2121
#include "swift/AST/EvaluatorDependencies.h"
22+
#include "swift/AST/SILGenRequests.h"
2223
#include "swift/AST/SimpleRequest.h"
2324

2425
namespace swift {
@@ -69,6 +70,23 @@ void simple_display(llvm::raw_ostream &out,
6970

7071
SourceLoc extractNearestSourceLoc(const SILPipelineExecutionDescriptor &desc);
7172

73+
/// Produces lowered SIL from a Swift file or module, ready for IRGen. This runs
74+
/// the diagnostic, optimization, and lowering SIL passes.
75+
class LoweredSILRequest
76+
: public SimpleRequest<LoweredSILRequest,
77+
std::unique_ptr<SILModule>(ASTLoweringDescriptor),
78+
RequestFlags::Uncached> {
79+
public:
80+
using SimpleRequest::SimpleRequest;
81+
82+
private:
83+
friend SimpleRequest;
84+
85+
// Evaluation.
86+
std::unique_ptr<SILModule> evaluate(Evaluator &evaluator,
87+
ASTLoweringDescriptor desc) const;
88+
};
89+
7290
/// Report that a request of the given kind is being evaluated, so it
7391
/// can be recorded by the stats reporter.
7492
template <typename Request>

include/swift/AST/SILOptimizerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@
1717
SWIFT_REQUEST(SILOptimizer, ExecuteSILPipelineRequest,
1818
evaluator::SideEffect(SILPipelineExecutionDescriptor),
1919
Uncached, NoLocationInfo)
20+
SWIFT_REQUEST(SILOptimizer, LoweredSILRequest,
21+
std::unique_ptr<SILModule>(ASTLoweringDescriptor),
22+
Uncached, NoLocationInfo)

lib/IRGen/IRGen.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/LinkLibrary.h"
2323
#include "swift/AST/ProtocolConformance.h"
2424
#include "swift/AST/SILGenRequests.h"
25+
#include "swift/AST/SILOptimizerRequests.h"
2526
#include "swift/Basic/Defer.h"
2627
#include "swift/Basic/Dwarf.h"
2728
#include "swift/Basic/Platform.h"
@@ -912,16 +913,27 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
912913
IRGenDescriptor desc) const {
913914
const auto &Opts = desc.Opts;
914915
const auto &PSPs = desc.PSPs;
916+
auto *M = desc.getParentModule();
917+
auto &Ctx = M->getASTContext();
918+
assert(!Ctx.hadError());
915919

920+
// If we've been provided a SILModule, use it. Otherwise request the lowered
921+
// SIL for the file or module.
916922
auto SILMod = std::unique_ptr<SILModule>(desc.SILMod);
917-
auto *M = desc.getParentModule();
923+
if (!SILMod) {
924+
auto loweringDesc =
925+
ASTLoweringDescriptor{desc.Ctx, desc.Conv, desc.SILOpts};
926+
SILMod = llvm::cantFail(Ctx.evaluator(LoweredSILRequest{loweringDesc}));
927+
928+
// If there was an error, bail.
929+
if (Ctx.hadError())
930+
return GeneratedModule::null();
931+
}
932+
918933
auto filesToEmit = desc.getFiles();
919934
auto *primaryFile =
920935
dyn_cast_or_null<SourceFile>(desc.Ctx.dyn_cast<FileUnit *>());
921936

922-
auto &Ctx = M->getASTContext();
923-
assert(!Ctx.hadError());
924-
925937
IRGenerator irgen(Opts, *SILMod);
926938

927939
auto targetMachine = irgen.createTargetMachine();
@@ -1429,35 +1441,15 @@ GeneratedModule OptimizedIRRequest::evaluate(Evaluator &evaluator,
14291441

14301442
bindExtensions(*parentMod);
14311443

1432-
// Type-check the files that need lowering to SIL.
1433-
auto loweringDesc = ASTLoweringDescriptor{desc.Ctx, desc.Conv, desc.SILOpts};
1434-
for (auto *file : loweringDesc.getFiles()) {
1444+
// Type-check the files that need emitting.
1445+
for (auto *file : desc.getFiles()) {
14351446
if (auto *SF = dyn_cast<SourceFile>(file))
14361447
performTypeChecking(*SF);
14371448
}
14381449

14391450
if (ctx.hadError())
14401451
return GeneratedModule::null();
14411452

1442-
auto silMod = llvm::cantFail(evaluator(ASTLoweringRequest{loweringDesc}));
1443-
silMod->installSILRemarkStreamer();
1444-
silMod->setSerializeSILAction([](){});
1445-
1446-
// Run SIL passes.
1447-
runSILDiagnosticPasses(*silMod);
1448-
runSILOptimizationPasses(*silMod);
1449-
silMod->verify();
1450-
1451-
if (ctx.hadError())
1452-
return GeneratedModule::null();
1453-
1454-
runSILLoweringPasses(*silMod);
1455-
1456-
if (ctx.hadError())
1457-
return GeneratedModule::null();
1458-
1459-
// Perform IRGen with the generated SILModule.
1460-
desc.SILMod = silMod.release();
14611453
auto irMod = llvm::cantFail(evaluator(IRGenRequest{desc}));
14621454
if (!irMod)
14631455
return irMod;

lib/SILOptimizer/PassManager/SILOptimizerRequests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ swift::extractNearestSourceLoc(const SILPipelineExecutionDescriptor &desc) {
5757
return extractNearestSourceLoc(desc.SM);
5858
}
5959

60+
//----------------------------------------------------------------------------//
61+
// LoweredSILRequest computation.
62+
//----------------------------------------------------------------------------//
63+
64+
std::unique_ptr<SILModule>
65+
LoweredSILRequest::evaluate(Evaluator &evaluator,
66+
ASTLoweringDescriptor desc) const {
67+
auto silMod = llvm::cantFail(evaluator(ASTLoweringRequest{desc}));
68+
silMod->installSILRemarkStreamer();
69+
silMod->setSerializeSILAction([]() {});
70+
71+
runSILDiagnosticPasses(*silMod);
72+
73+
{
74+
FrontendStatsTracer tracer(silMod->getASTContext().Stats,
75+
"SIL verification, pre-optimization");
76+
silMod->verify();
77+
}
78+
79+
runSILOptimizationPasses(*silMod);
80+
81+
{
82+
FrontendStatsTracer tracer(silMod->getASTContext().Stats,
83+
"SIL verification, post-optimization");
84+
silMod->verify();
85+
}
86+
87+
runSILLoweringPasses(*silMod);
88+
return silMod;
89+
}
90+
6091
// Define request evaluation functions for each of the SILGen requests.
6192
static AbstractRequestFunction *silOptimizerRequestFunctions[] = {
6293
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \

0 commit comments

Comments
 (0)