Skip to content

Added asynchronous compilation and in-memory/file cache to GpuCompiler. - #446

Open
AndreyPavlenko wants to merge 2 commits into
mainfrom
ap/cache
Open

AndreyPavlenko wants to merge 2 commits into
mainfrom
ap/cache

Conversation

@AndreyPavlenko

Copy link
Copy Markdown
Contributor

GpuCompiler::compile() now schedules the compilation on a thread pool and returns a shared_future. The pool size is set by the constructor concurrency argument (0 means all hardware threads) and can be overridden by the GC_THREADS environment variable.

The compiler calculates a BLAKE3 key from the source, the target arch, the IGC options, the wait mode, the Graph Compiler and ocloc versions. If the same source compilation is in progress, the pending shared_future is returned. Compiled programs are held weakly in memory and returned immediately on subsequent compilations of the same source.

If enabled (GC_FILE_CACHE_ON), the compiled binaries are also stored in a per-key directory of the file cache (GC_CACHE_DIR or the user cache directory), optionally with a checksum (GC_FILE_CACHE_CHECKSUM). CI enables the file cache, except for the IR dump runs.

@dchigarev dchigarev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async-compilation part looks good, left some comments.

CI enables the file cache, except for the IR dump runs.

Does it mean file-cache is enabled by default in CI? I would either disable it or always run two jobs: one with cache enabled and the other where it's disabled. So that we can both:

  • not depend on "corner" cases when something goes wrong with the caching and we get strange results in the "main" ci
  • test the caching mechanism by seeing if the result is different from the "no-cache" run

Comment thread lib/gc/Utils/Cache.h Resolved
Comment thread lib/gc/Utils/Cache.cpp Resolved
Comment thread lib/gc/Utils/Cache.cpp Outdated
Comment on lines +131 to +133
auto rmStaleDirs = [&now](StringRef root, std::chrono::hours staleTime,
StringRef skip = {}) {
std::error_code ec;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a third nested lambda here :)

I don't know, it's really hard to understand and verify the code's flow with all these immediately-invoked lambdas and their early returns (which is a legalized way to use goto semantic IMHO). I would prefer readability here (especially on a sensitive logic like this, where we delete directories on user's computer) and split this one mega-lambda into 3 (or even more) simpler functions that only do one thing at a time:

  • resolveCacheRoot
  • cleanupIfDue
  • removeStaleDirs

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified.

Comment thread lib/gc/Utils/Cache.cpp Outdated
}

// Clean up before resolving symlinks, so it never leaves the cache tree.
if (int interval = gcGetEnv("GC_CACHE_CLEAN_INTERVAL", cleanInterval);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the clean-up logic an optional feature that is disabled by default (e.g. make default value for GC_CACHE_CLEAN_INTERVAL to be 0 even when GC_CACHE_DIR is unset)?

I mean, libraries (pytorch, triton, pip, hf, triton) never clean their disk-cache automatically. Some of them have a dedicated method to do it manually (like pip cache purge) but it never happens automatically (especially on that small interval like 24 hours), I guess it should be either user's or OS'es responsibility to do it?

Some not very clever users (me) may store some of their temp files right at the cache directory (because they treat cached kernels in plain-text as kernel dumps and it appears to be a very handy place to store their experimental modified IRs right near the related dump), so we don't want to implicitly remove them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to clean the internal cache. 24 hours is the cleanup interval, but it cleans the entries older than GC_CACHE_MAX_HOURS (defaults to 30*24).

Comment thread lib/gc/Utils/Cache.cpp Resolved
Comment thread lib/gc/Utils/Cache.cpp Outdated
Comment on lines +46 to +47
if (auto ocloc = sys::findProgramByName("ocloc");
ocloc && !fs::createTemporaryFile("ocloc", "ver", out)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only way we can determine ocloc version? I mean, if LLVM is linked against a different version of ocloc it won't be correct? I usually redefine LD_LIBRARY_PATH to check compilation result between different IGC versions, so in this case oclocVersion would fail. Can we extract the version from the loaded version or it's impossible and we just have to accept the risk of incorrect cache-key in that corner case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best option is to get this info from upstream. I've created the PR llvm/llvm-project#243637

Comment thread lib/gc/GpuCompiler.cpp
Comment on lines +196 to +199
GpuCompiler &GpuCompiler::get() {
static GpuCompiler instance(gcGetEnv("GC_THREADS", 0u));
return instance;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the pool-size configurable via some parameter as well (e.g. build-opts or something)? So that we can potentially support OV's compilation_num_threads parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OV can just create its own instance of GC. Currently it uses this one, the shared instance.

Comment thread lib/gc/GpuCompiler.cpp
Comment on lines +49 to +50
: ctx(std::make_unique<mlir::MLIRContext>(
mlir::gc::getDialectRegistry())),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to the PR specifically, but have you considered having a single mlir-context between OV and GC? For example having a single static method in GC (like GraphCompiler::getContext()) so that we can reuse it between OV/GC and wouldn't need to dump&parse the module between contexts? Are there any downsides?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my initial intent, but I encountered context corruption issues. Seems, OV mutates the context, but I'm not sure. This requires additional investigation. Perhaps, it's doable.
Anyway, we need to print the module to calculate the hash.

Comment thread lib/gc/GpuCompiler.cpp Outdated
Comment on lines +82 to +88
gc::cache::Key key({
text,
opts.arch,
opts.igcOpts,
opts.wait ? "" : "!wait",
});
return cache.get(key, [&] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's easy to forget to add a new option to the key if we would extend otps. PassPipelineOptions should have a print that prints all the options, so that we'll be able to use it as a key, what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added GPUPipelineOptions::addCacheKeyOpts()

Comment thread lib/gc/GpuCompiler.cpp Resolved
@AndreyPavlenko
AndreyPavlenko force-pushed the ap/cache branch 2 times, most recently from 2af0f0c to 0ced049 Compare November 19, 2026 14:34
@AndreyPavlenko
AndreyPavlenko force-pushed the ap/cache branch 3 times, most recently from 5f5cb99 to f119d97 Compare November 30, 2026 20:12
GpuCompiler::compile() now schedules the compilation on a thread pool
and returns a shared_future. The pool size is set by the constructor
concurrency argument (0 means all hardware threads) and can be
overridden by the GC_THREADS environment variable.

The compiler calculates a BLAKE3 key from the source, the target arch,
the IGC options, the wait mode, the Graph Compiler and ocloc versions.
If the same source compilation is in progress, the pending
shared_future is returned. Compiled programs are held weakly in memory
and returned immediately on subsequent compilations of the same source.

If enabled (GC_FILE_CACHE_ON), the compiled binaries are also stored in
a per-key directory of the file cache (GC_CACHE_DIR or the user cache
directory), optionally with a checksum (GC_FILE_CACHE_CHECKSUM). CI
enables the file cache, except for the IR dump runs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants