[ScanDependency] Move binary module validation into scanner

Improve swift dependency scanner by validating and selecting dependency
module into scanner. This provides benefits that:
* Build system does not need to schedule interface compilation task if
  the candidate module is picked, it can just use the candidate module
  directly.
* There is no need for forwarding module in the explicit module build.
  Since the build system is coordinating the build, there is no need for
  the forwarding module in the module cache to avoid duplicated work,
* This also correctly supports all the module loading modes in the
  dependency scanner.

This is achieved by only adding validate and up-to-date binary module as
the candidate module for swift interface module dependency. This allows
caching build to construct the correct dependency in the CAS. If there
is a candidate module for the interface module, dependency scanner will
return a binary module dependency in the dependency graph.

The legacy behavior is mostly preserved with a hidden frontend flag
`-no-scanner-module-validation`, while the scanner output is mostly
interchangeable with new scanner behavior with `prefer-interface` module
loading mode except the candidate module will not be returned.

rdar://123711823
This commit is contained in:
Steven Wu
2024-02-28 16:35:22 -08:00
parent ac1ce06e2b
commit 0e12f2042e
54 changed files with 282 additions and 190 deletions

View File

@@ -1372,7 +1372,8 @@ std::error_code ModuleInterfaceLoader::findModuleFilesInDirectory(
}
std::vector<std::string>
ModuleInterfaceCheckerImpl::getCompiledModuleCandidatesForInterface(StringRef moduleName, StringRef interfacePath) {
ModuleInterfaceCheckerImpl::getCompiledModuleCandidatesForInterface(
StringRef moduleName, StringRef interfacePath) {
// Derive .swiftmodule path from the .swiftinterface path.
auto interfaceExt = file_types::getExtension(file_types::TY_SwiftModuleInterfaceFile);
auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
@@ -1392,17 +1393,32 @@ ModuleInterfaceCheckerImpl::getCompiledModuleCandidatesForInterface(StringRef mo
ModuleInterfaceLoaderImpl Impl(Ctx, modulePath, interfacePath, moduleName,
CacheDir, PrebuiltCacheDir, BackupInterfaceDir,
SourceLoc(), Opts,
RequiresOSSAModules,
nullptr,
ModuleLoadingMode::PreferSerialized);
SourceLoc(), Opts, RequiresOSSAModules,
nullptr, Ctx.SearchPathOpts.ModuleLoadMode);
std::vector<std::string> results;
auto pair = Impl.getCompiledModuleCandidates();
// Add compiled module candidates only when they are non-empty.
if (!pair.first.empty())
results.push_back(pair.first);
if (!pair.second.empty())
results.push_back(pair.second);
std::string adjacentMod, prebuiltMod;
std::tie(adjacentMod, prebuiltMod) = Impl.getCompiledModuleCandidates();
auto validateModule = [&](StringRef modulePath) {
// Legacy behavior do not validate module.
if (Ctx.SearchPathOpts.NoScannerModuleValidation)
return true;
// If we picked the other module already, no need to validate this one since
// it should not be used anyway.
if (!results.empty())
return false;
SmallVector<FileDependency, 16> deps;
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
return Impl.upToDateChecker.swiftModuleIsUpToDate(
modulePath, Impl.rebuildInfo, deps, moduleBuffer);
};
// Add compiled module candidates only when they are non-empty and up-to-date.
if (!adjacentMod.empty() && validateModule(adjacentMod))
results.push_back(adjacentMod);
if (!prebuiltMod.empty() && validateModule(prebuiltMod))
results.push_back(prebuiltMod);
return results;
}
@@ -1862,6 +1878,12 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
genericSubInvocation.getSearchPathOptions().PluginSearchOpts =
searchPathOpts.PluginSearchOpts;
// Get module loading behavior options.
genericSubInvocation.getSearchPathOptions().NoScannerModuleValidation =
searchPathOpts.NoScannerModuleValidation;
genericSubInvocation.getSearchPathOptions().ModuleLoadMode =
searchPathOpts.ModuleLoadMode;
auto &subClangImporterOpts = genericSubInvocation.getClangImporterOptions();
// Respect the detailed-record preprocessor setting of the parent context.
// This, and the "raw" clang module format it implicitly enables, are