@@ -65,6 +65,15 @@ std::mutex ZeCall::GlobalLock;
65
65
// Controls Level Zero calls tracing in zePrint.
66
66
static bool ZeDebug = false ;
67
67
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
+
68
77
static void zePrint (const char *Format, ...) {
69
78
if (ZeDebug) {
70
79
va_list Args;
@@ -549,6 +558,8 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
549
558
ze_module_handle_t SrcMod,
550
559
ze_module_handle_t *DestMod);
551
560
561
+ static bool setEnvVar (const char *var, const char *value);
562
+
552
563
static pi_result getOrCreatePlatform (ze_driver_handle_t ZeDriver,
553
564
pi_platform *Platform);
554
565
@@ -567,12 +578,37 @@ static bool isOnlineLinkEnabled();
567
578
// End forward declarations for mock Level Zero APIs
568
579
std::once_flag OnceFlag;
569
580
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
+
570
598
pi_result piPlatformsGet (pi_uint32 NumEntries, pi_platform *Platforms,
571
599
pi_uint32 *NumPlatforms) {
572
600
573
601
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) {
575
604
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
+ }
576
612
577
613
static const char *SerializeMode = std::getenv (" ZE_SERIALIZE" );
578
614
static const pi_uint32 SerializeModeValue =
@@ -586,6 +622,13 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
586
622
return PI_INVALID_VALUE;
587
623
}
588
624
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
+
589
632
// TODO: We can still safely recover if something goes wrong during the init.
590
633
// Implement handling segfault using sigaction.
591
634
0 commit comments