Skip to content

[clang][cas] Avoid calling freezeConfig for every cache key #9803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 43 additions & 36 deletions clang/lib/Frontend/CompileJobCacheKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ canonicalizeForCaching(llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
//
// TODO: Extract CASOptions.Path first if we need it later since it'll
// disappear here.
Invocation.getCASOpts().freezeConfig(Diags);
Invocation.getCASOpts() = {};
// Set the CASPath to the hash schema to match CASOptions::freezeConfig.
Invocation.getCASOpts().CASPath =
CAS.getContext().getHashSchemaIdentifier().str();

// TODO: Canonicalize DiagnosticOptions here to be "serialized" only. Pass in
// a hook to mirror diagnostics to stderr (when writing there), and handle
Expand All @@ -223,47 +226,51 @@ clang::canonicalizeAndCreateCacheKeys(ObjectStore &CAS,
DiagnosticsEngine &Diags,
CompilerInvocation &CI,
CompileJobCachingOptions &Opts) {
if (!CI.getFrontendOpts().CASInputFileCacheKey.empty()) {
Opts = canonicalizeForCaching(CAS, Diags, CI);
auto CacheKey = createCompileJobCacheKeyImpl(CAS, Diags, CI);
if (!CacheKey)
return std::nullopt;

auto ID = CAS.parseID(CI.getFrontendOpts().CASInputFileCacheKey);
if (!ID) {
Diags.Report(diag::err_cas_cannot_parse_input_cache_key)
<< CI.getFrontendOpts().CASInputFileCacheKey;
return std::nullopt;
}
auto Value = Cache.get(*ID);
if (!Value) {
Diags.Report(diag::err_cas_cannot_lookup_input_cache_key)
<< Value.takeError();
return std::nullopt;
}
if (!*Value) {
Diags.Report(diag::err_cas_missing_input_cache_entry)
<< llvm::cas::ObjectStore::createUnknownObjectError(*ID);
return std::nullopt;
}
// Preserve and freeze CASOptions so that we do not modify behaviour of
// Invocation.getCASOpts().getOrCreateDatabases().
CASOptions CASOpts(CI.getCASOpts());
CASOpts.freezeConfig(Diags);

CI.getFrontendOpts().CASInputFileCASID = Value.get()->toString();
CI.getFrontendOpts().CASInputFileCacheKey.clear();
Opts = canonicalizeForCaching(CAS, Diags, CI);
auto CacheKey = createCompileJobCacheKeyImpl(CAS, Diags, CI);
if (!CacheKey)
return std::nullopt;

CompilerInvocation CICopy = CI;
(void)canonicalizeForCaching(CAS, Diags, CICopy);
auto CanonicalCacheKey = createCompileJobCacheKeyImpl(CAS, Diags, CICopy);
if (!CanonicalCacheKey)
return std::nullopt;
assert(CI.getCASOpts().CASPath == CASOpts.CASPath &&
"cas instance has incompatible hash with cas options");
CI.getCASOpts() = std::move(CASOpts);

return std::make_pair(*CacheKey, *CanonicalCacheKey);
if (CI.getFrontendOpts().CASInputFileCacheKey.empty())
return std::make_pair(*CacheKey, *CacheKey);

auto ID = CAS.parseID(CI.getFrontendOpts().CASInputFileCacheKey);
if (!ID) {
Diags.Report(diag::err_cas_cannot_parse_input_cache_key)
<< CI.getFrontendOpts().CASInputFileCacheKey;
return std::nullopt;
}
auto Value = Cache.get(*ID);
if (!Value) {
Diags.Report(diag::err_cas_cannot_lookup_input_cache_key)
<< Value.takeError();
return std::nullopt;
}
if (!*Value) {
Diags.Report(diag::err_cas_missing_input_cache_entry)
<< llvm::cas::ObjectStore::createUnknownObjectError(*ID);
return std::nullopt;
}

Opts = canonicalizeForCaching(CAS, Diags, CI);
auto CacheKey = createCompileJobCacheKeyImpl(CAS, Diags, CI);
if (!CacheKey)
CI.getFrontendOpts().CASInputFileCASID = Value.get()->toString();
CI.getFrontendOpts().CASInputFileCacheKey.clear();

CompilerInvocation CICopy = CI;
(void)canonicalizeForCaching(CAS, Diags, CICopy);
auto CanonicalCacheKey = createCompileJobCacheKeyImpl(CAS, Diags, CICopy);
if (!CanonicalCacheKey)
return std::nullopt;
return std::make_pair(*CacheKey, *CacheKey);

return std::make_pair(*CacheKey, *CanonicalCacheKey);
}

std::optional<llvm::cas::CASID>
Expand Down