diff --git a/sycl/include/sycl/detail/device_filter.hpp b/sycl/include/sycl/detail/device_filter.hpp index f5aec21290009..5574bf69a3484 100644 --- a/sycl/include/sycl/detail/device_filter.hpp +++ b/sycl/include/sycl/detail/device_filter.hpp @@ -53,6 +53,21 @@ struct ods_target { ods_target(backend be) { Backend = be; }; ods_target(){}; friend std::ostream &operator<<(std::ostream &Out, const ods_target &Target); + +#if __cplusplus >= 202002L + bool operator==(const ods_target &Other) const = default; +#else + bool operator==(const ods_target &Other) const { + return Backend == Other.Backend && DeviceType == Other.DeviceType && + HasDeviceWildCard == Other.HasDeviceWildCard && + DeviceNum == Other.DeviceNum && + HasSubDeviceWildCard == Other.HasSubDeviceWildCard && + HasSubSubDeviceWildCard == Other.HasSubSubDeviceWildCard && + SubSubDeviceNum == Other.SubSubDeviceNum && + IsNegativeTarget == Other.IsNegativeTarget && + MatchesSeen == Other.MatchesSeen; + } +#endif }; class ods_target_list { diff --git a/sycl/test-e2e/OneapiDeviceSelector/backendonly_error.cpp b/sycl/test-e2e/OneapiDeviceSelector/backendonly_error.cpp deleted file mode 100644 index 3a5378d933cc6..0000000000000 --- a/sycl/test-e2e/OneapiDeviceSelector/backendonly_error.cpp +++ /dev/null @@ -1,6 +0,0 @@ -// REQUIRES: level_zero -// RUN: %clangxx -fsycl -fsycl-targets=%{sycl_triple} %S/Inputs/trivial.cpp -o %t.out - -// Calling ONEAPI_DEVICE_SELECTOR with a backend and no device should result in -// an error. -// RUN: env ONEAPI_DEVICE_SELECTOR=level_zero %{run-unfiltered-devices} not %t.out diff --git a/sycl/test-e2e/OneapiDeviceSelector/case_sensitivity.cpp b/sycl/test-e2e/OneapiDeviceSelector/case_sensitivity.cpp deleted file mode 100644 index e545c37da9b40..0000000000000 --- a/sycl/test-e2e/OneapiDeviceSelector/case_sensitivity.cpp +++ /dev/null @@ -1,8 +0,0 @@ - -// does not actually require OpenCL or GPU. Just testing parsing. - -// RUN: %clangxx -fsycl -fsycl-targets=%{sycl_triple} %S/Inputs/trivial.cpp -o %t.out -// RUN: env ONEAPI_DEVICE_SELECTOR="OPENCL:*" %{run-unfiltered-devices} %t.out -// RUN: env ONEAPI_DEVICE_SELECTOR="opencl:*" %{run-unfiltered-devices} %t.out -// RUN: env ONEAPI_DEVICE_SELECTOR="*:GPU" %{run-unfiltered-devices} %t.out -// RUN: env ONEAPI_DEVICE_SELECTOR="*:gpu" %{run-unfiltered-devices} %t.out diff --git a/sycl/unittests/CMakeLists.txt b/sycl/unittests/CMakeLists.txt index cc103358a10ad..a6d7ba4cf6d3b 100644 --- a/sycl/unittests/CMakeLists.txt +++ b/sycl/unittests/CMakeLists.txt @@ -59,3 +59,4 @@ if (NOT WIN32) endif() add_subdirectory(sampler) add_subdirectory(reduction) +add_subdirectory(OneAPIDeviceSelector) diff --git a/sycl/unittests/OneAPIDeviceSelector/CMakeLists.txt b/sycl/unittests/OneAPIDeviceSelector/CMakeLists.txt new file mode 100644 index 0000000000000..32e20acc8529c --- /dev/null +++ b/sycl/unittests/OneAPIDeviceSelector/CMakeLists.txt @@ -0,0 +1,3 @@ +add_sycl_unittest(OneAPIDeviceSelectorTests OBJECT + Tests.cpp +) diff --git a/sycl/unittests/OneAPIDeviceSelector/Tests.cpp b/sycl/unittests/OneAPIDeviceSelector/Tests.cpp new file mode 100644 index 0000000000000..5575080262567 --- /dev/null +++ b/sycl/unittests/OneAPIDeviceSelector/Tests.cpp @@ -0,0 +1,57 @@ +//===---------------------------- Tests.cpp -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include +#include + +#include + +#include +#include +#include + +TEST(OneAPIDeviceSelector, IsCaseInsensitive) { + ASSERT_EQ(sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("OPENCL:*"), + sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("opencl:*")) + << " backend should be case-insensitive"; + + ASSERT_EQ(sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("*:GPU"), + sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("*:gpu")) + << " device type should be case-insensitive"; +} + +TEST(OneAPIDeviceSelector, EmitsErrorIfOnlyBackendIsSpecified) { + try { + std::ignore = sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("level_zero"); + FAIL() << "An exception was expected"; + } catch (const sycl::exception &e) { + ASSERT_EQ(e.code(), sycl::errc::invalid); + ASSERT_EQ(std::string(e.what()), + "Incomplete selector! Try 'level_zero:*' if all " + "devices under the backend was original intention."); + } +} + +TEST(OneAPIDeviceSelector, EmitsErrorIfBackendStringIsInvalid) { + try { + std::ignore = sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("macaroni:*"); + FAIL() << "An exception was expected"; + } catch (const sycl::exception &e) { + ASSERT_EQ(e.code(), sycl::errc::invalid); + // FIXME: the error below could be better + ASSERT_EQ(std::string(e.what()), + "ONEAPI_DEVICE_SELECTOR parsing error. Backend is required but " + "missing from \"macaroni:*\""); + } +} + +// TODO: test case ":" +// TODO: test case ":cpu" +// TODO: test case "level_zero:cpu:cpu" +// TODO: test case "opencl:" +// TODO: other positive test cases for parsing device, sub-devices, etc.