Skip to content

Commit 7b89afb

Browse files
[DepScan] Teach dependency scanner to remap path for canonicalization
Allow DependencyScanner to canonicalize path using a prefix map. When option `-scanner-prefix-map` option is used, dependency scanner will remap all the input paths in following: * all the paths in the CAS file system or clang include tree * all the paths related to input on the command-line returned by scanner This allows all the input paths to be canonicalized so cache key can be computed reguardless of the exact on disk path. The sourceFile field is not remapped so build system can track the exact file as on the local file system.
1 parent e2a210b commit 7b89afb

20 files changed

+212
-38
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ REMARK(matching_output_produced,none,
485485
// Caching related diagnostics
486486
ERROR(error_caching_no_cas_fs, none,
487487
"caching is enabled without -cas-fs option, input is not immutable", ())
488+
ERROR(error_prefix_mapping, none, "cannot create scanner prefix mapping: '%0'", (StringRef))
488489

489490
REMARK(replay_output, none, "replay output file '%0': key '%1'", (StringRef, StringRef))
490491
REMARK(output_cache_miss, none, "cache miss output file '%0': key '%1'", (StringRef, StringRef))

include/swift/AST/ModuleDependencies.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/CAS/ObjectStore.h"
3737
#include "llvm/Support/Error.h"
3838
#include "llvm/Support/Mutex.h"
39+
#include "llvm/Support/PrefixMapper.h"
3940
#include "llvm/Support/VirtualFileSystem.h"
4041
#include <string>
4142
#include <unordered_map>
@@ -790,16 +791,18 @@ using ModuleDependenciesKindRefMap =
790791
/// Track swift dependency
791792
class SwiftDependencyTracker {
792793
public:
793-
SwiftDependencyTracker(llvm::cas::CachingOnDiskFileSystem &FS)
794-
: FS(FS.createProxyFS()) {}
794+
SwiftDependencyTracker(llvm::cas::CachingOnDiskFileSystem &FS,
795+
llvm::TreePathPrefixMapper *Mapper)
796+
: FS(FS.createProxyFS()), Mapper(Mapper) {}
795797

796798
void startTracking();
797-
void addCommonSearchPathDeps(const SearchPathOptions& Opts);
799+
void addCommonSearchPathDeps(const SearchPathOptions &Opts);
798800
void trackFile(const Twine &path) { (void)FS->status(path); }
799801
llvm::Expected<llvm::cas::ObjectProxy> createTreeFromDependencies();
800802

801803
private:
802804
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
805+
llvm::TreePathPrefixMapper *Mapper;
803806
};
804807

805808
// MARK: SwiftDependencyScanningService
@@ -840,6 +843,9 @@ class SwiftDependencyScanningService {
840843
/// The common dependencies that is needed for every swift compiler instance.
841844
std::vector<std::string> CommonDependencyFiles;
842845

846+
/// File prefix mapper.
847+
std::unique_ptr<llvm::TreePathPrefixMapper> Mapper;
848+
843849
/// The global file system cache.
844850
llvm::Optional<
845851
clang::tooling::dependencies::DependencyScanningFilesystemSharedCache>
@@ -894,11 +900,11 @@ class SwiftDependencyScanningService {
894900
return *CacheFS;
895901
}
896902

897-
llvm::Optional<SwiftDependencyTracker> createSwiftDependencyTracker() const {
903+
llvm::Optional<SwiftDependencyTracker> createSwiftDependencyTracker() {
898904
if (!CacheFS)
899905
return llvm::None;
900906

901-
return SwiftDependencyTracker(*CacheFS);
907+
return SwiftDependencyTracker(*CacheFS, Mapper.get());
902908
}
903909

904910
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getClangScanningFS() const {
@@ -912,6 +918,16 @@ class SwiftDependencyScanningService {
912918
return llvm::vfs::createPhysicalFileSystem();
913919
}
914920

921+
bool hasPathMapping() const {
922+
return Mapper && !Mapper->getMappings().empty();
923+
}
924+
llvm::TreePathPrefixMapper *getPrefixMapper() const { return Mapper.get(); }
925+
std::string remapPath(StringRef Path) const {
926+
if (!Mapper)
927+
return Path.str();
928+
return Mapper->mapToString(Path);
929+
}
930+
915931
/// Wrap the filesystem on the specified `CompilerInstance` with a
916932
/// caching `DependencyScanningWorkerFilesystem`
917933
void overlaySharedFilesystemCacheForCompilation(CompilerInstance &Instance);

include/swift/AST/ModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
namespace llvm {
3636
class FileCollectorBase;
37+
class TreePathPrefixMapper;
3738
namespace vfs {
3839
class OutputBackend;
3940
}
@@ -345,6 +346,7 @@ class ModuleLoader {
345346
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
346347
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
347348
InterfaceSubContextDelegate &delegate,
349+
llvm::TreePathPrefixMapper *mapper = nullptr,
348350
bool isTestableImport = false) = 0;
349351
};
350352

include/swift/AST/SearchPathOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ class SearchPathOptions {
461461
/// Don't look in for compiler-provided modules.
462462
bool SkipRuntimeLibraryImportPaths = false;
463463

464+
/// Scanner Prefix Mapper.
465+
std::vector<std::string> ScannerPrefixMapper;
466+
464467
/// When set, don't validate module system dependencies.
465468
///
466469
/// If a system header is modified and this is not set, the compiler will

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,18 @@ class ClangImporter final : public ClangModuleLoader {
434434

435435
void verifyAllModules() override;
436436

437+
using RemapPathCallback = llvm::function_ref<std::string(StringRef)>;
437438
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1> bridgeClangModuleDependencies(
438439
const clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
439-
StringRef moduleOutputPath);
440+
StringRef moduleOutputPath, RemapPathCallback remapPath = nullptr);
440441

441442
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
442443
getModuleDependencies(StringRef moduleName, StringRef moduleOutputPath,
443444
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
444445
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
445446
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
446447
InterfaceSubContextDelegate &delegate,
448+
llvm::TreePathPrefixMapper *mapper,
447449
bool isTestableImport = false) override;
448450

449451
void recordBridgingHeaderOptions(

include/swift/Frontend/CachingUtils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/CAS/ActionCache.h"
2121
#include "llvm/CAS/CASReference.h"
2222
#include "llvm/CAS/ObjectStore.h"
23+
#include "llvm/Support/PrefixMapper.h"
2324
#include "llvm/Support/VirtualFileSystem.h"
2425
#include "llvm/Support/VirtualOutputBackend.h"
2526
#include <memory>
@@ -59,6 +60,10 @@ llvm::Error storeCachedCompilerOutput(llvm::cas::ObjectStore &CAS,
5960
llvm::Expected<llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>>
6061
createCASFileSystem(llvm::cas::ObjectStore &CAS, ArrayRef<std::string> FSRoots,
6162
ArrayRef<std::string> IncludeTreeRoots);
62-
}
63+
64+
std::vector<std::string> remapPathsFromCommandLine(
65+
ArrayRef<std::string> Args,
66+
llvm::function_ref<std::string(StringRef)> RemapCallback);
67+
} // namespace swift
6368

6469
#endif

include/swift/Frontend/CompileJobCacheKey.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ createCompileJobCacheKeyForOutput(llvm::cas::ObjectStore &CAS,
4343
llvm::cas::ObjectRef BaseKey,
4444
StringRef ProducingInput,
4545
file_types::ID OutputType);
46-
}
46+
} // namespace swift
4747

4848
#endif

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ def backup_module_interface_path_EQ :
7979

8080
} // end let Flags = [FrontendOption, NoDriverOption, CacheInvariant]
8181

82+
def primary_file : Separate<["-"], "primary-file">,
83+
Flags<[FrontendOption, NoDriverOption, ArgumentIsPath]>,
84+
HelpText<"Produce output for this file, not the whole module">;
8285

8386
let Flags = [FrontendOption, NoDriverOption] in {
8487

8588
def triple : Separate<["-"], "triple">, Alias<target>;
8689

87-
def primary_file : Separate<["-"], "primary-file">,
88-
HelpText<"Produce output for this file, not the whole module">;
89-
9090
def frontend_parseable_output : Flag<["-"], "frontend-parseable-output">,
9191
HelpText<"Emit textual output in a parseable format">;
9292

include/swift/Option/Options.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,18 @@ def clang_scanner_module_cache_path : Separate<["-"], "clang-scanner-module-cach
18531853
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
18541854
HelpText<"Specifies the Clang dependency scanner module cache path">;
18551855

1856+
def scanner_prefix_map : Separate<["-"], "scanner-prefix-map">,
1857+
Flags<[FrontendOption, NewDriverOnlyOption]>,
1858+
HelpText<"Remap paths reported by dependency scanner">, MetaVarName<"<prefix=replacement>">;
1859+
1860+
def scanner_prefix_map_sdk : Separate<["-"], "scanner-prefix-map-sdk">,
1861+
Flags<[NewDriverOnlyOption]>,
1862+
HelpText<"Remap paths within SDK reported by dependency scanner">, MetaVarName<"<path>">;
1863+
1864+
def scanner_prefix_map_toolchain : Separate<["-"], "scanner-prefix-map-toolchain">,
1865+
Flags<[NewDriverOnlyOption]>,
1866+
HelpText<"Remap paths within toolchain directory reported by dependency scanner">, MetaVarName<"<path>">;
1867+
18561868
// END ONLY SUPPORTED IN NEW DRIVER
18571869

18581870

include/swift/Sema/SourceLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class SourceLoader : public ModuleLoader {
103103
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
104104
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
105105
InterfaceSubContextDelegate &delegate,
106+
llvm::TreePathPrefixMapper *mapper,
106107
bool isTestableImport) override;
107108
};
108109
}

0 commit comments

Comments
 (0)