@@ -404,7 +404,7 @@ class KernelProgramCache {
404
404
std::pair<std::shared_ptr<ProgramBuildResult>, bool >
405
405
getOrInsertProgram (const ProgramCacheKeyT &CacheKey) {
406
406
auto LockedCache = acquireCachedPrograms ();
407
- auto &ProgCache = LockedCache.get ();
407
+ ProgramCache &ProgCache = LockedCache.get ();
408
408
auto [It, DidInsert] = ProgCache.Cache .try_emplace (CacheKey, nullptr );
409
409
if (DidInsert) {
410
410
It->second = std::make_shared<ProgramBuildResult>(getAdapter ());
@@ -426,7 +426,7 @@ class KernelProgramCache {
426
426
bool insertBuiltProgram (const ProgramCacheKeyT &CacheKey,
427
427
ur_program_handle_t Program) {
428
428
auto LockedCache = acquireCachedPrograms ();
429
- auto &ProgCache = LockedCache.get ();
429
+ ProgramCache &ProgCache = LockedCache.get ();
430
430
auto [It, DidInsert] = ProgCache.Cache .try_emplace (CacheKey, nullptr );
431
431
if (DidInsert) {
432
432
It->second = std::make_shared<ProgramBuildResult>(getAdapter (),
@@ -491,7 +491,7 @@ class KernelProgramCache {
491
491
// Save kernel in fast cache only if the corresponding program is also
492
492
// in the cache.
493
493
auto LockedCache = acquireCachedPrograms ();
494
- auto &ProgCache = LockedCache.get ();
494
+ ProgramCache &ProgCache = LockedCache.get ();
495
495
if (ProgCache.ProgramSizeMap .find (CacheVal->MProgramHandle ) ==
496
496
ProgCache.ProgramSizeMap .end ())
497
497
return ;
@@ -631,7 +631,7 @@ class KernelProgramCache {
631
631
while (CurrCacheSize > DesiredCacheSize && !MEvictionList.empty ()) {
632
632
ProgramCacheKeyT CacheKey = ProgramEvictionList.front ();
633
633
auto LockedCache = acquireCachedPrograms ();
634
- auto &ProgCache = LockedCache.get ();
634
+ ProgramCache &ProgCache = LockedCache.get ();
635
635
CurrCacheSize = removeProgramByKey (CacheKey, ProgCache);
636
636
// Remove the program from the eviction list.
637
637
MEvictionList.popFront ();
@@ -748,15 +748,23 @@ class KernelProgramCache {
748
748
// /
749
749
// / \return a pointer to cached build result, return value must not be
750
750
// / nullptr.
751
+ // /
752
+ // / Note that build result might be immediately evicted (if it's bigger than
753
+ // / current threshold), so the caller *must* assume (potentially shared)
754
+ // / ownership. In other words, `std::shared_ptr` in the return type is
755
+ // / unavoidable.
751
756
template <errc Errc, typename GetCachedBuildFT, typename BuildFT,
752
757
typename EvictFT = void *>
753
- auto getOrBuild (GetCachedBuildFT &&GetCachedBuild, BuildFT &&Build,
754
- EvictFT &&EvictFunc = nullptr ) {
758
+ auto /* std::shared_ptr<BuildResult> */
759
+ getOrBuild (GetCachedBuildFT &&GetCachedBuild, BuildFT &&Build,
760
+ EvictFT &&EvictFunc = nullptr ) {
755
761
using BuildState = KernelProgramCache::BuildState;
756
762
constexpr size_t MaxAttempts = 2 ;
757
763
for (size_t AttemptCounter = 0 ;; ++AttemptCounter) {
758
- auto Res = GetCachedBuild ();
764
+ auto /* std::pair<std::shared_ptr<BuildResult>, bool> */ Res =
765
+ GetCachedBuild ();
759
766
auto &BuildResult = Res.first ;
767
+ assert (BuildResult != nullptr );
760
768
BuildState Expected = BuildState::BS_Initial;
761
769
BuildState Desired = BuildState::BS_InProgress;
762
770
if (!BuildResult->State .compare_exchange_strong (Expected, Desired)) {
@@ -825,7 +833,7 @@ class KernelProgramCache {
825
833
826
834
void removeAllRelatedEntries (uint32_t ImageId) {
827
835
auto LockedCache = acquireCachedPrograms ();
828
- auto &ProgCache = LockedCache.get ();
836
+ ProgramCache &ProgCache = LockedCache.get ();
829
837
830
838
auto It = std::find_if (
831
839
ProgCache.KeyMap .begin (), ProgCache.KeyMap .end (),
0 commit comments