Skip to content

Commit d91c49d

Browse files
authored
[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
1 parent 02a20e5 commit d91c49d

15 files changed

+91
-54
lines changed

sycl/test-e2e/CompositeDevice/composite_device.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: env ZE_FLAT_DEVICE_HIERARCHY=COMPOSITE %{run} %t.out
44
// RUN: env ZE_FLAT_DEVICE_HIERARCHY=FLAT %{run} %t.out
55

6+
#include "../helpers.hpp"
67
#include <sycl/detail/core.hpp>
78
#include <sycl/ext/oneapi/experimental/composite_device.hpp>
89

@@ -15,18 +16,7 @@ bool isL0Backend(sycl::backend backend) {
1516
}
1617

1718
bool isCombinedMode() {
18-
char *Mode = nullptr;
19-
bool Res = false;
20-
#ifdef _WIN32
21-
size_t Size = 0;
22-
auto Err = _dupenv_s(&Mode, &Size, "ZE_FLAT_DEVICE_HIERARCHY");
23-
Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0);
24-
free(Mode);
25-
#else
26-
Mode = std::getenv("ZE_FLAT_DEVICE_HIERARCHY");
27-
Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0);
28-
#endif
29-
return Res;
19+
return env::getVal("ZE_FLAT_DEVICE_HIERARCHY") == "COMBINED";
3020
}
3121

3222
int main() {

sycl/test-e2e/Config/allowlist.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// 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
1313
// RUN: env TEST_INCORRECT_VALUE=1 env SYCL_DEVICE_ALLOWLIST="IncorrectKey:{{.*}}" ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run-unfiltered-devices} %t.out
1414

15+
#include "../helpers.hpp"
1516
#include <algorithm>
1617
#include <cstdlib>
1718
#include <exception>
@@ -33,7 +34,7 @@ static void replaceSpecialCharacters(std::string &Str) {
3334
int main() {
3435

3536
// Expected that the allowlist filter is not set
36-
if (getenv("PRINT_PLATFORM_INFO")) {
37+
if (env::isDefined("PRINT_PLATFORM_INFO")) {
3738
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
3839
std::string Name = Platform.get_info<sycl::info::platform::name>();
3940
std::string Ver = Platform.get_info<sycl::info::platform::version>();
@@ -51,7 +52,7 @@ int main() {
5152
}
5253

5354
// Expected that the allowlist filter is not set
54-
if (getenv("PRINT_DEVICE_INFO")) {
55+
if (env::isDefined("PRINT_DEVICE_INFO")) {
5556
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
5657
const sycl::device Dev = Platform.get_devices().at(0);
5758
std::string Name = Dev.get_info<sycl::info::device::name>();
@@ -71,7 +72,7 @@ int main() {
7172
}
7273

7374
// Expected the allowlist to be set with the "PRINT_DEVICE_INFO" run result
74-
if (getenv("TEST_DEVICE_AVAILABLE")) {
75+
if (env::isDefined("TEST_DEVICE_AVAILABLE")) {
7576
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
7677
if (Platform.get_devices().size() != 1)
7778
throw std::runtime_error("Expected only one device.");
@@ -82,13 +83,13 @@ int main() {
8283
}
8384

8485
// Expected the allowlist to be set but empty
85-
if (getenv("TEST_DEVICE_IS_NOT_AVAILABLE")) {
86+
if (env::isDefined("TEST_DEVICE_IS_NOT_AVAILABLE")) {
8687
if (!sycl::platform::get_platforms().empty())
8788
throw std::runtime_error("Expected no device is available");
8889
return 0;
8990
}
9091

91-
if (getenv("TEST_INCORRECT_VALUE")) {
92+
if (env::isDefined("TEST_INCORRECT_VALUE")) {
9293
try {
9394
sycl::platform::get_platforms();
9495
} catch (sycl::exception &E) {

sycl/test-e2e/Config/env_vars.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// RUN: %if cpu %{ env SYCL_PROGRAM_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %}
1414
// RUN: %if cpu %{ env SYCL_PROGRAM_APPEND_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %}
1515

16+
#include "../helpers.hpp"
1617
#include <cassert>
1718
#include <sycl/detail/core.hpp>
1819

@@ -22,7 +23,7 @@ int main() {
2223
int data = 5;
2324
buffer<int, 1> buf(&data, range<1>(1));
2425
queue myQueue;
25-
bool shouldCrash = getenv("SHOULD_CRASH");
26+
bool shouldCrash = env::isDefined("SHOULD_CRASH");
2627
try {
2728
myQueue.submit([&](handler &cgh) {
2829
auto B = buf.get_access<access::mode::read_write>(cgh);

sycl/test-e2e/DeviceLib/assert-windows.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//
2323
// CHECK-FALLBACK: ---> urProgramLink
2424

25+
#include "../helpers.hpp"
2526
#include <array>
2627
#include <assert.h>
2728
#include <iostream>
@@ -47,7 +48,7 @@ void simple_vadd(const std::array<T, N> &VA, const std::array<T, N> &VB,
4748
}
4849
});
4950

50-
int shouldCrash = getenv("SHOULD_CRASH") ? 1 : 0;
51+
bool shouldCrash = env::isDefined("SHOULD_CRASH");
5152

5253
sycl::range<1> numOfItems{N};
5354
sycl::buffer<T, 1> bufferA(VA.data(), numOfItems);

sycl/test-e2e/DeviceLib/assert.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
// Note that the work-item that hits the assert first may vary, since the order
9292
// of execution is undefined. We catch only the first one (whatever id it is).
9393

94+
#include "../helpers.hpp"
9495
#include "sycl/backend/opencl.hpp"
9596
#include <array>
9697
#include <assert.h>
@@ -128,7 +129,7 @@ void simple_vadd(const std::array<T, N> &VA, const std::array<T, N> &VB,
128129
if (sycl::opencl::has_extension(dev, "cl_intel_devicelib_assert")) {
129130
unsupported = false;
130131
}
131-
if (unsupported && getenv("SKIP_IF_NO_EXT")) {
132+
if (unsupported && env::isDefined("SKIP_IF_NO_EXT")) {
132133
fprintf(stderr, "Device has no support for cl_intel_devicelib_assert, "
133134
"skipping the test\n");
134135
exit(EXIT_SKIP_TEST);
@@ -160,7 +161,7 @@ int main() {
160161
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SKIP_TEST) {
161162
return 0;
162163
}
163-
if (getenv("SHOULD_CRASH")) {
164+
if (env::isDefined("SHOULD_CRASH")) {
164165
if (!WIFSIGNALED(status)) {
165166
fprintf(stderr, "error: process did not terminate by a signal\n");
166167
return 1;
@@ -176,12 +177,13 @@ int main() {
176177
}
177178
int sig = WTERMSIG(status);
178179
int expected = 0;
179-
if (const char *env = getenv("EXPECTED_SIGNAL")) {
180-
if (0 == strcmp(env, "SIGABRT")) {
180+
const std::string envVal = env::getVal("EXPECTED_SIGNAL");
181+
if (!envVal.empty()) {
182+
if (envVal == "SIGABRT") {
181183
expected = SIGABRT;
182-
} else if (0 == strcmp(env, "SIGSEGV")) {
184+
} else if (envVal == "SIGSEGV") {
183185
expected = SIGSEGV;
184-
} else if (0 == strcmp(env, "SIGIOT")) {
186+
} else if (envVal == "SIGIOT") {
185187
expected = SIGIOT;
186188
}
187189
if (!expected) {

sycl/test-e2e/FilterSelector/select_device.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,45 @@
1515

1616
#include <iostream>
1717

18+
#include "../helpers.hpp"
1819
#include <sycl/detail/core.hpp>
1920

2021
using namespace sycl;
2122
using namespace std;
2223

2324
int main() {
24-
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
25+
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
2526
std::string forcedPIs;
26-
if (envVal) {
27+
if (envVal.empty()) {
2728
forcedPIs = envVal;
2829
}
29-
if (!envVal || forcedPIs == "*" ||
30+
if (!envVal.empty() || forcedPIs == "*" ||
3031
forcedPIs.find("level_zero:gpu") != std::string::npos) {
3132
default_selector ds;
3233
device d = ds.select_device();
3334
string name = d.get_platform().get_info<info::platform::name>();
3435
assert(name.find("Level-Zero") != string::npos);
3536
}
36-
if (envVal && forcedPIs != "*" &&
37+
if (envVal.empty() && forcedPIs != "*" &&
3738
forcedPIs.find("opencl:gpu") != std::string::npos) {
3839
gpu_selector gs;
3940
device d = gs.select_device();
4041
string name = d.get_platform().get_info<info::platform::name>();
4142
assert(name.find("OpenCL") != string::npos);
4243
}
43-
if (!envVal || forcedPIs == "*" ||
44+
if (!envVal.empty() || forcedPIs == "*" ||
4445
forcedPIs.find("cpu") != std::string::npos) {
4546
cpu_selector cs;
4647
device d = cs.select_device();
4748
}
48-
if (!envVal || forcedPIs == "*" ||
49+
if (!envVal.empty() || forcedPIs == "*" ||
4950
forcedPIs.find("fpga") != std::string::npos) {
5051
accelerator_selector as;
5152
device d = as.select_device();
5253
}
53-
if (envVal && (forcedPIs.find("cpu") == std::string::npos &&
54-
forcedPIs.find("opencl") == std::string::npos &&
55-
forcedPIs.find("*") == std::string::npos)) {
54+
if (envVal.empty() && (forcedPIs.find("cpu") == std::string::npos &&
55+
forcedPIs.find("opencl") == std::string::npos &&
56+
forcedPIs.find("*") == std::string::npos)) {
5657
try {
5758
cpu_selector cs;
5859
device d = cs.select_device();

sycl/test-e2e/FilterSelector/select_device_acc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
#include <iostream>
1212

13+
#include "../helpers.hpp"
1314
#include <sycl/detail/core.hpp>
1415

1516
using namespace sycl;
1617
using namespace std;
1718

1819
int main() {
19-
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
20+
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
2021
std::string forcedPIs;
21-
if (envVal) {
22+
if (envVal.empty()) {
2223
forcedPIs = envVal;
2324
}
2425
{

sycl/test-e2e/FilterSelector/select_device_cpu.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
#include <iostream>
1212

13+
#include "../helpers.hpp"
1314
#include <sycl/ext/oneapi/filter_selector.hpp>
1415

1516
using namespace sycl;
1617
using namespace std;
1718

1819
int main() {
19-
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
20+
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
2021
std::string forcedPIs;
21-
if (envVal) {
22+
if (envVal.empty()) {
2223
forcedPIs = envVal;
2324
}
2425
{

sycl/test-e2e/FilterSelector/select_device_cuda.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
#include <iostream>
1212

13+
#include "../helpers.hpp"
1314
#include <sycl/detail/core.hpp>
1415

1516
using namespace sycl;
1617
using namespace std;
1718

1819
int main() {
19-
const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR");
20+
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
2021
string forcedPIs;
21-
if (envVal) {
22+
if (envVal.empty()) {
2223
forcedPIs = envVal;
2324
}
2425

sycl/test-e2e/FilterSelector/select_device_level_zero.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
//
99
// REQUIRES: level_zero,gpu
1010

11+
#include "../helpers.hpp"
1112
#include <iostream>
1213
#include <sycl/detail/core.hpp>
1314

1415
using namespace sycl;
1516
using namespace std;
1617

1718
int main() {
18-
const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR");
19+
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
1920
string forcedPIs;
20-
if (envVal) {
21+
if (envVal.empty()) {
2122
forcedPIs = envVal;
2223
}
2324

0 commit comments

Comments
 (0)