Skip to content

Commit bcb6699

Browse files
authored
Merge pull request #32914 from hamishknight/a-series-of-tubes
2 parents 1224d8b + 4ddb7f8 commit bcb6699

File tree

10 files changed

+250
-131
lines changed

10 files changed

+250
-131
lines changed

include/swift/AST/IRGenRequests.h

+45-11
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#include "swift/AST/SimpleRequest.h"
2323
#include "swift/Basic/PrimarySpecificPaths.h"
2424
#include "llvm/ADT/StringSet.h"
25+
#include "llvm/Target/TargetMachine.h"
2526

2627
namespace swift {
2728
class SourceFile;
2829
class IRGenOptions;
2930
class SILModule;
31+
class SILOptions;
3032
struct TBDGenOptions;
3133

3234
namespace irgen {
@@ -39,6 +41,7 @@ namespace llvm {
3941
class GlobalVariable;
4042
class LLVMContext;
4143
class Module;
44+
class TargetMachine;
4245

4346
namespace orc {
4447
class ThreadSafeModule;
@@ -58,8 +61,9 @@ class GeneratedModule final {
5861
private:
5962
std::unique_ptr<llvm::LLVMContext> Context;
6063
std::unique_ptr<llvm::Module> Module;
64+
std::unique_ptr<llvm::TargetMachine> Target;
6165

62-
GeneratedModule() : Context(nullptr), Module(nullptr) {}
66+
GeneratedModule() : Context(nullptr), Module(nullptr), Target(nullptr) {}
6367

6468
GeneratedModule(GeneratedModule const &) = delete;
6569
GeneratedModule &operator=(GeneratedModule const &) = delete;
@@ -70,10 +74,13 @@ class GeneratedModule final {
7074
/// The given pointers must not be null. If a null \c GeneratedModule is
7175
/// needed, use \c GeneratedModule::null() instead.
7276
explicit GeneratedModule(std::unique_ptr<llvm::LLVMContext> &&Context,
73-
std::unique_ptr<llvm::Module> &&Module)
74-
: Context(std::move(Context)), Module(std::move(Module)) {
77+
std::unique_ptr<llvm::Module> &&Module,
78+
std::unique_ptr<llvm::TargetMachine> &&Target)
79+
: Context(std::move(Context)), Module(std::move(Module)),
80+
Target(std::move(Target)) {
7581
assert(getModule() && "Use GeneratedModule::null() instead");
7682
assert(getContext() && "Use GeneratedModule::null() instead");
83+
assert(getTargetMachine() && "Use GeneratedModule::null() instead");
7784
}
7885

7986
GeneratedModule(GeneratedModule &&) = default;
@@ -97,6 +104,9 @@ class GeneratedModule final {
97104
const llvm::LLVMContext *getContext() const { return Context.get(); }
98105
llvm::LLVMContext *getContext() { return Context.get(); }
99106

107+
const llvm::TargetMachine *getTargetMachine() const { return Target.get(); }
108+
llvm::TargetMachine *getTargetMachine() { return Target.get(); }
109+
100110
public:
101111
/// Release ownership of the context and module to the caller, consuming
102112
/// this value in the process.
@@ -114,12 +124,15 @@ class GeneratedModule final {
114124
};
115125

116126
struct IRGenDescriptor {
117-
llvm::PointerUnion<ModuleDecl *, SourceFile *> Ctx;
127+
llvm::PointerUnion<FileUnit *, ModuleDecl *> Ctx;
118128

119129
const IRGenOptions &Opts;
120130
const TBDGenOptions &TBDOpts;
131+
const SILOptions &SILOpts;
121132

133+
Lowering::TypeConverter &Conv;
122134
SILModule *SILMod;
135+
123136
StringRef ModuleName;
124137
const PrimarySpecificPaths &PSPs;
125138
StringRef PrivateDiscriminator;
@@ -142,14 +155,17 @@ struct IRGenDescriptor {
142155

143156
public:
144157
static IRGenDescriptor
145-
forFile(SourceFile &SF, const IRGenOptions &Opts,
146-
const TBDGenOptions &TBDOpts, std::unique_ptr<SILModule> &&SILMod,
158+
forFile(FileUnit *file, const IRGenOptions &Opts,
159+
const TBDGenOptions &TBDOpts, const SILOptions &SILOpts,
160+
Lowering::TypeConverter &Conv, std::unique_ptr<SILModule> &&SILMod,
147161
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
148162
StringRef PrivateDiscriminator,
149-
llvm::GlobalVariable **outModuleHash) {
150-
return IRGenDescriptor{&SF,
163+
llvm::GlobalVariable **outModuleHash = nullptr) {
164+
return IRGenDescriptor{file,
151165
Opts,
152166
TBDOpts,
167+
SILOpts,
168+
Conv,
153169
SILMod.release(),
154170
ModuleName,
155171
PSPs,
@@ -160,14 +176,17 @@ struct IRGenDescriptor {
160176

161177
static IRGenDescriptor
162178
forWholeModule(ModuleDecl *M, const IRGenOptions &Opts,
163-
const TBDGenOptions &TBDOpts,
179+
const TBDGenOptions &TBDOpts, const SILOptions &SILOpts,
180+
Lowering::TypeConverter &Conv,
164181
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
165182
const PrimarySpecificPaths &PSPs,
166-
ArrayRef<std::string> parallelOutputFilenames,
167-
llvm::GlobalVariable **outModuleHash) {
183+
ArrayRef<std::string> parallelOutputFilenames = {},
184+
llvm::GlobalVariable **outModuleHash = nullptr) {
168185
return IRGenDescriptor{M,
169186
Opts,
170187
TBDOpts,
188+
SILOpts,
189+
Conv,
171190
SILMod.release(),
172191
ModuleName,
173192
PSPs,
@@ -217,6 +236,21 @@ void simple_display(llvm::raw_ostream &out, const IRGenDescriptor &d);
217236

218237
SourceLoc extractNearestSourceLoc(const IRGenDescriptor &desc);
219238

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+
220254
/// The zone number for IRGen.
221255
#define SWIFT_TYPEID_ZONE IRGen
222256
#define SWIFT_TYPEID_HEADER "swift/AST/IRGenTypeIDZone.def"

include/swift/AST/IRGenTypeIDZone.def

+3
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)

include/swift/Subsystems.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ namespace swift {
204204
std::string>
205205
getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx);
206206

207-
/// Turn the given Swift module into either LLVM IR or native code
208-
/// and return the generated LLVM IR module.
209-
/// If you set an outModuleHash, then you need to call performLLVM.
207+
/// Turn the given Swift module into LLVM IR and return the generated module.
208+
/// To compile and output the generated code, call \c performLLVM.
210209
GeneratedModule
211210
performIRGeneration(ModuleDecl *M, const IRGenOptions &Opts,
212211
const TBDGenOptions &TBDOpts,
@@ -215,11 +214,10 @@ namespace swift {
215214
ArrayRef<std::string> parallelOutputFilenames,
216215
llvm::GlobalVariable **outModuleHash = nullptr);
217216

218-
/// Turn the given Swift module into either LLVM IR or native code
219-
/// and return the generated LLVM IR module.
220-
/// If you set an outModuleHash, then you need to call performLLVM.
217+
/// Turn the given Swift file into LLVM IR and return the generated module.
218+
/// To compile and output the generated code, call \c performLLVM.
221219
GeneratedModule
222-
performIRGeneration(SourceFile &SF, const IRGenOptions &Opts,
220+
performIRGeneration(FileUnit *file, const IRGenOptions &Opts,
223221
const TBDGenOptions &TBDOpts,
224222
std::unique_ptr<SILModule> SILMod,
225223
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
@@ -232,6 +230,15 @@ namespace swift {
232230
void performLLVMOptimizations(const IRGenOptions &Opts, llvm::Module *Module,
233231
llvm::TargetMachine *TargetMachine);
234232

233+
/// Compiles and writes the given LLVM module into an output stream in the
234+
/// format specified in the \c IRGenOptions.
235+
bool compileAndWriteLLVM(llvm::Module *module,
236+
llvm::TargetMachine *targetMachine,
237+
const IRGenOptions &opts,
238+
UnifiedStatsReporter *stats, DiagnosticEngine &diags,
239+
llvm::raw_pwrite_stream &out,
240+
llvm::sys::Mutex *diagMutex = nullptr);
241+
235242
/// Wrap a serialized module inside a swift AST section in an object file.
236243
void createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
237244
StringRef OutputPath);
@@ -251,15 +258,13 @@ namespace swift {
251258
/// was already compiled, may be null if not desired.
252259
/// \param Module LLVM module to code gen, required.
253260
/// \param TargetMachine target of code gen, required.
254-
/// \param effectiveLanguageVersion version of the language, effectively.
255261
/// \param OutputFilename Filename for output.
256262
bool performLLVM(const IRGenOptions &Opts,
257263
DiagnosticEngine &Diags,
258264
llvm::sys::Mutex *DiagMutex,
259265
llvm::GlobalVariable *HashGlobal,
260266
llvm::Module *Module,
261267
llvm::TargetMachine *TargetMachine,
262-
const version::Version &effectiveLanguageVersion,
263268
StringRef OutputFilename,
264269
UnifiedStatsReporter *Stats);
265270

lib/Basic/Version.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ std::string getSwiftFullVersion(Version effectiveVersion) {
405405
OS << "-dev";
406406
#endif
407407

408-
if (!(effectiveVersion == Version::getCurrentLanguageVersion())) {
408+
if (effectiveVersion != Version::getCurrentLanguageVersion()) {
409409
OS << " effective-" << effectiveVersion;
410410
}
411411

lib/FrontendTool/FrontendTool.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ generateIR(const IRGenOptions &IRGenOpts, const TBDGenOptions &TBDOpts,
15081508
llvm::GlobalVariable *&HashGlobal,
15091509
ArrayRef<std::string> parallelOutputFilenames) {
15101510
if (auto *SF = MSF.dyn_cast<SourceFile *>()) {
1511-
return performIRGeneration(*SF, IRGenOpts, TBDOpts,
1511+
return performIRGeneration(SF, IRGenOpts, TBDOpts,
15121512
std::move(SM), OutputFilename, PSPs,
15131513
SF->getPrivateDiscriminator().str(),
15141514
&HashGlobal);
@@ -1648,16 +1648,14 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
16481648
const auto &opts = Instance.getInvocation().getIRGenOptions();
16491649
std::unique_ptr<llvm::TargetMachine> TargetMachine =
16501650
createTargetMachine(opts, Instance.getASTContext());
1651-
version::Version EffectiveLanguageVersion =
1652-
Instance.getASTContext().LangOpts.EffectiveLanguageVersion;
16531651

16541652
// Free up some compiler resources now that we have an IRModule.
16551653
freeASTContextIfPossible(Instance);
16561654

16571655
// Now that we have a single IR Module, hand it over to performLLVM.
16581656
return performLLVM(opts, Instance.getDiags(), nullptr, HashGlobal, IRModule,
1659-
TargetMachine.get(), EffectiveLanguageVersion,
1660-
OutputFilename, Instance.getStatsReporter());
1657+
TargetMachine.get(), OutputFilename,
1658+
Instance.getStatsReporter());
16611659
}
16621660

16631661
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,

0 commit comments

Comments
 (0)