Skip to content

[SYCL] use vector instead of map as a kernel cache container #18833

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
Jun 6, 2025
Merged
Show file tree
Hide file tree
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
20 changes: 16 additions & 4 deletions sycl/source/detail/kernel_name_based_cache_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,27 @@ struct FastKernelCacheVal {
};
using FastKernelCacheValPtr = std::shared_ptr<FastKernelCacheVal>;

using FastKernelSubcacheMapT =
emhash8::HashMap<FastKernelCacheKeyT, FastKernelCacheValPtr>;

using FastKernelSubcacheMutexT = SpinLock;
using FastKernelSubcacheReadLockT = std::lock_guard<FastKernelSubcacheMutexT>;
using FastKernelSubcacheWriteLockT = std::lock_guard<FastKernelSubcacheMutexT>;

struct FastKernelEntryT {
FastKernelCacheKeyT Key;
FastKernelCacheValPtr Value;

FastKernelEntryT(FastKernelCacheKeyT Key, const FastKernelCacheValPtr &Value)
: Key(Key), Value(Value) {}

FastKernelEntryT(const FastKernelEntryT &) = default;
FastKernelEntryT &operator=(const FastKernelEntryT &) = default;
FastKernelEntryT(FastKernelEntryT &&) = default;
FastKernelEntryT &operator=(FastKernelEntryT &&) = default;
};

using FastKernelSubcacheEntriesT = std::vector<FastKernelEntryT>;

struct FastKernelSubcacheT {
FastKernelSubcacheMapT Map;
FastKernelSubcacheEntriesT Entries;
FastKernelSubcacheMutexT Mutex;
};

Expand Down
49 changes: 31 additions & 18 deletions sycl/source/detail/kernel_program_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ class KernelProgramCache {
}

// Single subcache might be used by different contexts.
FastKernelSubcacheMapT &CacheMap = MSubcachePtr->Map;
// Remove all entries from the subcache that are associated with the
// current context.
FastKernelSubcacheEntriesT &Entries = MSubcachePtr->Entries;
FastKernelSubcacheWriteLockT Lock{MSubcachePtr->Mutex};
for (auto it = CacheMap.begin(); it != CacheMap.end();) {
if (it->first.second == MUrContext) {
it = CacheMap.erase(it);
} else {
++it;
}
}
Entries.erase(std::remove_if(Entries.begin(), Entries.end(),
[this](const FastKernelEntryT &Entry) {
return Entry.Key.second == MUrContext;
}),
Entries.end());
}

FastKernelSubcacheT &get() { return *MSubcachePtr; }
Expand Down Expand Up @@ -476,14 +476,21 @@ class KernelProgramCache {
KernelSubcacheHint = &It.first->second.get();
}

const FastKernelSubcacheMapT &SubcacheMap = KernelSubcacheHint->Map;
const FastKernelSubcacheEntriesT &SubcacheEntries =
KernelSubcacheHint->Entries;
FastKernelSubcacheReadLockT SubcacheLock{KernelSubcacheHint->Mutex};
ur_context_handle_t Context = getURContext();
auto It = SubcacheMap.find(FastKernelCacheKeyT(Device, Context));
if (It != SubcacheMap.end()) {
const FastKernelCacheKeyT RequiredKey(Device, Context);
// Search for the kernel in the subcache.
auto It = std::find_if(SubcacheEntries.begin(), SubcacheEntries.end(),
[&](const FastKernelEntryT &Entry) {
return Entry.Key == RequiredKey;
});
if (It != SubcacheEntries.end()) {
traceKernel("Kernel fetched.", KernelName, true);
return It->second;
return It->Value;
}

return FastKernelCacheValPtr();
}

Expand Down Expand Up @@ -516,8 +523,8 @@ class KernelProgramCache {

FastKernelSubcacheWriteLockT SubcacheLock{KernelSubcacheHint->Mutex};
ur_context_handle_t Context = getURContext();
KernelSubcacheHint->Map.emplace(FastKernelCacheKeyT(Device, Context),
CacheVal);
KernelSubcacheHint->Entries.emplace_back(
FastKernelCacheKeyT(Device, Context), CacheVal);
}

// Expects locked program cache
Expand Down Expand Up @@ -562,15 +569,21 @@ class KernelProgramCache {
bool RemoveSubcache = false;
{
FastKernelSubcacheWriteLockT SubcacheLock{Subcache.Mutex};
Subcache.Map.erase(
FastKernelCacheKeyT(FastCacheKey.second, Context));
Subcache.Entries.erase(
std::remove_if(
Subcache.Entries.begin(), Subcache.Entries.end(),
[&](const FastKernelEntryT &Entry) {
return Entry.Key == FastKernelCacheKeyT(
FastCacheKey.second, Context);
}),
Subcache.Entries.end());
traceKernel("Kernel evicted.", FastCacheKey.first, true);

// Remove the subcache wrapper from this kernel program cache if
// the subcache no longer contains entries for this context.
RemoveSubcache = std::none_of(
Subcache.Map.begin(), Subcache.Map.end(),
[&](const auto &It) { return It.first.second == Context; });
Subcache.Entries.begin(), Subcache.Entries.end(),
[&](const auto &It) { return It.Key.second == Context; });
}
if (RemoveSubcache)
MFastKernelCache.erase(FastKernelCacheItr);
Expand Down