Skip to content

[Gardening] Sprinkle Const Qualification Around the Frontend #29155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 14, 2020
12 changes: 11 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class IRGenOptions {
/// Appends to \p os an arbitrary string representing all options which
/// influence the llvm compilation but are not reflected in the llvm module
/// itself.
void writeLLVMCodeGenOptionsTo(llvm::raw_ostream &os) {
void writeLLVMCodeGenOptionsTo(llvm::raw_ostream &os) const {
// We put a letter between each value simply to keep them from running into
// one another. There might be a vague correspondence between meaning and
// letter, but don't sweat it.
Expand Down Expand Up @@ -303,6 +303,16 @@ class IRGenOptions {
return OptMode == OptimizationMode::ForSize;
}

std::string getDebugFlags(StringRef PrivateDiscriminator) const {
std::string Flags = DebugFlags;
if (!PrivateDiscriminator.empty()) {
if (!Flags.empty())
Flags += " ";
Flags += ("-private-discriminator " + PrivateDiscriminator).str();
}
return Flags;
}

/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
llvm::hash_code getPCHHashComponents() const {
Expand Down
26 changes: 12 additions & 14 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class CompilerInvocation {
/// in generating a cached PCH file for the bridging header.
std::string getPCHHash() const;

SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() {
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() const {
if (getInputKind() == InputFileKind::SIL) {
return SourceFile::ImplicitModuleImportKind::None;
}
Expand Down Expand Up @@ -380,7 +380,7 @@ class CompilerInvocation {

SerializationOptions
computeSerializationOptions(const SupplementaryOutputPaths &outs,
bool moduleIsPublic);
bool moduleIsPublic) const;
};

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

ModuleDecl *MainModule = nullptr;
mutable ModuleDecl *MainModule = nullptr;
SerializedModuleLoader *SML = nullptr;
MemoryBufferSerializedModuleLoader *MemoryBufferLoader = nullptr;

Expand Down Expand Up @@ -453,14 +453,16 @@ class CompilerInstance {
void operator=(CompilerInstance &&) = delete;

SourceManager &getSourceMgr() { return SourceMgr; }
const SourceManager &getSourceMgr() const { return SourceMgr; }

DiagnosticEngine &getDiags() { return Diagnostics; }
const DiagnosticEngine &getDiags() const { return Diagnostics; }

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

ASTContext &getASTContext() {
return *Context;
}
ASTContext &getASTContext() { return *Context; }
const ASTContext &getASTContext() const { return *Context; }

bool hasASTContext() const { return Context != nullptr; }

SILOptions &getSILOptions() { return Invocation.getSILOptions(); }
Expand All @@ -483,11 +485,7 @@ class CompilerInstance {
DepTracker = llvm::make_unique<DependencyTracker>(TrackSystemDeps);
}
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }

/// Set the SIL module for this compilation instance.
///
/// The CompilerInstance takes ownership of the given SILModule object.
void setSILModule(std::unique_ptr<SILModule> M);
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }

SILModule *getSILModule() {
return TheSILModule.get();
Expand All @@ -499,7 +497,7 @@ class CompilerInstance {
return static_cast<bool>(TheSILModule);
}

ModuleDecl *getMainModule();
ModuleDecl *getMainModule() const;

MemoryBufferSerializedModuleLoader *
getMemoryBufferSerializedModuleLoader() const {
Expand All @@ -520,7 +518,7 @@ class CompilerInstance {

/// Gets the set of SourceFiles which are the primary inputs for this
/// CompilerInstance.
ArrayRef<SourceFile *> getPrimarySourceFiles() {
ArrayRef<SourceFile *> getPrimarySourceFiles() const {
return PrimarySourceFiles;
}

Expand All @@ -530,7 +528,7 @@ class CompilerInstance {
///
/// FIXME: This should be removed eventually, once there are no longer any
/// codepaths that rely on a single primary file.
SourceFile *getPrimarySourceFile() {
SourceFile *getPrimarySourceFile() const {
if (PrimarySourceFiles.empty()) {
return nullptr;
} else {
Expand Down
1 change: 1 addition & 0 deletions include/swift/IRGen/IRGenPublic.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class IRGenModule;
std::pair<IRGenerator *, IRGenModule *>
createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
StringRef MainInputFilenameForDebugInfo,
StringRef PrivateDiscriminator,
llvm::LLVMContext &LLVMContext);

/// Delete the IRGenModule and IRGenerator obtained by the above call.
Expand Down
5 changes: 4 additions & 1 deletion include/swift/Immediate/Immediate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
#ifndef SWIFT_IMMEDIATE_IMMEDIATE_H
#define SWIFT_IMMEDIATE_IMMEDIATE_H

#include <memory>
#include <string>
#include <vector>

namespace swift {
class CompilerInstance;
class IRGenOptions;
class SILOptions;
class SILModule;

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

void runREPL(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
bool ParseStdlib);
Expand Down
10 changes: 5 additions & 5 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class SILModule {
bool wholeModule;

/// The options passed into this SILModule.
SILOptions &Options;
const SILOptions &Options;

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

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

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

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

SILOptions &getOptions() const { return Options; }
const SILOptions &getOptions() const { return Options; }

using iterator = FunctionListType::iterator;
using const_iterator = FunctionListType::const_iterator;
Expand Down
21 changes: 11 additions & 10 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ namespace swift {
/// SIL of all files in the module is present in the SILModule.
std::unique_ptr<SILModule>
performSILGeneration(ModuleDecl *M, Lowering::TypeConverter &TC,
SILOptions &options);
const SILOptions &options);

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

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

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

/// Turn the given Swift module into either LLVM IR or native code
/// and return the generated LLVM IR module.
/// If you set an outModuleHash, then you need to call performLLVM.
std::unique_ptr<llvm::Module>
performIRGeneration(IRGenOptions &Opts, ModuleDecl *M,
performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
std::unique_ptr<SILModule> SILMod,
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
llvm::LLVMContext &LLVMContext,
Expand All @@ -281,25 +281,26 @@ namespace swift {
/// and return the generated LLVM IR module.
/// If you set an outModuleHash, then you need to call performLLVM.
std::unique_ptr<llvm::Module>
performIRGeneration(IRGenOptions &Opts, SourceFile &SF,
performIRGeneration(const IRGenOptions &Opts, SourceFile &SF,
std::unique_ptr<SILModule> SILMod,
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
StringRef PrivateDiscriminator,
llvm::LLVMContext &LLVMContext,
llvm::GlobalVariable **outModuleHash = nullptr,
llvm::StringSet<> *LinkerDirectives = nullptr);

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

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

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

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

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

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

/// A convenience wrapper for Parser functionality.
class ParserUnit {
Expand Down
52 changes: 52 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,54 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
}
}

static void
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
const FrontendOptions &FrontendOpts) {
// Set the OutputKind for the given Action.
IRGenOpts.OutputKind = [](FrontendOptions::ActionType Action) {
switch (Action) {
case FrontendOptions::ActionType::EmitIR:
return IRGenOutputKind::LLVMAssembly;
case FrontendOptions::ActionType::EmitBC:
return IRGenOutputKind::LLVMBitcode;
case FrontendOptions::ActionType::EmitAssembly:
return IRGenOutputKind::NativeAssembly;
case FrontendOptions::ActionType::Immediate:
return IRGenOutputKind::Module;
case FrontendOptions::ActionType::EmitObject:
default:
// Just fall back to emitting an object file. If we aren't going to run
// IRGen, it doesn't really matter what we put here anyways.
return IRGenOutputKind::ObjectFile;
}
}(FrontendOpts.RequestedAction);

// If we're in JIT mode, set the requisite flags.
if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::Immediate) {
IRGenOpts.UseJIT = true;
IRGenOpts.DebugInfoLevel = IRGenDebugInfoLevel::Normal;
IRGenOpts.DebugInfoFormat = IRGenDebugInfoFormat::DWARF;
}
}

static void
setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
const FrontendOptions &FrontendOpts) {
if (FrontendOpts.RequestedAction != FrontendOptions::ActionType::EmitPCH)
return;

// If there aren't any inputs, there's nothing to do.
if (!FrontendOpts.InputsAndOutputs.hasInputs())
return;

// If we aren't asked to output a bridging header, we don't need to set this.
if (ImporterOpts.PrecompiledHeaderOutputDir.empty())
return;

ImporterOpts.BridgingHeader =
FrontendOpts.InputsAndOutputs.getFilenameOfFirstInput();
}

void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
SearchPathOpts.RuntimeResourcePath = Path;
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
Expand Down Expand Up @@ -1486,6 +1534,10 @@ bool CompilerInvocation::parseArgs(
setDefaultPrebuiltCacheIfNecessary(FrontendOpts, SearchPathOpts,
LangOpts.Target);

// Now that we've parsed everything, setup some inter-option-dependent state.
setIRGenOutputOptsFromFrontendOptions(IRGenOpts, FrontendOpts);
setBridgingHeaderFromFrontendOptions(ClangImporterOpts, FrontendOpts);

return false;
}

Expand Down
8 changes: 2 additions & 6 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ CompilerInvocation::getModuleInterfaceOutputPathForWholeModule() const {
}

SerializationOptions CompilerInvocation::computeSerializationOptions(
const SupplementaryOutputPaths &outs, bool moduleIsPublic) {
const SupplementaryOutputPaths &outs, bool moduleIsPublic) const {
const FrontendOptions &opts = getFrontendOptions();

SerializationOptions serializationOpts;
Expand Down Expand Up @@ -185,10 +185,6 @@ void CompilerInstance::createSILModule() {
Invocation.getFrontendOptions().InputsAndOutputs.isWholeModule());
}

void CompilerInstance::setSILModule(std::unique_ptr<SILModule> M) {
TheSILModule = std::move(M);
}

void CompilerInstance::recordPrimaryInputBuffer(unsigned BufID) {
PrimaryBufferIDs.insert(BufID);
}
Expand Down Expand Up @@ -618,7 +614,7 @@ std::unique_ptr<SILModule> CompilerInstance::takeSILModule() {
return std::move(TheSILModule);
}

ModuleDecl *CompilerInstance::getMainModule() {
ModuleDecl *CompilerInstance::getMainModule() const {
if (!MainModule) {
Identifier ID = Context->getIdentifier(Invocation.getModuleName());
MainModule = ModuleDecl::create(ID, *Context);
Expand Down
Loading