Skip to content

Commit 1d24713

Browse files
[SYCL] Fix ONEAPI_DEVICE_SELECTOR handling of discard filters. (#13927)
Currently, ONEAPI_DEVICE_SELECTOR incorrectly parses '!', used for discard filters.
1 parent 46cfb06 commit 1d24713

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

sycl/source/detail/device_filter.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
201201
std::vector<std::string_view> Pair =
202202
tokenize(Entry, ":", true /* ProhibitEmptyTokens */);
203203

204+
// Error handling. ONEAPI_DEVICE_SELECTOR terms should be in the
205+
// format: <backend>:<devices>.
204206
if (Pair.empty()) {
205207
std::stringstream ss;
206208
ss << "Incomplete selector! Backend and device must be specified.";
@@ -210,8 +212,24 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
210212
ss << "Incomplete selector! Try '" << Pair[0]
211213
<< ":*' if all devices under the backend was original intention.";
212214
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
213-
} else if (Pair.size() == 2) {
214-
backend be = Parse_ODS_Backend(Pair[0], Entry); // Pair[0] is backend.
215+
} else if (Pair.size() > 2) {
216+
std::stringstream ss;
217+
ss << "Error parsing selector string \"" << Entry
218+
<< "\" Too many colons (:)";
219+
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
220+
}
221+
222+
// Parse ONEAPI_DEVICE_SELECTOR terms for Pair.size() == 2.
223+
else {
224+
225+
// Remove `!` from input backend string if it is present.
226+
std::string_view input_be = Pair[0];
227+
if (Pair[0][0] == '!')
228+
input_be = Pair[0].substr(1);
229+
230+
backend be = Parse_ODS_Backend(input_be, Entry);
231+
232+
// For each backend, we can have multiple targets, seperated by ','.
215233
std::vector<std::string_view> Targets = tokenize(Pair[1], ",");
216234
for (auto TargetStr : Targets) {
217235
ods_target DeviceTarget(be);
@@ -233,11 +251,6 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
233251
Parse_ODS_Device(DeviceTarget, TargetStr);
234252
Result.push_back(DeviceTarget);
235253
}
236-
} else if (Pair.size() > 2) {
237-
std::stringstream ss;
238-
ss << "Error parsing selector string \"" << Entry
239-
<< "\" Too many colons (:)";
240-
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
241254
}
242255
}
243256

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %{build} -o %t.out
2+
3+
// Test discard filters in ONEAPI_DEVICE_SELECTOR.
4+
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:gpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{gpu|GPU|Gpu}}{{.*}}]:[{{.*}}]"
5+
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:cpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|cpu}}{{.*}}]:[{{.*}}]"
6+
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:cpu,gpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|cpu|gpu|GPU|Gpu}}{{.*}}]:[{{.*}}]"
7+
8+
// RUN: env ONEAPI_DEVICE_SELECTOR="!opencl:*" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}]:[{{.*}}{{OpenCL|opencl|Opencl}}{{.*}}]"
9+
// RUN: env ONEAPI_DEVICE_SELECTOR="!level_zero:*" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}]:[{{.*}}{{Level-Zero}}{{.*}}]"
10+
11+
// RUN: env ONEAPI_DEVICE_SELECTOR="!level_zero:cpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|Cpu}}{{.*}}]:[{{.*}}{{Level-Zero}}{{.*}}]"
12+
13+
#include <iostream>
14+
#include <sycl/detail/core.hpp>
15+
using namespace sycl;
16+
17+
int main() {
18+
for (auto &d : device::get_devices()) {
19+
// Get device name and backend name.
20+
std::string device_name = d.get_info<info::device::name>();
21+
std::string be_name = d.get_platform().get_info<info::platform::name>();
22+
23+
std::cout << "[" << device_name << "]:[" << be_name << "]" << std::endl;
24+
}
25+
return 0;
26+
}

0 commit comments

Comments
 (0)