Skip to content

Commit 485d5e9

Browse files
committed
[IRGen] Introduce OptimizedIRRequest
Introduce a request that wraps the entire compiler pipeline and returns the optimized IR for a whole-module or file.
1 parent d05bfa0 commit 485d5e9

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

include/swift/AST/IRGenRequests.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace swift {
2828
class SourceFile;
2929
class IRGenOptions;
3030
class SILModule;
31+
class SILOptions;
3132
struct TBDGenOptions;
3233

3334
namespace irgen {
@@ -127,8 +128,11 @@ struct IRGenDescriptor {
127128

128129
const IRGenOptions &Opts;
129130
const TBDGenOptions &TBDOpts;
131+
const SILOptions &SILOpts;
130132

133+
Lowering::TypeConverter &Conv;
131134
SILModule *SILMod;
135+
132136
StringRef ModuleName;
133137
const PrimarySpecificPaths &PSPs;
134138
StringRef PrivateDiscriminator;
@@ -152,13 +156,16 @@ struct IRGenDescriptor {
152156
public:
153157
static IRGenDescriptor
154158
forFile(FileUnit *file, const IRGenOptions &Opts,
155-
const TBDGenOptions &TBDOpts, std::unique_ptr<SILModule> &&SILMod,
159+
const TBDGenOptions &TBDOpts, const SILOptions &SILOpts,
160+
Lowering::TypeConverter &Conv, std::unique_ptr<SILModule> &&SILMod,
156161
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
157162
StringRef PrivateDiscriminator,
158163
llvm::GlobalVariable **outModuleHash) {
159164
return IRGenDescriptor{file,
160165
Opts,
161166
TBDOpts,
167+
SILOpts,
168+
Conv,
162169
SILMod.release(),
163170
ModuleName,
164171
PSPs,
@@ -169,14 +176,17 @@ struct IRGenDescriptor {
169176

170177
static IRGenDescriptor
171178
forWholeModule(ModuleDecl *M, const IRGenOptions &Opts,
172-
const TBDGenOptions &TBDOpts,
179+
const TBDGenOptions &TBDOpts, const SILOptions &SILOpts,
180+
Lowering::TypeConverter &Conv,
173181
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
174182
const PrimarySpecificPaths &PSPs,
175183
ArrayRef<std::string> parallelOutputFilenames,
176184
llvm::GlobalVariable **outModuleHash) {
177185
return IRGenDescriptor{M,
178186
Opts,
179187
TBDOpts,
188+
SILOpts,
189+
Conv,
180190
SILMod.release(),
181191
ModuleName,
182192
PSPs,
@@ -226,6 +236,21 @@ void simple_display(llvm::raw_ostream &out, const IRGenDescriptor &d);
226236

227237
SourceLoc extractNearestSourceLoc(const IRGenDescriptor &desc);
228238

239+
/// Returns the optimized IR for a given file or module. Note this runs the
240+
/// entire compiler pipeline and ignores the passed SILModule.
241+
class OptimizedIRRequest
242+
: public SimpleRequest<OptimizedIRRequest, GeneratedModule(IRGenDescriptor),
243+
RequestFlags::Uncached> {
244+
public:
245+
using SimpleRequest::SimpleRequest;
246+
247+
private:
248+
friend SimpleRequest;
249+
250+
// Evaluation.
251+
GeneratedModule evaluate(Evaluator &evaluator, IRGenDescriptor desc) const;
252+
};
253+
229254
/// The zone number for IRGen.
230255
#define SWIFT_TYPEID_ZONE IRGen
231256
#define SWIFT_TYPEID_HEADER "swift/AST/IRGenTypeIDZone.def"

include/swift/AST/IRGenTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@
1717
SWIFT_REQUEST(IRGen, IRGenRequest,
1818
GeneratedModule(IRGenDescriptor),
1919
Uncached, NoLocationInfo)
20+
SWIFT_REQUEST(IRGen, OptimizedIRRequest,
21+
GeneratedModule(IRGenDescriptor),
22+
Uncached, NoLocationInfo)

lib/IRGen/IRGen.cpp

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/IRGenRequests.h"
2222
#include "swift/AST/LinkLibrary.h"
2323
#include "swift/AST/ProtocolConformance.h"
24+
#include "swift/AST/SILGenRequests.h"
2425
#include "swift/Basic/Defer.h"
2526
#include "swift/Basic/Dwarf.h"
2627
#include "swift/Basic/Platform.h"
@@ -1311,9 +1312,12 @@ GeneratedModule swift::performIRGeneration(
13111312
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
13121313
ArrayRef<std::string> parallelOutputFilenames,
13131314
llvm::GlobalVariable **outModuleHash) {
1315+
// Get a pointer to the SILModule to avoid a potential use-after-move.
1316+
const auto *SILModPtr = SILMod.get();
1317+
const auto &SILOpts = SILModPtr->getOptions();
13141318
auto desc = IRGenDescriptor::forWholeModule(
1315-
M, Opts, TBDOpts, std::move(SILMod), ModuleName, PSPs,
1316-
parallelOutputFilenames, outModuleHash);
1319+
M, Opts, TBDOpts, SILOpts, SILModPtr->Types, std::move(SILMod),
1320+
ModuleName, PSPs, parallelOutputFilenames, outModuleHash);
13171321

13181322
if (Opts.shouldPerformIRGenerationInParallel() &&
13191323
!parallelOutputFilenames.empty()) {
@@ -1332,9 +1336,12 @@ performIRGeneration(FileUnit *file, const IRGenOptions &Opts,
13321336
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
13331337
StringRef PrivateDiscriminator,
13341338
llvm::GlobalVariable **outModuleHash) {
1335-
auto desc = IRGenDescriptor::forFile(file, Opts, TBDOpts, std::move(SILMod),
1336-
ModuleName, PSPs, PrivateDiscriminator,
1337-
outModuleHash);
1339+
// Get a pointer to the SILModule to avoid a potential use-after-move.
1340+
const auto *SILModPtr = SILMod.get();
1341+
const auto &SILOpts = SILModPtr->getOptions();
1342+
auto desc = IRGenDescriptor::forFile(
1343+
file, Opts, TBDOpts, SILOpts, SILModPtr->Types, std::move(SILMod),
1344+
ModuleName, PSPs, PrivateDiscriminator, outModuleHash);
13381345
return llvm::cantFail(file->getASTContext().evaluator(IRGenRequest{desc}));
13391346
}
13401347

@@ -1409,3 +1416,54 @@ bool swift::performLLVM(const IRGenOptions &Opts, ASTContext &Ctx,
14091416
return true;
14101417
return false;
14111418
}
1419+
1420+
GeneratedModule OptimizedIRRequest::evaluate(Evaluator &evaluator,
1421+
IRGenDescriptor desc) const {
1422+
auto *parentMod = desc.getParentModule();
1423+
auto &ctx = parentMod->getASTContext();
1424+
1425+
// Resolve imports for all the source files.
1426+
for (auto *file : parentMod->getFiles()) {
1427+
if (auto *SF = dyn_cast<SourceFile>(file))
1428+
performImportResolution(*SF);
1429+
}
1430+
1431+
bindExtensions(*parentMod);
1432+
1433+
// Type-check the files that need lowering to SIL.
1434+
auto loweringDesc = ASTLoweringDescriptor{desc.Ctx, desc.Conv, desc.SILOpts};
1435+
for (auto *file : loweringDesc.getFiles()) {
1436+
if (auto *SF = dyn_cast<SourceFile>(file))
1437+
performTypeChecking(*SF);
1438+
}
1439+
1440+
if (ctx.hadError())
1441+
return GeneratedModule::null();
1442+
1443+
auto silMod = llvm::cantFail(evaluator(ASTLoweringRequest{loweringDesc}));
1444+
silMod->installSILRemarkStreamer();
1445+
silMod->setSerializeSILAction([](){});
1446+
1447+
// Run SIL passes.
1448+
runSILDiagnosticPasses(*silMod);
1449+
runSILOptimizationPasses(*silMod);
1450+
silMod->verify();
1451+
1452+
if (ctx.hadError())
1453+
return GeneratedModule::null();
1454+
1455+
runSILLoweringPasses(*silMod);
1456+
1457+
if (ctx.hadError())
1458+
return GeneratedModule::null();
1459+
1460+
// Perform IRGen with the generated SILModule.
1461+
desc.SILMod = silMod.release();
1462+
auto irMod = llvm::cantFail(evaluator(IRGenRequest{desc}));
1463+
if (!irMod)
1464+
return irMod;
1465+
1466+
performLLVMOptimizations(desc.Opts, irMod.getModule(),
1467+
irMod.getTargetMachine());
1468+
return irMod;
1469+
}

0 commit comments

Comments
 (0)