diff --git a/sycl/source/detail/config.cpp b/sycl/source/detail/config.cpp index 63e20207962f9..663747396ad82 100644 --- a/sycl/source/detail/config.cpp +++ b/sycl/source/detail/config.cpp @@ -180,6 +180,34 @@ const std::array, 8> &getSyclBeMap() { {"*", backend::all}}}; return SyclBeMap; } +namespace { + +unsigned int parseLevel(const char *ValStr) { + unsigned int intVal = 0; + + if (ValStr) { + try { + intVal = std::stoul(ValStr); + } catch (...) { + // If the value is not null and not a number, it is considered + // to enable disk cache tracing. This is the legacy behavior. + intVal = 1; + } + } + + // Legacy behavior. + if (intVal > 7) + intVal = 1; + + return intVal; +} + +} // namespace + +void SYCLConfigTrace::reset() { Level = parseLevel(BaseT::getRawValue()); } + +unsigned int SYCLConfigTrace::Level = + parseLevel(SYCLConfigTrace::BaseT::getRawValue()); } // namespace detail } // namespace _V1 diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 2eb3716c76a05..79cb8ab6f242c 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -709,52 +709,19 @@ template <> class SYCLConfig { // tracing of the corresponding caches. If the input value is not null and // not a valid number, the disk cache tracing will be enabled (depreciated // behavior). The default value is 0 and no tracing is enabled. -template <> class SYCLConfig { +class SYCLConfigTrace { using BaseT = SYCLConfigBase; enum TraceBitmask { DiskCache = 1, InMemCache = 2, KernelCompiler = 4 }; public: - static unsigned int get() { return getCachedValue(); } - static void reset() { (void)getCachedValue(true); } - static bool isTraceDiskCache() { - return getCachedValue() & TraceBitmask::DiskCache; - } - static bool isTraceInMemCache() { - return getCachedValue() & TraceBitmask::InMemCache; - } - static bool isTraceKernelCompiler() { - return getCachedValue() & TraceBitmask::KernelCompiler; - } + static unsigned int get() { return Level; } + static void reset(); + static bool isTraceDiskCache() { return Level & DiskCache; } + static bool isTraceInMemCache() { return Level & InMemCache; } + static bool isTraceKernelCompiler() { return Level & KernelCompiler; } private: - static unsigned int getCachedValue(bool ResetCache = false) { - const auto Parser = []() { - const char *ValStr = BaseT::getRawValue(); - int intVal = 0; - - if (ValStr) { - try { - intVal = std::stoi(ValStr); - } catch (...) { - // If the value is not null and not a number, it is considered - // to enable disk cache tracing. This is the legacy behavior. - intVal = 1; - } - } - - // Legacy behavior. - if (intVal > 7) - intVal = 1; - - return intVal; - }; - - static unsigned int Level = Parser(); - if (ResetCache) - Level = Parser(); - - return Level; - } + static unsigned int Level; }; // SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD accepts an integer that specifies diff --git a/sycl/source/detail/kernel_program_cache.cpp b/sycl/source/detail/kernel_program_cache.cpp index e73c0a19ef589..e778e13e6ce91 100644 --- a/sycl/source/detail/kernel_program_cache.cpp +++ b/sycl/source/detail/kernel_program_cache.cpp @@ -12,6 +12,18 @@ namespace sycl { inline namespace _V1 { namespace detail { + +void KernelProgramCache::traceKernelImpl(const char *Msg, + KernelNameStrRefT KernelName, + bool IsFastKernelCache) { + std::string Identifier = + "[IsFastCache: " + std::to_string(IsFastKernelCache) + + "][Key:{Name = " + KernelName.data() + "}]: "; + + std::cerr << "[In-Memory Cache][Thread Id:" << std::this_thread::get_id() + << "][Kernel Cache]" << Identifier << Msg << std::endl; +} + adapter_impl &KernelProgramCache::getAdapter() { return MParentContext.getAdapter(); } diff --git a/sycl/source/detail/kernel_program_cache.hpp b/sycl/source/detail/kernel_program_cache.hpp index d0a6b398528c4..e2f78a61e3774 100644 --- a/sycl/source/detail/kernel_program_cache.hpp +++ b/sycl/source/detail/kernel_program_cache.hpp @@ -331,7 +331,7 @@ class KernelProgramCache { template static inline void traceProgram(const MsgType &Msg, const ProgramCacheKeyT &CacheKey) { - if (!SYCLConfig::isTraceInMemCache()) + if (!SYCLConfigTrace::isTraceInMemCache()) return; int ImageId = CacheKey.first.second; @@ -361,21 +361,15 @@ class KernelProgramCache { << "][Program Cache]" << Identifier << Msg << std::endl; } + static void traceKernelImpl(const char *Msg, KernelNameStrRefT KernelName, + bool IsFastKernelCache); + // Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is // set. - template - static inline void traceKernel(const MsgType &Msg, - KernelNameStrRefT KernelName, - bool IsFastKernelCache = false) { - if (!SYCLConfig::isTraceInMemCache()) - return; - - std::string Identifier = - "[IsFastCache: " + std::to_string(IsFastKernelCache) + - "][Key:{Name = " + KernelName.data() + "}]: "; - - std::cerr << "[In-Memory Cache][Thread Id:" << std::this_thread::get_id() - << "][Kernel Cache]" << Identifier << Msg << std::endl; + static void traceKernel(const char *Msg, KernelNameStrRefT KernelName, + bool isFastKernelCache = false) { + if (__builtin_expect(SYCLConfigTrace::isTraceInMemCache(), false)) + traceKernelImpl(Msg, KernelName, isFastKernelCache); } Locked acquireCachedPrograms() { @@ -513,7 +507,7 @@ class KernelProgramCache { auto LockedCacheKP = acquireKernelsPerProgramCache(); // List kernels that are to be removed from the cache, if tracing is // enabled. - if (SYCLConfig::isTraceInMemCache()) { + if (__builtin_expect(SYCLConfigTrace::isTraceInMemCache(), false)) { for (const auto &Kernel : LockedCacheKP.get()[NativePrg]) traceKernel("Kernel evicted.", Kernel.first); } diff --git a/sycl/source/detail/persistent_device_code_cache.hpp b/sycl/source/detail/persistent_device_code_cache.hpp index 5d73db711d0cc..88bbaadb0eb34 100644 --- a/sycl/source/detail/persistent_device_code_cache.hpp +++ b/sycl/source/detail/persistent_device_code_cache.hpp @@ -222,9 +222,7 @@ class PersistentDeviceCodeCache { /* Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is set*/ static void trace(const std::string &msg, const std::string &path = "") { - static const bool traceEnabled = - SYCLConfig::isTraceDiskCache(); - if (traceEnabled) { + if (__builtin_expect(SYCLConfigTrace::isTraceDiskCache(), false)) { auto outputPath = path; std::replace(outputPath.begin(), outputPath.end(), '\\', '/'); std::cerr << "[Persistent Cache]: " << msg << outputPath << std::endl; @@ -232,9 +230,7 @@ class PersistentDeviceCodeCache { } static void trace_KernelCompiler(const std::string &msg, const std::string &path = "") { - static const bool traceEnabled = - SYCLConfig::isTraceKernelCompiler(); - if (traceEnabled) { + if (__builtin_expect(SYCLConfigTrace::isTraceKernelCompiler(), false)) { auto outputPath = path; std::replace(outputPath.begin(), outputPath.end(), '\\', '/'); std::cerr << "[kernel_compiler Persistent Cache]: " << msg << outputPath diff --git a/sycl/unittests/config/ConfigTests.cpp b/sycl/unittests/config/ConfigTests.cpp index 2391bb608a61e..b3d0baeeced80 100644 --- a/sycl/unittests/config/ConfigTests.cpp +++ b/sycl/unittests/config/ConfigTests.cpp @@ -28,8 +28,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 1"); } catch (sycl::exception &e) { EXPECT_EQ( std::string( @@ -46,8 +46,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 2"); } catch (sycl::exception &e) { EXPECT_EQ( std::string( @@ -64,8 +64,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 3"); } catch (sycl::exception &e) { EXPECT_EQ( std::string( @@ -82,8 +82,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 4"); } catch (sycl::exception &e) { EXPECT_EQ( std::string( @@ -103,8 +103,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 5"); } catch (sycl::exception &e) { EXPECT_TRUE(std::regex_match( e.what(), @@ -121,8 +121,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 6"); } catch (sycl::exception &e) { EXPECT_TRUE(std::regex_match( e.what(), std::regex("Variable name is more than ([\\d]+) or less " @@ -142,8 +142,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 7"); } catch (sycl::exception &e) { EXPECT_TRUE(std::regex_match( e.what(), std::regex("The value contains more than ([\\d]+) characters " @@ -159,8 +159,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 8"); } catch (sycl::exception &e) { EXPECT_TRUE(std::regex_match( e.what(), std::regex("The value contains more than ([\\d]+) characters " @@ -176,8 +176,8 @@ TEST(ConfigTests, CheckConfigProcessing) { File.close(); } try { - sycl::detail::SYCLConfig::get(); - throw std::logic_error("sycl::exception didn't throw"); + sycl::detail::readConfig(true); + throw std::logic_error("sycl::exception didn't throw 9"); } catch (sycl::exception &e) { EXPECT_TRUE(std::regex_match( e.what(), std::regex("The value contains more than ([\\d]+) characters " @@ -249,20 +249,17 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) { // Lambda to test parsing of SYCL_CACHE_TRACE auto TestConfig = [](int expectedValue, int expectedDiskCache, int expectedInMemCache, int expectedKernelCompiler) { - EXPECT_EQ(static_cast(expectedValue), - SYCLConfig::get()); + EXPECT_EQ(static_cast(expectedValue), SYCLConfigTrace::get()); EXPECT_EQ( expectedDiskCache, - static_cast( - sycl::detail::SYCLConfig::isTraceDiskCache())); + static_cast(sycl::detail::SYCLConfigTrace::isTraceDiskCache())); EXPECT_EQ( expectedInMemCache, - static_cast( - sycl::detail::SYCLConfig::isTraceInMemCache())); + static_cast(sycl::detail::SYCLConfigTrace::isTraceInMemCache())); EXPECT_EQ(expectedKernelCompiler, - static_cast(sycl::detail::SYCLConfig< - SYCL_CACHE_TRACE>::isTraceKernelCompiler())); + static_cast( + sycl::detail::SYCLConfigTrace::isTraceKernelCompiler())); }; // Lambda to set SYCL_CACHE_TRACE @@ -279,40 +276,40 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) { TestConfig(0, 0, 0, 0); SetSyclCacheTraceEnv("1"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(1, 1, 0, 0); SetSyclCacheTraceEnv("2"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(2, 0, 1, 0); SetSyclCacheTraceEnv("3"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(3, 1, 1, 0); SetSyclCacheTraceEnv("4"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(4, 0, 0, 1); SetSyclCacheTraceEnv("5"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(5, 1, 0, 1); SetSyclCacheTraceEnv("6"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(6, 0, 1, 1); SetSyclCacheTraceEnv("7"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(7, 1, 1, 1); SetSyclCacheTraceEnv("8"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(1, 1, 0, 0); // Set random non-null value. It should default to 1. SetSyclCacheTraceEnv("random"); - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(1, 1, 0, 0); // When SYCL_CACHE_TRACE is not set, it should default to 0. @@ -321,7 +318,7 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) { #else unsetenv("SYCL_CACHE_TRACE"); #endif - sycl::detail::SYCLConfig::reset(); + sycl::detail::SYCLConfigTrace::reset(); TestConfig(0, 0, 0, 0); }