aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
diff options
context:
space:
mode:
authorJan Svoboda <jan_svoboda@apple.com>2024-11-11 09:46:50 -0800
committerGitHub <noreply@github.com>2024-11-11 09:46:50 -0800
commit9d4837f47c48c634d4a0ac799188e1f5332495ef (patch)
tree0289dea3e70b2023885ceb555a2d6cd60c8c6e29 /clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
parentf895fc9550a207decc4b93dffb9d86ec8ac24985 (diff)
downloadllvm-9d4837f47c48c634d4a0ac799188e1f5332495ef.zip
llvm-9d4837f47c48c634d4a0ac799188e1f5332495ef.tar.gz
llvm-9d4837f47c48c634d4a0ac799188e1f5332495ef.tar.bz2
[clang][deps][modules] Allocate input file paths lazily (#114457)
This PR builds on top of #113984 and attempts to avoid allocating input file paths eagerly. Instead, the `InputFileInfo` type used by `ASTReader` now only holds `StringRef`s that point into the PCM file buffer, and the full input file paths get resolved on demand. The dependency scanner makes use of this in a bit of a roundabout way: `ModuleDeps` now only holds (an owning copy of) the short unresolved input file paths, which get resolved lazily. This can be a big win, I'm seeing up to a 5% speedup.
Diffstat (limited to 'clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp')
-rw-r--r--clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp41
1 files changed, 25 insertions, 16 deletions
diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index 637416c..2e97cac 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -21,6 +21,16 @@ using namespace clang;
using namespace tooling;
using namespace dependencies;
+void ModuleDeps::forEachFileDep(llvm::function_ref<void(StringRef)> Cb) const {
+ SmallString<0> PathBuf;
+ PathBuf.reserve(256);
+ for (StringRef FileDep : FileDeps) {
+ auto ResolvedFileDep =
+ ASTReader::ResolveImportedPath(PathBuf, FileDep, FileDepsBaseDir);
+ Cb(*ResolvedFileDep);
+ }
+}
+
const std::vector<std::string> &ModuleDeps::getBuildArguments() {
assert(!std::holds_alternative<std::monostate>(BuildInfo) &&
"Using uninitialized ModuleDeps");
@@ -596,6 +606,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
serialization::ModuleFile *MF =
MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
*M->getASTFile());
+ MD.FileDepsBaseDir = MF->BaseDirectory;
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
*MF, /*IncludeSystem=*/true,
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
@@ -604,9 +615,9 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
// actual on-disk module map file that allowed inferring the module,
// which is what we need for building the module explicitly
// Let's ignore this file.
- if (StringRef(IFI.Filename).ends_with("__inferred_module.map"))
+ if (IFI.UnresolvedImportedFilename.ends_with("__inferred_module.map"))
return;
- MDC.addFileDep(MD, IFI.Filename);
+ MDC.addFileDep(MD, IFI.UnresolvedImportedFilename);
});
llvm::DenseSet<const Module *> SeenDeps;
@@ -614,15 +625,20 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
addAllSubmoduleDeps(M, MD, SeenDeps);
addAllAffectingClangModules(M, MD, SeenDeps);
+ SmallString<0> PathBuf;
+ PathBuf.reserve(256);
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
*MF, /*IncludeSystem=*/true,
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
if (!(IFI.TopLevel && IFI.ModuleMap))
return;
- if (StringRef(IFI.FilenameAsRequested)
- .ends_with("__inferred_module.map"))
+ if (IFI.UnresolvedImportedFilenameAsRequested.ends_with(
+ "__inferred_module.map"))
return;
- MD.ModuleMapFileDeps.emplace_back(IFI.FilenameAsRequested);
+ auto ResolvedFilenameAsRequested = ASTReader::ResolveImportedPath(
+ PathBuf, IFI.UnresolvedImportedFilenameAsRequested,
+ MF->BaseDirectory);
+ MD.ModuleMapFileDeps.emplace_back(*ResolvedFilenameAsRequested);
});
CowCompilerInvocation CI =
@@ -779,23 +795,16 @@ static StringRef makeAbsoluteAndPreferred(CompilerInstance &CI, StringRef Path,
void ModuleDepCollector::addFileDep(StringRef Path) {
if (IsStdModuleP1689Format) {
// Within P1689 format, we don't want all the paths to be absolute path
- // since it may violate the tranditional make style dependencies info.
- FileDeps.push_back(std::string(Path));
+ // since it may violate the traditional make style dependencies info.
+ FileDeps.emplace_back(Path);
return;
}
llvm::SmallString<256> Storage;
Path = makeAbsoluteAndPreferred(ScanInstance, Path, Storage);
- FileDeps.push_back(std::string(Path));
+ FileDeps.emplace_back(Path);
}
void ModuleDepCollector::addFileDep(ModuleDeps &MD, StringRef Path) {
- if (IsStdModuleP1689Format) {
- MD.FileDeps.insert(Path);
- return;
- }
-
- llvm::SmallString<256> Storage;
- Path = makeAbsoluteAndPreferred(ScanInstance, Path, Storage);
- MD.FileDeps.insert(Path);
+ MD.FileDeps.emplace_back(Path);
}