Skip to content

Commit 45198f0

Browse files
Merge pull request #73064 from cachemeifyoucan/eng/PR-swift-caching-6.0-update
[6.0][Caching] Add swift caching changes to swift 6.0
2 parents a424d9b + 9d9d3e2 commit 45198f0

File tree

68 files changed

+868
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+868
-269
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ WARNING(could_not_rewrite_bridging_header,none,
4242
ERROR(bridging_header_pch_error,Fatal,
4343
"failed to emit precompiled header '%0' for bridging header '%1'",
4444
(StringRef, StringRef))
45+
ERROR(err_rewrite_bridging_header,none,
46+
"failed to serialize bridging header: '%0'", (StringRef))
4547

4648
ERROR(emit_pcm_error,Fatal,
4749
"failed to emit precompiled module '%0' for module map '%1'",

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ REMARK(module_api_import_aliases,none,
11931193
"%select{, which reexports definition from %2|}3",
11941194
(const Decl *, ModuleDecl *, ModuleDecl *, bool))
11951195

1196+
REMARK(skip_module_invalid,none,"skip invalid swiftmodule: %0", (StringRef))
1197+
REMARK(skip_module_testable,none,"skip swiftmodule built with '-enable-testing': %0", (StringRef))
1198+
11961199
REMARK(macro_loaded,none,
11971200
"loaded macro implementation module %0 from "
11981201
"%select{shared library '%2'|executable '%2'|"

include/swift/AST/ModuleDependencies.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,12 @@ class ModuleDependenciesCache {
10971097
std::optional<const ModuleDependencyInfo *>
10981098
findDependency(StringRef moduleName) const;
10991099

1100+
/// Look for known existing dependencies.
1101+
///
1102+
/// \returns the cached result.
1103+
const ModuleDependencyInfo &
1104+
findKnownDependency(const ModuleDependencyID &moduleID) const;
1105+
11001106
/// Record dependencies for the given module.
11011107
void recordDependency(StringRef moduleName,
11021108
ModuleDependencyInfo dependencies);

include/swift/AST/SearchPathOptions.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ enum class ModuleSearchPathKind {
3838
RuntimeLibrary,
3939
};
4040

41+
/// Specifies how to load modules when both a module interface and serialized
42+
/// AST are present, or whether to disallow one format or the other altogether.
43+
enum class ModuleLoadingMode {
44+
PreferInterface,
45+
PreferSerialized,
46+
OnlyInterface,
47+
OnlySerialized
48+
};
49+
4150
/// A single module search path that can come from different sources, e.g.
4251
/// framework search paths, import search path etc.
4352
class ModuleSearchPath : public llvm::RefCountedBase<ModuleSearchPath> {
@@ -499,6 +508,12 @@ class SearchPathOptions {
499508
/// original form.
500509
PathObfuscator DeserializedPathRecoverer;
501510

511+
/// Specify the module loading behavior of the compilation.
512+
ModuleLoadingMode ModuleLoadMode = ModuleLoadingMode::PreferSerialized;
513+
514+
/// Legacy scanner search behavior.
515+
bool NoScannerModuleValidation = false;
516+
502517
/// Return all module search paths that (non-recursively) contain a file whose
503518
/// name is in \p Filenames.
504519
SmallVector<const ModuleSearchPath *, 4>
@@ -546,7 +561,9 @@ class SearchPathOptions {
546561
RuntimeResourcePath,
547562
hash_combine_range(RuntimeLibraryImportPaths.begin(),
548563
RuntimeLibraryImportPaths.end()),
549-
DisableModulesValidateSystemDependencies);
564+
DisableModulesValidateSystemDependencies,
565+
NoScannerModuleValidation,
566+
ModuleLoadMode);
550567
}
551568

552569
/// Return a hash code of any components from these options that should

include/swift/ClangImporter/ClangImporter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,10 @@ class ClangImporter final : public ClangModuleLoader {
402402
getWrapperForModule(const clang::Module *mod,
403403
bool returnOverlayIfPossible = false) const override;
404404

405-
std::string getBridgingHeaderContents(StringRef headerPath, off_t &fileSize,
406-
time_t &fileModTime);
405+
std::string
406+
getBridgingHeaderContents(StringRef headerPath, off_t &fileSize,
407+
time_t &fileModTime,
408+
StringRef pchIncludeTree);
407409

408410
/// Makes a temporary replica of the ClangImporter's CompilerInstance, reads
409411
/// an Objective-C header file into the replica and emits a PCH file of its

include/swift/Option/FrontendOptions.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,4 +1337,10 @@ def disable_strict_concurrency_region_based_isolation : Flag<["-"],
13371337
HelpText<"Disable region based isolation when running with strict concurrency enabled. Only enabled with asserts">,
13381338
Flags<[HelpHidden]>;
13391339

1340+
def no_scanner_module_validation: Flag<["-"], "no-scanner-module-validation">,
1341+
HelpText<"Do not validate binary modules in scanner and delegate the validation to swift-frontend">;
1342+
def module_load_mode: Separate<["-"], "module-load-mode">,
1343+
MetaVarName<"only-interface|prefer-interface|prefer-serialized|only-serialized">,
1344+
HelpText<"Module loading mode">;
1345+
13401346
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Serialization/ScanningLoaders.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
3434

3535
/// Scan the given interface file to determine dependencies.
3636
llvm::ErrorOr<ModuleDependencyInfo>
37-
scanInterfaceFile(Twine moduleInterfacePath, bool isFramework);
37+
scanInterfaceFile(Twine moduleInterfacePath, bool isFramework,
38+
bool isTestableImport);
3839

3940
InterfaceSubContextDelegate &astDelegate;
4041

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,20 @@
1616
#include "swift/AST/FileUnit.h"
1717
#include "swift/AST/Module.h"
1818
#include "swift/AST/ModuleLoader.h"
19+
#include "swift/AST/SearchPathOptions.h"
1920
#include "llvm/Support/MemoryBuffer.h"
2021
#include "llvm/Support/PrefixMapper.h"
2122
#include "llvm/TargetParser/Triple.h"
2223

2324
namespace swift {
2425
class ModuleFile;
2526
class PathObfuscator;
27+
class ModuleFileSharedCore;
2628
enum class ModuleLoadingBehavior;
2729
namespace file_types {
2830
enum ID : uint8_t;
2931
}
3032

31-
/// Specifies how to load modules when both a module interface and serialized
32-
/// AST are present, or whether to disallow one format or the other altogether.
33-
enum class ModuleLoadingMode {
34-
PreferInterface,
35-
PreferSerialized,
36-
OnlyInterface,
37-
OnlySerialized
38-
};
39-
4033
/// How a dependency should be loaded.
4134
///
4235
/// \sa getTransitiveLoadingBehavior
@@ -170,22 +163,18 @@ class SerializedModuleLoaderBase : public ModuleLoader {
170163
}
171164

172165
/// Scan the given serialized module file to determine dependencies.
173-
llvm::ErrorOr<ModuleDependencyInfo> scanModuleFile(Twine modulePath, bool isFramework);
166+
llvm::ErrorOr<ModuleDependencyInfo>
167+
scanModuleFile(Twine modulePath, bool isFramework, bool isTestableImport);
174168

175169
struct BinaryModuleImports {
176170
llvm::StringSet<> moduleImports;
177171
std::string headerImport;
178172
};
179173

180-
static llvm::ErrorOr<BinaryModuleImports>
181-
getImportsOfModule(Twine modulePath,
174+
static BinaryModuleImports
175+
getImportsOfModule(const ModuleFileSharedCore &loadedModule,
182176
ModuleLoadingBehavior transitiveBehavior,
183-
bool isFramework,
184-
bool isRequiredOSSAModules,
185-
StringRef SDKName,
186-
StringRef packageName,
187-
llvm::vfs::FileSystem *fileSystem,
188-
PathObfuscator &recoverer);
177+
StringRef packageName, bool isTestableImport);
189178

190179
/// Load the module file into a buffer and also collect its module name.
191180
static std::unique_ptr<llvm::MemoryBuffer>

lib/AST/ModuleDependencies.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Frontend/Frontend.h"
2222
#include "llvm/CAS/CASProvidingFileSystem.h"
2323
#include "llvm/CAS/CachingOnDiskFileSystem.h"
24+
#include "llvm/Config/config.h"
2425
#include "llvm/Support/FileSystem.h"
2526
#include "llvm/Support/Path.h"
2627
#include "llvm/Support/PrefixMapper.h"
@@ -480,6 +481,58 @@ void SwiftDependencyTracker::addCommonSearchPathDeps(
480481
// Add VFSOverlay file.
481482
for (auto &Overlay: Opts.VFSOverlayFiles)
482483
FS->status(Overlay);
484+
485+
// Add plugin dylibs from the toolchain only by look through the plugin search
486+
// directory.
487+
auto recordFiles = [&](StringRef Path) {
488+
std::error_code EC;
489+
for (auto I = FS->dir_begin(Path, EC);
490+
!EC && I != llvm::vfs::directory_iterator(); I = I.increment(EC)) {
491+
if (I->type() != llvm::sys::fs::file_type::regular_file)
492+
continue;
493+
#if defined(_WIN32)
494+
constexpr StringRef libPrefix{};
495+
constexpr StringRef libSuffix = ".dll";
496+
#else
497+
constexpr StringRef libPrefix = "lib";
498+
constexpr StringRef libSuffix = LTDL_SHLIB_EXT;
499+
#endif
500+
StringRef filename = llvm::sys::path::filename(I->path());
501+
if (filename.starts_with(libPrefix) && filename.ends_with(libSuffix))
502+
FS->status(I->path());
503+
}
504+
};
505+
for (auto &entry : Opts.PluginSearchOpts) {
506+
switch (entry.getKind()) {
507+
508+
// '-load-plugin-library <library path>'.
509+
case PluginSearchOption::Kind::LoadPluginLibrary: {
510+
auto &val = entry.get<PluginSearchOption::LoadPluginLibrary>();
511+
FS->status(val.LibraryPath);
512+
break;
513+
}
514+
515+
// '-load-plugin-executable <executable path>#<module name>, ...'.
516+
case PluginSearchOption::Kind::LoadPluginExecutable: {
517+
// We don't have executable plugin in toolchain.
518+
break;
519+
}
520+
521+
// '-plugin-path <library search path>'.
522+
case PluginSearchOption::Kind::PluginPath: {
523+
auto &val = entry.get<PluginSearchOption::PluginPath>();
524+
recordFiles(val.SearchPath);
525+
break;
526+
}
527+
528+
// '-external-plugin-path <library search path>#<server path>'.
529+
case PluginSearchOption::Kind::ExternalPluginPath: {
530+
auto &val = entry.get<PluginSearchOption::ExternalPluginPath>();
531+
recordFiles(val.SearchPath);
532+
break;
533+
}
534+
}
535+
}
483536
}
484537

485538
void SwiftDependencyTracker::startTracking() {
@@ -739,6 +792,13 @@ ModuleDependenciesCache::findDependency(StringRef moduleName) const {
739792
return std::nullopt;
740793
}
741794

795+
const ModuleDependencyInfo &ModuleDependenciesCache::findKnownDependency(
796+
const ModuleDependencyID &moduleID) const {
797+
auto dep = findDependency(moduleID);
798+
assert(dep && "dependency unknown");
799+
return **dep;
800+
}
801+
742802
bool ModuleDependenciesCache::hasDependency(const ModuleDependencyID &moduleID) const {
743803
return hasDependency(moduleID.ModuleName, moduleID.Kind);
744804
}

lib/AST/PluginLoader.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/Basic/SourceManager.h"
1818
#include "swift/Parse/Lexer.h"
1919
#include "llvm/Config/config.h"
20+
#include "llvm/Support/VirtualFileSystem.h"
2021

2122
using namespace swift;
2223

@@ -59,6 +60,15 @@ static StringRef pluginModuleNameStringFromPath(StringRef path) {
5960
return "";
6061
}
6162

63+
static llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
64+
getPluginLoadingFS(ASTContext &Ctx) {
65+
// If there is a clang include tree FS, using real file system to load plugin
66+
// as the FS in SourceMgr doesn't support directory iterator.
67+
if (Ctx.ClangImporterOpts.HasClangIncludeTreeRoot)
68+
return llvm::vfs::getRealFileSystem();
69+
return Ctx.SourceMgr.getFileSystem();
70+
}
71+
6272
llvm::DenseMap<Identifier, PluginLoader::PluginEntry> &
6373
PluginLoader::getPluginMap() {
6474
if (PluginMap.has_value()) {
@@ -86,7 +96,7 @@ PluginLoader::getPluginMap() {
8696
(void)result;
8797
};
8898

89-
auto fs = Ctx.SourceMgr.getFileSystem();
99+
auto fs = getPluginLoadingFS(Ctx);
90100
std::error_code ec;
91101

92102
for (auto &entry : Ctx.SearchPathOpts.PluginSearchOpts) {
@@ -162,7 +172,7 @@ PluginLoader::lookupPluginByModuleName(Identifier moduleName) {
162172

163173
llvm::Expected<LoadedLibraryPlugin *>
164174
PluginLoader::loadLibraryPlugin(StringRef path) {
165-
auto fs = Ctx.SourceMgr.getFileSystem();
175+
auto fs = getPluginLoadingFS(Ctx);
166176
SmallString<128> resolvedPath;
167177
if (auto err = fs->getRealPath(path, resolvedPath)) {
168178
return llvm::createStringError(err, err.message());
@@ -186,7 +196,7 @@ PluginLoader::loadLibraryPlugin(StringRef path) {
186196

187197
llvm::Expected<LoadedExecutablePlugin *>
188198
PluginLoader::loadExecutablePlugin(StringRef path) {
189-
auto fs = Ctx.SourceMgr.getFileSystem();
199+
auto fs = getPluginLoadingFS(Ctx);
190200
SmallString<128> resolvedPath;
191201
if (auto err = fs->getRealPath(path, resolvedPath)) {
192202
return llvm::createStringError(err, err.message());

0 commit comments

Comments
 (0)