Skip to content

Commit 8630fe2

Browse files
committed
[SYCL] Do not use current directory for JIT cache
Remove storing JIT cache files to current directory when corresponding environment variables are not set.
1 parent 807fdae commit 8630fe2

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

sycl/doc/EnvironmentVariables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ subject to change. Do not rely on these variables in production code.
3737
| `SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING` | Any(\*) | Disables automatic rounding-up of `parallel_for` invocation ranges. |
3838
| `SYCL_ENABLE_PCI` | Integer | When set to 1, enables obtaining the GPU PCI address when using the Level Zero backend. The default is 0. |
3939
| `SYCL_HOST_UNIFIED_MEMORY` | Integer | Enforce host unified memory support or lack of it for the execution graph builder. If set to 0, it is enforced as not supported by all devices. If set to 1, it is enforced as supported by all devices. |
40-
| `SYCL_CACHE_DIR` | Path | Path to persistent cache root directory. Default values are `%AppData%\libsycl_cache` for Windows and `$XDG_CACHE_HOME/libsycl_cache` on Linux, if `XDG_CACHE_HOME` is not set then `$HOME/.cache/libsycl_cache`. |
40+
| `SYCL_CACHE_DIR` | Path | Path to persistent cache root directory. Default values are `%AppData%\libsycl_cache` for Windows and `$XDG_CACHE_HOME/libsycl_cache` on Linux, if `XDG_CACHE_HOME` is not set then `$HOME/.cache/libsycl_cache`. When none of the environment variables are set SYCL persistent cache is disabled. |
4141
| `SYCL_CACHE_TRACE` | Any(\*) | If the variable is set, messages are sent to std::cerr when caching events or non-blocking failures happen (e.g. unable to access cache item file). |
4242
| `SYCL_CACHE_DISABLE_PERSISTENT (deprecated)` | Any(\*) | Has no effect. |
4343
| `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. |

sycl/doc/KernelProgramCache.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ The device code image are stored on file system using structure below:
341341
<n>.bin
342342
```
343343

344-
- `<cache_root>` - root directory storing cache files;
344+
- `<cache_root>` - root directory storing cache files, that depends on
345+
environment variables (see SYCL_CACHE_DIR description in the
346+
[EnvironmentVariables.md](EnvironmentVariables.md));
345347
- `<device_hash>` - hash out of device information used to identify target
346348
device;
347349
- `<device_image_hash>` - hash made out of device image used as input for the

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ void PersistentDeviceCodeCache::putItemToDisc(
7878
const SerializedObj &SpecConsts, const std::string &BuildOptionsString,
7979
const RT::PiProgram &NativePrg) {
8080

81-
if (!isImageCached(Img))
81+
std::string DirName =
82+
getCacheItemPath(Device, Img, SpecConsts, BuildOptionsString);
83+
84+
if (!isImageCached(Img) || DirName.empty())
8285
return;
8386

8487
auto Plugin = detail::getSyclObjImpl(Device)->getPlugin();
85-
std::string DirName =
86-
getCacheItemPath(Device, Img, SpecConsts, BuildOptionsString);
8788

8889
size_t i = 0;
8990
std::string FileName;
@@ -136,13 +137,10 @@ std::vector<std::vector<char>> PersistentDeviceCodeCache::getItemFromDisc(
136137
const device &Device, const RTDeviceBinaryImage &Img,
137138
const SerializedObj &SpecConsts, const std::string &BuildOptionsString) {
138139

139-
if (!isImageCached(Img))
140-
return {};
141-
142140
std::string Path =
143141
getCacheItemPath(Device, Img, SpecConsts, BuildOptionsString);
144142

145-
if (!OSUtil::isPathPresent(Path))
143+
if (!isImageCached(Img) || Path.empty() || !OSUtil::isPathPresent(Path))
146144
return {};
147145

148146
int i = 0;
@@ -315,6 +313,10 @@ std::string PersistentDeviceCodeCache::getCacheItemPath(
315313
const device &Device, const RTDeviceBinaryImage &Img,
316314
const SerializedObj &SpecConsts, const std::string &BuildOptionsString) {
317315
static std::string cache_root{getRootDir()};
316+
if (cache_root.empty()) {
317+
trace("Disable persistent cache due to unconfigured cache root.");
318+
return {};
319+
}
318320

319321
std::string ImgString{(const char *)Img.getRawData().BinaryStart,
320322
Img.getSize()};
@@ -379,6 +381,8 @@ bool PersistentDeviceCodeCache::isEnabled() {
379381
}
380382

381383
/* Returns path for device code cache root directory
384+
* If environment variables are not available return an empty string to identify
385+
* that cache is not available.
382386
*/
383387
std::string PersistentDeviceCodeCache::getRootDir() {
384388
static const char *RootDir = SYCLConfig<SYCL_CACHE_DIR>::get();
@@ -391,15 +395,16 @@ std::string PersistentDeviceCodeCache::getRootDir() {
391395
#if defined(__SYCL_RT_OS_LINUX)
392396
static const char *CacheDir = std::getenv("XDG_CACHE_HOME");
393397
static const char *HomeDir = std::getenv("HOME");
398+
if (!CacheDir && !HomeDir)
399+
return {};
394400
static std::string Res{
395-
std::string(CacheDir
396-
? CacheDir
397-
: (HomeDir ? std::string(HomeDir) + "/.cache" : ".")) +
401+
std::string(CacheDir ? CacheDir : (std::string(HomeDir) + "/.cache")) +
398402
DeviceCodeCacheDir};
399403
#else
400404
static const char *AppDataDir = std::getenv("AppData");
401-
static std::string Res{std::string(AppDataDir ? AppDataDir : ".") +
402-
DeviceCodeCacheDir};
405+
if (!AppDataDir)
406+
return {};
407+
static std::string Res{std::string(AppDataDir) + DeviceCodeCacheDir};
403408
#endif
404409
return Res;
405410
}

0 commit comments

Comments
 (0)