Skip to content

Commit 4c6cda3

Browse files
[SYCL] Add support for L0 loader validation layer (#2520)
Extended debugging capabilities by setting two new environment variables ( ZE_ENABLE_VALIDATION_LAYER and ZE_ENABLE_PARAMETER_VALIDATION) that will enable the Level Zero loader validation layer. Also, implemented ZE_DEBUG as a bit mask.
1 parent d384295 commit 4c6cda3

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ std::mutex ZeCall::GlobalLock;
6565
// Controls Level Zero calls tracing in zePrint.
6666
static bool ZeDebug = false;
6767

68+
// Controls Level Zero validation layer and parameter validation.
69+
static bool ZeValidationLayer = false;
70+
71+
enum DebugLevel {
72+
ZE_DEBUG_BASIC = 0x1,
73+
ZE_DEBUG_VALIDATION = 0x2,
74+
ZE_DEBUG_ALL = -1
75+
};
76+
6877
static void zePrint(const char *Format, ...) {
6978
if (ZeDebug) {
7079
va_list Args;
@@ -549,6 +558,8 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
549558
ze_module_handle_t SrcMod,
550559
ze_module_handle_t *DestMod);
551560

561+
static bool setEnvVar(const char *var, const char *value);
562+
552563
static pi_result getOrCreatePlatform(ze_driver_handle_t ZeDriver,
553564
pi_platform *Platform);
554565

@@ -567,12 +578,37 @@ static bool isOnlineLinkEnabled();
567578
// End forward declarations for mock Level Zero APIs
568579
std::once_flag OnceFlag;
569580

581+
// This function will ensure compatibility with both Linux and Windowns for
582+
// setting environment variables.
583+
static bool setEnvVar(const char *name, const char *value) {
584+
#ifdef _WIN32
585+
int Res = _putenv_s(name, value);
586+
#else
587+
int Res = setenv(name, value, 1);
588+
#endif
589+
if (Res != 0) {
590+
zePrint(
591+
"Level Zero plugin was unable to set the environment variable: %s\n",
592+
name);
593+
return false;
594+
}
595+
return true;
596+
}
597+
570598
pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
571599
pi_uint32 *NumPlatforms) {
572600

573601
static const char *DebugMode = std::getenv("ZE_DEBUG");
574-
if (DebugMode)
602+
static const int DebugModeValue = DebugMode ? std::stoi(DebugMode) : 0;
603+
if (DebugModeValue == ZE_DEBUG_ALL) {
575604
ZeDebug = true;
605+
ZeValidationLayer = true;
606+
} else {
607+
if (DebugModeValue & ZE_DEBUG_BASIC)
608+
ZeDebug = true;
609+
if (DebugModeValue & ZE_DEBUG_VALIDATION)
610+
ZeValidationLayer = true;
611+
}
576612

577613
static const char *SerializeMode = std::getenv("ZE_SERIALIZE");
578614
static const pi_uint32 SerializeModeValue =
@@ -586,6 +622,13 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
586622
return PI_INVALID_VALUE;
587623
}
588624

625+
// Setting these environment variables before running zeInit will enable the
626+
// validation layer in the Level Zero loader.
627+
if (ZeValidationLayer) {
628+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
629+
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
630+
}
631+
589632
// TODO: We can still safely recover if something goes wrong during the init.
590633
// Implement handling segfault using sigaction.
591634

0 commit comments

Comments
 (0)