From 59ca873ca3e0c9aa093974b93f53334768103227 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Thu, 18 Sep 2025 09:25:26 -0700 Subject: [PATCH 1/2] [clang] Fix CAS initialization after upstream #158381 With the early initialization of the VFS, the CAS is now being initialized too late. This PR initializes CAS along with the VFS so that the CAS filesystem can sit at the bottom. --- clang/include/clang/Frontend/CompilerInstance.h | 6 ++++++ clang/lib/Frontend/CompileJobCache.cpp | 3 ++- clang/lib/Frontend/CompilerInstance.cpp | 4 ++++ clang/lib/Frontend/CompilerInvocation.cpp | 3 --- .../DependencyScanning/IncludeTreeActionController.cpp | 3 ++- clang/test/CAS/output-path-error.c | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index f21f5c29db3a1..ddffa0629ef64 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -530,6 +530,12 @@ class CompilerInstance : public ModuleLoader { /// Get the CAS, or create it using the configuration in CompilerInvocation. llvm::cas::ObjectStore &getOrCreateObjectStore(); llvm::cas::ActionCache &getOrCreateActionCache(); + std::shared_ptr getObjectStorePtr() const { + return CAS; + } + std::shared_ptr getActionCachePtr() const { + return ActionCache; + } /// @} /// @name Source Manager diff --git a/clang/lib/Frontend/CompileJobCache.cpp b/clang/lib/Frontend/CompileJobCache.cpp index f5f45b47f2d0c..580083d5cef89 100644 --- a/clang/lib/Frontend/CompileJobCache.cpp +++ b/clang/lib/Frontend/CompileJobCache.cpp @@ -296,7 +296,8 @@ std::optional CompileJobCache::initialize(CompilerInstance &Clang) { if (!CacheCompileJob) return std::nullopt; - std::tie(CAS, Cache) = Clang.createCASDatabases(); + CAS = Clang.getObjectStorePtr(); + Cache = Clang.getActionCachePtr(); if (!CAS || !Cache) return 1; // Exit with error! diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 238303ed963c3..b642d21a8f8cf 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -296,6 +296,10 @@ void CompilerInstance::createVirtualFileSystem( DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC, /*ShouldOwnClient=*/false); + std::tie(CAS, ActionCache) = + getInvocation().getCASOpts().getOrCreateDatabases( + Diags, /*CreateEmptyCASOnFailure=*/false); + VFS = createVFSFromCompilerInvocation(getInvocation(), Diags, std::move(BaseFS), CAS); // FIXME: Should this go into createVFSFromCompilerInvocation? diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 5c11a86711d79..fff5458f78847 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1536,9 +1536,6 @@ createBaseFS(const FileSystemOptions &FSOpts, const FrontendOptions &FEOpts, const CASOptions &CASOpts, DiagnosticsEngine &Diags, IntrusiveRefCntPtr BaseFS, std::shared_ptr OverrideCAS) { - if (!OverrideCAS) - return BaseFS; - if (FSOpts.CASFileSystemRootID.empty() && FEOpts.CASIncludeTreeID.empty()) return BaseFS; diff --git a/clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp b/clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp index e27bd3e218017..c6900f9b84290 100644 --- a/clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp +++ b/clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp @@ -282,10 +282,11 @@ void dependencies::addReversePrefixMappingFileSystem( llvm::PrefixMapper ReverseMapper; ReverseMapper.addInverseRange(PrefixMapper.getMappings()); ReverseMapper.sort(); - std::unique_ptr FS = + IntrusiveRefCntPtr FS = llvm::vfs::createPrefixMappingFileSystem( std::move(ReverseMapper), &ScanInstance.getVirtualFileSystem()); + ScanInstance.setVirtualFileSystem(FS); ScanInstance.getFileManager().setVirtualFileSystem(std::move(FS)); } diff --git a/clang/test/CAS/output-path-error.c b/clang/test/CAS/output-path-error.c index b217ba48402cf..a221f3bb29bf0 100644 --- a/clang/test/CAS/output-path-error.c +++ b/clang/test/CAS/output-path-error.c @@ -19,4 +19,4 @@ // RUN: cat %t/output.txt | FileCheck %s --check-prefix=ERROR // CACHE-MISS: remark: compile job cache miss -// ERROR: fatal error: CAS missing expected root-id +// ERROR: fatal error: CAS filesystem cannot be initialized from root-id 'llvmcas://{{.*}}': cannot get reference to root FS From 06cbfeebca01ae55677ae31d0107840fc6f3c41c Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Thu, 18 Sep 2025 11:10:57 -0700 Subject: [PATCH 2/2] createCASDatabases -> getOrCreateCASDatabases --- clang/include/clang/Frontend/CompilerInstance.h | 8 +------- clang/lib/Frontend/CompileJobCache.cpp | 3 +-- clang/lib/Frontend/CompilerInstance.cpp | 6 +++--- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index ddffa0629ef64..ad1c2cfdb2430 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -530,12 +530,6 @@ class CompilerInstance : public ModuleLoader { /// Get the CAS, or create it using the configuration in CompilerInvocation. llvm::cas::ObjectStore &getOrCreateObjectStore(); llvm::cas::ActionCache &getOrCreateActionCache(); - std::shared_ptr getObjectStorePtr() const { - return CAS; - } - std::shared_ptr getActionCachePtr() const { - return ActionCache; - } /// @} /// @name Source Manager @@ -1046,7 +1040,7 @@ class CompilerInstance : public ModuleLoader { std::pair, std::shared_ptr> - createCASDatabases(); + getOrCreateCASDatabases(); }; } // end namespace clang diff --git a/clang/lib/Frontend/CompileJobCache.cpp b/clang/lib/Frontend/CompileJobCache.cpp index 580083d5cef89..84e3ab22bc5b8 100644 --- a/clang/lib/Frontend/CompileJobCache.cpp +++ b/clang/lib/Frontend/CompileJobCache.cpp @@ -296,8 +296,7 @@ std::optional CompileJobCache::initialize(CompilerInstance &Clang) { if (!CacheCompileJob) return std::nullopt; - CAS = Clang.getObjectStorePtr(); - Cache = Clang.getActionCachePtr(); + std::tie(CAS, Cache) = Clang.getOrCreateCASDatabases(); if (!CAS || !Cache) return 1; // Exit with error! diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index b642d21a8f8cf..cf8a970067144 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -974,7 +974,7 @@ llvm::vfs::OutputBackend &CompilerInstance::getOrCreateOutputManager() { std::pair, std::shared_ptr> -CompilerInstance::createCASDatabases() { +CompilerInstance::getOrCreateCASDatabases() { // Create a new CAS databases from the CompilerInvocation. Future calls to // createFileManager() will use the same CAS. std::tie(CAS, ActionCache) = @@ -986,13 +986,13 @@ CompilerInstance::createCASDatabases() { llvm::cas::ObjectStore &CompilerInstance::getOrCreateObjectStore() { if (!CAS) - createCASDatabases(); + getOrCreateCASDatabases(); return *CAS; } llvm::cas::ActionCache &CompilerInstance::getOrCreateActionCache() { if (!ActionCache) - createCASDatabases(); + getOrCreateCASDatabases(); return *ActionCache; }