diff options
| author | Callum Fare <callum@codeplay.com> | 2024-12-05 08:34:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-05 09:34:04 +0100 |
| commit | fd3907ccb583df99e9c19d2fe84e4e7c52d75de9 (patch) | |
| tree | deaffb6b369c1ec87261df173b32717b07f7525c /offload/unittests/OffloadAPI | |
| parent | 636beb6a2833ee0290935f679252c1b662721b31 (diff) | |
| download | llvm-fd3907ccb583df99e9c19d2fe84e4e7c52d75de9.zip llvm-fd3907ccb583df99e9c19d2fe84e4e7c52d75de9.tar.gz llvm-fd3907ccb583df99e9c19d2fe84e4e7c52d75de9.tar.bz2 | |
Reland #118503: [Offload] Introduce offload-tblgen and initial new API implementation (#118614)
Reland #118503. Added a fix for builds with `-DBUILD_SHARED_LIBS=ON`
(see last commit). Otherwise the changes are identical.
---
### New API
Previous discussions at the LLVM/Offload meeting have brought up the
need for a new API for exposing the functionality of the plugins. This
change introduces a very small subset of a new API, which is primarily
for testing the offload tooling and demonstrating how a new API can fit
into the existing code base without being too disruptive. Exact designs
for these entry points and future additions can be worked out over time.
The new API does however introduce the bare minimum functionality to
implement device discovery for Unified Runtime and SYCL. This means that
the `urinfo` and `sycl-ls` tools can be used on top of Offload. A
(rough) implementation of a Unified Runtime adapter (aka plugin) for
Offload is available
[here](https://github.com/callumfare/unified-runtime/tree/offload_adapter).
Our intention is to maintain this and use it to implement and test
Offload API changes with SYCL.
### Demoing the new API
```sh
# From the runtime build directory
$ ninja LibomptUnitTests
$ OFFLOAD_TRACE=1 ./offload/unittests/OffloadAPI/offload.unittests
```
### Open questions and future work
* Only some of the available device info is exposed, and not all the
possible device queries needed for SYCL are implemented by the plugins.
A sensible next step would be to refactor and extend the existing device
info queries in the plugins. The existing info queries are all strings,
but the new API introduces the ability to return any arbitrary type.
* It may be sensible at some point for the plugins to implement the new
API directly, and the higher level code on top of it could be made
generic, but this is more of a long-term possibility.
Diffstat (limited to 'offload/unittests/OffloadAPI')
14 files changed, 618 insertions, 0 deletions
diff --git a/offload/unittests/OffloadAPI/CMakeLists.txt b/offload/unittests/OffloadAPI/CMakeLists.txt new file mode 100644 index 0000000..033ee2b6 --- /dev/null +++ b/offload/unittests/OffloadAPI/CMakeLists.txt @@ -0,0 +1,16 @@ +set(PLUGINS_TEST_COMMON LLVMOffload) +set(PLUGINS_TEST_INCLUDE ${LIBOMPTARGET_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/common) + +add_libompt_unittest("offload.unittests" + ${CMAKE_CURRENT_SOURCE_DIR}/common/Environment.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatform.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatformCount.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatformInfo.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatformInfoSize.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/device/olGetDevice.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/device/olGetDeviceCount.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/device/olGetDeviceInfo.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/device/olGetDeviceInfoSize.cpp) +add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON}) +target_link_libraries("offload.unittests" PRIVATE ${PLUGINS_TEST_COMMON}) +target_include_directories("offload.unittests" PRIVATE ${PLUGINS_TEST_INCLUDE}) diff --git a/offload/unittests/OffloadAPI/common/Environment.cpp b/offload/unittests/OffloadAPI/common/Environment.cpp new file mode 100644 index 0000000..f07a66c --- /dev/null +++ b/offload/unittests/OffloadAPI/common/Environment.cpp @@ -0,0 +1,96 @@ +//===------- Offload API tests - gtest environment ------------------------===// +// +// 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 "Environment.hpp" +#include "Fixtures.hpp" +#include "llvm/Support/CommandLine.h" +#include <OffloadAPI.h> + +using namespace llvm; + +// Wrapper so we don't have to constantly init and shutdown Offload in every +// test, while having sensible lifetime for the platform environment +struct OffloadInitWrapper { + OffloadInitWrapper() { olInit(); } + ~OffloadInitWrapper() { olShutDown(); } +}; +static OffloadInitWrapper Wrapper{}; + +static cl::opt<std::string> + SelectedPlatform("platform", cl::desc("Only test the specified platform"), + cl::value_desc("platform")); + +std::ostream &operator<<(std::ostream &Out, + const ol_platform_handle_t &Platform) { + size_t Size; + olGetPlatformInfoSize(Platform, OL_PLATFORM_INFO_NAME, &Size); + std::vector<char> Name(Size); + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_NAME, Size, Name.data()); + Out << Name.data(); + return Out; +} + +std::ostream &operator<<(std::ostream &Out, + const std::vector<ol_platform_handle_t> &Platforms) { + for (auto Platform : Platforms) { + Out << "\n * \"" << Platform << "\""; + } + return Out; +} + +const std::vector<ol_platform_handle_t> &TestEnvironment::getPlatforms() { + static std::vector<ol_platform_handle_t> Platforms{}; + + if (Platforms.empty()) { + uint32_t PlatformCount = 0; + olGetPlatformCount(&PlatformCount); + if (PlatformCount > 0) { + Platforms.resize(PlatformCount); + olGetPlatform(PlatformCount, Platforms.data()); + } + } + + return Platforms; +} + +// Get a single platform, which may be selected by the user. +ol_platform_handle_t TestEnvironment::getPlatform() { + static ol_platform_handle_t Platform = nullptr; + const auto &Platforms = getPlatforms(); + + if (!Platform) { + if (SelectedPlatform != "") { + for (const auto CandidatePlatform : Platforms) { + std::stringstream PlatformName; + PlatformName << CandidatePlatform; + if (SelectedPlatform == PlatformName.str()) { + Platform = CandidatePlatform; + return Platform; + } + } + std::cout << "No platform found with the name \"" << SelectedPlatform + << "\". Choose from:" << Platforms << "\n"; + std::exit(1); + } else { + // Pick a single platform. We prefer one that has available devices, but + // just pick the first initially in case none have any devices. + Platform = Platforms[0]; + for (auto CandidatePlatform : Platforms) { + uint32_t NumDevices = 0; + if (olGetDeviceCount(CandidatePlatform, &NumDevices) == OL_SUCCESS) { + if (NumDevices > 0) { + Platform = CandidatePlatform; + break; + } + } + } + } + } + + return Platform; +} diff --git a/offload/unittests/OffloadAPI/common/Environment.hpp b/offload/unittests/OffloadAPI/common/Environment.hpp new file mode 100644 index 0000000..6dba238 --- /dev/null +++ b/offload/unittests/OffloadAPI/common/Environment.hpp @@ -0,0 +1,17 @@ +//===------- Offload API tests - gtest environment ------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +namespace TestEnvironment { +const std::vector<ol_platform_handle_t> &getPlatforms(); +ol_platform_handle_t getPlatform(); +} // namespace TestEnvironment diff --git a/offload/unittests/OffloadAPI/common/Fixtures.hpp b/offload/unittests/OffloadAPI/common/Fixtures.hpp new file mode 100644 index 0000000..410a435 --- /dev/null +++ b/offload/unittests/OffloadAPI/common/Fixtures.hpp @@ -0,0 +1,64 @@ +//===------- Offload API tests - gtest fixtures --==-----------------------===// +// +// 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 <OffloadAPI.h> +#include <OffloadPrint.hpp> +#include <gtest/gtest.h> + +#include "Environment.hpp" + +#pragma once + +#ifndef ASSERT_SUCCESS +#define ASSERT_SUCCESS(ACTUAL) ASSERT_EQ(OL_SUCCESS, ACTUAL) +#endif + +// TODO: rework this so the EXPECTED/ACTUAL results are readable +#ifndef ASSERT_ERROR +#define ASSERT_ERROR(EXPECTED, ACTUAL) \ + do { \ + ol_result_t Res = ACTUAL; \ + ASSERT_TRUE(Res && (Res->Code == EXPECTED)); \ + } while (0) +#endif + +#define RETURN_ON_FATAL_FAILURE(...) \ + __VA_ARGS__; \ + if (this->HasFatalFailure() || this->IsSkipped()) { \ + return; \ + } \ + (void)0 + +struct offloadTest : ::testing::Test { + // No special behavior now, but just in case we need to override it in future +}; + +struct offloadPlatformTest : offloadTest { + void SetUp() override { + RETURN_ON_FATAL_FAILURE(offloadTest::SetUp()); + + Platform = TestEnvironment::getPlatform(); + ASSERT_NE(Platform, nullptr); + } + + ol_platform_handle_t Platform; +}; + +struct offloadDeviceTest : offloadPlatformTest { + void SetUp() override { + RETURN_ON_FATAL_FAILURE(offloadPlatformTest::SetUp()); + + uint32_t NumDevices; + ASSERT_SUCCESS(olGetDeviceCount(Platform, &NumDevices)); + if (NumDevices == 0) + GTEST_SKIP() << "No available devices on this platform."; + ASSERT_SUCCESS(olGetDevice(Platform, 1, &Device)); + } + + ol_device_handle_t Device; +}; diff --git a/offload/unittests/OffloadAPI/device/olDeviceInfo.hpp b/offload/unittests/OffloadAPI/device/olDeviceInfo.hpp new file mode 100644 index 0000000..0691525 --- /dev/null +++ b/offload/unittests/OffloadAPI/device/olDeviceInfo.hpp @@ -0,0 +1,21 @@ +//===------- Offload API tests - Helpers for device info query testing ----===// +// +// 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 +// +//===----------------------------------------------------------------------===// +#pragma once + +#include <unordered_map> +#include <vector> + +// TODO: We could autogenerate these +inline std::vector<ol_device_info_t> DeviceQueries = { + OL_DEVICE_INFO_TYPE, OL_DEVICE_INFO_PLATFORM, OL_DEVICE_INFO_NAME, + OL_DEVICE_INFO_VENDOR, OL_DEVICE_INFO_DRIVER_VERSION}; + +inline std::unordered_map<ol_device_info_t, size_t> DeviceInfoSizeMap = { + {OL_DEVICE_INFO_TYPE, sizeof(ol_device_type_t)}, + {OL_DEVICE_INFO_PLATFORM, sizeof(ol_platform_handle_t)}, +}; diff --git a/offload/unittests/OffloadAPI/device/olGetDevice.cpp b/offload/unittests/OffloadAPI/device/olGetDevice.cpp new file mode 100644 index 0000000..68d4682d --- /dev/null +++ b/offload/unittests/OffloadAPI/device/olGetDevice.cpp @@ -0,0 +1,39 @@ +//===------- Offload API tests - olGetDevice -------------------------===// +// +// 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 "../common/Fixtures.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +using olGetDeviceTest = offloadPlatformTest; + +TEST_F(olGetDeviceTest, Success) { + uint32_t Count = 0; + ASSERT_SUCCESS(olGetDeviceCount(Platform, &Count)); + if (Count == 0) + GTEST_SKIP() << "No available devices on this platform."; + + std::vector<ol_device_handle_t> Devices(Count); + ASSERT_SUCCESS(olGetDevice(Platform, Count, Devices.data())); + for (auto Device : Devices) { + ASSERT_NE(nullptr, Device); + } +} + +TEST_F(olGetDeviceTest, SuccessSubsetOfDevices) { + uint32_t Count; + ASSERT_SUCCESS(olGetDeviceCount(Platform, &Count)); + if (Count < 2) + GTEST_SKIP() << "Only one device is available on this platform."; + + std::vector<ol_device_handle_t> Devices(Count - 1); + ASSERT_SUCCESS(olGetDevice(Platform, Count - 1, Devices.data())); + for (auto Device : Devices) { + ASSERT_NE(nullptr, Device); + } +} diff --git a/offload/unittests/OffloadAPI/device/olGetDeviceCount.cpp b/offload/unittests/OffloadAPI/device/olGetDeviceCount.cpp new file mode 100644 index 0000000..ef377d6 --- /dev/null +++ b/offload/unittests/OffloadAPI/device/olGetDeviceCount.cpp @@ -0,0 +1,28 @@ +//===------- Offload API tests - olGetDeviceCount --------------------===// +// +// 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 "../common/Fixtures.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +using olGetDeviceCountTest = offloadPlatformTest; + +TEST_F(olGetDeviceCountTest, Success) { + uint32_t Count = 0; + ASSERT_SUCCESS(olGetDeviceCount(Platform, &Count)); +} + +TEST_F(olGetDeviceCountTest, InvalidNullPlatform) { + uint32_t Count = 0; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olGetDeviceCount(nullptr, &Count)); +} + +TEST_F(olGetDeviceCountTest, InvalidNullPointer) { + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, + olGetDeviceCount(Platform, nullptr)); +} diff --git a/offload/unittests/OffloadAPI/device/olGetDeviceInfo.cpp b/offload/unittests/OffloadAPI/device/olGetDeviceInfo.cpp new file mode 100644 index 0000000..c936802 --- /dev/null +++ b/offload/unittests/OffloadAPI/device/olGetDeviceInfo.cpp @@ -0,0 +1,76 @@ +//===------- Offload API tests - olGetDeviceInfo ---------------------===// +// +// 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 "../common/Fixtures.hpp" +#include "olDeviceInfo.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +struct olGetDeviceInfoTest : offloadDeviceTest, + ::testing::WithParamInterface<ol_device_info_t> { + + void SetUp() override { RETURN_ON_FATAL_FAILURE(offloadDeviceTest::SetUp()); } +}; + +INSTANTIATE_TEST_SUITE_P( + , olGetDeviceInfoTest, ::testing::ValuesIn(DeviceQueries), + [](const ::testing::TestParamInfo<ol_device_info_t> &info) { + std::stringstream ss; + ss << info.param; + return ss.str(); + }); + +TEST_P(olGetDeviceInfoTest, Success) { + ol_device_info_t InfoType = GetParam(); + size_t Size = 0; + + ASSERT_SUCCESS(olGetDeviceInfoSize(Device, InfoType, &Size)); + + std::vector<char> InfoData(Size); + ASSERT_SUCCESS(olGetDeviceInfo(Device, InfoType, Size, InfoData.data())); + + if (InfoType == OL_DEVICE_INFO_PLATFORM) { + auto *ReturnedPlatform = + reinterpret_cast<ol_platform_handle_t *>(InfoData.data()); + ASSERT_EQ(Platform, *ReturnedPlatform); + } +} + +TEST_F(olGetDeviceInfoTest, InvalidNullHandleDevice) { + ol_device_type_t DeviceType; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, + olGetDeviceInfo(nullptr, OL_DEVICE_INFO_TYPE, + sizeof(ol_device_type_t), &DeviceType)); +} + +TEST_F(olGetDeviceInfoTest, InvalidEnumerationInfoType) { + ol_device_type_t DeviceType; + ASSERT_ERROR(OL_ERRC_INVALID_ENUMERATION, + olGetDeviceInfo(Device, OL_DEVICE_INFO_FORCE_UINT32, + sizeof(ol_device_type_t), &DeviceType)); +} + +TEST_F(olGetDeviceInfoTest, InvalidSizePropSize) { + ol_device_type_t DeviceType; + ASSERT_ERROR(OL_ERRC_INVALID_SIZE, + olGetDeviceInfo(Device, OL_DEVICE_INFO_TYPE, 0, &DeviceType)); +} + +TEST_F(olGetDeviceInfoTest, InvalidSizePropSizeSmall) { + ol_device_type_t DeviceType; + ASSERT_ERROR(OL_ERRC_INVALID_SIZE, + olGetDeviceInfo(Device, OL_DEVICE_INFO_TYPE, + sizeof(DeviceType) - 1, &DeviceType)); +} + +TEST_F(olGetDeviceInfoTest, InvalidNullPointerPropValue) { + ol_device_type_t DeviceType; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, + olGetDeviceInfo(Device, OL_DEVICE_INFO_TYPE, sizeof(DeviceType), + nullptr)); +} diff --git a/offload/unittests/OffloadAPI/device/olGetDeviceInfoSize.cpp b/offload/unittests/OffloadAPI/device/olGetDeviceInfoSize.cpp new file mode 100644 index 0000000..9e792d1c --- /dev/null +++ b/offload/unittests/OffloadAPI/device/olGetDeviceInfoSize.cpp @@ -0,0 +1,58 @@ +//===------- Offload API tests - olGetDeviceInfoSize -----------------===// +// +// 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 <OffloadAPI.h> + +#include "../common/Fixtures.hpp" +#include "olDeviceInfo.hpp" + +struct olGetDeviceInfoSizeTest + : offloadDeviceTest, + ::testing::WithParamInterface<ol_device_info_t> { + + void SetUp() override { RETURN_ON_FATAL_FAILURE(offloadDeviceTest::SetUp()); } +}; + +// TODO: We could autogenerate the list of enum values +INSTANTIATE_TEST_SUITE_P( + , olGetDeviceInfoSizeTest, ::testing::ValuesIn(DeviceQueries), + [](const ::testing::TestParamInfo<ol_device_info_t> &info) { + std::stringstream ss; + ss << info.param; + return ss.str(); + }); + +TEST_P(olGetDeviceInfoSizeTest, Success) { + ol_device_info_t InfoType = GetParam(); + size_t Size = 0; + + ASSERT_SUCCESS(olGetDeviceInfoSize(Device, InfoType, &Size)); + auto ExpectedSize = DeviceInfoSizeMap.find(InfoType); + if (ExpectedSize != DeviceInfoSizeMap.end()) { + ASSERT_EQ(Size, ExpectedSize->second); + } else { + ASSERT_NE(Size, 0lu); + } +} + +TEST_F(olGetDeviceInfoSizeTest, InvalidNullHandle) { + size_t Size = 0; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, + olGetDeviceInfoSize(nullptr, OL_DEVICE_INFO_TYPE, &Size)); +} + +TEST_F(olGetDeviceInfoSizeTest, InvalidDeviceInfoEnumeration) { + size_t Size = 0; + ASSERT_ERROR(OL_ERRC_INVALID_ENUMERATION, + olGetDeviceInfoSize(Device, OL_DEVICE_INFO_FORCE_UINT32, &Size)); +} + +TEST_F(olGetDeviceInfoSizeTest, InvalidNullPointer) { + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, + olGetDeviceInfoSize(Device, OL_DEVICE_INFO_TYPE, nullptr)); +} diff --git a/offload/unittests/OffloadAPI/platform/olGetPlatform.cpp b/offload/unittests/OffloadAPI/platform/olGetPlatform.cpp new file mode 100644 index 0000000..4a2f9e8 --- /dev/null +++ b/offload/unittests/OffloadAPI/platform/olGetPlatform.cpp @@ -0,0 +1,28 @@ +//===------- Offload API tests - olGetPlatform -----------------------===// +// +// 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 "../common/Fixtures.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +using olGetPlatformTest = offloadTest; + +TEST_F(olGetPlatformTest, Success) { + uint32_t PlatformCount; + ASSERT_SUCCESS(olGetPlatformCount(&PlatformCount)); + std::vector<ol_platform_handle_t> Platforms(PlatformCount); + ASSERT_SUCCESS(olGetPlatform(PlatformCount, Platforms.data())); +} + +TEST_F(olGetPlatformTest, InvalidNumEntries) { + uint32_t PlatformCount; + ASSERT_SUCCESS(olGetPlatformCount(&PlatformCount)); + std::vector<ol_platform_handle_t> Platforms(PlatformCount); + ASSERT_ERROR(OL_ERRC_INVALID_SIZE, + olGetPlatform(PlatformCount + 1, Platforms.data())); +} diff --git a/offload/unittests/OffloadAPI/platform/olGetPlatformCount.cpp b/offload/unittests/OffloadAPI/platform/olGetPlatformCount.cpp new file mode 100644 index 0000000..15b4b6a --- /dev/null +++ b/offload/unittests/OffloadAPI/platform/olGetPlatformCount.cpp @@ -0,0 +1,22 @@ +//===------- Offload API tests - olGetPlatformCount ------------------===// +// +// 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 "../common/Fixtures.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +using olGetPlatformCountTest = offloadTest; + +TEST_F(olGetPlatformCountTest, Success) { + uint32_t PlatformCount; + ASSERT_SUCCESS(olGetPlatformCount(&PlatformCount)); +} + +TEST_F(olGetPlatformCountTest, InvalidNullPointer) { + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, olGetPlatformCount(nullptr)); +} diff --git a/offload/unittests/OffloadAPI/platform/olGetPlatformInfo.cpp b/offload/unittests/OffloadAPI/platform/olGetPlatformInfo.cpp new file mode 100644 index 0000000..c646bdc --- /dev/null +++ b/offload/unittests/OffloadAPI/platform/olGetPlatformInfo.cpp @@ -0,0 +1,76 @@ +//===------- Offload API tests - olGetPlatformInfo -------------------===// +// +// 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 <OffloadAPI.h> + +#include "../common/Fixtures.hpp" +#include "olPlatformInfo.hpp" + +struct olGetPlatformInfoTest + : offloadPlatformTest, + ::testing::WithParamInterface<ol_platform_info_t> {}; + +INSTANTIATE_TEST_SUITE_P( + olGetPlatformInfo, olGetPlatformInfoTest, + ::testing::ValuesIn(PlatformQueries), + [](const ::testing::TestParamInfo<ol_platform_info_t> &info) { + std::stringstream ss; + ss << info.param; + return ss.str(); + }); + +TEST_P(olGetPlatformInfoTest, Success) { + size_t Size = 0; + ol_platform_info_t InfoType = GetParam(); + + ASSERT_SUCCESS(olGetPlatformInfoSize(Platform, InfoType, &Size)); + std::vector<char> InfoData(Size); + ASSERT_SUCCESS(olGetPlatformInfo(Platform, InfoType, Size, InfoData.data())); + + // Info types with a dynamic size are all char[] so we can verify the returned + // string is the expected size. + auto ExpectedSize = PlatformInfoSizeMap.find(InfoType); + if (ExpectedSize == PlatformInfoSizeMap.end()) { + ASSERT_EQ(Size, strlen(InfoData.data()) + 1); + } +} + +TEST_F(olGetPlatformInfoTest, InvalidNullHandle) { + ol_platform_backend_t Backend; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, + olGetPlatformInfo(nullptr, OL_PLATFORM_INFO_BACKEND, + sizeof(Backend), &Backend)); +} + +TEST_F(olGetPlatformInfoTest, InvalidPlatformInfoEnumeration) { + ol_platform_backend_t Backend; + ASSERT_ERROR(OL_ERRC_INVALID_ENUMERATION, + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_FORCE_UINT32, + sizeof(Backend), &Backend)); +} + +TEST_F(olGetPlatformInfoTest, InvalidSizeZero) { + ol_platform_backend_t Backend; + ASSERT_ERROR( + OL_ERRC_INVALID_SIZE, + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, 0, &Backend)); +} + +TEST_F(olGetPlatformInfoTest, InvalidSizeSmall) { + ol_platform_backend_t Backend; + ASSERT_ERROR(OL_ERRC_INVALID_SIZE, + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, + sizeof(Backend) - 1, &Backend)); +} + +TEST_F(olGetPlatformInfoTest, InvalidNullPointerPropValue) { + ol_platform_backend_t Backend; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, + sizeof(Backend), nullptr)); +} diff --git a/offload/unittests/OffloadAPI/platform/olGetPlatformInfoSize.cpp b/offload/unittests/OffloadAPI/platform/olGetPlatformInfoSize.cpp new file mode 100644 index 0000000..7c92740 --- /dev/null +++ b/offload/unittests/OffloadAPI/platform/olGetPlatformInfoSize.cpp @@ -0,0 +1,57 @@ +//===------- Offload API tests - olGetPlatformInfoSize ---------------===// +// +// 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 <OffloadAPI.h> + +#include "../common/Fixtures.hpp" +#include "olPlatformInfo.hpp" + +struct olGetPlatformInfoSizeTest + : offloadPlatformTest, + ::testing::WithParamInterface<ol_platform_info_t> {}; + +INSTANTIATE_TEST_SUITE_P( + olGetPlatformInfoSize, olGetPlatformInfoSizeTest, + ::testing::ValuesIn(PlatformQueries), + [](const ::testing::TestParamInfo<ol_platform_info_t> &info) { + std::stringstream ss; + ss << info.param; + return ss.str(); + }); + +TEST_P(olGetPlatformInfoSizeTest, Success) { + size_t Size = 0; + ol_platform_info_t InfoType = GetParam(); + + ASSERT_SUCCESS(olGetPlatformInfoSize(Platform, InfoType, &Size)); + auto ExpectedSize = PlatformInfoSizeMap.find(InfoType); + if (ExpectedSize != PlatformInfoSizeMap.end()) { + ASSERT_EQ(Size, ExpectedSize->second); + } else { + ASSERT_NE(Size, 0lu); + } +} + +TEST_F(olGetPlatformInfoSizeTest, InvalidNullHandle) { + size_t Size = 0; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, + olGetPlatformInfoSize(nullptr, OL_PLATFORM_INFO_BACKEND, &Size)); +} + +TEST_F(olGetPlatformInfoSizeTest, InvalidPlatformInfoEnumeration) { + size_t Size = 0; + ASSERT_ERROR( + OL_ERRC_INVALID_ENUMERATION, + olGetPlatformInfoSize(Platform, OL_PLATFORM_INFO_FORCE_UINT32, &Size)); +} + +TEST_F(olGetPlatformInfoSizeTest, InvalidNullPointer) { + ASSERT_ERROR( + OL_ERRC_INVALID_NULL_POINTER, + olGetPlatformInfoSize(Platform, OL_PLATFORM_INFO_BACKEND, nullptr)); +} diff --git a/offload/unittests/OffloadAPI/platform/olPlatformInfo.hpp b/offload/unittests/OffloadAPI/platform/olPlatformInfo.hpp new file mode 100644 index 0000000..d49cdb9 --- /dev/null +++ b/offload/unittests/OffloadAPI/platform/olPlatformInfo.hpp @@ -0,0 +1,20 @@ +//===------- Offload API tests - Helpers for platform info query testing --===// +// +// 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 +// +//===----------------------------------------------------------------------===// +#pragma once + +#include <vector> + +// TODO: We could autogenerate these + +inline std::vector<ol_platform_info_t> PlatformQueries = { + OL_PLATFORM_INFO_NAME, OL_PLATFORM_INFO_VENDOR_NAME, + OL_PLATFORM_INFO_VERSION, OL_PLATFORM_INFO_BACKEND}; + +inline std::unordered_map<ol_platform_info_t, size_t> PlatformInfoSizeMap = { + {OL_PLATFORM_INFO_BACKEND, sizeof(ol_platform_backend_t)}, +}; |
