Skip to content

Commit e3b8ea8

Browse files
committed
Add Offload API unittests
1 parent 9cd2bbe commit e3b8ea8

File tree

10 files changed

+450
-3
lines changed

10 files changed

+450
-3
lines changed

offload/new-api/src/offload_impl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ using namespace llvm::omp::target::plugin;
2525
struct offload_device_handle_t_ {
2626
int DeviceNum;
2727
GenericDeviceTy &Device;
28+
offload_platform_handle_t Platform;
2829
};
2930

3031
struct offload_platform_handle_t_ {
@@ -59,7 +60,7 @@ void initPlugins() {
5960
DevNum++) {
6061
if (Platform.Plugin->init_device(DevNum) == OFFLOAD_SUCCESS) {
6162
Platform.Devices.emplace_back(offload_device_handle_t_{
62-
DevNum, Platform.Plugin->getDevice(DevNum)});
63+
DevNum, Platform.Plugin->getDevice(DevNum), &Platform});
6364
}
6465
}
6566
}
@@ -173,6 +174,8 @@ offload_result_t offloadDeviceGetInfo_impl(offload_device_handle_t hDevice,
173174
};
174175

175176
switch (propName) {
177+
case OFFLOAD_DEVICE_INFO_PLATFORM:
178+
return ReturnValue(hDevice->Platform);
176179
case OFFLOAD_DEVICE_INFO_TYPE:
177180
return ReturnValue(OFFLOAD_DEVICE_TYPE_GPU);
178181
case OFFLOAD_DEVICE_INFO_NAME:
@@ -183,7 +186,7 @@ offload_result_t offloadDeviceGetInfo_impl(offload_device_handle_t hDevice,
183186
return ReturnValue(
184187
GetInfo({"CUDA Driver Version", "HSA Runtime Version"}).c_str());
185188
default:
186-
return OFFLOAD_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
189+
return OFFLOAD_RESULT_ERROR_INVALID_ENUMERATION;
187190
}
188191

189192
return OFFLOAD_RESULT_SUCCESS;

offload/unittests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ function(add_libompt_unittest test_dirname)
55
add_unittest(LibomptUnitTests ${test_dirname} ${ARGN})
66
endfunction()
77

8-
add_subdirectory(Plugins)
8+
# add_subdirectory(Plugins)
9+
add_subdirectory(OffloadAPI)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(PLUGINS_TEST_COMMON offload_new)
2+
set(PLUGINS_TEST_INCLUDE ${LIBOMPTARGET_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/common)
3+
4+
add_libompt_unittest("offload.unittests"
5+
${CMAKE_CURRENT_SOURCE_DIR}/common/environment.cpp
6+
${CMAKE_CURRENT_SOURCE_DIR}/platform/offloadPlatformGet.cpp
7+
${CMAKE_CURRENT_SOURCE_DIR}/platform/offloadPlatformGetInfo.cpp
8+
${CMAKE_CURRENT_SOURCE_DIR}/device/offloadDeviceGet.cpp
9+
${CMAKE_CURRENT_SOURCE_DIR}/device/offloadDeviceGetInfo.cpp)
10+
add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON})
11+
target_link_libraries("offload.unittests" PRIVATE ${PLUGINS_TEST_COMMON})
12+
target_include_directories("offload.unittests" PRIVATE ${PLUGINS_TEST_INCLUDE})
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===------- Offload API tests - gtest environment ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "environment.hpp"
10+
#include "fixtures.hpp"
11+
#include "llvm/Support/CommandLine.h"
12+
#include <offload_api.h>
13+
14+
using namespace llvm;
15+
16+
static cl::opt<std::string>
17+
SelectedPlatform("platform", cl::desc("Only test the specified platform"),
18+
cl::value_desc("platform"));
19+
20+
std::ostream &operator<<(std::ostream &Out,
21+
const offload_platform_handle_t &Platform) {
22+
size_t Size;
23+
offloadPlatformGetInfo(Platform, OFFLOAD_PLATFORM_INFO_NAME, 0, nullptr,
24+
&Size);
25+
std::vector<char> Name(Size);
26+
offloadPlatformGetInfo(Platform, OFFLOAD_PLATFORM_INFO_NAME, Size,
27+
Name.data(), nullptr);
28+
Out << Name.data();
29+
return Out;
30+
}
31+
32+
std::ostream &
33+
operator<<(std::ostream &Out,
34+
const std::vector<offload_platform_handle_t> &Platforms) {
35+
for (auto Platform : Platforms) {
36+
Out << "\n * \"" << Platform << "\"";
37+
}
38+
return Out;
39+
}
40+
41+
const std::vector<offload_platform_handle_t> &TestEnvironment::getPlatforms() {
42+
static std::vector<offload_platform_handle_t> Platforms{};
43+
44+
if (Platforms.empty()) {
45+
uint32_t PlatformCount = 0;
46+
offloadPlatformGet(0, nullptr, &PlatformCount);
47+
if (PlatformCount > 0) {
48+
Platforms.resize(PlatformCount);
49+
offloadPlatformGet(PlatformCount, Platforms.data(), nullptr);
50+
}
51+
}
52+
53+
return Platforms;
54+
}
55+
56+
// Get a single platform, which may be selected by the user.
57+
offload_platform_handle_t TestEnvironment::getPlatform() {
58+
static offload_platform_handle_t Platform = nullptr;
59+
const auto &Platforms = getPlatforms();
60+
61+
if (!Platform) {
62+
if (SelectedPlatform != "") {
63+
for (const auto CandidatePlatform : Platforms) {
64+
std::stringstream PlatformName;
65+
PlatformName << CandidatePlatform;
66+
if (SelectedPlatform == PlatformName.str()) {
67+
Platform = CandidatePlatform;
68+
return Platform;
69+
}
70+
}
71+
std::cout << "No platform found with the name \"" << SelectedPlatform
72+
<< "\". Choose from:" << Platforms << "\n";
73+
std::exit(1);
74+
} else {
75+
// Pick a single platform. We prefer one that has available devices, but
76+
// just pick the first initially in case none have any devices.
77+
Platform = Platforms[0];
78+
for (auto CandidatePlatform : Platforms) {
79+
uint32_t NumDevices = 0;
80+
if (offloadDeviceGet(CandidatePlatform, OFFLOAD_DEVICE_TYPE_ALL, 0,
81+
nullptr, &NumDevices) == OFFLOAD_RESULT_SUCCESS) {
82+
if (NumDevices > 0) {
83+
Platform = CandidatePlatform;
84+
break;
85+
}
86+
}
87+
}
88+
}
89+
}
90+
91+
return Platform;
92+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===------- Offload API tests - gtest environment ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <gtest/gtest.h>
12+
#include <offload_api.h>
13+
14+
namespace TestEnvironment {
15+
const std::vector<offload_platform_handle_t> &getPlatforms();
16+
offload_platform_handle_t getPlatform();
17+
} // namespace TestEnvironment
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===------- Offload API tests - gtest fixtures --==-----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <gtest/gtest.h>
10+
#include <offload_api.h>
11+
#include <offload_print.hpp>
12+
13+
#include "environment.hpp"
14+
15+
#pragma once
16+
17+
#ifndef ASSERT_SUCCESS
18+
#define ASSERT_SUCCESS(ACTUAL) ASSERT_EQ(OFFLOAD_RESULT_SUCCESS, ACTUAL)
19+
#endif
20+
21+
#define RETURN_ON_FATAL_FAILURE(...) \
22+
__VA_ARGS__; \
23+
if (this->HasFatalFailure() || this->IsSkipped()) { \
24+
return; \
25+
} \
26+
(void)0
27+
28+
struct offloadPlatformTest : ::testing::Test {
29+
void SetUp() override {
30+
RETURN_ON_FATAL_FAILURE(::testing::Test::SetUp());
31+
32+
Platform = TestEnvironment::getPlatform();
33+
ASSERT_NE(Platform, nullptr);
34+
}
35+
36+
offload_platform_handle_t Platform;
37+
};
38+
39+
struct offloadDeviceTest : offloadPlatformTest {
40+
void SetUp() override {
41+
RETURN_ON_FATAL_FAILURE(offloadPlatformTest::SetUp());
42+
43+
uint32_t NumDevices;
44+
ASSERT_SUCCESS(offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, 0,
45+
nullptr, &NumDevices));
46+
if (NumDevices == 0) {
47+
GTEST_SKIP() << "No available devices on this platform.";
48+
}
49+
ASSERT_SUCCESS(offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, 1,
50+
&Device, nullptr));
51+
}
52+
53+
offload_device_handle_t Device;
54+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===------- Offload API tests - offloadDeviceGet -------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../common/fixtures.hpp"
10+
#include <gtest/gtest.h>
11+
#include <offload_api.h>
12+
13+
using offloadDeviceGetTest = offloadPlatformTest;
14+
15+
TEST_F(offloadDeviceGetTest, Success) {
16+
uint32_t Count = 0;
17+
ASSERT_SUCCESS(
18+
offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, 0, nullptr, &Count));
19+
ASSERT_NE(Count, 0lu);
20+
std::vector<offload_device_handle_t> Devices(Count);
21+
ASSERT_SUCCESS(offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, Count,
22+
Devices.data(), nullptr));
23+
for (auto Device : Devices) {
24+
ASSERT_NE(nullptr, Device);
25+
}
26+
}
27+
28+
TEST_F(offloadDeviceGetTest, SuccessSubsetOfDevices) {
29+
uint32_t Count;
30+
ASSERT_SUCCESS(
31+
offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, 0, nullptr, &Count));
32+
if (Count < 2) {
33+
GTEST_SKIP() << "Only one device is available on this platform.";
34+
}
35+
std::vector<offload_device_handle_t> Devices(Count - 1);
36+
ASSERT_SUCCESS(offloadDeviceGet(Platform, OFFLOAD_DEVICE_TYPE_ALL, Count - 1,
37+
Devices.data(), nullptr));
38+
for (auto Device : Devices) {
39+
ASSERT_NE(nullptr, Device);
40+
}
41+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===------- Offload API tests - offloadDeviceGetInfo ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../common/fixtures.hpp"
10+
#include <gtest/gtest.h>
11+
#include <offload_api.h>
12+
13+
struct offloadDeviceGetInfoTest
14+
: offloadDeviceTest,
15+
::testing::WithParamInterface<offload_device_info_t> {
16+
17+
void SetUp() override { RETURN_ON_FATAL_FAILURE(offloadDeviceTest::SetUp()); }
18+
};
19+
20+
// TODO: We could autogenerate the list of enum values
21+
INSTANTIATE_TEST_SUITE_P(
22+
, offloadDeviceGetInfoTest,
23+
::testing::Values(OFFLOAD_DEVICE_INFO_TYPE, OFFLOAD_DEVICE_INFO_PLATFORM,
24+
OFFLOAD_DEVICE_INFO_NAME, OFFLOAD_DEVICE_INFO_VENDOR,
25+
OFFLOAD_DEVICE_INFO_DRIVER_VERSION),
26+
[](const ::testing::TestParamInfo<offload_device_info_t> &info) {
27+
std::stringstream ss;
28+
ss << info.param;
29+
return ss.str();
30+
});
31+
32+
// TODO: We could autogenerate this
33+
std::unordered_map<offload_device_info_t, size_t> DeviceInfoSizeMap = {
34+
{OFFLOAD_DEVICE_INFO_TYPE, sizeof(offload_device_type_t)},
35+
{OFFLOAD_DEVICE_INFO_PLATFORM, sizeof(offload_platform_handle_t)},
36+
};
37+
38+
TEST_P(offloadDeviceGetInfoTest, Success) {
39+
offload_device_info_t InfoType = GetParam();
40+
size_t Size = 0;
41+
42+
ASSERT_SUCCESS(offloadDeviceGetInfo(Device, InfoType, 0, nullptr, &Size));
43+
auto ExpectedSize = DeviceInfoSizeMap.find(InfoType);
44+
if (ExpectedSize != DeviceInfoSizeMap.end()) {
45+
ASSERT_EQ(Size, ExpectedSize->second);
46+
} else {
47+
ASSERT_NE(Size, 0lu);
48+
}
49+
50+
std::vector<char> InfoData(Size);
51+
ASSERT_SUCCESS(
52+
offloadDeviceGetInfo(Device, InfoType, Size, InfoData.data(), nullptr));
53+
54+
if (InfoType == OFFLOAD_DEVICE_INFO_PLATFORM) {
55+
auto *ReturnedPlatform =
56+
reinterpret_cast<offload_platform_handle_t *>(InfoData.data());
57+
ASSERT_EQ(Platform, *ReturnedPlatform);
58+
}
59+
}
60+
61+
TEST_F(offloadDeviceGetInfoTest, InvalidNullHandleDevice) {
62+
offload_device_type_t DeviceType;
63+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_NULL_HANDLE,
64+
offloadDeviceGetInfo(nullptr, OFFLOAD_DEVICE_INFO_TYPE,
65+
sizeof(offload_device_type_t), &DeviceType,
66+
nullptr));
67+
}
68+
69+
TEST_F(offloadDeviceGetInfoTest, InvalidEnumerationInfoType) {
70+
offload_device_type_t DeviceType;
71+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_ENUMERATION,
72+
offloadDeviceGetInfo(Device, OFFLOAD_DEVICE_INFO_FORCE_UINT32,
73+
sizeof(offload_device_type_t), &DeviceType,
74+
nullptr));
75+
}
76+
77+
TEST_F(offloadDeviceGetInfoTest, InvalidSizePropSize) {
78+
offload_device_type_t DeviceType;
79+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_SIZE,
80+
offloadDeviceGetInfo(Device, OFFLOAD_DEVICE_INFO_TYPE, 0,
81+
&DeviceType, nullptr));
82+
}
83+
84+
TEST_F(offloadDeviceGetInfoTest, InvalidSizePropSizeSmall) {
85+
offload_device_type_t DeviceType;
86+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_SIZE,
87+
offloadDeviceGetInfo(Device, OFFLOAD_DEVICE_INFO_TYPE,
88+
sizeof(DeviceType) - 1, &DeviceType, nullptr));
89+
}
90+
91+
TEST_F(offloadDeviceGetInfoTest, InvalidNullPointerPropValue) {
92+
offload_device_type_t DeviceType;
93+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_NULL_POINTER,
94+
offloadDeviceGetInfo(Device, OFFLOAD_DEVICE_INFO_TYPE,
95+
sizeof(DeviceType), nullptr, nullptr));
96+
}
97+
98+
TEST_F(offloadDeviceGetInfoTest, InvalidNullPointerPropSizeRet) {
99+
ASSERT_EQ(OFFLOAD_RESULT_ERROR_INVALID_NULL_POINTER,
100+
offloadDeviceGetInfo(Device, OFFLOAD_DEVICE_INFO_TYPE, 0, nullptr,
101+
nullptr));
102+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===------- Offload API tests - offloadPlatformGet -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <gtest/gtest.h>
10+
#include <offload_api.h>
11+
#include "../common/fixtures.hpp"
12+
13+
using offloadPlatformGetTest = ::testing::Test;
14+
15+
TEST_F(offloadPlatformGetTest, Success) {
16+
uint32_t PlatformCount;
17+
ASSERT_SUCCESS(offloadPlatformGet(0, nullptr, &PlatformCount));
18+
std::vector<offload_platform_handle_t> Platforms(PlatformCount);
19+
ASSERT_SUCCESS(offloadPlatformGet(PlatformCount, Platforms.data(), nullptr));
20+
}
21+
22+
TEST_F(offloadPlatformGetTest, InvalidNumEntries) {
23+
uint32_t PlatformCount;
24+
ASSERT_SUCCESS(offloadPlatformGet(0, nullptr, &PlatformCount));
25+
std::vector<offload_platform_handle_t> Platforms(PlatformCount);
26+
ASSERT_EQ(offloadPlatformGet(0, Platforms.data(), nullptr),
27+
OFFLOAD_RESULT_ERROR_INVALID_SIZE);
28+
}

0 commit comments

Comments
 (0)