Skip to content

Commit eed2d03

Browse files
[SYCL] Repurpose SYCL_CACHE_TRACE to enable fine-grained tracing of SYCL caches (#15822)
Currently, we use SYCL_CACHE_TRACE for events in persistent cache only. This PR repurposes SYCL_CACHE_TRACE to also enable tracing of in-memory cache and kernel_compiler. After this change, SYCL_CACHE_TRACE will accept the following bit-masks: | Bit-mask | Corresponding cache tracing | | ------ | ----------- | | 0x01 | Enable tracing of persistent cache | | 0x02 | Enable tracing of in-memory cache | | 0x04 | Enable tracing of `kernel_compiler` cache | Any valid combination of the above bit-masks can be used to enable/disable tracing of the corresponding caches. --------- Co-authored-by: Steffen Larsen <[email protected]>
1 parent a0b5f56 commit eed2d03

File tree

6 files changed

+169
-7
lines changed

6 files changed

+169
-7
lines changed

sycl/doc/EnvironmentVariables.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ variables in production code.</span>
210210
| `SYCL_USE_KERNEL_SPV` | Path to the SPIR-V binary | Load device image from the specified file. If runtime is unable to read the file, `sycl::runtime_error` exception is thrown. The image is assumed to have been created using the `-fno-sycl-dead-args-optimization` option. |
211211
| `SYCL_DUMP_IMAGES` | Any(\*) | Dump device image binaries to file. Control has no effect if `SYCL_USE_KERNEL_SPV` is set. |
212212
| `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. |
213-
| `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). |
213+
| `SYCL_CACHE_TRACE` | Described [below](#sycl_cache_trace-options). | Enable tracing for different SYCL and `kernel_compiler` caches. |
214214
| `SYCL_PARALLEL_FOR_RANGE_ROUNDING_TRACE` | Any(\*) | Enables tracing of `parallel_for` invocations with rounded-up ranges. |
215215
| `SYCL_PI_SUPPRESS_ERROR_MESSAGE` | Any(\*) | Suppress printing of error message, only used for CI in order not to interrupt errors generated by underlying toolchains; note that the variable only modifies the printing of the error message (error value, name, description and location), the handling of error return code and aborting/throwing behaviour remains unchanged. |
216216
| `SYCL_JIT_COMPILER_DEBUG` | Any(\*) | Passes can specify their own debug types, `sycl-spec-const-materializer` enables debug output generation in specialization constants materialization pass. |
@@ -245,6 +245,17 @@ Supported tracing levels are in the table below
245245
| 2 | Enable tracing of the UR calls |
246246
| -1 | Enable all levels of tracing |
247247

248+
### `SYCL_CACHE_TRACE` Options
249+
250+
`SYCL_CACHE_TRACE` accepts a bit-mask to control the tracing of different SYCL caches. The input value is parsed as an integer and the following bit-masks are used to determine the tracing behavior:
251+
| Bit-mask | Corresponding cache tracing |
252+
| ------ | ----------- |
253+
| 0x01 | Enable tracing of persistent cache |
254+
| 0x02 | Enable tracing of in-memory cache |
255+
| 0x04 | Enable tracing of `kernel_compiler` cache |
256+
257+
Any valid combination of the above bit-masks can be used to enable/disable tracing of the corresponding caches. If the input value is not 0 and not a valid number, the disk cache tracing will be enabled (deprecated behavior).
258+
The default value is 0 and no tracing is enabled.
248259

249260
## Debugging variables for Level Zero Plugin
250261

sycl/source/detail/config.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CONFIG(SYCL_PROGRAM_APPEND_COMPILE_OPTIONS, 64, __SYCL_PROGRAM_APPEND_COMPILE_OP
2626
CONFIG(SYCL_HOST_UNIFIED_MEMORY, 1, __SYCL_HOST_UNIFIED_MEMORY)
2727
// 260 (Windows limit) - 12 (filename) - 84 (cache directory structure)
2828
CONFIG(SYCL_CACHE_DIR, 164, __SYCL_CACHE_DIR)
29-
CONFIG(SYCL_CACHE_TRACE, 1, __SYCL_CACHE_TRACE)
29+
CONFIG(SYCL_CACHE_TRACE, 4, __SYCL_CACHE_TRACE)
3030
CONFIG(SYCL_CACHE_DISABLE_PERSISTENT, 1, __SYCL_CACHE_DISABLE_PERSISTENT)
3131
CONFIG(SYCL_CACHE_PERSISTENT, 1, __SYCL_CACHE_PERSISTENT)
3232
CONFIG(SYCL_CACHE_EVICTION_DISABLE, 1, __SYCL_CACHE_EVICTION_DISABLE)

sycl/source/detail/config.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,64 @@ template <> class SYCLConfig<SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES> {
698698
}
699699
};
700700

701+
// SYCL_CACHE_TRACE accepts a bit-mask to control the tracing of
702+
// different SYCL caches. The input value is parsed as an integer and
703+
// the following bit-masks is used to determine the tracing behavior:
704+
// 0x01 - trace disk cache
705+
// 0x02 - trace in-memory cache
706+
// 0x04 - trace kernel_compiler cache
707+
// Any valid combination of the above bit-masks can be used to enable/disable
708+
// tracing of the corresponding caches. If the input value is not null and
709+
// not a valid number, the disk cache tracing will be enabled (depreciated
710+
// behavior). The default value is 0 and no tracing is enabled.
711+
template <> class SYCLConfig<SYCL_CACHE_TRACE> {
712+
using BaseT = SYCLConfigBase<SYCL_CACHE_TRACE>;
713+
enum TraceBitmask { DiskCache = 1, InMemCache = 2, KernelCompiler = 4 };
714+
715+
public:
716+
static unsigned int get() { return getCachedValue(); }
717+
static void reset() { (void)getCachedValue(true); }
718+
static bool isTraceDiskCache() {
719+
return getCachedValue() & TraceBitmask::DiskCache;
720+
}
721+
static bool isTraceInMemCache() {
722+
return getCachedValue() & TraceBitmask::InMemCache;
723+
}
724+
static bool isTraceKernelCompiler() {
725+
return getCachedValue() & TraceBitmask::KernelCompiler;
726+
}
727+
728+
private:
729+
static unsigned int getCachedValue(bool ResetCache = false) {
730+
const auto Parser = []() {
731+
const char *ValStr = BaseT::getRawValue();
732+
int intVal = 0;
733+
734+
if (ValStr) {
735+
try {
736+
intVal = std::stoi(ValStr);
737+
} catch (...) {
738+
// If the value is not null and not a number, it is considered
739+
// to enable disk cache tracing. This is the legacy behavior.
740+
intVal = 1;
741+
}
742+
}
743+
744+
// Legacy behavior.
745+
if (intVal > 7)
746+
intVal = 1;
747+
748+
return intVal;
749+
};
750+
751+
static unsigned int Level = Parser();
752+
if (ResetCache)
753+
Level = Parser();
754+
755+
return Level;
756+
}
757+
};
758+
701759
#undef INVALID_CONFIG_EXCEPTION
702760

703761
} // namespace detail

sycl/source/detail/persistent_device_code_cache.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ class PersistentDeviceCodeCache {
190190

191191
/* Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is set*/
192192
static void trace(const std::string &msg) {
193-
static const char *TraceEnabled = SYCLConfig<SYCL_CACHE_TRACE>::get();
194-
if (TraceEnabled)
195-
std::cerr << "*** Code caching: " << msg << std::endl;
193+
static const bool traceEnabled =
194+
SYCLConfig<SYCL_CACHE_TRACE>::isTraceDiskCache();
195+
if (traceEnabled)
196+
std::cerr << "[Persistent Cache]: " << msg << std::endl;
196197
}
197198
};
198199
} // namespace detail

sycl/test-e2e/KernelAndProgram/test_cache_jit_aot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
// RUN: %{cache_vars} %{run-unfiltered-devices} %t.out 2>&1 | FileCheck %s --check-prefixes RESULT1
6767
// ******************************
6868

69-
// CHECK-CACHE-WRITE: Code caching: device binary has been cached
70-
// CHECK-CACHE-READ: Code caching: using cached device binary
69+
// CHECK-CACHE-WRITE: [Persistent Cache]: device binary has been cached
70+
// CHECK-CACHE-READ: [Persistent Cache]: using cached device binary
7171

7272
// RESULT1: Result (0): 1
7373
// RESULT1: Result (1): 1

sycl/unittests/config/ConfigTests.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,95 @@ TEST(ConfigTests, CheckConfigProcessing) {
232232
sycl::detail::SYCLConfig<
233233
sycl::detail::SYCL_PRINT_EXECUTION_GRAPH>::get());
234234
}
235+
236+
// SYCL_CACHE_TRACE accepts a bit-mask to control the tracing of
237+
// different SYCL caches. The input value is parsed as an integer and
238+
// the following bit-masks is used to determine the tracing behavior:
239+
// 0x01 - trace disk cache
240+
// 0x02 - trace in-memory cache
241+
// 0x04 - trace kernel_compiler cache
242+
// Any valid combination of the above bit-masks can be used to enable/disable
243+
// tracing of the corresponding caches. If the input value is not null and
244+
// not a valid number, the disk cache tracing will be enabled (depreciated
245+
// behavior). The default value is 0 and no tracing is enabled.
246+
using namespace sycl::detail;
247+
TEST(ConfigTests, CheckSyclCacheTraceTest) {
248+
249+
// Lambda to test parsing of SYCL_CACHE_TRACE
250+
auto TestConfig = [](int expectedValue, int expectedDiskCache,
251+
int expectedInMemCache, int expectedKernelCompiler) {
252+
EXPECT_EQ(static_cast<unsigned int>(expectedValue),
253+
SYCLConfig<SYCL_CACHE_TRACE>::get());
254+
255+
EXPECT_EQ(
256+
expectedDiskCache,
257+
static_cast<int>(
258+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::isTraceDiskCache()));
259+
EXPECT_EQ(
260+
expectedInMemCache,
261+
static_cast<int>(
262+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache()));
263+
EXPECT_EQ(expectedKernelCompiler,
264+
static_cast<int>(sycl::detail::SYCLConfig<
265+
SYCL_CACHE_TRACE>::isTraceKernelCompiler()));
266+
};
267+
268+
// Lambda to set SYCL_CACHE_TRACE
269+
auto SetSyclCacheTraceEnv = [](const char *value) {
270+
#ifdef _WIN32
271+
_putenv_s("SYCL_CACHE_TRACE", value);
272+
#else
273+
setenv("SYCL_CACHE_TRACE", value, 1);
274+
#endif
275+
};
276+
277+
SetSyclCacheTraceEnv("0");
278+
sycl::detail::readConfig(true);
279+
TestConfig(0, 0, 0, 0);
280+
281+
SetSyclCacheTraceEnv("1");
282+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
283+
TestConfig(1, 1, 0, 0);
284+
285+
SetSyclCacheTraceEnv("2");
286+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
287+
TestConfig(2, 0, 1, 0);
288+
289+
SetSyclCacheTraceEnv("3");
290+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
291+
TestConfig(3, 1, 1, 0);
292+
293+
SetSyclCacheTraceEnv("4");
294+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
295+
TestConfig(4, 0, 0, 1);
296+
297+
SetSyclCacheTraceEnv("5");
298+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
299+
TestConfig(5, 1, 0, 1);
300+
301+
SetSyclCacheTraceEnv("6");
302+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
303+
TestConfig(6, 0, 1, 1);
304+
305+
SetSyclCacheTraceEnv("7");
306+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
307+
TestConfig(7, 1, 1, 1);
308+
309+
SetSyclCacheTraceEnv("8");
310+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
311+
TestConfig(1, 1, 0, 0);
312+
313+
// Set random non-null value. It should default to 1.
314+
SetSyclCacheTraceEnv("random");
315+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
316+
TestConfig(1, 1, 0, 0);
317+
318+
// When SYCL_CACHE_TRACE is not set, it should default to 0.
319+
#ifdef _WIN32
320+
_putenv_s("SYCL_CACHE_TRACE", "");
321+
#else
322+
unsetenv("SYCL_CACHE_TRACE");
323+
#endif
324+
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
325+
TestConfig(0, 0, 0, 0);
326+
}

0 commit comments

Comments
 (0)