From d91c49d63737e74b632f368729642ea09e6343b0 Mon Sep 17 00:00:00 2001 From: David Garcia Orozco Date: Fri, 30 Aug 2024 08:28:44 -0700 Subject: [PATCH] [SYCL][E2E] Add helper functions to access environment variables (#15226) The usage of `getenv` is deprecated on Windows in favor of `_dupenv_s`, this change adds helper functions to use the appropriate function depending on the OS. Should resolve the non XPTI test failures from #15026 --- .../CompositeDevice/composite_device.cpp | 14 ++------ sycl/test-e2e/Config/allowlist.cpp | 11 ++++--- sycl/test-e2e/Config/env_vars.cpp | 3 +- sycl/test-e2e/DeviceLib/assert-windows.cpp | 3 +- sycl/test-e2e/DeviceLib/assert.cpp | 14 ++++---- .../test-e2e/FilterSelector/select_device.cpp | 19 +++++------ .../FilterSelector/select_device_acc.cpp | 5 +-- .../FilterSelector/select_device_cpu.cpp | 5 +-- .../FilterSelector/select_device_cuda.cpp | 5 +-- .../select_device_level_zero.cpp | 5 +-- .../FilterSelector/select_device_opencl.cpp | 5 +-- .../Plugin/interop-level-zero-thread.cpp | 3 +- .../Plugin/level_zero_ext_intel_cslice.cpp | 7 ++-- sycl/test-e2e/Regression/device_num.cpp | 14 ++++---- sycl/test-e2e/helpers.hpp | 32 +++++++++++++++++++ 15 files changed, 91 insertions(+), 54 deletions(-) diff --git a/sycl/test-e2e/CompositeDevice/composite_device.cpp b/sycl/test-e2e/CompositeDevice/composite_device.cpp index e718887993529..decc112d0049d 100644 --- a/sycl/test-e2e/CompositeDevice/composite_device.cpp +++ b/sycl/test-e2e/CompositeDevice/composite_device.cpp @@ -3,6 +3,7 @@ // RUN: env ZE_FLAT_DEVICE_HIERARCHY=COMPOSITE %{run} %t.out // RUN: env ZE_FLAT_DEVICE_HIERARCHY=FLAT %{run} %t.out +#include "../helpers.hpp" #include #include @@ -15,18 +16,7 @@ bool isL0Backend(sycl::backend backend) { } bool isCombinedMode() { - char *Mode = nullptr; - bool Res = false; -#ifdef _WIN32 - size_t Size = 0; - auto Err = _dupenv_s(&Mode, &Size, "ZE_FLAT_DEVICE_HIERARCHY"); - Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0); - free(Mode); -#else - Mode = std::getenv("ZE_FLAT_DEVICE_HIERARCHY"); - Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0); -#endif - return Res; + return env::getVal("ZE_FLAT_DEVICE_HIERARCHY") == "COMBINED"; } int main() { diff --git a/sycl/test-e2e/Config/allowlist.cpp b/sycl/test-e2e/Config/allowlist.cpp index 56dfbc081fb06..cc3a8c6b30221 100644 --- a/sycl/test-e2e/Config/allowlist.cpp +++ b/sycl/test-e2e/Config/allowlist.cpp @@ -12,6 +12,7 @@ // RUN: env TEST_DEVICE_IS_NOT_AVAILABLE=1 env SYCL_DEVICE_ALLOWLIST="PlatformName:{{SUCH NAME DOESN'T EXIST}}" ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run-unfiltered-devices} %t.out // RUN: env TEST_INCORRECT_VALUE=1 env SYCL_DEVICE_ALLOWLIST="IncorrectKey:{{.*}}" ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run-unfiltered-devices} %t.out +#include "../helpers.hpp" #include #include #include @@ -33,7 +34,7 @@ static void replaceSpecialCharacters(std::string &Str) { int main() { // Expected that the allowlist filter is not set - if (getenv("PRINT_PLATFORM_INFO")) { + if (env::isDefined("PRINT_PLATFORM_INFO")) { for (const sycl::platform &Platform : sycl::platform::get_platforms()) { std::string Name = Platform.get_info(); std::string Ver = Platform.get_info(); @@ -51,7 +52,7 @@ int main() { } // Expected that the allowlist filter is not set - if (getenv("PRINT_DEVICE_INFO")) { + if (env::isDefined("PRINT_DEVICE_INFO")) { for (const sycl::platform &Platform : sycl::platform::get_platforms()) { const sycl::device Dev = Platform.get_devices().at(0); std::string Name = Dev.get_info(); @@ -71,7 +72,7 @@ int main() { } // Expected the allowlist to be set with the "PRINT_DEVICE_INFO" run result - if (getenv("TEST_DEVICE_AVAILABLE")) { + if (env::isDefined("TEST_DEVICE_AVAILABLE")) { for (const sycl::platform &Platform : sycl::platform::get_platforms()) { if (Platform.get_devices().size() != 1) throw std::runtime_error("Expected only one device."); @@ -82,13 +83,13 @@ int main() { } // Expected the allowlist to be set but empty - if (getenv("TEST_DEVICE_IS_NOT_AVAILABLE")) { + if (env::isDefined("TEST_DEVICE_IS_NOT_AVAILABLE")) { if (!sycl::platform::get_platforms().empty()) throw std::runtime_error("Expected no device is available"); return 0; } - if (getenv("TEST_INCORRECT_VALUE")) { + if (env::isDefined("TEST_INCORRECT_VALUE")) { try { sycl::platform::get_platforms(); } catch (sycl::exception &E) { diff --git a/sycl/test-e2e/Config/env_vars.cpp b/sycl/test-e2e/Config/env_vars.cpp index 372d23886fec0..00f17c508ccae 100644 --- a/sycl/test-e2e/Config/env_vars.cpp +++ b/sycl/test-e2e/Config/env_vars.cpp @@ -13,6 +13,7 @@ // RUN: %if cpu %{ env SYCL_PROGRAM_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %} // RUN: %if cpu %{ env SYCL_PROGRAM_APPEND_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %} +#include "../helpers.hpp" #include #include @@ -22,7 +23,7 @@ int main() { int data = 5; buffer buf(&data, range<1>(1)); queue myQueue; - bool shouldCrash = getenv("SHOULD_CRASH"); + bool shouldCrash = env::isDefined("SHOULD_CRASH"); try { myQueue.submit([&](handler &cgh) { auto B = buf.get_access(cgh); diff --git a/sycl/test-e2e/DeviceLib/assert-windows.cpp b/sycl/test-e2e/DeviceLib/assert-windows.cpp index 1aa11bebec67e..3c35c2371e5f4 100644 --- a/sycl/test-e2e/DeviceLib/assert-windows.cpp +++ b/sycl/test-e2e/DeviceLib/assert-windows.cpp @@ -22,6 +22,7 @@ // // CHECK-FALLBACK: ---> urProgramLink +#include "../helpers.hpp" #include #include #include @@ -47,7 +48,7 @@ void simple_vadd(const std::array &VA, const std::array &VB, } }); - int shouldCrash = getenv("SHOULD_CRASH") ? 1 : 0; + bool shouldCrash = env::isDefined("SHOULD_CRASH"); sycl::range<1> numOfItems{N}; sycl::buffer bufferA(VA.data(), numOfItems); diff --git a/sycl/test-e2e/DeviceLib/assert.cpp b/sycl/test-e2e/DeviceLib/assert.cpp index af70c6b950835..6edabb07a34b3 100644 --- a/sycl/test-e2e/DeviceLib/assert.cpp +++ b/sycl/test-e2e/DeviceLib/assert.cpp @@ -91,6 +91,7 @@ // Note that the work-item that hits the assert first may vary, since the order // of execution is undefined. We catch only the first one (whatever id it is). +#include "../helpers.hpp" #include "sycl/backend/opencl.hpp" #include #include @@ -128,7 +129,7 @@ void simple_vadd(const std::array &VA, const std::array &VB, if (sycl::opencl::has_extension(dev, "cl_intel_devicelib_assert")) { unsupported = false; } - if (unsupported && getenv("SKIP_IF_NO_EXT")) { + if (unsupported && env::isDefined("SKIP_IF_NO_EXT")) { fprintf(stderr, "Device has no support for cl_intel_devicelib_assert, " "skipping the test\n"); exit(EXIT_SKIP_TEST); @@ -160,7 +161,7 @@ int main() { if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SKIP_TEST) { return 0; } - if (getenv("SHOULD_CRASH")) { + if (env::isDefined("SHOULD_CRASH")) { if (!WIFSIGNALED(status)) { fprintf(stderr, "error: process did not terminate by a signal\n"); return 1; @@ -176,12 +177,13 @@ int main() { } int sig = WTERMSIG(status); int expected = 0; - if (const char *env = getenv("EXPECTED_SIGNAL")) { - if (0 == strcmp(env, "SIGABRT")) { + const std::string envVal = env::getVal("EXPECTED_SIGNAL"); + if (!envVal.empty()) { + if (envVal == "SIGABRT") { expected = SIGABRT; - } else if (0 == strcmp(env, "SIGSEGV")) { + } else if (envVal == "SIGSEGV") { expected = SIGSEGV; - } else if (0 == strcmp(env, "SIGIOT")) { + } else if (envVal == "SIGIOT") { expected = SIGIOT; } if (!expected) { diff --git a/sycl/test-e2e/FilterSelector/select_device.cpp b/sycl/test-e2e/FilterSelector/select_device.cpp index ffe93571ab948..18959b9189255 100644 --- a/sycl/test-e2e/FilterSelector/select_device.cpp +++ b/sycl/test-e2e/FilterSelector/select_device.cpp @@ -15,44 +15,45 @@ #include +#include "../helpers.hpp" #include using namespace sycl; using namespace std; int main() { - const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); std::string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } - if (!envVal || forcedPIs == "*" || + if (!envVal.empty() || forcedPIs == "*" || forcedPIs.find("level_zero:gpu") != std::string::npos) { default_selector ds; device d = ds.select_device(); string name = d.get_platform().get_info(); assert(name.find("Level-Zero") != string::npos); } - if (envVal && forcedPIs != "*" && + if (envVal.empty() && forcedPIs != "*" && forcedPIs.find("opencl:gpu") != std::string::npos) { gpu_selector gs; device d = gs.select_device(); string name = d.get_platform().get_info(); assert(name.find("OpenCL") != string::npos); } - if (!envVal || forcedPIs == "*" || + if (!envVal.empty() || forcedPIs == "*" || forcedPIs.find("cpu") != std::string::npos) { cpu_selector cs; device d = cs.select_device(); } - if (!envVal || forcedPIs == "*" || + if (!envVal.empty() || forcedPIs == "*" || forcedPIs.find("fpga") != std::string::npos) { accelerator_selector as; device d = as.select_device(); } - if (envVal && (forcedPIs.find("cpu") == std::string::npos && - forcedPIs.find("opencl") == std::string::npos && - forcedPIs.find("*") == std::string::npos)) { + if (envVal.empty() && (forcedPIs.find("cpu") == std::string::npos && + forcedPIs.find("opencl") == std::string::npos && + forcedPIs.find("*") == std::string::npos)) { try { cpu_selector cs; device d = cs.select_device(); diff --git a/sycl/test-e2e/FilterSelector/select_device_acc.cpp b/sycl/test-e2e/FilterSelector/select_device_acc.cpp index 4b3db255b0853..1b4f9c74d3e1e 100644 --- a/sycl/test-e2e/FilterSelector/select_device_acc.cpp +++ b/sycl/test-e2e/FilterSelector/select_device_acc.cpp @@ -10,15 +10,16 @@ #include +#include "../helpers.hpp" #include using namespace sycl; using namespace std; int main() { - const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); std::string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } { diff --git a/sycl/test-e2e/FilterSelector/select_device_cpu.cpp b/sycl/test-e2e/FilterSelector/select_device_cpu.cpp index 5d0294c55d547..27f81fa4f3c84 100644 --- a/sycl/test-e2e/FilterSelector/select_device_cpu.cpp +++ b/sycl/test-e2e/FilterSelector/select_device_cpu.cpp @@ -10,15 +10,16 @@ #include +#include "../helpers.hpp" #include using namespace sycl; using namespace std; int main() { - const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); std::string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } { diff --git a/sycl/test-e2e/FilterSelector/select_device_cuda.cpp b/sycl/test-e2e/FilterSelector/select_device_cuda.cpp index 372c1771bcba8..ac1aa120f1929 100644 --- a/sycl/test-e2e/FilterSelector/select_device_cuda.cpp +++ b/sycl/test-e2e/FilterSelector/select_device_cuda.cpp @@ -10,15 +10,16 @@ #include +#include "../helpers.hpp" #include using namespace sycl; using namespace std; int main() { - const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } diff --git a/sycl/test-e2e/FilterSelector/select_device_level_zero.cpp b/sycl/test-e2e/FilterSelector/select_device_level_zero.cpp index 53d16b798ebcc..5a913fa9a5359 100644 --- a/sycl/test-e2e/FilterSelector/select_device_level_zero.cpp +++ b/sycl/test-e2e/FilterSelector/select_device_level_zero.cpp @@ -8,6 +8,7 @@ // // REQUIRES: level_zero,gpu +#include "../helpers.hpp" #include #include @@ -15,9 +16,9 @@ using namespace sycl; using namespace std; int main() { - const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } diff --git a/sycl/test-e2e/FilterSelector/select_device_opencl.cpp b/sycl/test-e2e/FilterSelector/select_device_opencl.cpp index 8f7d52780a12d..af3dc10a0d5b5 100644 --- a/sycl/test-e2e/FilterSelector/select_device_opencl.cpp +++ b/sycl/test-e2e/FilterSelector/select_device_opencl.cpp @@ -8,6 +8,7 @@ // // REQUIRES: opencl,gpu,cpu,accelerator +#include "../helpers.hpp" #include #include @@ -15,9 +16,9 @@ using namespace sycl; using namespace std; int main() { - const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); string forcedPIs; - if (envVal) { + if (envVal.empty()) { forcedPIs = envVal; } diff --git a/sycl/test-e2e/Plugin/interop-level-zero-thread.cpp b/sycl/test-e2e/Plugin/interop-level-zero-thread.cpp index 129d43dc3ceb1..f4294d654a460 100644 --- a/sycl/test-e2e/Plugin/interop-level-zero-thread.cpp +++ b/sycl/test-e2e/Plugin/interop-level-zero-thread.cpp @@ -23,6 +23,7 @@ // CHECK: Running iteration 18 // CHECK: Running iteration 19 +#include "../helpers.hpp" #include #include #include @@ -144,7 +145,7 @@ void worker() { sycl::info::event::command_execution_status>() == sycl::info::event_command_status::complete; - if (getenv("QUERY_STATUS") != nullptr) { + if (env::isDefined("QUERY_STATUS")) { auto ev1 = sycl::get_native( op.sycl_event_sync); auto ev2 = sycl::get_native( diff --git a/sycl/test-e2e/Plugin/level_zero_ext_intel_cslice.cpp b/sycl/test-e2e/Plugin/level_zero_ext_intel_cslice.cpp index f49f5a44ea5ff..b7197461a00b5 100644 --- a/sycl/test-e2e/Plugin/level_zero_ext_intel_cslice.cpp +++ b/sycl/test-e2e/Plugin/level_zero_ext_intel_cslice.cpp @@ -28,6 +28,7 @@ // RUN: %{setup_env} env SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=0 SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING=1 \ // RUN: UR_L0_DEBUG=1 %{run} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-PVC +#include "../helpers.hpp" #include using namespace sycl; @@ -35,9 +36,9 @@ using namespace sycl; // Specified in the RUN line. static constexpr int NumCSlices = 4; static const bool ExposeCSliceInAffinityPartitioning = [] { - const char *Flag = - std::getenv("SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING"); - return Flag ? std::atoi(Flag) != 0 : false; + std::string Flag = + env::getVal("SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING"); + return !Flag.empty() ? std::stoi(Flag) != 0 : false; }(); template diff --git a/sycl/test-e2e/Regression/device_num.cpp b/sycl/test-e2e/Regression/device_num.cpp index cc70ea5295406..026b8f38e2f4e 100644 --- a/sycl/test-e2e/Regression/device_num.cpp +++ b/sycl/test-e2e/Regression/device_num.cpp @@ -8,6 +8,7 @@ // Temporarily disable on L0 due to fails in CI // UNSUPPORTED: level_zero +#include "../helpers.hpp" #include #include #include @@ -76,7 +77,7 @@ void PrintSystemConfiguration() { using DevInfo = std::pair; using DevInfoMap = std::map>; -bool ReadInitialSystemConfiguration(char *fileName, DevInfoMap &devices) { +bool ReadInitialSystemConfiguration(const char *fileName, DevInfoMap &devices) { fstream confFile; confFile.open(fileName, ios::in); if (!confFile.is_open()) @@ -133,20 +134,21 @@ int GetPreferredDeviceIndex(const std::vector &devices, int main() { // Expected that the sycl device filter is not set - if (getenv("PRINT_FULL_DEVICE_INFO")) { + if (env::isDefined("PRINT_FULL_DEVICE_INFO")) { PrintSystemConfiguration(); return 0; } DevInfoMap unfilteredDevices; - assert(ReadInitialSystemConfiguration(getenv("TEST_DEV_CONFIG_FILE_NAME"), - unfilteredDevices) && + assert(ReadInitialSystemConfiguration( + env::getVal("TEST_DEV_CONFIG_FILE_NAME").c_str(), + unfilteredDevices) && "Failed to parse file with initial system configuration data"); - const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR"); + std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR"); int deviceNum; std::cout << "ONEAPI_DEVICE_SELECTOR=" << envVal << std::endl; - deviceNum = std::atoi(std::string(envVal).substr(2).c_str()); + deviceNum = std::stoi(envVal.substr(2)); auto devices = device::get_devices(); std::cout << "Device count to analyze =" << devices.size() << std::endl; diff --git a/sycl/test-e2e/helpers.hpp b/sycl/test-e2e/helpers.hpp index 6b7434f4c7a9d..0b684b486a8ab 100644 --- a/sycl/test-e2e/helpers.hpp +++ b/sycl/test-e2e/helpers.hpp @@ -102,3 +102,35 @@ OutputIterator inclusive_scan(InputIterator first, InputIterator last, return result; } } // namespace emu + +namespace env { + +bool isDefined(const char *name) { + char *buf = nullptr; +#ifdef _WIN32 + size_t sz; + _dupenv_s(&buf, &sz, name); + free(buf); +#else + buf = getenv(name); +#endif + return buf != nullptr; +} + +std::string getVal(const char *name) { + char *buf = nullptr; + std::string res = ""; +#ifdef _WIN32 + size_t sz; + _dupenv_s(&buf, &sz, name); + if (buf != nullptr) + res = std::string(buf); + free(buf); +#else + buf = getenv(name); + if (buf != nullptr) + res = std::string(buf); +#endif + return res; +} +} // namespace env