Merge pull request #71118 from cachemeifyoucan/eng/PR-119275464

[Caching] Create clang importer from cc1 args directly
This commit is contained in:
Steven Wu
2024-03-11 13:50:34 -07:00
committed by GitHub
20 changed files with 430 additions and 163 deletions

View File

@@ -1433,12 +1433,10 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
SearchPathOpts.CandidateCompiledModules);
}
static bool readSwiftInterfaceVersionAndArgs(SourceManager &SM,
DiagnosticEngine &Diags,
llvm::StringSaver &ArgSaver,
SwiftInterfaceInfo &interfaceInfo,
StringRef interfacePath,
SourceLoc diagnosticLoc) {
static bool readSwiftInterfaceVersionAndArgs(
SourceManager &SM, DiagnosticEngine &Diags, llvm::StringSaver &ArgSaver,
SwiftInterfaceInfo &interfaceInfo, StringRef interfacePath,
SourceLoc diagnosticLoc, llvm::Triple preferredTarget) {
llvm::vfs::FileSystem &fs = *SM.getFileSystem();
auto FileOrError = swift::vfs::getFileOrSTDIN(fs, interfacePath);
if (!FileOrError) {
@@ -1461,7 +1459,8 @@ static bool readSwiftInterfaceVersionAndArgs(SourceManager &SM,
}
if (extractCompilerFlagsFromInterface(interfacePath, SB, ArgSaver,
interfaceInfo.Arguments)) {
interfaceInfo.Arguments,
preferredTarget)) {
InterfaceSubContextDelegateImpl::diagnose(
interfacePath, diagnosticLoc, SM, &Diags,
diag::error_extracting_version_from_module_interface);
@@ -1543,9 +1542,10 @@ bool ModuleInterfaceLoader::buildExplicitSwiftModuleFromSwiftInterface(
llvm::BumpPtrAllocator alloc;
llvm::StringSaver ArgSaver(alloc);
SwiftInterfaceInfo InterfaceInfo;
readSwiftInterfaceVersionAndArgs(Instance.getSourceMgr(), Instance.getDiags(),
ArgSaver, InterfaceInfo, interfacePath,
SourceLoc());
readSwiftInterfaceVersionAndArgs(
Instance.getSourceMgr(), Instance.getDiags(), ArgSaver, InterfaceInfo,
interfacePath, SourceLoc(),
Instance.getInvocation().getLangOptions().Target);
auto Builder = ExplicitModuleInterfaceBuilder(
Instance, &Instance.getDiags(), Instance.getSourceMgr(),
@@ -1673,6 +1673,11 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
Feature::LayoutPrespecialization);
}
genericSubInvocation.getClangImporterOptions().DirectClangCC1ModuleBuild =
clangImporterOpts.DirectClangCC1ModuleBuild;
genericSubInvocation.getClangImporterOptions().ClangImporterDirectCC1Scan =
clangImporterOpts.ClangImporterDirectCC1Scan;
// Validate Clang modules once per-build session flags must be consistent
// across all module sub-invocations
if (clangImporterOpts.ValidateModulesOnce) {
@@ -1688,6 +1693,8 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
genericSubInvocation.getCASOptions().CASOpts = casOpts.CASOpts;
casOpts.enumerateCASConfigurationFlags(
[&](StringRef Arg) { GenericArgs.push_back(ArgSaver.save(Arg)); });
// ClangIncludeTree is default on when caching is enabled.
genericSubInvocation.getClangImporterOptions().UseClangIncludeTree = true;
}
if (!clangImporterOpts.UseClangIncludeTree) {
@@ -1700,7 +1707,8 @@ bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
CompilerInvocation &subInvocation, SwiftInterfaceInfo &interfaceInfo,
StringRef interfacePath, SourceLoc diagnosticLoc) {
if (readSwiftInterfaceVersionAndArgs(SM, *Diags, ArgSaver, interfaceInfo,
interfacePath, diagnosticLoc))
interfacePath, diagnosticLoc,
subInvocation.getLangOptions().Target))
return true;
// Prior to Swift 5.9, swiftinterfaces were always built (accidentally) with
@@ -1788,9 +1796,6 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
GenericArgs.push_back("-application-extension");
}
// Save the parent invocation's Target Triple
ParentInvocationTarget = langOpts.Target;
// Pass down -explicit-swift-module-map-file
StringRef explicitSwiftModuleMap = searchPathOpts.ExplicitSwiftModuleMap;
genericSubInvocation.getSearchPathOptions().ExplicitSwiftModuleMap =
@@ -2053,25 +2058,6 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
BuildArgs.insert(BuildArgs.end(), interfaceInfo.Arguments.begin(),
interfaceInfo.Arguments.end());
// If the target triple parsed from the Swift interface file differs
// only in subarchitecture from the original target triple, then
// we have loaded a Swift interface from a different-but-compatible
// architecture slice. Use the original subarchitecture.
llvm::Triple parsedTargetTriple(subInvocation.getTargetTriple());
if (parsedTargetTriple.getSubArch() != originalTargetTriple.getSubArch() &&
parsedTargetTriple.getArch() == originalTargetTriple.getArch() &&
parsedTargetTriple.getVendor() == originalTargetTriple.getVendor() &&
parsedTargetTriple.getOS() == originalTargetTriple.getOS() &&
parsedTargetTriple.getEnvironment()
== originalTargetTriple.getEnvironment()) {
parsedTargetTriple.setArchName(originalTargetTriple.getArchName());
subInvocation.setTargetTriple(parsedTargetTriple.str());
// Overload the target in the BuildArgs as well
BuildArgs.push_back("-target");
BuildArgs.push_back(parsedTargetTriple.str());
}
// restore `StrictImplicitModuleContext`
subInvocation.getFrontendOptions().StrictImplicitModuleContext =
StrictImplicitModuleContext;
@@ -2420,6 +2406,11 @@ struct ExplicitCASModuleLoader::Implementation {
std::set<std::string> moduleMapsSeen;
std::vector<std::string> &extraClangArgs = Ctx.ClangImporterOpts.ExtraArgs;
// Append -Xclang if we are not in direct cc1 mode.
auto appendXclang = [&]() {
if (!Ctx.ClangImporterOpts.DirectClangCC1ModuleBuild)
extraClangArgs.push_back("-Xclang");
};
for (auto &entry : ExplicitClangModuleMap) {
const auto &moduleMapPath = entry.getValue().moduleMapPath;
if (!moduleMapPath.empty() &&
@@ -2438,11 +2429,11 @@ struct ExplicitCASModuleLoader::Implementation {
}
auto cachePath = entry.getValue().moduleCacheKey;
if (cachePath) {
extraClangArgs.push_back("-Xclang");
appendXclang();
extraClangArgs.push_back("-fmodule-file-cache-key");
extraClangArgs.push_back("-Xclang");
appendXclang();
extraClangArgs.push_back(modulePath);
extraClangArgs.push_back("-Xclang");
appendXclang();
extraClangArgs.push_back(*cachePath);
}
}