Skip to content

Commit 129f39d

Browse files
authored
Merge pull request #21513 from jrose-apple/cachet
[ParseableInterface] Pass prebuilt cache path down to sub-invocations And several other fixes that got folded into this PR.
2 parents aea1ef8 + c88c8b5 commit 129f39d

17 files changed

+346
-113
lines changed

include/swift/Basic/STLExtras.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,30 @@ inline T accumulate(const Container &C, T init, BinaryOperation op) {
792792
return std::accumulate(C.begin(), C.end(), init, op);
793793
}
794794

795+
/// Returns true if the range defined by \p mainBegin ..< \p mainEnd starts with
796+
/// the same elements as the range defined by \p prefixBegin ..< \p prefixEnd.
797+
///
798+
/// This includes cases where the prefix range is empty, as well as when the two
799+
/// ranges are the same length and contain the same elements.
800+
template <typename MainInputIterator, typename PrefixInputIterator>
801+
inline bool hasPrefix(MainInputIterator mainBegin,
802+
const MainInputIterator mainEnd,
803+
PrefixInputIterator prefixBegin,
804+
const PrefixInputIterator prefixEnd) {
805+
while (prefixBegin != prefixEnd) {
806+
// If "main" is shorter than "prefix", it does not start with "prefix".
807+
if (mainBegin == mainEnd)
808+
return false;
809+
// If there's a mismatch, "main" does not start with "prefix".
810+
if (*mainBegin != *prefixBegin)
811+
return false;
812+
++prefixBegin;
813+
++mainBegin;
814+
}
815+
// If we checked every element of "prefix", "main" does start with "prefix".
816+
return true;
817+
}
818+
795819
/// Provides default implementations of !=, <=, >, and >= based on == and <.
796820
template <typename T>
797821
class RelationalOperationsBase {

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class FrontendOptions {
7272
/// The path to which we should store indexing data, if any.
7373
std::string IndexStorePath;
7474

75+
/// The path to look in when loading a parseable interface file, to see if a
76+
/// binary module has already been built for use by the compiler.
77+
std::string PrebuiltModuleCachePath;
78+
7579
/// Emit index data for imported serialized swift system modules.
7680
bool IndexSystemModules = false;
7781

include/swift/Frontend/ParseableInterfaceSupport.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ getModuleCachePathFromClang(const clang::CompilerInstance &Instance);
6161
/// directory, and loading the serialized .swiftmodules from there.
6262
class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
6363
explicit ParseableInterfaceModuleLoader(ASTContext &ctx, StringRef cacheDir,
64+
StringRef prebuiltCacheDir,
6465
DependencyTracker *tracker,
6566
ModuleLoadingMode loadMode)
6667
: SerializedModuleLoaderBase(ctx, tracker, loadMode),
67-
CacheDir(cacheDir)
68+
CacheDir(cacheDir), PrebuiltCacheDir(prebuiltCacheDir)
6869
{}
6970

7071
std::string CacheDir;
72+
std::string PrebuiltCacheDir;
7173

7274
/// Wire up the SubInvocation's InputsAndOutputs to contain both input and
7375
/// output filenames.
@@ -77,20 +79,25 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
7779
static void configureSubInvocationInputsAndOutputs(
7880
CompilerInvocation &SubInvocation, StringRef InPath, StringRef OutPath);
7981

80-
std::error_code
81-
openModuleFiles(AccessPathElem ModuleID, StringRef DirName,
82-
StringRef ModuleFilename, StringRef ModuleDocFilename,
83-
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
84-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
85-
llvm::SmallVectorImpl<char> &Scratch) override;
82+
static bool buildSwiftModuleFromSwiftInterface(
83+
clang::vfs::FileSystem &FS, DiagnosticEngine &Diags, SourceLoc DiagLoc,
84+
CompilerInvocation &SubInvocation, StringRef InPath, StringRef OutPath,
85+
StringRef ModuleCachePath, DependencyTracker *OuterTracker,
86+
bool ShouldSerializeDeps);
87+
88+
std::error_code findModuleFilesInDirectory(
89+
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
90+
StringRef ModuleDocFilename,
91+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
92+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
8693

8794
public:
8895
static std::unique_ptr<ParseableInterfaceModuleLoader>
89-
create(ASTContext &ctx, StringRef cacheDir,
90-
DependencyTracker *tracker,
91-
ModuleLoadingMode loadMode) {
96+
create(ASTContext &ctx, StringRef cacheDir, StringRef prebuiltCacheDir,
97+
DependencyTracker *tracker, ModuleLoadingMode loadMode) {
9298
return std::unique_ptr<ParseableInterfaceModuleLoader>(
93-
new ParseableInterfaceModuleLoader(ctx, cacheDir, tracker, loadMode));
99+
new ParseableInterfaceModuleLoader(ctx, cacheDir, prebuiltCacheDir,
100+
tracker, loadMode));
94101
}
95102

96103
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
@@ -100,6 +107,7 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
100107
/// testing purposes.
101108
static bool buildSwiftModuleFromSwiftInterface(ASTContext &Ctx,
102109
StringRef CacheDir,
110+
StringRef PrebuiltCacheDir,
103111
StringRef ModuleName,
104112
StringRef InPath,
105113
StringRef OutPath);

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ def build_module_from_parseable_interface :
515515
HelpText<"Treat the (single) input as a swiftinterface and produce a module">,
516516
ModeOpt;
517517

518+
def prebuilt_module_cache_path :
519+
Separate<["-"], "prebuilt-module-cache-path">,
520+
HelpText<"Directory of prebuilt modules for loading parseable interfaces">;
521+
def prebuilt_module_cache_path_EQ :
522+
Joined<["-"], "prebuilt-module-cache-path=">,
523+
Alias<prebuilt_module_cache_path>;
524+
518525
def enable_resilience_bypass : Flag<["-"], "enable-resilience-bypass">,
519526
HelpText<"Completely bypass resilience when accessing types in resilient frameworks">;
520527

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ class SerializedModuleLoaderBase : public ModuleLoader {
5151
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
5252
bool &isFramework);
5353

54-
virtual std::error_code
55-
openModuleFiles(AccessPathElem ModuleID, StringRef DirName,
56-
StringRef ModuleFilename, StringRef ModuleDocFilename,
54+
virtual std::error_code findModuleFilesInDirectory(
55+
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
56+
StringRef ModuleDocFilename,
57+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
58+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) = 0;
59+
60+
std::error_code
61+
openModuleFiles(AccessPathElem ModuleID,
62+
StringRef ModulePath, StringRef ModuleDocPath,
5763
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
58-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
59-
llvm::SmallVectorImpl<char> &Scratch);
64+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
6065

6166
/// If the module loader subclass knows that all options have been tried for
6267
/// loading an architecture-specific file out of a swiftmodule bundle, try
@@ -128,12 +133,11 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
128133
: SerializedModuleLoaderBase(ctx, tracker, loadMode)
129134
{}
130135

131-
std::error_code
132-
openModuleFiles(AccessPathElem ModuleID, StringRef DirName,
133-
StringRef ModuleFilename, StringRef ModuleDocFilename,
134-
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
135-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
136-
llvm::SmallVectorImpl<char> &Scratch) override;
136+
std::error_code findModuleFilesInDirectory(
137+
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
138+
StringRef ModuleDocFilename,
139+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
140+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
137141

138142
bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
139143
StringRef moduleName,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ bool ArgsToFrontendOptionsConverter::convert(
6060
if (const Arg *A = Args.getLastArg(OPT_index_store_path)) {
6161
Opts.IndexStorePath = A->getValue();
6262
}
63+
if (const Arg *A = Args.getLastArg(OPT_prebuilt_module_cache_path)) {
64+
Opts.PrebuiltModuleCachePath = A->getValue();
65+
}
6366

6467
Opts.IndexSystemModules |= Args.hasArg(OPT_index_system_modules);
6568

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ bool CompilerInstance::setUpModuleLoaders() {
331331
if (MLM != ModuleLoadingMode::OnlySerialized) {
332332
auto const &Clang = clangImporter->getClangInstance();
333333
std::string ModuleCachePath = getModuleCachePathFromClang(Clang);
334+
StringRef PrebuiltModuleCachePath =
335+
Invocation.getFrontendOptions().PrebuiltModuleCachePath;
334336
auto PIML = ParseableInterfaceModuleLoader::create(*Context,
335337
ModuleCachePath,
338+
PrebuiltModuleCachePath,
336339
getDependencyTracker(),
337340
MLM);
338341
Context->addModuleLoader(std::move(PIML));

0 commit comments

Comments
 (0)