Skip to content

Commit b7b2795

Browse files
committed
Addressing code review. Removed the helper class, hence we do not need to manage any states.
1 parent 69e88b1 commit b7b2795

File tree

3 files changed

+105
-147
lines changed

3 files changed

+105
-147
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -628,53 +628,20 @@ struct SwiftInterfaceInfo {
628628
std::optional<version::Version> CompilerToolsVersion;
629629
};
630630

631-
/// A small helper class that expands the module name to a full output path
632-
/// with context hash.
633-
/// The cannonical way to use this class is the following:
634-
/// 1. Create an instance through the constructor.
635-
/// 2. Optional: prune the extra args if necessary.
636-
/// 3. Obtain the expanded name that contains a full path, and a hash.
637-
/// expandedName is computed only once, and never updated.
638-
/// Currently, there is no use case where we first obtain expandedName,
639-
/// then prune the extra args, and finally obtain expandedName again.
640-
/// Pruning always happens before the first time we call getExpandedName().
641-
/// Therefore it is correct to calculate expandedName only once.
642-
class InterfaceModuleOutputPathResolver {
643-
public:
644-
using ArgListTy = std::vector<std::string>;
645-
struct ResultTy {
646-
llvm::SmallString<256> outputPath;
647-
648-
// Hash points to a segment of outputPath.
649-
StringRef hash;
650-
};
651-
652-
private:
653-
const StringRef moduleName;
654-
const StringRef interfacePath;
655-
const StringRef sdkPath;
656-
const CompilerInvocation &CI;
631+
namespace SwiftInterfaceModuleOutputPathResolution {
632+
struct ResultTy {
633+
llvm::SmallString<256> outputPath;
657634

658-
// This field contains the clang ExtraArgs that affect how clang types are
659-
// imported into swift module. See the constructor for how this field is set,
660-
// and see pruneExtraArgs to see how it can be updated.
661-
ArgListTy extraArgs;
635+
// Hash points to a segment of outputPath.
636+
StringRef hash;
637+
};
662638

663-
std::unique_ptr<ResultTy> resolvedOutputPath;
639+
using ArgListTy = std::vector<std::string>;
664640

665-
std::string getHash();
666-
667-
public:
668-
InterfaceModuleOutputPathResolver(const StringRef moduleName,
669-
const StringRef interfacePath,
670-
const StringRef sdkPath,
671-
const CompilerInvocation &CI)
672-
: moduleName(moduleName), interfacePath(interfacePath), sdkPath(sdkPath),
673-
CI(CI), extraArgs(CI.getClangImporterOptions()
674-
.getReducedExtraArgsForSwiftModuleDependency()) {}
675-
ResultTy getOutputPath();
676-
void pruneExtraArgs(std::function<void(ArgListTy &)> filter);
677-
};
641+
void getOutputPath(ResultTy &outputPath, const StringRef &moduleName,
642+
const StringRef &interfacePath, const StringRef &sdkPath,
643+
const CompilerInvocation &CI, const ArgListTy &extraArgs);
644+
} // namespace SwiftInterfaceModuleOutputPathResolution
678645

679646
struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
680647
private:
@@ -750,11 +717,12 @@ struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
750717

751718
~InterfaceSubContextDelegateImpl() = default;
752719

753-
/// includes a hash of relevant key data.
754-
InterfaceModuleOutputPathResolver::ResultTy
755-
getCachedOutputPath(StringRef moduleName, StringRef interfacePath,
756-
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);
757724
};
725+
758726
} // namespace swift
759727

760728
#endif

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,6 @@ class ExplicitModuleDependencyResolver {
8888
tracker(std::move(tracker)) {
8989
// Copy commandline.
9090
commandline = resolvingDepInfo.getCommandline();
91-
92-
if (resolvingDepInfo.isSwiftInterfaceModule()) {
93-
auto swiftTextualDeps = resolvingDepInfo.getAsSwiftInterfaceModule();
94-
auto &interfacePath = swiftTextualDeps->swiftInterfaceFile;
95-
auto SDKPath = instance.getASTContext().SearchPathOpts.getSDKPath();
96-
outputPathResolver = std::make_unique<InterfaceModuleOutputPathResolver>(
97-
moduleID.ModuleName, interfacePath, SDKPath,
98-
instance.getInvocation());
99-
}
10091
}
10192

10293
llvm::Error
@@ -178,12 +169,10 @@ class ExplicitModuleDependencyResolver {
178169
}
179170
}
180171

181-
// It is necessary to update the command line to take
182-
// the pruned unused VFS overlay into account before remapping the
183-
// command line.
172+
SwiftInterfaceModuleOutputPathResolution::ResultTy swiftInterfaceOutputPath;
184173
if (resolvingDepInfo.isSwiftInterfaceModule()) {
185-
pruneUnusedVFSOverlay();
186-
updateSwiftInterfaceModuleOutputPath();
174+
pruneUnusedVFSOverlay(swiftInterfaceOutputPath);
175+
updateSwiftInterfaceModuleOutputPath(swiftInterfaceOutputPath);
187176
}
188177

189178
// Update the dependency in the cache with the modified command-line.
@@ -197,7 +186,7 @@ class ExplicitModuleDependencyResolver {
197186
}
198187

199188
auto dependencyInfoCopy = resolvingDepInfo;
200-
if (auto err = finalize(dependencyInfoCopy))
189+
if (auto err = finalize(dependencyInfoCopy, swiftInterfaceOutputPath))
201190
return err;
202191

203192
dependencyInfoCopy.setIsFinalized(true);
@@ -207,15 +196,16 @@ class ExplicitModuleDependencyResolver {
207196

208197
private:
209198
// Finalize the resolving dependency info.
210-
llvm::Error finalize(ModuleDependencyInfo &depInfo) {
199+
llvm::Error finalize(ModuleDependencyInfo &depInfo,
200+
const SwiftInterfaceModuleOutputPathResolution::ResultTy
201+
&swiftInterfaceModuleOutputPath) {
211202
if (resolvingDepInfo.isSwiftPlaceholderModule())
212203
return llvm::Error::success();
213204

214-
if (outputPathResolver) {
215-
auto resolvedPath = outputPathResolver->getOutputPath();
216-
depInfo.setOutputPathAndHash(resolvedPath.outputPath.str().str(),
217-
resolvedPath.hash.str());
218-
}
205+
if (resolvingDepInfo.isSwiftInterfaceModule())
206+
depInfo.setOutputPathAndHash(
207+
swiftInterfaceModuleOutputPath.outputPath.str().str(),
208+
swiftInterfaceModuleOutputPath.hash.str());
219209

220210
// Add macros.
221211
for (auto &macro : macros)
@@ -396,16 +386,18 @@ class ExplicitModuleDependencyResolver {
396386
}
397387
}
398388

399-
void pruneUnusedVFSOverlay() {
389+
void pruneUnusedVFSOverlay(
390+
SwiftInterfaceModuleOutputPathResolution::ResultTy &outputPath) {
400391
// Pruning of unused VFS overlay options for Clang dependencies is performed
401392
// by the Clang dependency scanner.
402393
if (moduleID.Kind == ModuleDependencyKind::Clang)
403394
return;
404395

396+
// Prune the command line.
405397
std::vector<std::string> resolvedCommandLine;
406398
size_t skip = 0;
407-
for (auto it = commandline.begin(), end = commandline.end();
408-
it != end; it++) {
399+
for (auto it = commandline.begin(), end = commandline.end(); it != end;
400+
it++) {
409401
if (skip) {
410402
skip--;
411403
continue;
@@ -423,50 +415,50 @@ class ExplicitModuleDependencyResolver {
423415
resolvedCommandLine.push_back(*it);
424416
}
425417

426-
// Filter the extra args that we use to calculate the hash of the module.
427-
// The key assumption is that these args are clang importer args, so we
428-
// do not need to check for -Xcc.
429-
auto extraArgsFilter =
430-
[&](InterfaceModuleOutputPathResolver::ArgListTy &args) {
431-
bool skip = false;
432-
InterfaceModuleOutputPathResolver::ArgListTy newArgs;
433-
for (auto it = args.begin(), end = args.end(); it != end; it++) {
434-
if (skip) {
435-
skip = false;
436-
continue;
437-
}
438-
439-
if ((it + 1) != end && isVFSOverlayFlag(*it)) {
440-
if (!usedVFSOverlayPaths.contains(*(it + 1))) {
441-
skip = true;
442-
continue;
443-
}
444-
}
445-
446-
newArgs.push_back(*it);
447-
}
448-
args = std::move(newArgs);
449-
return;
450-
};
418+
commandline = std::move(resolvedCommandLine);
451419

452-
if (outputPathResolver)
453-
outputPathResolver->pruneExtraArgs(extraArgsFilter);
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();
454423

455-
commandline = std::move(resolvedCommandLine);
456-
}
424+
SwiftInterfaceModuleOutputPathResolution::ArgListTy extraArgsList;
425+
const SwiftInterfaceModuleOutputPathResolution::ArgListTy
426+
&clangImpporterOptions =
427+
CI.getClangImporterOptions()
428+
.getReducedExtraArgsForSwiftModuleDependency();
457429

458-
void updateSwiftInterfaceModuleOutputPath() {
459-
// The command line needs update once we prune the unused VFS overlays.
460-
// The update consists of two steps.
461-
// 1. Obtain the output path, which includes the module hash that takes the
462-
// VFS pruning into account.
463-
// 2. Update `-o `'s value on the command line with the new output path.
430+
skip = 0;
431+
for (auto it = clangImpporterOptions.begin(),
432+
end = clangImpporterOptions.end();
433+
it != end; it++) {
434+
if (skip) {
435+
skip = 0;
436+
continue;
437+
}
438+
439+
if ((it + 1) != end && isVFSOverlayFlag(*it)) {
440+
if (!usedVFSOverlayPaths.contains(*(it + 1))) {
441+
skip = 1;
442+
continue;
443+
}
444+
}
464445

465-
assert(outputPathResolver &&
466-
"Can only update if we have an outputPathResolver.");
467-
const auto &expandedName = outputPathResolver->getOutputPath();
446+
extraArgsList.push_back(*it);
447+
}
448+
449+
auto swiftTextualDeps = resolvingDepInfo.getAsSwiftInterfaceModule();
450+
auto &interfacePath = swiftTextualDeps->swiftInterfaceFile;
451+
auto sdkPath = instance.getASTContext().SearchPathOpts.getSDKPath();
452+
SwiftInterfaceModuleOutputPathResolution::getOutputPath(
453+
outputPath, moduleID.ModuleName, interfacePath, sdkPath, CI,
454+
extraArgsList);
455+
456+
return;
457+
}
468458

469-
StringRef outputName = expandedName.outputPath.str();
459+
void updateSwiftInterfaceModuleOutputPath(
460+
const SwiftInterfaceModuleOutputPathResolution::ResultTy &outputPath) {
461+
StringRef outputName = outputPath.outputPath.str();
470462

471463
bool isOutputPath = false;
472464
for (auto &A : commandline) {
@@ -604,7 +596,6 @@ class ExplicitModuleDependencyResolver {
604596
ModuleDependenciesCache &cache;
605597
CompilerInstance &instance;
606598
const ModuleDependencyInfo &resolvingDepInfo;
607-
std::unique_ptr<InterfaceModuleOutputPathResolver> outputPathResolver;
608599

609600
std::optional<SwiftDependencyTracker> tracker;
610601
std::vector<std::string> rootIDs;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,10 @@ class ModuleInterfaceLoaderImpl {
10981098
requiresOSSAModules);
10991099

11001100
// Compute the output path if we're loading or emitting a cached module.
1101-
auto resolvedOutputPath = astDelegate.getCachedOutputPath(
1102-
moduleName, interfacePath, ctx.SearchPathOpts.getSDKPath());
1101+
SwiftInterfaceModuleOutputPathResolution::ResultTy resolvedOutputPath;
1102+
astDelegate.getCachedOutputPath(resolvedOutputPath, moduleName,
1103+
interfacePath,
1104+
ctx.SearchPathOpts.getSDKPath());
11031105
auto &cachedOutputPath = resolvedOutputPath.outputPath;
11041106

11051107
// Try to find the right module for this interface, either alongside it,
@@ -2005,13 +2007,14 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
20052007

20062008
/// Calculate an output filename in \p genericSubInvocation's cache path that
20072009
/// includes a hash of relevant key data.
2008-
InterfaceModuleOutputPathResolver::ResultTy
2009-
InterfaceSubContextDelegateImpl::getCachedOutputPath(StringRef moduleName,
2010-
StringRef interfacePath,
2011-
StringRef sdkPath) {
2012-
InterfaceModuleOutputPathResolver resolver(moduleName, interfacePath, sdkPath,
2013-
genericSubInvocation);
2014-
return resolver.getOutputPath();
2010+
void InterfaceSubContextDelegateImpl::getCachedOutputPath(
2011+
SwiftInterfaceModuleOutputPathResolution::ResultTy &resolvedOutputPath,
2012+
StringRef moduleName, StringRef interfacePath, StringRef sdkPath) {
2013+
SwiftInterfaceModuleOutputPathResolution::getOutputPath(
2014+
resolvedOutputPath, moduleName, interfacePath, sdkPath,
2015+
genericSubInvocation,
2016+
genericSubInvocation.getClangImporterOptions()
2017+
.getReducedExtraArgsForSwiftModuleDependency());
20152018
}
20162019

20172020
std::error_code
@@ -2090,8 +2093,8 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
20902093
}
20912094

20922095
// Calculate output path of the module.
2093-
auto resolvedOutputPath =
2094-
getCachedOutputPath(moduleName, interfacePath, sdkPath);
2096+
SwiftInterfaceModuleOutputPathResolution::ResultTy resolvedOutputPath;
2097+
getCachedOutputPath(resolvedOutputPath, moduleName, interfacePath, sdkPath);
20952098

20962099
// If no specific output path is given, use the hashed output path.
20972100
if (outputPath.empty()) {
@@ -2744,24 +2747,7 @@ std::unique_ptr<ExplicitCASModuleLoader> ExplicitCASModuleLoader::create(
27442747
return result;
27452748
}
27462749

2747-
InterfaceModuleOutputPathResolver::ResultTy
2748-
InterfaceModuleOutputPathResolver::getOutputPath() {
2749-
if (!resolvedOutputPath) {
2750-
resolvedOutputPath = std::make_unique<ResultTy>();
2751-
auto &outputPath = resolvedOutputPath->outputPath;
2752-
outputPath = CI.getClangModuleCachePath();
2753-
llvm::sys::path::append(outputPath, moduleName);
2754-
outputPath.append("-");
2755-
auto hashStart = outputPath.size();
2756-
outputPath.append(getHash());
2757-
resolvedOutputPath->hash = outputPath.str().substr(hashStart);
2758-
outputPath.append(".");
2759-
auto outExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
2760-
outputPath.append(outExt);
2761-
}
2762-
return *resolvedOutputPath;
2763-
}
2764-
2750+
namespace swift::SwiftInterfaceModuleOutputPathResolution {
27652751
/// Construct a key for the .swiftmodule being generated. There is a
27662752
/// balance to be struck here between things that go in the cache key and
27672753
/// things that go in the "up to date" check of the cache entry. We want to
@@ -2772,7 +2758,10 @@ InterfaceModuleOutputPathResolver::getOutputPath() {
27722758
/// -- rather than making a new one and potentially filling up the cache
27732759
/// with dead entries -- when other factors change, such as the contents of
27742760
/// the .swiftinterface input or its dependencies.
2775-
std::string InterfaceModuleOutputPathResolver::getHash() {
2761+
static std::string getContextHash(const CompilerInvocation &CI,
2762+
const StringRef &interfacePath,
2763+
const StringRef &sdkPath,
2764+
const ArgListTy &extraArgs) {
27762765
// When doing dependency scanning for explicit module, use strict context hash
27772766
// to ensure sound module hash.
27782767
bool useStrictCacheHash = CI.getFrontendOptions().RequestedAction ==
@@ -2842,9 +2831,19 @@ std::string InterfaceModuleOutputPathResolver::getHash() {
28422831
return llvm::toString(llvm::APInt(64, H), 36, /*Signed=*/false);
28432832
}
28442833

2845-
void InterfaceModuleOutputPathResolver::pruneExtraArgs(
2846-
std::function<void(ArgListTy &)> filter) {
2847-
assert(!resolvedOutputPath &&
2848-
"Cannot prune again after the output path has been resolved.");
2849-
filter(extraArgs);
2834+
void getOutputPath(ResultTy &resolvedOutputPath, const StringRef &moduleName,
2835+
const StringRef &interfacePath, const StringRef &sdkPath,
2836+
const CompilerInvocation &CI, const ArgListTy &extraArgs) {
2837+
auto &outputPath = resolvedOutputPath.outputPath;
2838+
outputPath = CI.getClangModuleCachePath();
2839+
llvm::sys::path::append(outputPath, moduleName);
2840+
outputPath.append("-");
2841+
auto hashStart = outputPath.size();
2842+
outputPath.append(getContextHash(CI, interfacePath, sdkPath, extraArgs));
2843+
resolvedOutputPath.hash = outputPath.str().substr(hashStart);
2844+
outputPath.append(".");
2845+
auto outExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
2846+
outputPath.append(outExt);
2847+
return;
28502848
}
2849+
} // namespace swift::SwiftInterfaceModuleOutputPathResolution

0 commit comments

Comments
 (0)