Skip to content

Commit 0e2094d

Browse files
[SYCL] Implement eviction for in-memory program cache (#16062)
Fixes: CMPLRLLVM-27640, #2517 The PR implements LRU cache eviction policy for in-memory program caches. The high-level idea is to store programs in a linked-list, called eviction list. When the program is first added to the cache, it is also added to the eviction list. When a program is fetched from cache, we move the program to the end of the eviction list. So, that the programs at the beginning of the eviction list are always least recently used. When adding a new program to cache, we check if the size of the program cache exceeds the threshold, if so, we evict the program from cache and corresponding kernels from Kernel and fast kernel cache. This PR also adds a new environment variable, `SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD` that user can use to control the size of in-memory cache. By default, cache eviction is disabled.
1 parent f0899ff commit 0e2094d

File tree

9 files changed

+708
-11
lines changed

9 files changed

+708
-11
lines changed

sycl/doc/EnvironmentVariables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ compiler and runtime.
1414
| `SYCL_CACHE_DISABLE_PERSISTENT (deprecated)` | Any(\*) | Has no effect. |
1515
| `SYCL_CACHE_PERSISTENT` | Integer | Controls persistent device compiled code cache. Turns it on if set to '1' and turns it off if set to '0'. When cache is enabled SYCL runtime will try to cache and reuse JIT-compiled binaries. Default is off. |
1616
| `SYCL_CACHE_IN_MEM` | '1' or '0' | Enable ('1') or disable ('0') in-memory caching of device compiled code. When cache is enabled SYCL runtime will try to cache and reuse JIT-compiled binaries. Default is '1'. |
17+
| `SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD` | Positive integer | `SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD` accepts an integer that specifies the maximum size of the in-memory program cache in bytes. Eviction is performed when the cache size exceeds the threshold. The default value is 0 which means that eviction is disabled. |
1718
| `SYCL_CACHE_EVICTION_DISABLE` | Any(\*) | Switches persistent cache eviction off when the variable is set. |
1819
| `SYCL_CACHE_MAX_SIZE` | Positive integer | Persistent cache eviction is triggered once total size of cached images exceeds the value in megabytes (default - 8 192 for 8 GB). Set to 0 to disable size-based cache eviction. |
1920
| `SYCL_CACHE_THRESHOLD` | Positive integer | Persistent cache eviction threshold in days (default value is 7 for 1 week). Set to 0 for disabling time-based cache eviction. |

sycl/source/detail/config.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ CONFIG(SYCL_HOST_UNIFIED_MEMORY, 1, __SYCL_HOST_UNIFIED_MEMORY)
2727
// 260 (Windows limit) - 12 (filename) - 84 (cache directory structure)
2828
CONFIG(SYCL_CACHE_DIR, 164, __SYCL_CACHE_DIR)
2929
CONFIG(SYCL_CACHE_TRACE, 4, __SYCL_CACHE_TRACE)
30+
CONFIG(SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD, 16,
31+
__SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD)
3032
CONFIG(SYCL_CACHE_DISABLE_PERSISTENT, 1, __SYCL_CACHE_DISABLE_PERSISTENT)
3133
CONFIG(SYCL_CACHE_PERSISTENT, 1, __SYCL_CACHE_PERSISTENT)
3234
CONFIG(SYCL_CACHE_EVICTION_DISABLE, 1, __SYCL_CACHE_EVICTION_DISABLE)

sycl/source/detail/config.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,56 @@ template <> class SYCLConfig<SYCL_CACHE_TRACE> {
756756
}
757757
};
758758

759+
// SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD accepts an integer that specifies
760+
// the maximum size of the in-memory Program cache.
761+
// Cache eviction is performed when the cache size exceeds the threshold.
762+
// The thresholds are specified in bytes.
763+
// The default value is "0" which means that eviction is disabled.
764+
template <> class SYCLConfig<SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD> {
765+
using BaseT = SYCLConfigBase<SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD>;
766+
767+
public:
768+
static int get() { return getCachedValue(); }
769+
static void reset() { (void)getCachedValue(true); }
770+
771+
static int getProgramCacheSize() { return getCachedValue(); }
772+
773+
static bool isProgramCacheEvictionEnabled() {
774+
return getProgramCacheSize() > 0;
775+
}
776+
777+
private:
778+
static int getCachedValue(bool ResetCache = false) {
779+
const auto Parser = []() {
780+
const char *ValStr = BaseT::getRawValue();
781+
782+
// Disable eviction by default.
783+
if (!ValStr)
784+
return 0;
785+
786+
int CacheSize = 0;
787+
try {
788+
CacheSize = std::stoi(ValStr);
789+
if (CacheSize < 0)
790+
throw INVALID_CONFIG_EXCEPTION(BaseT, "Value must be non-negative");
791+
} catch (...) {
792+
std::string Msg = std::string{
793+
"Invalid input to SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD. Please try "
794+
"a positive integer."};
795+
throw exception(make_error_code(errc::runtime), Msg);
796+
}
797+
798+
return CacheSize;
799+
};
800+
801+
static auto EvictionThresholds = Parser();
802+
if (ResetCache)
803+
EvictionThresholds = Parser();
804+
805+
return EvictionThresholds;
806+
}
807+
};
808+
759809
#undef INVALID_CONFIG_EXCEPTION
760810

761811
} // namespace detail

0 commit comments

Comments
 (0)