16
16
#include < gtest/gtest.h>
17
17
#include < helpers/PiMock.hpp>
18
18
#include < llvm/Support/FileSystem.h>
19
+ #include < optional>
19
20
#include < vector>
20
21
21
22
#define ASSERT_NO_ERROR (x ) \
28
29
// TODO: Introduce common unit tests header and move it there
29
30
static void set_env (const char *name, const char *value) {
30
31
#ifdef _WIN32
31
- (void )_putenv_s (name, value);
32
+ (void )_putenv_s (name, value ? value : " " );
32
33
#else
33
- (void )setenv (name, value, /* overwrite*/ 1 );
34
+ if (value)
35
+ (void )setenv (name, value, /* overwrite*/ 1 );
36
+ else
37
+ (void )unsetenv (name);
34
38
#endif
35
39
}
36
40
@@ -88,12 +92,47 @@ class PersistenDeviceCodeCache : public ::testing::Test {
88
92
return _putenv_s (name, value);
89
93
}
90
94
#endif
95
+
96
+ std::optional<std::string> SYCLCachePersistentBefore;
97
+ bool SYCLCachePersistentChanged = false ;
98
+
99
+ // Caches the initial value of the SYCL_CACHE_PERSISTENT environment variable
100
+ // before overwriting it with the new value.
101
+ // Tear-down will reset the environment variable.
102
+ void SetSYCLCachePersistentEnv (const char *NewValue) {
103
+ char *SYCLCachePersistent = getenv (" SYCL_CACHE_PERSISTENT" );
104
+ // We can skip if the new value is the same as the old one.
105
+ if ((!NewValue && !SYCLCachePersistent) ||
106
+ (NewValue && SYCLCachePersistent &&
107
+ !strcmp (NewValue, SYCLCachePersistent)))
108
+ return ;
109
+
110
+ // Cache the old value of SYCL_CACHE_PERSISTENT if it is not already saved.
111
+ if (!SYCLCachePersistentChanged && SYCLCachePersistent)
112
+ SYCLCachePersistentBefore = std::string{SYCLCachePersistent};
113
+
114
+ // Set the environment variable and signal the configuration file and the
115
+ // persistent cache.
116
+ set_env (" SYCL_CACHE_PERSISTENT" , NewValue);
117
+ sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
118
+ detail::PersistentDeviceCodeCache::reparseConfig ();
119
+ SYCLCachePersistentChanged = true ;
120
+ }
121
+
91
122
virtual void SetUp () {
92
123
EXPECT_NE (getenv (" SYCL_CACHE_DIR" ), nullptr )
93
124
<< " Please set SYCL_CACHE_DIR environment variable pointing to cache "
94
125
" location." ;
95
126
}
96
127
128
+ virtual void TearDown () {
129
+ // If we changed the cache, set it back to the old value.
130
+ if (SYCLCachePersistentChanged)
131
+ SetSYCLCachePersistentEnv (SYCLCachePersistentBefore
132
+ ? SYCLCachePersistentBefore->c_str ()
133
+ : nullptr );
134
+ }
135
+
97
136
PersistenDeviceCodeCache () : Plt{default_selector ()} {
98
137
99
138
if (Plt.is_host () || Plt.get_backend () != backend::opencl) {
@@ -119,8 +158,7 @@ class PersistenDeviceCodeCache : public ::testing::Test {
119
158
return ;
120
159
}
121
160
122
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
123
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
161
+ SetSYCLCachePersistentEnv (" 1" );
124
162
125
163
std::string BuildOptions{" --concurrent-access=" +
126
164
std::to_string (ThreadCount)};
@@ -188,8 +226,7 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
188
226
return ;
189
227
}
190
228
191
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
192
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
229
+ SetSYCLCachePersistentEnv (" 1" );
193
230
194
231
std::string Key{' 1' , ' \0 ' , ' 3' , ' 4' , ' \0 ' };
195
232
std::vector<unsigned char > SpecConst (Key.begin (), Key.end ());
@@ -247,8 +284,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
247
284
return ;
248
285
}
249
286
250
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
251
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
287
+ SetSYCLCachePersistentEnv (" 1" );
252
288
253
289
std::string BuildOptions{" --corrupted-file" };
254
290
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
@@ -318,8 +354,7 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
318
354
return ;
319
355
}
320
356
321
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
322
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
357
+ SetSYCLCachePersistentEnv (" 1" );
323
358
324
359
std::string BuildOptions{" --obsolete-lock" };
325
360
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
@@ -375,8 +410,7 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
375
410
return ;
376
411
}
377
412
378
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
379
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
413
+ SetSYCLCachePersistentEnv (" 1" );
380
414
381
415
std::string BuildOptions{" --build-options" };
382
416
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
0 commit comments