Skip to content

Fix unnecessary one-time recompile of stdlib with -enable-ossa-flag #39516

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 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Import.h"
#include "swift/AST/SILOptions.h"
#include "swift/AST/SearchPathOptions.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeAlignments.h"
Expand All @@ -38,8 +39,8 @@
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/DataTypes.h"
Expand Down Expand Up @@ -221,11 +222,10 @@ class ASTContext final {
void operator=(const ASTContext&) = delete;

ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
SearchPathOptions &SearchPathOpts,
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
ClangImporterOptions &ClangImporterOpts,
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
SourceManager &SourceMgr,
DiagnosticEngine &Diags);
SourceManager &SourceMgr, DiagnosticEngine &Diags);

public:
// Members that should only be used by ASTContext.cpp.
Expand All @@ -237,7 +237,7 @@ class ASTContext final {
void operator delete(void *Data) throw();

static ASTContext *get(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
SearchPathOptions &SearchPathOpts,
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
ClangImporterOptions &ClangImporterOpts,
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
SourceManager &SourceMgr, DiagnosticEngine &Diags);
Expand All @@ -255,6 +255,9 @@ class ASTContext final {
/// The type checker options.
const TypeCheckerOptions &TypeCheckerOpts;

/// Options for SIL.
const SILOptions &SILOpts;

/// The search path options used by this AST context.
SearchPathOptions &SearchPathOpts;

Expand Down
1 change: 1 addition & 0 deletions include/swift/Serialization/SerializationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ namespace swift {
bool IsSIB = false;
bool DisableCrossModuleIncrementalInfo = false;
bool StaticLibrary = false;
bool IsOSSA = false;
};

} // end namespace swift
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class SerializedModuleLoaderBase : public ModuleLoader {
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework);

bool isRequiredOSSAModules() const;

/// Check whether the module with a given name can be imported without
/// importing it.
///
Expand Down
8 changes: 7 additions & 1 deletion include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ enum class Status {
/// The precise revision version doesn't match.
RevisionIncompatible,

/// The module is required to be in OSSA, but is not.
NotInOSSA,

/// The module file depends on another module that can't be loaded.
MissingDependency,

Expand Down Expand Up @@ -186,13 +189,16 @@ class ExtendedValidationInfo {
///
/// \param data A buffer containing the serialized AST. Result information
/// refers directly into this buffer.
/// \param requiresOSSAModules If true, necessitates the module to be
/// compiled with -enable-ossa-modules.
/// \param[out] extendedInfo If present, will be populated with additional
/// compilation options serialized into the AST at build time that may be
/// necessary to load it properly.
/// \param[out] dependencies If present, will be populated with list of
/// input files the module depends on, if present in INPUT_BLOCK.
ValidationInfo validateSerializedAST(
StringRef data, ExtendedValidationInfo *extendedInfo = nullptr,
StringRef data, bool requiresOSSAModules,
ExtendedValidationInfo *extendedInfo = nullptr,
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies =
nullptr);

Expand Down
3 changes: 2 additions & 1 deletion include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace swift {
class GenericParamList;
class IRGenOptions;
class LangOptions;
class SILOptions;
class ModuleDecl;
/// A opaque syntax node created by a \c SyntaxParseAction, whose contents
/// must be interpreted by the \c SyntaxParseAction which created it.
Expand Down Expand Up @@ -283,7 +284,7 @@ namespace swift {
public:
ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID,
const LangOptions &LangOpts, const TypeCheckerOptions &TyOpts,
StringRef ModuleName,
const SILOptions &SILOpts, StringRef ModuleName,
std::shared_ptr<SyntaxParseActions> spActions = nullptr,
SyntaxParsingCache *SyntaxCache = nullptr);
ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID);
Expand Down
42 changes: 18 additions & 24 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,11 @@ void ASTContext::operator delete(void *Data) throw() {
}

ASTContext *ASTContext::get(LangOptions &langOpts,
TypeCheckerOptions &typeckOpts,
TypeCheckerOptions &typeckOpts, SILOptions &silOpts,
SearchPathOptions &SearchPathOpts,
ClangImporterOptions &ClangImporterOpts,
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
SourceManager &SourceMgr,
DiagnosticEngine &Diags) {
SourceManager &SourceMgr, DiagnosticEngine &Diags) {
// If more than two data structures are concatentated, then the aggregate
// size math needs to become more complicated due to per-struct alignment
// constraints.
Expand All @@ -600,33 +599,28 @@ ASTContext *ASTContext::get(LangOptions &langOpts,
llvm::alignAddr(impl, llvm::Align(alignof(Implementation))));
new (impl) Implementation();
return new (mem)
ASTContext(langOpts, typeckOpts, SearchPathOpts, ClangImporterOpts,
SymbolGraphOpts, SourceMgr, Diags);
ASTContext(langOpts, typeckOpts, silOpts, SearchPathOpts,
ClangImporterOpts, SymbolGraphOpts, SourceMgr, Diags);
}

ASTContext::ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
SearchPathOptions &SearchPathOpts,
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
ClangImporterOptions &ClangImporterOpts,
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
SourceManager &SourceMgr, DiagnosticEngine &Diags)
: LangOpts(langOpts),
TypeCheckerOpts(typeckOpts),
SearchPathOpts(SearchPathOpts),
ClangImporterOpts(ClangImporterOpts),
SymbolGraphOpts(SymbolGraphOpts),
SourceMgr(SourceMgr), Diags(Diags),
evaluator(Diags, langOpts),
TheBuiltinModule(createBuiltinModule(*this)),
StdlibModuleName(getIdentifier(STDLIB_NAME)),
SwiftShimsModuleName(getIdentifier(SWIFT_SHIMS_NAME)),
TheErrorType(
new (*this, AllocationArena::Permanent)
ErrorType(*this, Type(), RecursiveTypeProperties::HasError)),
TheUnresolvedType(new (*this, AllocationArena::Permanent)
UnresolvedType(*this)),
TheEmptyTupleType(TupleType::get(ArrayRef<TupleTypeElt>(), *this)),
TheAnyType(ProtocolCompositionType::get(*this, ArrayRef<Type>(),
/*HasExplicitAnyObject=*/false)),
: LangOpts(langOpts), TypeCheckerOpts(typeckOpts), SILOpts(silOpts),
SearchPathOpts(SearchPathOpts), ClangImporterOpts(ClangImporterOpts),
SymbolGraphOpts(SymbolGraphOpts), SourceMgr(SourceMgr), Diags(Diags),
evaluator(Diags, langOpts), TheBuiltinModule(createBuiltinModule(*this)),
StdlibModuleName(getIdentifier(STDLIB_NAME)),
SwiftShimsModuleName(getIdentifier(SWIFT_SHIMS_NAME)),
TheErrorType(new (*this, AllocationArena::Permanent) ErrorType(
*this, Type(), RecursiveTypeProperties::HasError)),
TheUnresolvedType(new (*this, AllocationArena::Permanent)
UnresolvedType(*this)),
TheEmptyTupleType(TupleType::get(ArrayRef<TupleTypeElt>(), *this)),
TheAnyType(ProtocolCompositionType::get(*this, ArrayRef<Type>(),
/*HasExplicitAnyObject=*/false)),
#define SINGLETON_TYPE(SHORT_ID, ID) \
The##SHORT_ID##Type(new (*this, AllocationArena::Permanent) \
ID##Type(*this)),
Expand Down
4 changes: 3 additions & 1 deletion lib/ASTSectionImporter/ASTSectionImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "swift/ASTSectionImporter/ASTSectionImporter.h"
#include "../Serialization/ModuleFormat.h"
#include "swift/AST/ASTContext.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Serialization/Validation.h"
Expand All @@ -34,7 +35,8 @@ bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
// An AST section consists of one or more AST modules, optionally with
// headers. Iterate over all AST modules.
while (!buf.empty()) {
auto info = serialization::validateSerializedAST(buf);
auto info = serialization::validateSerializedAST(
buf, Loader.isRequiredOSSAModules());

assert(info.name.size() < (2 << 10) && "name failed sanity check");

Expand Down
8 changes: 4 additions & 4 deletions lib/DriverTool/modulewrap_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
SearchPathOpts.RuntimeResourcePath = std::string(RuntimeResourcePath.str());

SourceManager SrcMgr;
SILOptions SILOpts;
TypeCheckerOptions TypeCheckOpts;
LangOptions LangOpts;
ClangImporterOptions ClangImporterOpts;
symbolgraphgen::SymbolGraphOptions SymbolGraphOpts;
LangOpts.Target = Invocation.getTargetTriple();
ASTContext &ASTCtx = *ASTContext::get(LangOpts, TypeCheckOpts, SearchPathOpts,
ClangImporterOpts, SymbolGraphOpts, SrcMgr,
Instance.getDiags());
ASTContext &ASTCtx = *ASTContext::get(
LangOpts, TypeCheckOpts, SILOpts, SearchPathOpts, ClangImporterOpts,
SymbolGraphOpts, SrcMgr, Instance.getDiags());
registerParseRequestFunctions(ASTCtx.evaluator);
registerTypeCheckerRequestFunctions(ASTCtx.evaluator);

ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, ""), true);
ModuleDecl *M = ModuleDecl::create(ASTCtx.getIdentifier("swiftmodule"), ASTCtx);
SILOptions SILOpts;
std::unique_ptr<Lowering::TypeConverter> TC(new Lowering::TypeConverter(*M));
std::unique_ptr<SILModule> SM = SILModule::createEmptyModule(M, *TC, SILOpts);
createSwiftModuleObjectFile(*SM, (*ErrOrBuf)->getBuffer(),
Expand Down
8 changes: 4 additions & 4 deletions lib/DriverTool/swift_indent_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class FormatterDocument {

void updateCode(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
BufferID = SM.addNewSourceBuffer(std::move(Buffer));
Parser.reset(new ParserUnit(SM, SourceFileKind::Main,
BufferID, CompInv.getLangOptions(),
CompInv.getTypeCheckerOptions(),
CompInv.getModuleName()));
Parser.reset(new ParserUnit(
SM, SourceFileKind::Main, BufferID, CompInv.getLangOptions(),
CompInv.getTypeCheckerOptions(), CompInv.getSILOptions(),
CompInv.getModuleName()));
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
Parser->parse();
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,8 +2146,8 @@ bool CompilerInvocation::parseArgs(
serialization::Status
CompilerInvocation::loadFromSerializedAST(StringRef data) {
serialization::ExtendedValidationInfo extendedInfo;
serialization::ValidationInfo info =
serialization::validateSerializedAST(data, &extendedInfo);
serialization::ValidationInfo info = serialization::validateSerializedAST(
data, getSILOptions().EnableOSSAModules, &extendedInfo);

if (info.status != serialization::Status::Valid)
return info.status;
Expand Down Expand Up @@ -2182,7 +2182,8 @@ CompilerInvocation::setUpInputForSILTool(
InputFile(inputFilename, bePrimary, fileBufOrErr.get().get(), file_types::TY_SIL));

auto result = serialization::validateSerializedAST(
fileBufOrErr.get()->getBuffer(), &extendedInfo);
fileBufOrErr.get()->getBuffer(), getSILOptions().EnableOSSAModules,
&extendedInfo);
bool hasSerializedAST = result.status == serialization::Status::Valid;

if (hasSerializedAST) {
Expand Down
7 changes: 4 additions & 3 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(

serializationOpts.StaticLibrary = opts.Static;

serializationOpts.IsOSSA = getSILOptions().EnableOSSAModules;

return serializationOpts;
}

Expand Down Expand Up @@ -215,9 +217,8 @@ bool CompilerInstance::setUpASTContextIfNeeded() {

Context.reset(ASTContext::get(
Invocation.getLangOptions(), Invocation.getTypeCheckerOptions(),
Invocation.getSearchPathOptions(),
Invocation.getClangImporterOptions(),
Invocation.getSymbolGraphOptions(),
Invocation.getSILOptions(), Invocation.getSearchPathOptions(),
Invocation.getClangImporterOptions(), Invocation.getSymbolGraphOptions(),
SourceMgr, Diagnostics));
registerParseRequestFunctions(Context->evaluator);
registerTypeCheckerRequestFunctions(Context->evaluator);
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/ModuleInterfaceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
}
if (ShouldSerializeDeps)
SerializationOpts.Dependencies = Deps;
SerializationOpts.IsOSSA = SILOpts.EnableOSSAModules;

SILMod->setSerializeSILAction([&]() {
if (isTypeChecking)
return;
Expand Down
17 changes: 7 additions & 10 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ class DiscoveredModule {

namespace path = llvm::sys::path;

static bool serializedASTLooksValid(const llvm::MemoryBuffer &buf) {
auto VI = serialization::validateSerializedAST(buf.getBuffer());
static bool serializedASTLooksValid(const llvm::MemoryBuffer &buf,
bool requiresOSSAModules) {
auto VI = serialization::validateSerializedAST(buf.getBuffer(),
requiresOSSAModules);
return VI.status == serialization::Status::Valid;
}

Expand Down Expand Up @@ -497,7 +499,8 @@ class ModuleInterfaceLoaderImpl {

LLVM_DEBUG(llvm::dbgs() << "Validating deps of " << path << "\n");
auto validationInfo = serialization::validateSerializedAST(
buf.getBuffer(), /*ExtendedValidationInfo=*/nullptr, &allDeps);
buf.getBuffer(), requiresOSSAModules,
/*ExtendedValidationInfo=*/nullptr, &allDeps);

if (validationInfo.status != serialization::Status::Valid) {
rebuildInfo.setSerializationStatus(path, validationInfo.status);
Expand Down Expand Up @@ -538,7 +541,7 @@ class ModuleInterfaceLoaderImpl {

// First, make sure the underlying module path exists and is valid.
auto modBuf = fs.getBufferForFile(fwd.underlyingModulePath);
if (!modBuf || !serializedASTLooksValid(*modBuf.get()))
if (!modBuf || !serializedASTLooksValid(*modBuf.get(), requiresOSSAModules))
return false;

// Next, check the dependencies in the forwarding file.
Expand Down Expand Up @@ -646,12 +649,6 @@ class ModuleInterfaceLoaderImpl {
}

std::pair<std::string, std::string> getCompiledModuleCandidates() {
// If we require ossa modules, then we /always/ rebuild the module interface
// regardless of the module loading mode.
if (requiresOSSAModules) {
return {};
}

std::pair<std::string, std::string> result;
// Keep track of whether we should attempt to load a .swiftmodule adjacent
// to the .swiftinterface.
Expand Down
3 changes: 2 additions & 1 deletion lib/IDE/CompletionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@ bool CompletionInstance::performCachedOperationIfPossible(

LangOptions langOpts = CI.getASTContext().LangOpts;
TypeCheckerOptions typeckOpts = CI.getASTContext().TypeCheckerOpts;
SILOptions silOpts = CI.getASTContext().SILOpts;
SearchPathOptions searchPathOpts = CI.getASTContext().SearchPathOpts;
DiagnosticEngine tmpDiags(tmpSM);
ClangImporterOptions clangOpts;
symbolgraphgen::SymbolGraphOptions symbolOpts;
std::unique_ptr<ASTContext> tmpCtx(
ASTContext::get(langOpts, typeckOpts, searchPathOpts, clangOpts,
ASTContext::get(langOpts, typeckOpts, silOpts, searchPathOpts, clangOpts,
symbolOpts, tmpSM, tmpDiags));
registerParseRequestFunctions(tmpCtx->evaluator);
registerIDERequestFunctions(tmpCtx->evaluator);
Expand Down
6 changes: 2 additions & 4 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1522,10 +1522,8 @@ void Parser::parseAllAvailabilityMacroArguments() {
// Create temporary parser.
int bufferID = SM.addMemBufferCopy(macro,
"-define-availability argument");
swift::ParserUnit PU(SM,
SourceFileKind::Main, bufferID,
LangOpts,
TypeCheckerOptions(), "unknown");
swift::ParserUnit PU(SM, SourceFileKind::Main, bufferID, LangOpts,
TypeCheckerOptions(), SILOptions(), "unknown");

ForwardingDiagnosticConsumer PDC(Context.Diags);
PU.getDiagnosticEngine().addConsumer(PDC);
Expand Down
Loading