From 5a1ee6fe645120b36d9222fb979d02cfede7a90b Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Mon, 19 Oct 2026 15:50:04 -0700 Subject: [PATCH] [SYCL][Unit] Fix persistent cache unit tests --- sycl/source/detail/config.hpp | 2 +- .../PersistentDeviceCodeCache.cpp | 66 ++++++++++++------- .../MultipleDevsKernelBundle.cpp | 17 ++--- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 883e741ddbf6d..ece1734a58a84 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -645,7 +645,7 @@ template <> class SYCLConfig { static bool getCachedValue(bool ResetCache = false) { static bool Val = parseValue(); if (ResetCache) { - Val = BaseT::getRawValue(); + Val = parseValue(); } return Val; } diff --git a/sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp b/sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp index 091b4e18107ed..0d460279f35c4 100644 --- a/sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp +++ b/sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp @@ -11,7 +11,9 @@ #include "detail/persistent_device_code_cache.hpp" #include #include +#include #include +#include #include #include #include @@ -29,18 +31,6 @@ << "error message: " << EC.message() << "\n"; \ } -// TODO: Introduce common unit tests header and move it there -static void set_env(const char *name, const char *value) { -#ifdef _WIN32 - (void)_putenv_s(name, value ? value : ""); -#else - if (value) - (void)setenv(name, value, /*overwrite*/ 1); - else - (void)unsetenv(name); -#endif -} - namespace { using namespace sycl; @@ -114,6 +104,12 @@ class PersistentDeviceCodeCache std::string RootSYCLCacheDir; + // Cache directory of the current test, see SetTestSYCLCacheDirEnv(). + static constexpr const char *TestCacheDirEnvVar = + "SYCL_UNITTEST_PERSISTENT_CACHE_TEST_DIR"; + std::string TestCacheDir; + bool OwnsTestCacheDir = false; + // This test makes many calls to llvm::sys::fs calls, which as of RFC // https://discourse.llvm.org/t/rfc-file-system-sandboxing-in-clang-llvm/88791 // now errors upon using llvm::sys::fs calls without disabling the sandbox. @@ -139,23 +135,37 @@ class PersistentDeviceCodeCache // Set the environment variable and signal the configuration file and the // persistent cache. - set_env("SYCL_CACHE_PERSISTENT", NewValue); + unittest::set_env("SYCL_CACHE_PERSISTENT", NewValue); sycl::detail::SYCLConfig::reset(); SYCLCachePersistentChanged = true; } // Set SYCL_CACHE_MAX_SIZE. void SetDiskCacheEvictionEnv(const char *NewValue) { - set_env("SYCL_CACHE_MAX_SIZE", NewValue); + unittest::set_env("SYCL_CACHE_MAX_SIZE", NewValue); sycl::detail::SYCLConfig::reset(); } - void AppendToSYCLCacheDirEnv(const char *SubDir) { - std::string NewSYCLCacheDirPath{RootSYCLCacheDir}; - if (NewSYCLCacheDirPath.back() != '\\' && NewSYCLCacheDirPath.back() != '/') - NewSYCLCacheDirPath += '/'; - NewSYCLCacheDirPath += SubDir; - set_env("SYCL_CACHE_DIR", NewSYCLCacheDirPath.c_str()); + // Points SYCL_CACHE_DIR to a cache directory owned by the current test. + // + // The directory is unique per test *and* per process: the same test may be + // run by several processes at once (e.g. concurrent test runs sharing + // SYCL_CACHE_DIR) and those must not remove or overwrite each other's cache + // items. + void SetTestSYCLCacheDirEnv() { + if (const char *InheritedDir = getenv(TestCacheDirEnvVar); + InheritedDir && *InheritedDir) { + TestCacheDir = InheritedDir; + } else { + llvm::SmallString<256> UniqueDir; + ASSERT_NO_ERROR(llvm::sys::fs::create_directories(RootSYCLCacheDir)); + ASSERT_NO_ERROR(llvm::sys::fs::createUniqueDirectory( + RootSYCLCacheDir + "/pdcc", UniqueDir)); + TestCacheDir = std::string{UniqueDir}; + OwnsTestCacheDir = true; + unittest::set_env(TestCacheDirEnvVar, TestCacheDir.c_str()); + } + unittest::set_env("SYCL_CACHE_DIR", TestCacheDir.c_str()); sycl::detail::SYCLConfig::reset(); } @@ -178,7 +188,7 @@ class PersistentDeviceCodeCache } void ResetSYCLCacheDirEnv() { - set_env("SYCL_CACHE_DIR", RootSYCLCacheDir.c_str()); + unittest::set_env("SYCL_CACHE_DIR", RootSYCLCacheDir.c_str()); sycl::detail::SYCLConfig::reset(); } @@ -187,10 +197,11 @@ class PersistentDeviceCodeCache FAIL() << "Please set SYCL_CACHE_DIR environment variable pointing to " "cache location."; - // Append the test name to the cache dir to prevent conflicts with other - // tests running in parallel. - AppendToSYCLCacheDirEnv( - ::testing::UnitTest::GetInstance()->current_test_info()->name()); + // Use a separate cache directory to prevent conflicts with other tests + // running in parallel. + SetTestSYCLCacheDirEnv(); + if (HasFatalFailure()) + return; // Enable persistent cache SetSYCLCachePersistentEnv("1"); @@ -206,6 +217,11 @@ class PersistentDeviceCodeCache // Reset SYCL_CACHE_MAX_SIZE. SetDiskCacheEvictionEnv(nullptr); ResetSYCLCacheDirEnv(); + + if (OwnsTestCacheDir) { + unittest::set_env(TestCacheDirEnvVar, nullptr); + (void)llvm::sys::fs::remove_directories(TestCacheDir); + } } PersistentDeviceCodeCache() : Mock{}, Plt{sycl::platform()} { diff --git a/sycl/unittests/program_manager/MultipleDevsKernelBundle.cpp b/sycl/unittests/program_manager/MultipleDevsKernelBundle.cpp index 3c1f667980cb3..ac45ebfdacab2 100644 --- a/sycl/unittests/program_manager/MultipleDevsKernelBundle.cpp +++ b/sycl/unittests/program_manager/MultipleDevsKernelBundle.cpp @@ -489,12 +489,12 @@ static ur_result_t redefinedurProgramGetInfo(void *pParams) { return UR_RESULT_SUCCESS; } -// https://github.com/intel/llvm/issues/18122 -TEST_P(MultipleDevsKernelBundleTest, DISABLED_PersistentCache) { - // Create temporary directory for the persistent cache in the directory of the - // test binary. - std::string PersistentCachePath = sycl::detail::OSUtil::getCurrentDSODir() + - detail::OSUtil::DirSep + "persistent_cache"; +TEST_P(MultipleDevsKernelBundleTest, PersistentCache) { + llvm::SmallString<256> PersistentCachePath; + ASSERT_NO_ERROR(llvm::sys::fs::createUniqueDirectory( + sycl::detail::OSUtil::getCurrentDSODir() + detail::OSUtil::DirSep + + "persistent_cache", + PersistentCachePath)); // Set environment variables to enable persistent cache and set the cache // path. ScopedEnvVar var1("SYCL_CACHE_PERSISTENT", "1", @@ -509,9 +509,6 @@ TEST_P(MultipleDevsKernelBundleTest, DISABLED_PersistentCache) { mock::getCallbacks().set_after_callback("urProgramGetInfo", &redefinedurProgramGetInfo); - std::string CacheRoot = detail::PersistentDeviceCodeCache::getRootDir(); - ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(CacheRoot)); - ASSERT_NO_ERROR(llvm::sys::fs::create_directories(CacheRoot)); // Get devices and create a context with at least 3 devices std::vector Devices = @@ -549,7 +546,7 @@ TEST_P(MultipleDevsKernelBundleTest, DISABLED_PersistentCache) { // Verify the number of urProgramCreateWithBinary calls EXPECT_EQ(ProgramCreateWithBinaryCounter, 1) << "Expect 1 urProgramCreateWithBinary calls"; - ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(CacheRoot)); + ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(PersistentCachePath)); } INSTANTIATE_TEST_SUITE_P(MultipleDevsKernelBundleTestInstance,