Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/source/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ template <> class SYCLConfig<SYCL_CACHE_IN_MEM> {
static bool getCachedValue(bool ResetCache = false) {
static bool Val = parseValue();
if (ResetCache) {
Val = BaseT::getRawValue();
Val = parseValue();
}
return Val;
}
Expand Down
66 changes: 41 additions & 25 deletions sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "detail/persistent_device_code_cache.hpp"
#include <detail/device_binary_image.hpp>
#include <gtest/gtest.h>
#include <helpers/ScopedEnvVar.hpp>
#include <helpers/UrMock.hpp>
#include <llvm/ADT/SmallString.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/IOSandbox.h>
#include <sycl/detail/os_util.hpp>
Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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<sycl::detail::SYCL_CACHE_PERSISTENT>::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<sycl::detail::SYCL_CACHE_MAX_SIZE>::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));
Comment on lines +161 to +163

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be an overkill - we never use a relative SYCL_CACHE_DIR in unit tests, so would not run into this problem.

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<sycl::detail::SYCL_CACHE_DIR>::reset();
}

Expand All @@ -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<sycl::detail::SYCL_CACHE_DIR>::reset();
}

Expand All @@ -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");
Expand All @@ -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()} {
Expand Down
17 changes: 7 additions & 10 deletions sycl/unittests/program_manager/MultipleDevsKernelBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<sycl::device> Devices =
Expand Down Expand Up @@ -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,
Expand Down
Loading