@@ -913,6 +913,51 @@ static std::string GetClangModulesCacheProperty() {
913913 return std::string (path);
914914}
915915
916+ static void ConfigureCASStorage (SwiftASTContext *m_ast_context,
917+ FileSpec CandidateConfigSearchPath) {
918+ // Config CAS from properties.
919+ llvm::cas::CASConfiguration cas_config;
920+ cas_config.CASPath =
921+ ModuleList::GetGlobalModuleListProperties ().GetCASOnDiskPath ().GetPath ();
922+ cas_config.PluginPath =
923+ ModuleList::GetGlobalModuleListProperties ().GetCASPluginPath ().GetPath ();
924+ cas_config.PluginOptions =
925+ ModuleList::GetGlobalModuleListProperties ().GetCASPluginOptions ();
926+
927+ auto &m_description = m_ast_context->GetDescription ();
928+
929+ if (!cas_config.CASPath .empty ()) {
930+ if (auto maybe_cas = cas_config.createDatabases ()) {
931+ m_ast_context->SetCASStorage (std::move (maybe_cas->first ),
932+ std::move (maybe_cas->second ));
933+ m_ast_context->GetCASOptions ().Config = cas_config;
934+ LOG_PRINTF (GetLog (LLDBLog::Types),
935+ " Setup CAS from module list properties with cas path: %s" ,
936+ cas_config.CASPath .c_str ());
937+ return ;
938+ } else
939+ llvm::consumeError (maybe_cas.takeError ());
940+ }
941+
942+ // Try search from candidiate path.
943+ auto search_config = llvm::cas::CASConfiguration::createFromSearchConfigFile (
944+ CandidateConfigSearchPath.GetPath ());
945+ if (!search_config)
946+ return ;
947+
948+ if (auto maybe_cas = search_config->second .createDatabases ()) {
949+ m_ast_context->SetCASStorage (std::move (maybe_cas->first ),
950+ std::move (maybe_cas->second ));
951+ m_ast_context->GetCASOptions ().Config = search_config->second ;
952+ LOG_PRINTF (GetLog (LLDBLog::Types), " Setup CAS from cas config file: %s" ,
953+ search_config->first .c_str ());
954+ return ;
955+ } else
956+ llvm::consumeError (maybe_cas.takeError ());
957+
958+ return ;
959+ }
960+
916961SwiftASTContext::ScopedDiagnostics::ScopedDiagnostics (
917962 swift::DiagnosticConsumer &consumer)
918963 : m_consumer(consumer),
@@ -1930,41 +1975,80 @@ void SwiftASTContext::AddExtraClangCC1Args(
19301975 return ;
19311976 }
19321977
1933- // Clear module cache key and other CAS options to load modules from disk
1934- // directly.
1935- invocation.getFrontendOpts ().ModuleCacheKeys .clear ();
1936- invocation.getCASOpts () = clang::CASOptions ();
1937-
1938- // Ignore CAS info inside modules when loading.
1939- invocation.getFrontendOpts ().ModuleLoadIgnoreCAS = true ;
1940-
19411978 // Add options to allow clang importer to do implicit module build.
19421979 invocation.getLangOpts ().ImplicitModules = true ;
19431980 invocation.getHeaderSearchOpts ().ImplicitModuleMaps = true ;
19441981 invocation.getHeaderSearchOpts ().ModuleCachePath =
19451982 GetCompilerInvocation ().getClangModuleCachePath ().str ();
19461983
1947- // Remove non-existing modules in a systematic way.
1948- auto CheckFileExists = [&](const std::string &file) -> bool {
1949- if (llvm::sys::fs::exists (file))
1950- return true ;
1951- std::string warn;
1952- llvm::raw_string_ostream (warn)
1953- << " Nonexistent explicit module file " << file;
1954- AddDiagnostic (eSeverityWarning, warn);
1955- return false ;
1956- };
1957- for (auto it = invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .begin ();
1958- it != invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .end ();) {
1959- if (!CheckFileExists (it->second ))
1960- it = invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .erase (it);
1961- else
1962- ++it;
1984+ bool use_cas_module = m_cas && m_action_cache;
1985+ if (use_cas_module) {
1986+ // Load from CAS.
1987+ invocation.getCASOpts ().CASPath = GetCASOptions ().Config .CASPath ;
1988+ invocation.getCASOpts ().PluginPath = GetCASOptions ().Config .PluginPath ;
1989+ invocation.getCASOpts ().PluginOptions =
1990+ GetCASOptions ().Config .PluginOptions ;
1991+
1992+ // Check the module availability in CAS, if not, fallback to regular load.
1993+ auto CheckModuleInCAS = [&](const std::string &key) {
1994+ auto id = m_cas->parseID (key);
1995+ if (!id) {
1996+ llvm::consumeError (id.takeError ());
1997+ return false ;
1998+ }
1999+ auto lookup = m_action_cache->get (*id);
2000+ if (!lookup) {
2001+ llvm::consumeError (lookup.takeError ());
2002+ return false ;
2003+ }
2004+ return (bool )*lookup;
2005+ };
2006+
2007+ use_cas_module = llvm::all_of (
2008+ invocation.getFrontendOpts ().ModuleCacheKeys , [&](const auto &entry) {
2009+ auto exist = CheckModuleInCAS (entry.second );
2010+ if (!exist) {
2011+ LOG_PRINTF (GetLog (LLDBLog::Types),
2012+ " module '%s' cannot be load "
2013+ " from CAS using key: %s, fallback to "
2014+ " load from file system" ,
2015+ entry.first .c_str (), entry.second .c_str ());
2016+ }
2017+ return exist;
2018+ });
2019+ }
2020+
2021+ if (!use_cas_module) {
2022+ // Clear module cache key and other CAS options to load modules from disk
2023+ // directly.
2024+ invocation.getFrontendOpts ().ModuleCacheKeys .clear ();
2025+ invocation.getCASOpts () = clang::CASOptions ();
2026+
2027+ // Ignore CAS info inside modules when loading.
2028+ invocation.getFrontendOpts ().ModuleLoadIgnoreCAS = true ;
2029+
2030+ // Remove non-existing modules in a systematic way.
2031+ auto CheckFileExists = [&](const std::string &file) -> bool {
2032+ if (llvm::sys::fs::exists (file))
2033+ return true ;
2034+ std::string warn;
2035+ llvm::raw_string_ostream (warn)
2036+ << " Nonexistent explicit module file " << file;
2037+ AddDiagnostic (eSeverityWarning, warn);
2038+ return false ;
2039+ };
2040+ for (auto it = invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .begin ();
2041+ it != invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .end ();) {
2042+ if (!CheckFileExists (it->second ))
2043+ it = invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .erase (it);
2044+ else
2045+ ++it;
2046+ }
2047+ invocation.getFrontendOpts ().ModuleFiles .erase (
2048+ llvm::remove_if (invocation.getFrontendOpts ().ModuleFiles ,
2049+ [&](const auto &mod) { return !CheckFileExists (mod); }),
2050+ invocation.getFrontendOpts ().ModuleFiles .end ());
19632051 }
1964- invocation.getFrontendOpts ().ModuleFiles .erase (
1965- llvm::remove_if (invocation.getFrontendOpts ().ModuleFiles ,
1966- [&](const auto &mod) { return !CheckFileExists (mod); }),
1967- invocation.getFrontendOpts ().ModuleFiles .end ());
19682052
19692053 invocation.generateCC1CommandLine (
19702054 [&](const llvm::Twine &arg) { dest.push_back (arg.str ()); });
@@ -2546,6 +2630,8 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25462630 }
25472631 }
25482632
2633+ ConfigureCASStorage (swift_ast_sp.get (), module .GetFileSpec ());
2634+
25492635 // The serialized triple is the triple of the last binary
25502636 // __swiftast section that was processed. Instead of relying on
25512637 // the section contents order, we overwrite the triple in the
@@ -3027,6 +3113,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
30273113 }
30283114 }
30293115
3116+ ConfigureCASStorage (swift_ast_sp.get (), sc.module_sp ->GetFileSpec ());
3117+
30303118 std::string resource_dir = HostInfo::GetSwiftResourceDir (
30313119 triple, swift_ast_sp->GetPlatformSDKPath ());
30323120 ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
0 commit comments