Skip to content

Commit c5bb05d

Browse files
[SYCL]Added function for setting env var in plugin
This function will ensure compatibility with both Linux and Windowns for setting environment variables.
1 parent 72862fa commit c5bb05d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626
#include "usm_allocator.hpp"
2727

28-
#ifdef _WIN32
29-
#define setenv(name, value, overwrite) _putenv_s(name, value)
30-
#endif
31-
3228
namespace {
3329

3430
// Controls Level Zero calls serialization to w/a Level Zero driver being not MT
@@ -561,6 +557,8 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
561557
ze_module_handle_t SrcMod,
562558
ze_module_handle_t *DestMod);
563559

560+
static void setEnvVar(const char *var, const char *value);
561+
564562
// Forward declarations for mock implementations of Level Zero APIs that
565563
// do not yet work in the driver.
566564
// TODO: Remove these mock definitions when they work in the driver.
@@ -576,6 +574,16 @@ static bool isOnlineLinkEnabled();
576574
// End forward declarations for mock Level Zero APIs
577575
std::once_flag OnceFlag;
578576

577+
// This function will ensure compatibility with both Linux and Windowns for
578+
// setting environment variables.
579+
static void setEnvVar(const char *name, const char *value) {
580+
#ifdef _WIN32
581+
_putenv_s(name, value);
582+
#else
583+
setenv(name, value, 1);
584+
#endif
585+
}
586+
579587
pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
580588
pi_uint32 *NumPlatforms) {
581589

@@ -610,9 +618,11 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
610618
static std::vector<pi_platform> PiPlatformsCache;
611619
static std::mutex PiPlatformsCacheMutex;
612620

621+
// Setting these environment variables before running zeInit will enable the
622+
// validation layer in the Level Zero loader.
613623
if (ZeValidationLayer) {
614-
setenv("ZE_ENABLE_VALIDATION_LAYER", "1", 1);
615-
setenv("ZE_ENABLE_PARAMETER_VALIDATION", "1", 1);
624+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
625+
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
616626
}
617627

618628
// This is a good time to initialize Level Zero.

0 commit comments

Comments
 (0)