Skip to content

Commit c470c81

Browse files
Merge pull request #66940 from cachemeifyoucan/eng/PR-dep-sharing-serivce-issue
[CAS][DependencyScanning] Don't keep a shared state of common file deps
2 parents fab9e4b + bed01ad commit c470c81

File tree

6 files changed

+115
-23
lines changed

6 files changed

+115
-23
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#ifndef SWIFT_AST_MODULE_DEPENDENCIES_H
1919
#define SWIFT_AST_MODULE_DEPENDENCIES_H
2020

21-
#include "swift/Basic/LLVM.h"
2221
#include "swift/AST/Import.h"
22+
#include "swift/AST/SearchPathOptions.h"
23+
#include "swift/Basic/LLVM.h"
2324
#include "clang/CAS/CASOptions.h"
2425
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
2526
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
@@ -31,12 +32,12 @@
3132
#include "llvm/CAS/CASReference.h"
3233
#include "llvm/CAS/CachingOnDiskFileSystem.h"
3334
#include "llvm/CAS/ObjectStore.h"
34-
#include "llvm/Support/Mutex.h"
3535
#include "llvm/Support/Error.h"
36+
#include "llvm/Support/Mutex.h"
3637
#include "llvm/Support/VirtualFileSystem.h"
3738
#include <string>
38-
#include <vector>
3939
#include <unordered_map>
40+
#include <vector>
4041

4142
namespace swift {
4243

@@ -733,17 +734,16 @@ using ModuleDependenciesKindRefMap =
733734
/// Track swift dependency
734735
class SwiftDependencyTracker {
735736
public:
736-
SwiftDependencyTracker(llvm::cas::CachingOnDiskFileSystem &FS,
737-
const std::vector<std::string> &CommonFiles)
738-
: FS(FS.createProxyFS()), Files(CommonFiles) {}
737+
SwiftDependencyTracker(llvm::cas::CachingOnDiskFileSystem &FS)
738+
: FS(FS.createProxyFS()) {}
739739

740740
void startTracking();
741+
void addCommonSearchPathDeps(const SearchPathOptions& Opts);
741742
void trackFile(const Twine &path) { (void)FS->status(path); }
742743
llvm::Expected<llvm::cas::ObjectProxy> createTreeFromDependencies();
743744

744745
private:
745746
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
746-
const std::vector<std::string> &Files;
747747
};
748748

749749
// MARK: SwiftDependencyScanningService
@@ -848,7 +848,7 @@ class SwiftDependencyScanningService {
848848
if (!CacheFS)
849849
return llvm::None;
850850

851-
return SwiftDependencyTracker(*CacheFS, CommonDependencyFiles);
851+
return SwiftDependencyTracker(*CacheFS);
852852
}
853853

854854
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getClangScanningFS() const {

lib/AST/ModuleDependencies.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,31 @@ SwiftDependencyScanningService::SwiftDependencyScanningService() {
394394
SharedFilesystemCache.emplace();
395395
}
396396

397+
void SwiftDependencyTracker::addCommonSearchPathDeps(
398+
const SearchPathOptions &Opts) {
399+
// Add SDKSetting file.
400+
SmallString<256> SDKSettingPath;
401+
llvm::sys::path::append(SDKSettingPath, Opts.getSDKPath(),
402+
"SDKSettings.json");
403+
FS->status(SDKSettingPath);
404+
405+
// Add Legacy layout file.
406+
const std::vector<std::string> AllSupportedArches = {
407+
"arm64", "arm64e", "x86_64", "i386",
408+
"armv7", "armv7s", "armv7k", "arm64_32"};
409+
410+
for (auto RuntimeLibPath : Opts.RuntimeLibraryPaths) {
411+
std::error_code EC;
412+
for (auto &Arch : AllSupportedArches) {
413+
SmallString<256> LayoutFile(RuntimeLibPath);
414+
llvm::sys::path::append(LayoutFile, "layout-" + Arch + ".yaml");
415+
FS->status(LayoutFile);
416+
}
417+
}
418+
}
419+
397420
void SwiftDependencyTracker::startTracking() {
398421
FS->trackNewAccesses();
399-
400-
for (auto &file : Files)
401-
(void)FS->status(file);
402422
}
403423

404424
llvm::Expected<llvm::cas::ObjectProxy>
@@ -459,11 +479,6 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
459479
}
460480
}
461481

462-
// Fetch some dependency files from clang importer.
463-
auto clangImporter = static_cast<ClangImporter *>(
464-
Instance.getASTContext().getClangModuleLoader());
465-
clangImporter->addClangInvovcationDependencies(CommonDependencyFiles);
466-
467482
auto CachingFS =
468483
llvm::cas::createCachingOnDiskFileSystem(Instance.getObjectStore());
469484
if (!CachingFS) {

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ forEachBatchEntry(CompilerInstance &invocationInstance,
16261626
return false;
16271627
}
16281628

1629-
static ModuleDependencyInfo identifyMainModuleDependencies(
1629+
static llvm::ErrorOr<ModuleDependencyInfo> identifyMainModuleDependencies(
16301630
CompilerInstance &instance,
16311631
llvm::Optional<SwiftDependencyTracker> tracker = llvm::None) {
16321632
ModuleDecl *mainModule = instance.getMainModule();
@@ -1659,8 +1659,23 @@ static ModuleDependencyInfo identifyMainModuleDependencies(
16591659
continue;
16601660
tracker->trackFile(sf->getFilename());
16611661
}
1662-
auto root = cantFail(tracker->createTreeFromDependencies());
1663-
rootID = root.getID().toString();
1662+
tracker->addCommonSearchPathDeps(
1663+
instance.getInvocation().getSearchPathOptions());
1664+
// Fetch some dependency files from clang importer.
1665+
std::vector<std::string> clangDependencyFiles;
1666+
auto clangImporter = static_cast<ClangImporter *>(
1667+
instance.getASTContext().getClangModuleLoader());
1668+
clangImporter->addClangInvovcationDependencies(clangDependencyFiles);
1669+
llvm::for_each(clangDependencyFiles,
1670+
[&](std::string &file) { tracker->trackFile(file); });
1671+
1672+
auto root = tracker->createTreeFromDependencies();
1673+
if (!root) {
1674+
instance.getASTContext().Diags.diagnose(SourceLoc(), diag::error_cas,
1675+
toString(root.takeError()));
1676+
return std::make_error_code(std::errc::io_error);
1677+
}
1678+
rootID = root->getID().toString();
16641679
}
16651680

16661681
auto mainDependencies =
@@ -1927,11 +1942,14 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
19271942
// First, identify the dependencies of the main module
19281943
auto mainDependencies = identifyMainModuleDependencies(
19291944
instance, cache.getScanService().createSwiftDependencyTracker());
1945+
if (!mainDependencies)
1946+
return mainDependencies.getError();
19301947
auto &ctx = instance.getASTContext();
19311948

19321949
// Add the main module.
19331950
StringRef mainModuleName = mainModule->getNameStr();
1934-
auto mainModuleID = ModuleDependencyID{mainModuleName.str(), mainDependencies.getKind()};
1951+
auto mainModuleID =
1952+
ModuleDependencyID{mainModuleName.str(), mainDependencies->getKind()};
19351953

19361954
ModuleDependencyIDSetVector allModules;
19371955
allModules.insert(mainModuleID);
@@ -1943,9 +1961,9 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
19431961
cache.updateDependency(
19441962
std::make_pair(mainModuleName.str(),
19451963
ModuleDependencyKind::SwiftSource),
1946-
std::move(mainDependencies));
1964+
std::move(*mainDependencies));
19471965
} else {
1948-
cache.recordDependency(mainModuleName, std::move(mainDependencies));
1966+
cache.recordDependency(mainModuleName, std::move(*mainDependencies));
19491967
}
19501968

19511969
auto ModuleCachePath = getModuleCachePathFromClang(
@@ -2051,8 +2069,10 @@ llvm::ErrorOr<swiftscan_import_set_t>
20512069
swift::dependencies::performModulePrescan(CompilerInstance &instance) {
20522070
// Execute import prescan, and write JSON output to the output stream
20532071
auto mainDependencies = identifyMainModuleDependencies(instance);
2072+
if (!mainDependencies)
2073+
return mainDependencies.getError();
20542074
auto *importSet = new swiftscan_import_set_s;
2055-
importSet->imports = create_set(mainDependencies.getModuleImports());
2075+
importSet->imports = create_set(mainDependencies->getModuleImports());
20562076
return importSet;
20572077
}
20582078

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ ErrorOr<ModuleDependencyInfo> ModuleDependencyScanner::scanInterfaceFile(
174174
std::string RootID;
175175
if (dependencyTracker) {
176176
dependencyTracker->startTracking();
177+
dependencyTracker->addCommonSearchPathDeps(Ctx.SearchPathOpts);
178+
std::vector<std::string> clangDependencyFiles;
179+
auto clangImporter =
180+
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
181+
clangImporter->addClangInvovcationDependencies(clangDependencyFiles);
182+
llvm::for_each(clangDependencyFiles, [&](std::string &file) {
183+
dependencyTracker->trackFile(file);
184+
});
177185
dependencyTracker->trackFile(moduleInterfacePath);
178186
auto RootOrError = dependencyTracker->createTreeFromDependencies();
179187
if (!RootOrError)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// REQUIRES: objc_interop
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: mkdir -p %t/clang-module-cache
5+
// RUN: mkdir -p %t/cas
6+
// RUN: split-file %s %t
7+
// RUN: %hmaptool write %t/hmap.json %t/empty.hmap
8+
9+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache \
10+
// RUN: %t/Test.swift -o %t/deps.json -cache-compile-job -cas-path %t/cas -clang-include-tree \
11+
// RUN: -Xcc -fmodule-map-file=%t/module.modulemap -Xcc -ivfsoverlay -Xcc %t/empty.yaml \
12+
// RUN: -Xcc -I%t/empty.hmap
13+
14+
// RUN: %S/Inputs/SwiftDepsExtractor.py %t/deps.json deps casFSRootID > %t/fs.casid
15+
// RUN: llvm-cas --cas %t/cas --ls-tree-recursive @%t/fs.casid | %FileCheck %s -DDIR=%basename_t -check-prefix FS_ROOT
16+
// RUN: %S/Inputs/SwiftDepsExtractor.py %t/deps.json clang:Dummy clangIncludeTree > %t/tree.casid
17+
// RUN: clang-cas-test --cas %t/cas --print-include-tree @%t/tree.casid | %FileCheck %s -DDIR=%basename_t -check-prefix INCLUDE_TREE
18+
19+
// FS_ROOT: [[DIR]].tmp/empty.hmap
20+
// FS_ROOT: [[DIR]].tmp/empty.yaml
21+
22+
// INCLUDE_TREE: [[DIR]].tmp/Dummy.h
23+
24+
//--- Test.swift
25+
import Dummy
26+
func test() {}
27+
28+
//--- module.modulemap
29+
module Dummy {
30+
umbrella header "Dummy.h"
31+
}
32+
33+
//--- Dummy.h
34+
void dummy(void);
35+
36+
//--- hmap.json
37+
{
38+
"mappings": {}
39+
}
40+
41+
//--- empty.yaml
42+
{
43+
"version": 0,
44+
"case-sensitive": "false",
45+
"use-external-names": true,
46+
"roots": []
47+
}
48+

test/lit.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,7 @@ config.substitutions.append(('%target-resilience-test', config.target_resilience
26402640

26412641
config.substitutions.append(('%llvm-profdata', config.llvm_profdata))
26422642
config.substitutions.append(('%llvm-cov', config.llvm_cov))
2643+
config.substitutions.append(('%hmaptool', os.path.join(config.llvm_src_root, '..', 'clang', 'utils', 'hmaptool', 'hmaptool')))
26432644

26442645
# Set up the host library environment.
26452646
if hasattr(config, 'target_library_path_var'):

0 commit comments

Comments
 (0)