llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[libclang] Introduce an API to check whether a cached compilation is materialized

Open jansvoboda11 opened this issue 2 years ago • 3 comments

This PR adds new libclang API that allows clients to ask whether a cached compilation and its outputs are materialized in the CAS database. This is necessary during incremental builds, where other tasks may depend on the CAS outputs of the compilation at hand. Until now, the only way to implement this check was with:

CXCASCachedCompilation CachedCompilation = clang_experimental_cas_getCachedCompilation(/*...*/);
size_t NumOutputs = clang_experimental_cas_CachedCompilation_getNumOutputs(CachedCompilation);
for (size_t I = 0; I != NumOutputs; ++I)
  if (!clang_experimental_cas_CachedCompilation_isOutputMaterialized(CachedCompilation, I))
    return false;
return true;

This is problematic due to the fact that getCachedCompilation() actually loads the compilation object. If the database is an OnDiskGraphDB with FaultInPolicy::FullTree, all referenced objects (i.e. the outputs) are eagerly loaded too. This means that just performing this "is materialized" check will prevent those object ever being marked as garbage and pruned when the size limit is reached, which is not desirable. The new API never loads the objects - it instead uses the narrower ObjectStore::isMaterialized() API.

jansvoboda11 avatar Jul 12 '24 22:07 jansvoboda11

(Just a note that I'll be making this new implementation actually work in a separate PR that makes ObjectStore::isMaterialized() check the entire object graph, not just the given node.)

jansvoboda11 avatar Jul 12 '24 22:07 jansvoboda11

This means that just performing this "is materialized" check will prevent those object ever being marked as garbage and pruned when the size limit is reached, which is not desirable.

These are for compilations that the build system tracks as currently up-to-date based on source dependencies. If we prune them out it's going to cause another behavior that seems maybe more undesirable; compilations will be performed solely to populate the CAS again, seemingly without any reason from the perspective of the user (the user did not touch any source files that would cause those compilations to run), causing confusion and slower build times.

I'm kinda leaning that this would be worse than making sure that the outputs for the current compilations stay in the CAS.

akyrtzi avatar Jul 12 '24 22:07 akyrtzi

If the database is an OnDiskGraphDB with FaultInPolicy::FullTree, all referenced objects (i.e. the outputs) are eagerly loaded too. This means that just performing this "is materialized" check will prevent those object ever being marked as garbage and pruned when the size limit is reached, which is not desirable.

I'm not sure this is a real problem. The materialization check applies only to a build that was actually triggered, so it's still a signal that these objects are "recently used" and should be kept longer than other objects we haven't looked at recently.

That being said, if the materialization check is cheaper than loading the objects it might still be a good idea to speed up incremental builds.

benlangmuir avatar Jul 15 '24 16:07 benlangmuir