Skip to content

Commit daf1fa6

Browse files
authored
[UR] Make ONEAPI_DEVICE_SELECTOR prefilter test pass (#17667)
The prefilter.cpp test was failing, this fixes it as follows: * Strings that can't be parsed are now treated as `*:*` rather than `!*:*`. * Backend names are validated; any invalid ones cause the entire string to be rejected. * In ur_lib.cpp, the parser is updated to handle failing to parse the string (doesn't affect the test, but a nice to have). * Some selector strings in the test itself were invalid and updated. This was done according to the specification of ONEAPI_DEVICE_SELECTOR located at https://github.com/intel/llvm/blob/sycl/sycl/doc/EnvironmentVariables.md#oneapi_device_selector .
1 parent f5d4c1e commit daf1fa6

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

unified-runtime/source/loader/ur_adapter_registry.hpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class AdapterRegistry {
234234
// success.
235235
logger::error("ERROR: missing backend, format of filter = "
236236
"'[!]backend:filterStrings'");
237-
return UR_RESULT_SUCCESS;
237+
odsEnvMap = std::nullopt;
238238
}
239239
logger::debug("getenv_to_map parsed env var and {} a map",
240240
(odsEnvMap.has_value() ? "produced" : "failed to produce"));
@@ -245,20 +245,34 @@ class AdapterRegistry {
245245
EnvVarMap mapODS =
246246
odsEnvMap.has_value() ? odsEnvMap.value() : EnvVarMap{{"*", {"*"}}};
247247

248+
// Check all backends are valid backend names
249+
for (auto entry : mapODS) {
250+
if (entry.first == "*" || entry.first == "!*") {
251+
continue;
252+
}
253+
auto check = [&](const ur_adapter_manifest &m) {
254+
if (entry.first == m.name || entry.first == "!" + m.name) {
255+
return true;
256+
}
257+
return false;
258+
};
259+
if (std::any_of(ur_adapter_manifests.begin(), ur_adapter_manifests.end(),
260+
check)) {
261+
continue;
262+
}
263+
264+
// Backend name is not legal, wipe the list
265+
mapODS = EnvVarMap{{"*", {"*"}}};
266+
break;
267+
}
268+
248269
std::vector<FilterTerm> positiveFilters;
249270
std::vector<FilterTerm> negativeFilters;
250271

251272
for (auto &termPair : mapODS) {
252273
std::string backend = termPair.first;
253274
// TODO: Figure out how to process all ODS errors rather than returning
254275
// on the first error.
255-
if (backend.empty()) {
256-
// FIXME: never true because getenv_to_map rejects this case
257-
// malformed term: missing backend -- output ERROR, then continue
258-
logger::error("ERROR: missing backend, format of filter = "
259-
"'[!]backend:filterStrings'");
260-
continue;
261-
}
262276
logger::debug("ONEAPI_DEVICE_SELECTOR Pre-Filter with backend '{}' ",
263277
backend);
264278

unified-runtime/source/loader/ur_lib.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,15 @@ ur_result_t urDeviceGetSelected(ur_platform_handle_t hPlatform,
330330
// (If we wished to preserve the ordering of terms, we could replace
331331
// `std::map` with `std::queue<std::pair<key_type_t, value_type_t>>` or
332332
// something similar.)
333-
auto maybeEnvVarMap =
334-
getenv_to_map("ONEAPI_DEVICE_SELECTOR", /* reject_empty= */ false,
335-
/* allow_duplicate= */ false, /* lower= */ true);
333+
std::optional<EnvVarMap> maybeEnvVarMap{};
334+
try {
335+
maybeEnvVarMap =
336+
getenv_to_map("ONEAPI_DEVICE_SELECTOR", /* reject_empty= */ false,
337+
/* allow_duplicate= */ false, /* lower= */ true);
338+
} catch (...) {
339+
logger::error("ERROR: could not parse ONEAPI_DEVICE_SELECTOR string");
340+
return UR_RESULT_ERROR_INVALID_VALUE;
341+
}
336342
logger::debug(
337343
"getenv_to_map parsed env var and {} a map",
338344
(maybeEnvVarMap.has_value() ? "produced" : "failed to produce"));

unified-runtime/test/conformance/device/urDeviceGetSelected.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingBackend) {
187187
uur::set_env("ONEAPI_DEVICE_SELECTOR", ":garbage");
188188
uint32_t count = 0;
189189
ASSERT_EQ_RESULT(
190-
UR_RESULT_ERROR_UNKNOWN,
190+
UR_RESULT_ERROR_INVALID_VALUE,
191191
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
192192
ASSERT_EQ(count, 0);
193193
}
@@ -227,7 +227,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterString) {
227227
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0,,2");
228228
uint32_t count = 0;
229229
ASSERT_EQ_RESULT(
230-
UR_RESULT_ERROR_UNKNOWN,
230+
UR_RESULT_ERROR_INVALID_VALUE,
231231
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
232232
ASSERT_EQ(count, 0);
233233
}

unified-runtime/test/loader/adapter_registry/prefilter.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST_F(adapterPreFilterTest, testPrefilterDiscardFilterMultipleBackends) {
6363
}
6464

6565
TEST_F(adapterPreFilterTest, testPrefilterAcceptAndDiscardFilter) {
66-
SetUp("!cuda:*;level_zero:*");
66+
SetUp("level_zero:*;!cuda:*");
6767
auto levelZeroExists =
6868
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
6969
EXPECT_TRUE(levelZeroExists);
@@ -76,7 +76,7 @@ TEST_F(adapterPreFilterTest, testPrefilterAcceptAndDiscardFilter) {
7676
}
7777

7878
TEST_F(adapterPreFilterTest, testPrefilterDiscardFilterAll) {
79-
SetUp("*");
79+
SetUp("*:*");
8080
auto levelZeroExists =
8181
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
8282
EXPECT_TRUE(levelZeroExists);
@@ -115,10 +115,10 @@ TEST_F(adapterPreFilterTest, testPrefilterWithInvalidBackend) {
115115
}
116116

117117
TEST_F(adapterPreFilterTest, testPrefilterWithNotAllAndAcceptFilter) {
118-
SetUp("!*;level_zero");
118+
SetUp("level_zero:*;!*:*");
119119
auto levelZeroExists =
120120
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
121-
EXPECT_TRUE(levelZeroExists);
121+
EXPECT_FALSE(levelZeroExists);
122122
auto openclExists =
123123
std::any_of(registry->cbegin(), registry->cend(), hasOpenclLibName);
124124
EXPECT_FALSE(openclExists);
@@ -128,7 +128,7 @@ TEST_F(adapterPreFilterTest, testPrefilterWithNotAllAndAcceptFilter) {
128128
}
129129

130130
TEST_F(adapterPreFilterTest, testPrefilterWithNotAllFilter) {
131-
SetUp("!*");
131+
SetUp("!*:*");
132132
auto levelZeroExists =
133133
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
134134
EXPECT_FALSE(levelZeroExists);

0 commit comments

Comments
 (0)