Skip to content

Commit 2800768

Browse files
committed
[Frontend] Intro global control to force loading from swiftinterface
Intro ASTContext::setIgnoreAdjacentModules to change module loading to accept load only resilient modules from their swiftinterfaces, ignoring the adjacent module and any silencing swiftinterfaces errors.
1 parent 41ec454 commit 2800768

File tree

7 files changed

+45
-10
lines changed

7 files changed

+45
-10
lines changed

include/swift/AST/ASTContext.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ class ASTContext final {
307307
/// The name of the SwiftShims module "SwiftShims".
308308
Identifier SwiftShimsModuleName;
309309

310+
/// Should we globally ignore swiftmodule files adjacent to swiftinterface
311+
/// files?
312+
bool IgnoreAdjacentModules = false;
313+
310314
// Define the set of known identifiers.
311315
#define IDENTIFIER_WITH_NAME(Name, IdStr) Identifier Id_##Name;
312316
#include "swift/AST/KnownIdentifiers.def"
@@ -1165,7 +1169,10 @@ class ASTContext final {
11651169
/// in this context.
11661170
void addLoadedModule(ModuleDecl *M);
11671171

1168-
public:
1172+
/// Change the behavior of all loaders to ignore swiftmodules next to
1173+
/// swiftinterfaces and clear the in-memory cache on a change of value.
1174+
void setIgnoreAdjacentModules(bool value);
1175+
11691176
/// Retrieve the current generation number, which reflects the
11701177
/// number of times a module import has caused mass invalidation of
11711178
/// lookup tables.

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
439439
StringRef OutPath, StringRef ABIOutputPath,
440440
bool SerializeDependencyHashes,
441441
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions Opts,
442-
RequireOSSAModules_t RequireOSSAModules);
442+
RequireOSSAModules_t RequireOSSAModules,
443+
bool silenceInterfaceDiagnostics);
443444

444445
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
445446
/// a swiftmodule file).

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,14 @@ void ASTContext::addLoadedModule(ModuleDecl *M) {
21912191
getImpl().LoadedModules[M->getRealName()] = M;
21922192
}
21932193

2194+
void ASTContext::setIgnoreAdjacentModules(bool value) {
2195+
// Clear the cache if we expect a different view on the same modules.
2196+
if (IgnoreAdjacentModules != value)
2197+
getImpl().LoadedModules.clear();
2198+
2199+
IgnoreAdjacentModules = value;
2200+
}
2201+
21942202
rewriting::RewriteContext &
21952203
ASTContext::getRewriteContext() {
21962204
auto &rewriteCtx = getImpl().TheRewriteContext;

lib/Frontend/ModuleInterfaceBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ImplicitModuleInterfaceBuilder {
4747
const StringRef backupInterfaceDir;
4848
const StringRef ABIDescriptorPath;
4949
const bool disableInterfaceFileLock;
50+
const bool silenceInterfaceDiagnostics;
5051
const SourceLoc diagnosticLoc;
5152
DependencyTracker *const dependencyTracker;
5253
SmallVector<StringRef, 3> extraDependencies;
@@ -87,6 +88,7 @@ class ImplicitModuleInterfaceBuilder {
8788
StringRef moduleName, StringRef moduleCachePath,
8889
StringRef backupInterfaceDir, StringRef prebuiltCachePath,
8990
StringRef ABIDescriptorPath, bool disableInterfaceFileLock = false,
91+
bool silenceInterfaceDiagnostics = false,
9092
SourceLoc diagnosticLoc = SourceLoc(),
9193
DependencyTracker *tracker = nullptr)
9294
: sourceMgr(sourceMgr), diags(diags), subASTDelegate(subASTDelegate),
@@ -95,6 +97,7 @@ class ImplicitModuleInterfaceBuilder {
9597
backupInterfaceDir(backupInterfaceDir),
9698
ABIDescriptorPath(ABIDescriptorPath),
9799
disableInterfaceFileLock(disableInterfaceFileLock),
100+
silenceInterfaceDiagnostics(silenceInterfaceDiagnostics),
98101
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {}
99102

100103
/// Ensures the requested file name is added as a dependency of the resulting

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ class ModuleInterfaceLoaderImpl {
677677
std::pair<std::string, std::string> getCompiledModuleCandidates() {
678678
std::pair<std::string, std::string> result;
679679
// Should we attempt to load a swiftmodule adjacent to the swiftinterface?
680-
bool shouldLoadAdjacentModule = true;
680+
bool shouldLoadAdjacentModule = !ctx.IgnoreAdjacentModules;
681681

682682
// Don't use the adjacent swiftmodule for frameworks from the public
683683
// Frameworks folder of the SDK.
@@ -1031,7 +1031,8 @@ class ModuleInterfaceLoaderImpl {
10311031
ctx.SourceMgr, diagsToUse,
10321032
astDelegate, interfacePath, moduleName, cacheDir,
10331033
prebuiltCacheDir, backupInterfaceDir, StringRef(),
1034-
Opts.disableInterfaceLock, diagnosticLoc,
1034+
Opts.disableInterfaceLock,
1035+
ctx.IgnoreAdjacentModules, diagnosticLoc,
10351036
dependencyTracker);
10361037
// If we found an out-of-date .swiftmodule, we still want to add it as
10371038
// a dependency of the .swiftinterface. That way if it's updated, but
@@ -1063,7 +1064,8 @@ class ModuleInterfaceLoaderImpl {
10631064
ImplicitModuleInterfaceBuilder fallbackBuilder(
10641065
ctx.SourceMgr, &ctx.Diags, astDelegate, backupPath, moduleName, cacheDir,
10651066
prebuiltCacheDir, backupInterfaceDir, StringRef(),
1066-
Opts.disableInterfaceLock, diagnosticLoc,
1067+
Opts.disableInterfaceLock,
1068+
ctx.IgnoreAdjacentModules, diagnosticLoc,
10671069
dependencyTracker);
10681070
if (rebuildInfo.sawOutOfDateModule(modulePath))
10691071
fallbackBuilder.addExtraDependency(modulePath);
@@ -1247,7 +1249,8 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
12471249
StringRef OutPath, StringRef ABIOutputPath,
12481250
bool SerializeDependencyHashes,
12491251
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions LoaderOpts,
1250-
RequireOSSAModules_t RequireOSSAModules) {
1252+
RequireOSSAModules_t RequireOSSAModules,
1253+
bool silenceInterfaceDiagnostics) {
12511254
InterfaceSubContextDelegateImpl astDelegate(
12521255
SourceMgr, &Diags, SearchPathOpts, LangOpts, ClangOpts, LoaderOpts,
12531256
/*CreateCacheDirIfAbsent*/ true, CacheDir, PrebuiltCacheDir,
@@ -1256,7 +1259,8 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
12561259
ImplicitModuleInterfaceBuilder builder(SourceMgr, &Diags, astDelegate, InPath,
12571260
ModuleName, CacheDir, PrebuiltCacheDir,
12581261
BackupInterfaceDir, ABIOutputPath,
1259-
LoaderOpts.disableInterfaceLock);
1262+
LoaderOpts.disableInterfaceLock,
1263+
silenceInterfaceDiagnostics);
12601264
// FIXME: We really only want to serialize 'important' dependencies here, if
12611265
// we want to ship the built swiftmodules to another machine.
12621266
auto failed = builder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
@@ -1274,7 +1278,8 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
12741278
ImplicitModuleInterfaceBuilder backupBuilder(SourceMgr, &Diags, astDelegate, backInPath,
12751279
ModuleName, CacheDir, PrebuiltCacheDir,
12761280
BackupInterfaceDir, ABIOutputPath,
1277-
LoaderOpts.disableInterfaceLock);
1281+
LoaderOpts.disableInterfaceLock,
1282+
silenceInterfaceDiagnostics);
12781283
// Ensure we can rebuild module after user changed the original interface file.
12791284
backupBuilder.addExtraDependency(InPath);
12801285
// FIXME: We really only want to serialize 'important' dependencies here, if

lib/FrontendTool/FrontendTool.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
424424
StringRef ABIPath = Instance.getPrimarySpecificPathsForAtMostOnePrimary()
425425
.SupplementaryOutputs.ABIDescriptorOutputPath;
426426

427+
bool IgnoreAdjacentModules = false;
428+
if (Invocation.getFrontendOptions().RequestedAction !=
429+
FrontendOptions::ActionType::CompileModuleFromInterface &&
430+
Invocation.getFrontendOptions().RequestedAction !=
431+
FrontendOptions::ActionType::TypecheckModuleFromInterface &&
432+
!Invocation.getFrontendOptions().ExplicitInterfaceBuild) {
433+
IgnoreAdjacentModules = Instance.getASTContext().IgnoreAdjacentModules;
434+
}
435+
427436
// If an explicit interface build was requested, bypass the creation of a new
428437
// sub-instance from the interface which will build it in a separate thread,
429438
// and isntead directly use the current \c Instance for compilation.
@@ -444,7 +453,8 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
444453
Invocation.getOutputFilename(), ABIPath,
445454
FEOpts.SerializeModuleInterfaceDependencyHashes,
446455
FEOpts.shouldTrackSystemDependencies(), LoaderOpts,
447-
RequireOSSAModules_t(Invocation.getSILOptions()));
456+
RequireOSSAModules_t(Invocation.getSILOptions()),
457+
IgnoreAdjacentModules);
448458
}
449459

450460
static bool compileLLVMIR(CompilerInstance &Instance) {

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ std::error_code ImplicitSerializedModuleLoader::findModuleFilesInDirectory(
444444
(!ModuleBuffer && !ModuleDocBuffer)) &&
445445
"Module and Module Doc buffer must both be initialized or NULL");
446446

447-
if (LoadMode == ModuleLoadingMode::OnlyInterface)
447+
if (LoadMode == ModuleLoadingMode::OnlyInterface ||
448+
Ctx.IgnoreAdjacentModules)
448449
return std::make_error_code(std::errc::not_supported);
449450

450451
auto ModuleErr = openModuleFile(ModuleID, BaseName, ModuleBuffer);

0 commit comments

Comments
 (0)