Skip to content

Commit 5e155d9

Browse files
authored
Merge pull request #29155 from CodaFi/west-const-story
[Gardening] Sprinkle Const Qualification Around the Frontend
2 parents 2354ceb + 2af598f commit 5e155d9

File tree

22 files changed

+230
-198
lines changed

22 files changed

+230
-198
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class IRGenOptions {
272272
/// Appends to \p os an arbitrary string representing all options which
273273
/// influence the llvm compilation but are not reflected in the llvm module
274274
/// itself.
275-
void writeLLVMCodeGenOptionsTo(llvm::raw_ostream &os) {
275+
void writeLLVMCodeGenOptionsTo(llvm::raw_ostream &os) const {
276276
// We put a letter between each value simply to keep them from running into
277277
// one another. There might be a vague correspondence between meaning and
278278
// letter, but don't sweat it.
@@ -303,6 +303,16 @@ class IRGenOptions {
303303
return OptMode == OptimizationMode::ForSize;
304304
}
305305

306+
std::string getDebugFlags(StringRef PrivateDiscriminator) const {
307+
std::string Flags = DebugFlags;
308+
if (!PrivateDiscriminator.empty()) {
309+
if (!Flags.empty())
310+
Flags += " ";
311+
Flags += ("-private-discriminator " + PrivateDiscriminator).str();
312+
}
313+
return Flags;
314+
}
315+
306316
/// Return a hash code of any components from these options that should
307317
/// contribute to a Swift Bridging PCH hash.
308318
llvm::hash_code getPCHHashComponents() const {

include/swift/Frontend/Frontend.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class CompilerInvocation {
328328
/// in generating a cached PCH file for the bridging header.
329329
std::string getPCHHash() const;
330330

331-
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() {
331+
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() const {
332332
if (getInputKind() == InputFileKind::SIL) {
333333
return SourceFile::ImplicitModuleImportKind::None;
334334
}
@@ -380,7 +380,7 @@ class CompilerInvocation {
380380

381381
SerializationOptions
382382
computeSerializationOptions(const SupplementaryOutputPaths &outs,
383-
bool moduleIsPublic);
383+
bool moduleIsPublic) const;
384384
};
385385

386386
/// A class which manages the state and execution of the compiler.
@@ -404,7 +404,7 @@ class CompilerInstance {
404404
/// Null if no tracker.
405405
std::unique_ptr<DependencyTracker> DepTracker;
406406

407-
ModuleDecl *MainModule = nullptr;
407+
mutable ModuleDecl *MainModule = nullptr;
408408
SerializedModuleLoader *SML = nullptr;
409409
MemoryBufferSerializedModuleLoader *MemoryBufferLoader = nullptr;
410410

@@ -453,14 +453,16 @@ class CompilerInstance {
453453
void operator=(CompilerInstance &&) = delete;
454454

455455
SourceManager &getSourceMgr() { return SourceMgr; }
456+
const SourceManager &getSourceMgr() const { return SourceMgr; }
456457

457458
DiagnosticEngine &getDiags() { return Diagnostics; }
459+
const DiagnosticEngine &getDiags() const { return Diagnostics; }
458460

459461
llvm::vfs::FileSystem &getFileSystem() { return *SourceMgr.getFileSystem(); }
460462

461-
ASTContext &getASTContext() {
462-
return *Context;
463-
}
463+
ASTContext &getASTContext() { return *Context; }
464+
const ASTContext &getASTContext() const { return *Context; }
465+
464466
bool hasASTContext() const { return Context != nullptr; }
465467

466468
SILOptions &getSILOptions() { return Invocation.getSILOptions(); }
@@ -483,11 +485,7 @@ class CompilerInstance {
483485
DepTracker = llvm::make_unique<DependencyTracker>(TrackSystemDeps);
484486
}
485487
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
486-
487-
/// Set the SIL module for this compilation instance.
488-
///
489-
/// The CompilerInstance takes ownership of the given SILModule object.
490-
void setSILModule(std::unique_ptr<SILModule> M);
488+
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }
491489

492490
SILModule *getSILModule() {
493491
return TheSILModule.get();
@@ -499,7 +497,7 @@ class CompilerInstance {
499497
return static_cast<bool>(TheSILModule);
500498
}
501499

502-
ModuleDecl *getMainModule();
500+
ModuleDecl *getMainModule() const;
503501

504502
MemoryBufferSerializedModuleLoader *
505503
getMemoryBufferSerializedModuleLoader() const {
@@ -520,7 +518,7 @@ class CompilerInstance {
520518

521519
/// Gets the set of SourceFiles which are the primary inputs for this
522520
/// CompilerInstance.
523-
ArrayRef<SourceFile *> getPrimarySourceFiles() {
521+
ArrayRef<SourceFile *> getPrimarySourceFiles() const {
524522
return PrimarySourceFiles;
525523
}
526524

@@ -530,7 +528,7 @@ class CompilerInstance {
530528
///
531529
/// FIXME: This should be removed eventually, once there are no longer any
532530
/// codepaths that rely on a single primary file.
533-
SourceFile *getPrimarySourceFile() {
531+
SourceFile *getPrimarySourceFile() const {
534532
if (PrimarySourceFiles.empty()) {
535533
return nullptr;
536534
} else {

include/swift/IRGen/IRGenPublic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class IRGenModule;
3131
std::pair<IRGenerator *, IRGenModule *>
3232
createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
3333
StringRef MainInputFilenameForDebugInfo,
34+
StringRef PrivateDiscriminator,
3435
llvm::LLVMContext &LLVMContext);
3536

3637
/// Delete the IRGenModule and IRGenerator obtained by the above call.

include/swift/Immediate/Immediate.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#ifndef SWIFT_IMMEDIATE_IMMEDIATE_H
1919
#define SWIFT_IMMEDIATE_IMMEDIATE_H
2020

21+
#include <memory>
2122
#include <string>
2223
#include <vector>
2324

2425
namespace swift {
2526
class CompilerInstance;
2627
class IRGenOptions;
2728
class SILOptions;
29+
class SILModule;
2830

2931
// Using LLVM containers to store command-line arguments turns out
3032
// to be a lose, because LLVM's execution engine demands this vector
@@ -37,7 +39,8 @@ namespace swift {
3739
///
3840
/// \return the result returned from main(), if execution succeeded
3941
int RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
40-
IRGenOptions &IRGenOpts, const SILOptions &SILOpts);
42+
const IRGenOptions &IRGenOpts, const SILOptions &SILOpts,
43+
std::unique_ptr<SILModule> &&SM);
4144

4245
void runREPL(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
4346
bool ParseStdlib);

include/swift/SIL/SILModule.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class SILModule {
254254
bool wholeModule;
255255

256256
/// The options passed into this SILModule.
257-
SILOptions &Options;
257+
const SILOptions &Options;
258258

259259
/// Set if the SILModule was serialized already. It is used
260260
/// to ensure that the module is serialized only once.
@@ -270,7 +270,7 @@ class SILModule {
270270
// Intentionally marked private so that we need to use 'constructSIL()'
271271
// to construct a SILModule.
272272
SILModule(ModuleDecl *M, Lowering::TypeConverter &TC,
273-
SILOptions &Options, const DeclContext *associatedDC,
273+
const SILOptions &Options, const DeclContext *associatedDC,
274274
bool wholeModule);
275275

276276
SILModule(const SILModule&) = delete;
@@ -351,13 +351,13 @@ class SILModule {
351351
/// source file.
352352
static std::unique_ptr<SILModule>
353353
constructSIL(ModuleDecl *M, Lowering::TypeConverter &TC,
354-
SILOptions &Options, FileUnit *sf = nullptr);
354+
const SILOptions &Options, FileUnit *sf = nullptr);
355355

356356
/// Create and return an empty SIL module that we can
357357
/// later parse SIL bodies directly into, without converting from an AST.
358358
static std::unique_ptr<SILModule>
359359
createEmptyModule(ModuleDecl *M, Lowering::TypeConverter &TC,
360-
SILOptions &Options,
360+
const SILOptions &Options,
361361
bool WholeModule = false);
362362

363363
/// Get the Swift module associated with this SIL module.
@@ -390,7 +390,7 @@ class SILModule {
390390
/// Returns true if it is the optimized OnoneSupport module.
391391
bool isOptimizedOnoneSupportModule() const;
392392

393-
SILOptions &getOptions() const { return Options; }
393+
const SILOptions &getOptions() const { return Options; }
394394

395395
using iterator = FunctionListType::iterator;
396396
using const_iterator = FunctionListType::const_iterator;

include/swift/Subsystems.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ namespace swift {
234234
/// SIL of all files in the module is present in the SILModule.
235235
std::unique_ptr<SILModule>
236236
performSILGeneration(ModuleDecl *M, Lowering::TypeConverter &TC,
237-
SILOptions &options);
237+
const SILOptions &options);
238238

239239
/// Turn a source file into SIL IR.
240240
std::unique_ptr<SILModule>
241241
performSILGeneration(FileUnit &SF, Lowering::TypeConverter &TC,
242-
SILOptions &options);
242+
const SILOptions &options);
243243

244244
using ModuleOrSourceFile = PointerUnion<ModuleDecl *, SourceFile *>;
245245

@@ -263,13 +263,13 @@ namespace swift {
263263
/// Get the CPU, subtarget feature options, and triple to use when emitting code.
264264
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
265265
std::string>
266-
getIRTargetOptions(IRGenOptions &Opts, ASTContext &Ctx);
266+
getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx);
267267

268268
/// Turn the given Swift module into either LLVM IR or native code
269269
/// and return the generated LLVM IR module.
270270
/// If you set an outModuleHash, then you need to call performLLVM.
271271
std::unique_ptr<llvm::Module>
272-
performIRGeneration(IRGenOptions &Opts, ModuleDecl *M,
272+
performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
273273
std::unique_ptr<SILModule> SILMod,
274274
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
275275
llvm::LLVMContext &LLVMContext,
@@ -281,25 +281,26 @@ namespace swift {
281281
/// and return the generated LLVM IR module.
282282
/// If you set an outModuleHash, then you need to call performLLVM.
283283
std::unique_ptr<llvm::Module>
284-
performIRGeneration(IRGenOptions &Opts, SourceFile &SF,
284+
performIRGeneration(const IRGenOptions &Opts, SourceFile &SF,
285285
std::unique_ptr<SILModule> SILMod,
286286
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
287+
StringRef PrivateDiscriminator,
287288
llvm::LLVMContext &LLVMContext,
288289
llvm::GlobalVariable **outModuleHash = nullptr,
289290
llvm::StringSet<> *LinkerDirectives = nullptr);
290291

291292
/// Given an already created LLVM module, construct a pass pipeline and run
292293
/// the Swift LLVM Pipeline upon it. This does not cause the module to be
293294
/// printed, only to be optimized.
294-
void performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,
295+
void performLLVMOptimizations(const IRGenOptions &Opts, llvm::Module *Module,
295296
llvm::TargetMachine *TargetMachine);
296297

297298
/// Wrap a serialized module inside a swift AST section in an object file.
298299
void createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
299300
StringRef OutputPath);
300301

301302
/// Turn the given LLVM module into native code and return true on error.
302-
bool performLLVM(IRGenOptions &Opts, ASTContext &Ctx, llvm::Module *Module,
303+
bool performLLVM(const IRGenOptions &Opts, ASTContext &Ctx, llvm::Module *Module,
303304
StringRef OutputFilename,
304305
UnifiedStatsReporter *Stats=nullptr);
305306

@@ -314,7 +315,7 @@ namespace swift {
314315
/// \param TargetMachine target of code gen, required.
315316
/// \param effectiveLanguageVersion version of the language, effectively.
316317
/// \param OutputFilename Filename for output.
317-
bool performLLVM(IRGenOptions &Opts, DiagnosticEngine *Diags,
318+
bool performLLVM(const IRGenOptions &Opts, DiagnosticEngine *Diags,
318319
llvm::sys::Mutex *DiagMutex,
319320
llvm::GlobalVariable *HashGlobal,
320321
llvm::Module *Module,
@@ -324,13 +325,13 @@ namespace swift {
324325
UnifiedStatsReporter *Stats=nullptr);
325326

326327
/// Dump YAML describing all fixed-size types imported from the given module.
327-
bool performDumpTypeInfo(IRGenOptions &Opts,
328+
bool performDumpTypeInfo(const IRGenOptions &Opts,
328329
SILModule &SILMod,
329330
llvm::LLVMContext &LLVMContext);
330331

331332
/// Creates a TargetMachine from the IRGen opts and AST Context.
332333
std::unique_ptr<llvm::TargetMachine>
333-
createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx);
334+
createTargetMachine(const IRGenOptions &Opts, ASTContext &Ctx);
334335

335336
/// A convenience wrapper for Parser functionality.
336337
class ParserUnit {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,54 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
111111
}
112112
}
113113

114+
static void
115+
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
116+
const FrontendOptions &FrontendOpts) {
117+
// Set the OutputKind for the given Action.
118+
IRGenOpts.OutputKind = [](FrontendOptions::ActionType Action) {
119+
switch (Action) {
120+
case FrontendOptions::ActionType::EmitIR:
121+
return IRGenOutputKind::LLVMAssembly;
122+
case FrontendOptions::ActionType::EmitBC:
123+
return IRGenOutputKind::LLVMBitcode;
124+
case FrontendOptions::ActionType::EmitAssembly:
125+
return IRGenOutputKind::NativeAssembly;
126+
case FrontendOptions::ActionType::Immediate:
127+
return IRGenOutputKind::Module;
128+
case FrontendOptions::ActionType::EmitObject:
129+
default:
130+
// Just fall back to emitting an object file. If we aren't going to run
131+
// IRGen, it doesn't really matter what we put here anyways.
132+
return IRGenOutputKind::ObjectFile;
133+
}
134+
}(FrontendOpts.RequestedAction);
135+
136+
// If we're in JIT mode, set the requisite flags.
137+
if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::Immediate) {
138+
IRGenOpts.UseJIT = true;
139+
IRGenOpts.DebugInfoLevel = IRGenDebugInfoLevel::Normal;
140+
IRGenOpts.DebugInfoFormat = IRGenDebugInfoFormat::DWARF;
141+
}
142+
}
143+
144+
static void
145+
setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
146+
const FrontendOptions &FrontendOpts) {
147+
if (FrontendOpts.RequestedAction != FrontendOptions::ActionType::EmitPCH)
148+
return;
149+
150+
// If there aren't any inputs, there's nothing to do.
151+
if (!FrontendOpts.InputsAndOutputs.hasInputs())
152+
return;
153+
154+
// If we aren't asked to output a bridging header, we don't need to set this.
155+
if (ImporterOpts.PrecompiledHeaderOutputDir.empty())
156+
return;
157+
158+
ImporterOpts.BridgingHeader =
159+
FrontendOpts.InputsAndOutputs.getFilenameOfFirstInput();
160+
}
161+
114162
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
115163
SearchPathOpts.RuntimeResourcePath = Path;
116164
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
@@ -1486,6 +1534,10 @@ bool CompilerInvocation::parseArgs(
14861534
setDefaultPrebuiltCacheIfNecessary(FrontendOpts, SearchPathOpts,
14871535
LangOpts.Target);
14881536

1537+
// Now that we've parsed everything, setup some inter-option-dependent state.
1538+
setIRGenOutputOptsFromFrontendOptions(IRGenOpts, FrontendOpts);
1539+
setBridgingHeaderFromFrontendOptions(ClangImporterOpts, FrontendOpts);
1540+
14891541
return false;
14901542
}
14911543

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ CompilerInvocation::getModuleInterfaceOutputPathForWholeModule() const {
142142
}
143143

144144
SerializationOptions CompilerInvocation::computeSerializationOptions(
145-
const SupplementaryOutputPaths &outs, bool moduleIsPublic) {
145+
const SupplementaryOutputPaths &outs, bool moduleIsPublic) const {
146146
const FrontendOptions &opts = getFrontendOptions();
147147

148148
SerializationOptions serializationOpts;
@@ -185,10 +185,6 @@ void CompilerInstance::createSILModule() {
185185
Invocation.getFrontendOptions().InputsAndOutputs.isWholeModule());
186186
}
187187

188-
void CompilerInstance::setSILModule(std::unique_ptr<SILModule> M) {
189-
TheSILModule = std::move(M);
190-
}
191-
192188
void CompilerInstance::recordPrimaryInputBuffer(unsigned BufID) {
193189
PrimaryBufferIDs.insert(BufID);
194190
}
@@ -618,7 +614,7 @@ std::unique_ptr<SILModule> CompilerInstance::takeSILModule() {
618614
return std::move(TheSILModule);
619615
}
620616

621-
ModuleDecl *CompilerInstance::getMainModule() {
617+
ModuleDecl *CompilerInstance::getMainModule() const {
622618
if (!MainModule) {
623619
Identifier ID = Context->getIdentifier(Invocation.getModuleName());
624620
MainModule = ModuleDecl::create(ID, *Context);

0 commit comments

Comments
 (0)