Skip to content

Commit 810ded7

Browse files
committed
[clang][cas] Avoid calling freezeConfig for every cache key
With the libclang caching APIs, it is possible to configure CASOptions separately from the CAS used for scanning. In this setup, the CAS is not pre-cached in the CASOptions and ends up being re-opened for every cache key, which is expensive with some plugin CAS. Instead, get the hash identifier from the already open CAS and only freezeConfig when canonicalizing for an actual compilation. rdar://141555438 (cherry picked from commit 3c460cc)
1 parent 9c7f25b commit 810ded7

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

clang/lib/Frontend/CompileJobCacheKey.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ canonicalizeForCaching(llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
181181
//
182182
// TODO: Extract CASOptions.Path first if we need it later since it'll
183183
// disappear here.
184-
Invocation.getCASOpts().freezeConfig(Diags);
184+
Invocation.getCASOpts() = {};
185+
// Set the CASPath to the hash schema to match CASOptions::freezeConfig.
186+
Invocation.getCASOpts().CASPath =
187+
CAS.getContext().getHashSchemaIdentifier().str();
185188

186189
// TODO: Canonicalize DiagnosticOptions here to be "serialized" only. Pass in
187190
// a hook to mirror diagnostics to stderr (when writing there), and handle
@@ -194,8 +197,20 @@ canonicalizeForCaching(llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
194197
std::optional<llvm::cas::CASID> clang::canonicalizeAndCreateCacheKey(
195198
llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
196199
CompilerInvocation &Invocation, CompileJobCachingOptions &Opts) {
200+
// Preserve and freeze CASOptions so that we do not modify behaviour of
201+
// Invocation.getCASOpts().getOrCreateDatabases().
202+
CASOptions CASOpts(Invocation.getCASOpts());
203+
CASOpts.freezeConfig(Diags);
204+
197205
Opts = canonicalizeForCaching(CAS, Diags, Invocation);
198-
return createCompileJobCacheKeyImpl(CAS, Diags, Invocation);
206+
auto CacheKey = createCompileJobCacheKeyImpl(CAS, Diags, Invocation);
207+
if (!CacheKey)
208+
return std::nullopt;
209+
210+
assert(Invocation.getCASOpts().CASPath == CASOpts.CASPath &&
211+
"cas instance has incompatible hash with cas options");
212+
Invocation.getCASOpts() = std::move(CASOpts);
213+
return CacheKey;
199214
}
200215

201216
std::optional<llvm::cas::CASID>

0 commit comments

Comments
 (0)