Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions unified-runtime/source/loader/ur_adapter_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class AdapterRegistry {
// success.
logger::error("ERROR: missing backend, format of filter = "
"'[!]backend:filterStrings'");
return UR_RESULT_SUCCESS;
odsEnvMap = std::nullopt;
}
logger::debug("getenv_to_map parsed env var and {} a map",
(odsEnvMap.has_value() ? "produced" : "failed to produce"));
Expand All @@ -245,20 +245,34 @@ class AdapterRegistry {
EnvVarMap mapODS =
odsEnvMap.has_value() ? odsEnvMap.value() : EnvVarMap{{"*", {"*"}}};

// Check all backends are valid backend names
for (auto entry : mapODS) {
if (entry.first == "*" || entry.first == "!*") {
continue;
}
auto check = [&](const ur_adapter_manifest &m) {
if (entry.first == m.name || entry.first == "!" + m.name) {
return true;
}
return false;
};
if (std::any_of(ur_adapter_manifests.begin(), ur_adapter_manifests.end(),
check)) {
continue;
}

// Backend name is not legal, wipe the list
mapODS = EnvVarMap{{"*", {"*"}}};
break;
}

std::vector<FilterTerm> positiveFilters;
std::vector<FilterTerm> negativeFilters;

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

Expand Down
12 changes: 9 additions & 3 deletions unified-runtime/source/loader/ur_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,15 @@ ur_result_t urDeviceGetSelected(ur_platform_handle_t hPlatform,
// (If we wished to preserve the ordering of terms, we could replace
// `std::map` with `std::queue<std::pair<key_type_t, value_type_t>>` or
// something similar.)
auto maybeEnvVarMap =
getenv_to_map("ONEAPI_DEVICE_SELECTOR", /* reject_empty= */ false,
/* allow_duplicate= */ false, /* lower= */ true);
std::optional<EnvVarMap> maybeEnvVarMap{};
try {
maybeEnvVarMap =
getenv_to_map("ONEAPI_DEVICE_SELECTOR", /* reject_empty= */ false,
/* allow_duplicate= */ false, /* lower= */ true);
} catch (...) {
logger::error("ERROR: could not parse ONEAPI_DEVICE_SELECTOR string");
return UR_RESULT_ERROR_INVALID_VALUE;
}
logger::debug(
"getenv_to_map parsed env var and {} a map",
(maybeEnvVarMap.has_value() ? "produced" : "failed to produce"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingBackend) {
setenv("ONEAPI_DEVICE_SELECTOR", ":garbage", 1);
uint32_t count = 0;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_UNKNOWN,
UR_RESULT_ERROR_INVALID_VALUE,
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
ASSERT_EQ(count, 0);
}
Expand Down Expand Up @@ -204,7 +204,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterString) {
setenv("ONEAPI_DEVICE_SELECTOR", "*:0,,2", 1);
uint32_t count = 0;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_UNKNOWN,
UR_RESULT_ERROR_INVALID_VALUE,
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
ASSERT_EQ(count, 0);
}
Expand Down
10 changes: 5 additions & 5 deletions unified-runtime/test/loader/adapter_registry/prefilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST_F(adapterPreFilterTest, testPrefilterDiscardFilterMultipleBackends) {
}

TEST_F(adapterPreFilterTest, testPrefilterAcceptAndDiscardFilter) {
SetUp("!cuda:*;level_zero:*");
SetUp("level_zero:*;!cuda:*");
auto levelZeroExists =
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
EXPECT_TRUE(levelZeroExists);
Expand All @@ -76,7 +76,7 @@ TEST_F(adapterPreFilterTest, testPrefilterAcceptAndDiscardFilter) {
}

TEST_F(adapterPreFilterTest, testPrefilterDiscardFilterAll) {
SetUp("*");
SetUp("*:*");
auto levelZeroExists =
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
EXPECT_TRUE(levelZeroExists);
Expand Down Expand Up @@ -115,10 +115,10 @@ TEST_F(adapterPreFilterTest, testPrefilterWithInvalidBackend) {
}

TEST_F(adapterPreFilterTest, testPrefilterWithNotAllAndAcceptFilter) {
SetUp("!*;level_zero");
SetUp("level_zero:*;!*:*");
auto levelZeroExists =
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
EXPECT_TRUE(levelZeroExists);
EXPECT_FALSE(levelZeroExists);
auto openclExists =
std::any_of(registry->cbegin(), registry->cend(), hasOpenclLibName);
EXPECT_FALSE(openclExists);
Expand All @@ -128,7 +128,7 @@ TEST_F(adapterPreFilterTest, testPrefilterWithNotAllAndAcceptFilter) {
}

TEST_F(adapterPreFilterTest, testPrefilterWithNotAllFilter) {
SetUp("!*");
SetUp("!*:*");
auto levelZeroExists =
std::any_of(registry->cbegin(), registry->cend(), haslevelzeroLibName);
EXPECT_FALSE(levelZeroExists);
Expand Down