Skip to content

Commit e0359f7

Browse files
authored
Merge pull request #79297 from qiongsiwu/upstream_rehash_after_vfs_overlay_prune
[Dependency Scanning] Update Swift Interface Module's Output Path after `vfs` Pruning
2 parents f0050df + 23e863d commit e0359f7

File tree

8 files changed

+309
-163
lines changed

8 files changed

+309
-163
lines changed

include/swift/AST/ModuleDependencies.h

+15-17
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class SwiftInterfaceModuleDependenciesStorage
266266
: public ModuleDependencyInfoStorageBase {
267267
public:
268268
/// Destination output path
269-
const std::string moduleOutputPath;
269+
std::string moduleOutputPath;
270270

271271
/// The Swift interface file to be used to generate the module file.
272272
const std::string swiftInterfaceFile;
@@ -275,7 +275,7 @@ class SwiftInterfaceModuleDependenciesStorage
275275
const std::vector<std::string> compiledModuleCandidates;
276276

277277
/// The hash value that will be used for the generated module
278-
const std::string contextHash;
278+
std::string contextHash;
279279

280280
/// A flag that indicates this dependency is a framework
281281
const bool isFramework;
@@ -290,22 +290,20 @@ class SwiftInterfaceModuleDependenciesStorage
290290
const std::string userModuleVersion;
291291

292292
SwiftInterfaceModuleDependenciesStorage(
293-
StringRef moduleOutputPath, StringRef swiftInterfaceFile,
293+
StringRef swiftInterfaceFile,
294294
ArrayRef<StringRef> compiledModuleCandidates,
295295
ArrayRef<ScannerImportStatementInfo> moduleImports,
296296
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
297297
ArrayRef<StringRef> buildCommandLine, ArrayRef<LinkLibrary> linkLibraries,
298-
StringRef contextHash, bool isFramework,
299-
bool isStatic, StringRef RootID, StringRef moduleCacheKey,
300-
StringRef userModuleVersion)
298+
bool isFramework, bool isStatic, StringRef RootID,
299+
StringRef moduleCacheKey, StringRef userModuleVersion)
301300
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftInterface,
302301
moduleImports, optionalModuleImports,
303302
linkLibraries, moduleCacheKey),
304-
moduleOutputPath(moduleOutputPath),
305303
swiftInterfaceFile(swiftInterfaceFile),
306304
compiledModuleCandidates(compiledModuleCandidates.begin(),
307305
compiledModuleCandidates.end()),
308-
contextHash(contextHash), isFramework(isFramework), isStatic(isStatic),
306+
isFramework(isFramework), isStatic(isStatic),
309307
textualModuleDetails(buildCommandLine, RootID),
310308
userModuleVersion(userModuleVersion) {}
311309

@@ -585,21 +583,18 @@ class ModuleDependencyInfo {
585583
/// Describe the module dependencies for a Swift module that can be
586584
/// built from a Swift interface file (\c .swiftinterface).
587585
static ModuleDependencyInfo forSwiftInterfaceModule(
588-
StringRef moduleOutputPath, StringRef swiftInterfaceFile,
589-
ArrayRef<StringRef> compiledCandidates, ArrayRef<StringRef> buildCommands,
586+
StringRef swiftInterfaceFile, ArrayRef<StringRef> compiledCandidates,
587+
ArrayRef<StringRef> buildCommands,
590588
ArrayRef<ScannerImportStatementInfo> moduleImports,
591589
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
592-
ArrayRef<LinkLibrary> linkLibraries,
593-
StringRef contextHash, bool isFramework, bool isStatic,
590+
ArrayRef<LinkLibrary> linkLibraries, bool isFramework, bool isStatic,
594591
StringRef CASFileSystemRootID, StringRef moduleCacheKey,
595592
StringRef userModuleVersion) {
596593
return ModuleDependencyInfo(
597594
std::make_unique<SwiftInterfaceModuleDependenciesStorage>(
598-
moduleOutputPath, swiftInterfaceFile, compiledCandidates,
599-
moduleImports, optionalModuleImports,
600-
buildCommands, linkLibraries, contextHash,
601-
isFramework, isStatic, CASFileSystemRootID, moduleCacheKey,
602-
userModuleVersion));
595+
swiftInterfaceFile, compiledCandidates, moduleImports,
596+
optionalModuleImports, buildCommands, linkLibraries, isFramework,
597+
isStatic, CASFileSystemRootID, moduleCacheKey, userModuleVersion));
603598
}
604599

605600
/// Describe the module dependencies for a serialized or parsed Swift module.
@@ -983,6 +978,9 @@ class ModuleDependencyInfo {
983978
/// Set the chained bridging header buffer.
984979
void setChainedBridgingHeaderBuffer(StringRef path, StringRef buffer);
985980

981+
/// Set the output path and the context hash.
982+
void setOutputPathAndHash(StringRef outputPath, StringRef hash);
983+
986984
/// Collect a map from a secondary module name to a list of cross-import
987985
/// overlays, when this current module serves as the primary module.
988986
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>

include/swift/Frontend/ModuleInterfaceLoader.h

+22-9
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,22 @@ struct SwiftInterfaceInfo {
628628
std::optional<version::Version> CompilerToolsVersion;
629629
};
630630

631-
struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
631+
namespace SwiftInterfaceModuleOutputPathResolution {
632+
struct ResultTy {
633+
llvm::SmallString<256> outputPath;
634+
635+
// Hash points to a segment of outputPath.
636+
StringRef hash;
637+
};
638+
639+
using ArgListTy = std::vector<std::string>;
640+
641+
void setOutputPath(ResultTy &outputPath, const StringRef &moduleName,
642+
const StringRef &interfacePath, const StringRef &sdkPath,
643+
const CompilerInvocation &CI, const ArgListTy &extraArgs);
644+
} // namespace SwiftInterfaceModuleOutputPathResolution
645+
646+
struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
632647
private:
633648
SourceManager &SM;
634649
public:
@@ -702,14 +717,12 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
702717

703718
~InterfaceSubContextDelegateImpl() = default;
704719

705-
/// includes a hash of relevant key data.
706-
StringRef computeCachedOutputPath(StringRef moduleName,
707-
StringRef UseInterfacePath,
708-
StringRef sdkPath,
709-
llvm::SmallString<256> &OutPath,
710-
StringRef &CacheHash);
711-
std::string getCacheHash(StringRef useInterfacePath, StringRef sdkPath);
720+
/// resolvedOutputPath includes a hash of relevant key data.
721+
void getCachedOutputPath(
722+
SwiftInterfaceModuleOutputPathResolution::ResultTy &resolvedOutputPath,
723+
StringRef moduleName, StringRef interfacePath, StringRef sdkPath);
712724
};
713-
}
725+
726+
} // namespace swift
714727

715728
#endif

lib/AST/ModuleDependencies.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,21 @@ void ModuleDependencyInfo::addSourceFile(StringRef sourceFile) {
465465
}
466466
}
467467

468+
void ModuleDependencyInfo::setOutputPathAndHash(StringRef outputPath,
469+
StringRef hash) {
470+
switch (getKind()) {
471+
case swift::ModuleDependencyKind::SwiftInterface: {
472+
auto swiftInterfaceStorage =
473+
cast<SwiftInterfaceModuleDependenciesStorage>(storage.get());
474+
swiftInterfaceStorage->moduleOutputPath = outputPath.str();
475+
swiftInterfaceStorage->contextHash = hash.str();
476+
break;
477+
}
478+
default:
479+
llvm_unreachable("Unexpected dependency kind");
480+
}
481+
}
482+
468483
SwiftDependencyScanningService::SwiftDependencyScanningService() {
469484
ClangScanningService.emplace(
470485
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
585585

586586
// Form the dependencies storage object
587587
auto moduleDep = ModuleDependencyInfo::forSwiftInterfaceModule(
588-
outputModulePath.value(), optionalSwiftInterfaceFile.value(),
589-
compiledCandidatesRefs, buildCommandRefs, importStatements,
590-
optionalImportStatements, linkLibraries, *contextHash,
591-
isFramework, isStatic, *rootFileSystemID, *moduleCacheKey,
592-
*userModuleVersion);
588+
optionalSwiftInterfaceFile.value(), compiledCandidatesRefs,
589+
buildCommandRefs, importStatements, optionalImportStatements,
590+
linkLibraries, isFramework, isStatic, *rootFileSystemID,
591+
*moduleCacheKey, *userModuleVersion);
593592

593+
moduleDep.setOutputPathAndHash(*outputModulePath, *contextHash);
594594
addCommonDependencyInfo(moduleDep);
595595
addSwiftCommonDependencyInfo(moduleDep);
596596
addSwiftTextualDependencyInfo(

lib/DependencyScan/ScanDependencies.cpp

+80-13
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace {
8181
class ExplicitModuleDependencyResolver {
8282
public:
8383
ExplicitModuleDependencyResolver(
84-
ModuleDependencyID moduleID, ModuleDependenciesCache &cache,
84+
const ModuleDependencyID &moduleID, ModuleDependenciesCache &cache,
8585
CompilerInstance &instance, std::optional<SwiftDependencyTracker> tracker)
8686
: moduleID(moduleID), cache(cache), instance(instance),
8787
resolvingDepInfo(cache.findKnownDependency(moduleID)),
@@ -169,7 +169,11 @@ class ExplicitModuleDependencyResolver {
169169
}
170170
}
171171

172-
pruneUnusedVFSOverlay();
172+
SwiftInterfaceModuleOutputPathResolution::ResultTy swiftInterfaceOutputPath;
173+
if (resolvingDepInfo.isSwiftInterfaceModule()) {
174+
pruneUnusedVFSOverlay(swiftInterfaceOutputPath);
175+
updateSwiftInterfaceModuleOutputPath(swiftInterfaceOutputPath);
176+
}
173177

174178
// Update the dependency in the cache with the modified command-line.
175179
if (resolvingDepInfo.isSwiftInterfaceModule() ||
@@ -182,7 +186,7 @@ class ExplicitModuleDependencyResolver {
182186
}
183187

184188
auto dependencyInfoCopy = resolvingDepInfo;
185-
if (auto err = finalize(dependencyInfoCopy))
189+
if (auto err = finalize(dependencyInfoCopy, swiftInterfaceOutputPath))
186190
return err;
187191

188192
dependencyInfoCopy.setIsFinalized(true);
@@ -192,14 +196,21 @@ class ExplicitModuleDependencyResolver {
192196

193197
private:
194198
// Finalize the resolving dependency info.
195-
llvm::Error finalize(ModuleDependencyInfo &depInfo) {
199+
llvm::Error finalize(ModuleDependencyInfo &depInfo,
200+
const SwiftInterfaceModuleOutputPathResolution::ResultTy
201+
&swiftInterfaceModuleOutputPath) {
196202
if (resolvingDepInfo.isSwiftPlaceholderModule())
197203
return llvm::Error::success();
198204

205+
if (resolvingDepInfo.isSwiftInterfaceModule())
206+
depInfo.setOutputPathAndHash(
207+
swiftInterfaceModuleOutputPath.outputPath.str().str(),
208+
swiftInterfaceModuleOutputPath.hash.str());
209+
199210
// Add macros.
200211
for (auto &macro : macros)
201-
depInfo.addMacroDependency(
202-
macro.first(), macro.second.LibraryPath, macro.second.ExecutablePath);
212+
depInfo.addMacroDependency(macro.first(), macro.second.LibraryPath,
213+
macro.second.ExecutablePath);
203214

204215
for (auto &macro : depInfo.getMacroDependencies()) {
205216
std::string arg = macro.second.LibraryPath + "#" +
@@ -213,8 +224,7 @@ class ExplicitModuleDependencyResolver {
213224
return err;
214225

215226
if (!bridgingHeaderBuildCmd.empty())
216-
depInfo.updateBridgingHeaderCommandLine(
217-
bridgingHeaderBuildCmd);
227+
depInfo.updateBridgingHeaderCommandLine(bridgingHeaderBuildCmd);
218228
if (!resolvingDepInfo.isSwiftBinaryModule()) {
219229
depInfo.updateCommandLine(commandline);
220230
if (auto err = updateModuleCacheKey(depInfo))
@@ -376,16 +386,18 @@ class ExplicitModuleDependencyResolver {
376386
}
377387
}
378388

379-
void pruneUnusedVFSOverlay() {
389+
void pruneUnusedVFSOverlay(
390+
SwiftInterfaceModuleOutputPathResolution::ResultTy &outputPath) {
380391
// Pruning of unused VFS overlay options for Clang dependencies is performed
381392
// by the Clang dependency scanner.
382393
if (moduleID.Kind == ModuleDependencyKind::Clang)
383394
return;
384395

396+
// Prune the command line.
385397
std::vector<std::string> resolvedCommandLine;
386398
size_t skip = 0;
387-
for (auto it = commandline.begin(), end = commandline.end();
388-
it != end; it++) {
399+
for (auto it = commandline.begin(), end = commandline.end(); it != end;
400+
it++) {
389401
if (skip) {
390402
skip--;
391403
continue;
@@ -402,7 +414,62 @@ class ExplicitModuleDependencyResolver {
402414
}
403415
resolvedCommandLine.push_back(*it);
404416
}
417+
405418
commandline = std::move(resolvedCommandLine);
419+
420+
// Prune the clang impoter options. We do not need to deal with -Xcc because
421+
// these are clang options.
422+
const auto &CI = instance.getInvocation();
423+
424+
SwiftInterfaceModuleOutputPathResolution::ArgListTy extraArgsList;
425+
const auto &clangImporterOptions =
426+
CI.getClangImporterOptions()
427+
.getReducedExtraArgsForSwiftModuleDependency();
428+
429+
skip = 0;
430+
for (auto it = clangImporterOptions.begin(),
431+
end = clangImporterOptions.end();
432+
it != end; it++) {
433+
if (skip) {
434+
skip = 0;
435+
continue;
436+
}
437+
438+
if ((it + 1) != end && isVFSOverlayFlag(*it)) {
439+
if (!usedVFSOverlayPaths.contains(*(it + 1))) {
440+
skip = 1;
441+
continue;
442+
}
443+
}
444+
445+
extraArgsList.push_back(*it);
446+
}
447+
448+
auto swiftTextualDeps = resolvingDepInfo.getAsSwiftInterfaceModule();
449+
auto &interfacePath = swiftTextualDeps->swiftInterfaceFile;
450+
auto sdkPath = instance.getASTContext().SearchPathOpts.getSDKPath();
451+
SwiftInterfaceModuleOutputPathResolution::setOutputPath(
452+
outputPath, moduleID.ModuleName, interfacePath, sdkPath, CI,
453+
extraArgsList);
454+
455+
return;
456+
}
457+
458+
void updateSwiftInterfaceModuleOutputPath(
459+
const SwiftInterfaceModuleOutputPathResolution::ResultTy &outputPath) {
460+
StringRef outputName = outputPath.outputPath.str();
461+
462+
bool isOutputPath = false;
463+
for (auto &A : commandline) {
464+
if (isOutputPath) {
465+
A = outputName.str();
466+
break;
467+
} else if (A == "-o") {
468+
isOutputPath = true;
469+
}
470+
}
471+
472+
return;
406473
}
407474

408475
llvm::Error collectCASDependencies(ModuleDependencyInfo &dependencyInfoCopy) {
@@ -524,7 +591,7 @@ class ExplicitModuleDependencyResolver {
524591
}
525592

526593
private:
527-
ModuleDependencyID moduleID;
594+
const ModuleDependencyID &moduleID;
528595
ModuleDependenciesCache &cache;
529596
CompilerInstance &instance;
530597
const ModuleDependencyInfo &resolvingDepInfo;
@@ -540,7 +607,7 @@ class ExplicitModuleDependencyResolver {
540607
};
541608

542609
static llvm::Error resolveExplicitModuleInputs(
543-
ModuleDependencyID moduleID,
610+
const ModuleDependencyID &moduleID,
544611
const std::set<ModuleDependencyID> &dependencies,
545612
ModuleDependenciesCache &cache, CompilerInstance &instance,
546613
std::optional<std::set<ModuleDependencyID>> bridgingHeaderDeps,

0 commit comments

Comments
 (0)