Skip to content

Commit fec837a

Browse files
author
Harlan Haskins
authored
[ParseableInterfaces] Improvements to forwarding module dependencies (#23968)
- Fix a typo in the debug output for finding a prebuilt module - Don't add the prebuilt module path to the dependency tracker - Ensure we're registering the _forwarding module_'s dependencies in cached modules, not the prebuilt module's dependencies. - Check for the existence of a prebuilt module before doing the is-up-to-date check (which will have failed anyway if it didn't exist). - Test that forwarding module dependency collection works rdar://48659199
1 parent 3586952 commit fec837a

File tree

2 files changed

+102
-14
lines changed

2 files changed

+102
-14
lines changed

Diff for: lib/Frontend/ParseableInterfaceModuleLoader.cpp

+46-14
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,17 @@ class swift::ParseableInterfaceBuilder {
408408
StringRef DepName = Scratch.str();
409409

410410
assert(moduleCachePath.empty() || !DepName.startswith(moduleCachePath));
411-
assert(prebuiltCachePath.empty() || !DepName.startswith(prebuiltCachePath));
412411

413412
// Serialize the paths of dependencies in the SDK relative to it.
414413
Optional<StringRef> SDKRelativePath = getRelativeDepPath(DepName, SDKPath);
415414
StringRef DepNameToStore = SDKRelativePath.getValueOr(DepName);
416415
bool IsSDKRelative = SDKRelativePath.hasValue();
417416

417+
// Forwarding modules add the underlying prebuilt module to their
418+
// dependency list -- don't serialize that.
419+
if (!prebuiltCachePath.empty() && DepName.startswith(prebuiltCachePath))
420+
continue;
421+
418422
if (AllDepNames.insert(DepName).second && dependencyTracker) {
419423
dependencyTracker->addDependency(DepName, /*isSystem*/IsSDKRelative);
420424
}
@@ -828,6 +832,10 @@ class ParseableInterfaceModuleLoaderImpl {
828832
}
829833
path::append(scratch, path::filename(modulePath));
830834

835+
// If there isn't a file at this location, skip returning a path.
836+
if (!fs.exists(scratch))
837+
return None;
838+
831839
return scratch.str();
832840
}
833841

@@ -914,7 +922,7 @@ class ParseableInterfaceModuleLoaderImpl {
914922
return DiscoveredModule::prebuilt(*path, std::move(moduleBuffer));
915923
} else {
916924
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date prebuilt module at "
917-
<< modulePath << "\n");
925+
<< path->str() << "\n");
918926
}
919927
}
920928
}
@@ -951,40 +959,62 @@ class ParseableInterfaceModuleLoaderImpl {
951959

952960
/// Writes the "forwarding module" that will forward to a module in the
953961
/// prebuilt cache.
962+
///
954963
/// Since forwarding modules track dependencies separately from the module
955964
/// they point to, we'll need to grab the up-to-date file status while doing
956-
/// this.
957-
bool writeForwardingModule(const DiscoveredModule &mod,
958-
StringRef outputPath,
959-
ArrayRef<FileDependency> deps) {
965+
/// this. If the write was successful, it also updates the
966+
/// list of dependencies to match what was written to the forwarding file.
967+
bool writeForwardingModuleAndUpdateDeps(
968+
const DiscoveredModule &mod, StringRef outputPath,
969+
SmallVectorImpl<FileDependency> &deps) {
960970
assert(mod.isPrebuilt() &&
961971
"cannot write forwarding file for non-prebuilt module");
962972
ForwardingModule fwd(mod.path);
963973

974+
SmallVector<FileDependency, 16> depsAdjustedToMTime;
975+
964976
// FIXME: We need to avoid re-statting all these dependencies, otherwise
965977
// we may record out-of-date information.
966-
auto addDependency = [&](StringRef path) {
978+
auto addDependency = [&](StringRef path) -> FileDependency {
967979
auto status = fs.status(path);
968980
uint64_t mtime =
969981
status->getLastModificationTime().time_since_epoch().count();
970982
fwd.addDependency(path, status->getSize(), mtime);
983+
984+
// Construct new FileDependency matching what we've added to the
985+
// forwarding module. This is no longer SDK-relative because the absolute
986+
// path has already been resolved.
987+
return FileDependency::modTimeBased(path, /*isSDKRelative*/false,
988+
status->getSize(), mtime);
971989
};
972990

973-
// Add the prebuilt module as a dependency of the forwarding module.
974-
addDependency(fwd.underlyingModulePath);
991+
// Add the prebuilt module as a dependency of the forwarding module, but
992+
// don't add it to the outer dependency list.
993+
(void)addDependency(fwd.underlyingModulePath);
975994

976-
// Add all the dependencies from the prebuilt module.
995+
// Add all the dependencies from the prebuilt module, and update our list
996+
// of dependencies to reflect what's recorded in the forwarding module.
977997
SmallString<128> SDKRelativeBuffer;
978998
for (auto dep : deps) {
979-
addDependency(getFullDependencyPath(dep, SDKRelativeBuffer));
999+
auto adjustedDep =
1000+
addDependency(getFullDependencyPath(dep, SDKRelativeBuffer));
1001+
depsAdjustedToMTime.push_back(adjustedDep);
9801002
}
9811003

982-
return withOutputFile(diags, outputPath,
1004+
auto hadError = withOutputFile(diags, outputPath,
9831005
[&](llvm::raw_pwrite_stream &out) {
9841006
llvm::yaml::Output yamlWriter(out);
9851007
yamlWriter << fwd;
9861008
return false;
9871009
});
1010+
1011+
if (hadError)
1012+
return true;
1013+
1014+
// If and only if we succeeded writing the forwarding file, update the
1015+
// provided list of dependencies.
1016+
deps = depsAdjustedToMTime;
1017+
return false;
9881018
}
9891019

9901020
/// Looks up the best module to load for a given interface, and returns a
@@ -1033,9 +1063,11 @@ class ParseableInterfaceModuleLoaderImpl {
10331063
if (moduleOrErr) {
10341064
auto module = std::move(moduleOrErr.get());
10351065

1036-
// If it's prebuilt, use this time to generate a forwarding module.
1066+
// If it's prebuilt, use this time to generate a forwarding module and
1067+
// update the dependencies to use modification times.
10371068
if (module.isPrebuilt())
1038-
if (writeForwardingModule(module, cachedOutputPath, allDeps))
1069+
if (writeForwardingModuleAndUpdateDeps(module, cachedOutputPath,
1070+
allDeps))
10391071
return std::make_error_code(std::errc::not_supported);
10401072

10411073
// Report the module's dependencies to the dependencyTracker
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/mock-sdk)
3+
// RUN: %empty-directory(%t/ModuleCache)
4+
// RUN: %empty-directory(%t/PrebuiltModuleCache)
5+
6+
// This test makes sure that we propagate the right set of dependencies from a
7+
// prebuilt module to its corresponding forwarding module.
8+
9+
// Setup. Copy the mock SDK into the tmp directory.
10+
// RUN: cp -r %S/Inputs/mock-sdk/* %t/mock-sdk/.
11+
12+
// 1. Compile ExportedLib.swiftinterface, which a) is in the SDK, and b) depends
13+
// on a C module with headers that should be in the dependency list.
14+
// Put it in the prebuilt cache.
15+
16+
// RUN: %target-swift-frontend -build-module-from-parseable-interface %t/mock-sdk/ExportedLib.swiftinterface -sdk %t/mock-sdk -o %t/PrebuiltModuleCache/ExportedLib.swiftmodule -serialize-parseable-module-interface-dependency-hashes -track-system-dependencies
17+
18+
// 2. Make sure the prebuilt module we built has SomeCModule.h as a dependency.
19+
20+
// RUN: llvm-bcanalyzer -dump %t/PrebuiltModuleCache/ExportedLib.swiftmodule | grep 'FILE_DEPENDENCY.*SomeCModule.h'
21+
22+
// 3. Typecheck this file, which imports SdkLib, which imports ExportedLib.
23+
// Because ExportedLib is prebuilt, we expect a forwarding module for
24+
// ExportedLib in the module cache, and a serialized module for SdkLib in
25+
// the cache.
26+
27+
// RUN: %target-swift-frontend -typecheck %s -prebuilt-module-cache-path %t/PrebuiltModuleCache -module-cache-path %t/ModuleCache -sdk %t/mock-sdk -I %t/mock-sdk -track-system-dependencies
28+
29+
// 4. Make sure the forwarding module is installed in the cache.
30+
31+
// RUN: %{python} %S/Inputs/check-is-forwarding-module.py %t/ModuleCache/ExportedLib-*.swiftmodule
32+
33+
// 5. Make sure the forwarding module depends on the prebuilt module and the C
34+
// header.
35+
36+
// RUN: grep ' *path:.*ExportedLib.swiftmodule' %t/ModuleCache/ExportedLib-*.swiftmodule
37+
// RUN: grep ' *path:.*SomeCModule.h' %t/ModuleCache/ExportedLib-*.swiftmodule
38+
39+
// 6. Make sure the dependencies from the forwarding module make it into the
40+
// cached module.
41+
42+
// RUN: llvm-bcanalyzer -dump %t/ModuleCache/SdkLib-*.swiftmodule | grep 'FILE_DEPENDENCY.*SomeCModule.h'
43+
44+
// 7. Make sure the prebuilt ExportedLib module did NOT get propagated to SdkLib.
45+
46+
// RUN: llvm-bcanalyzer -dump %t/ModuleCache/SdkLib-*.swiftmodule | not grep 'FILE_DEPENDENCY.*PrebuiltModuleCache'
47+
48+
// 8. Make sure we re-build the SdkLib module if one of the dependencies changes its mtime (but not its hash).
49+
// This will ensure we recorded the forwarding module's dependencies, not the prebuilt.
50+
51+
// RUN: %{python} %S/Inputs/make-old.py %t/ModuleCache/SdkLib-*.swiftmodule
52+
// RUN: %{python} %S/Inputs/make-old.py %t/mock-sdk/usr/include/SomeCModule.h
53+
// RUN: %target-swift-frontend -typecheck %s -prebuilt-module-cache-path %t/PrebuiltModuleCache -module-cache-path %t/ModuleCache -sdk %t/mock-sdk -I %t/mock-sdk -track-system-dependencies
54+
// RUN: %{python} %S/Inputs/check-is-new.py %t/ModuleCache/SdkLib-*.swiftmodule
55+
56+
import SdkLib

0 commit comments

Comments
 (0)