Skip to content

[Offload] Add check-offload-unit for liboffload unittests #137312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
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
3 changes: 3 additions & 0 deletions offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ void initPlugins() {

// Preemptively initialize all devices in the plugin
for (auto &Platform : Platforms()) {
// Do not use the host plugin - it isn't supported.
if (Platform.BackendType == OL_PLATFORM_BACKEND_UNKNOWN)
continue;
auto Err = Platform.Plugin->init();
[[maybe_unused]] std::string InfoMsg = toString(std::move(Err));
for (auto DevNum = 0; DevNum < Platform.Plugin->number_of_devices();
Expand Down
12 changes: 12 additions & 0 deletions offload/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ add_offload_testsuite(check-offload
EXCLUDE_FROM_CHECK_ALL
DEPENDS llvm-offload-device-info omptarget ${OMP_DEPEND} ${LIBOMPTARGET_TESTED_PLUGINS}
ARGS ${LIBOMPTARGET_LIT_ARG_LIST})

# Add liboffload unit tests - the test binary will run on all available devices
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.site.cfg.in
${CMAKE_CURRENT_BINARY_DIR}/unit/lit.site.cfg
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.cfg.py)

add_lit_testsuite(check-offload-unit "Running offload unittest suites"
${CMAKE_CURRENT_BINARY_DIR}/unit
EXCLUDE_FROM_CHECK_ALL
DEPENDS LLVMOffload OffloadUnitTests)
2 changes: 1 addition & 1 deletion offload/test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ config.name = 'libomptarget :: ' + config.libomptarget_current_target
config.suffixes = ['.c', '.cpp', '.cc', '.f90', '.cu', '.td']

# excludes: A list of directories to exclude from the testuites.
config.excludes = ['Inputs']
config.excludes = ['Inputs', 'unit']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)
Expand Down
22 changes: 22 additions & 0 deletions offload/test/unit/lit.cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- Python -*-

# Configuration file for the 'lit' test runner.

import os
import subprocess

import lit.formats

# name: The name of this test suite.
config.name = "Offload-Unit"

# suffixes: A list of file extensions to treat as test files.
config.suffixes = []

# test_source_root: The root path where tests are located.
# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(config.library_dir, "unittests")
config.test_source_root = config.test_exec_root

# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.GoogleTest(config.llvm_build_mode, ".unittests")
8 changes: 8 additions & 0 deletions offload/test/unit/lit.site.cfg.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@AUTO_GEN_COMMENT@

config.library_dir = "@LIBOMPTARGET_LIBRARY_DIR@"
config.llvm_build_mode = lit_config.substitute("@LLVM_BUILD_MODE@")

# Let the main config do the real work.
lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/unit/lit.cfg.py")

9 changes: 4 additions & 5 deletions offload/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
add_custom_target(LibomptUnitTests)
set_target_properties(LibomptUnitTests PROPERTIES FOLDER "Tests/UnitTests")
add_custom_target(OffloadUnitTests)
set_target_properties(OffloadUnitTests PROPERTIES FOLDER "Tests/UnitTests")

function(add_libompt_unittest test_dirname)
add_unittest(LibomptUnitTests ${test_dirname} ${ARGN})
function(add_offload_unittest test_dirname)
add_unittest(OffloadUnitTests ${test_dirname} ${ARGN})
endfunction()

# add_subdirectory(Plugins)
add_subdirectory(OffloadAPI)
4 changes: 2 additions & 2 deletions offload/unittests/OffloadAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(PLUGINS_TEST_INCLUDE ${LIBOMPTARGET_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
add_subdirectory(device_code)
message(${OFFLOAD_TEST_DEVICE_CODE_PATH})

add_libompt_unittest("offload.unittests"
add_offload_unittest("offload.unittests"
${CMAKE_CURRENT_SOURCE_DIR}/common/Environment.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatformInfo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platform/olGetPlatformInfoSize.cpp
Expand All @@ -22,7 +22,7 @@ add_libompt_unittest("offload.unittests"
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olGetKernel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olLaunchKernel.cpp
)
add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON} LibomptUnitTestsDeviceBins)
add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON} OffloadUnitTestsDeviceBins)
target_compile_definitions("offload.unittests" PRIVATE DEVICE_CODE_PATH="${OFFLOAD_TEST_DEVICE_CODE_PATH}")
target_link_libraries("offload.unittests" PRIVATE ${PLUGINS_TEST_COMMON})
target_include_directories("offload.unittests" PRIVATE ${PLUGINS_TEST_INCLUDE})
69 changes: 47 additions & 22 deletions offload/unittests/OffloadAPI/common/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ raw_ostream &operator<<(raw_ostream &Out,
return Out;
}

raw_ostream &operator<<(raw_ostream &Out, const ol_device_handle_t &Device) {
size_t Size;
olGetDeviceInfoSize(Device, OL_DEVICE_INFO_NAME, &Size);
std::vector<char> Name(Size);
olGetDeviceInfo(Device, OL_DEVICE_INFO_NAME, Size, Name.data());
Out << Name.data();
return Out;
}

void printPlatforms() {
SmallDenseSet<ol_platform_handle_t> Platforms;
using DeviceVecT = SmallVector<ol_device_handle_t, 8>;
Expand All @@ -61,48 +70,64 @@ void printPlatforms() {
}
}

ol_device_handle_t TestEnvironment::getDevice() {
static ol_device_handle_t Device = nullptr;
const std::vector<TestEnvironment::Device> &TestEnvironment::getDevices() {
static std::vector<TestEnvironment::Device> Devices{};
if (Devices.empty()) {
// If a specific platform is requested, filter to devices belonging to it.
if (const char *EnvStr = getenv("OFFLOAD_UNITTEST_PLATFORM")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just be able to loop over all the discovered platforms? Most of this stuff is intended to be generic after all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible, it would require all tests being parameterized on the device, which wouldn't be a massive change.

One reason I added the explicit option was to avoid it auto selecting the host plugin, which is basically unsupported at this point. But I could make sure that the test environment filters it out of the device list, or even just filter it out at the liboffload implementation level since it's not really usable for anything just now.

Another reason was to allow the check-offload-unit tests to be broken down into a test suite config for each available platform (check-offload-unit-cuda etc), in the same way check-offload/check-libomptarget has sub-targets for every configuration. I feel like it's a little bit easier to parse what's actually being tested rather than one test suite that runs on a variable number of configurations.

I don't have strong feelings about it though so I'm happy to make the change if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, forgot about the host plugin and I doubt anyone cares about it. Honestly, the liboffload API probably shouldn't even present it to the user until someone cares enough to update it.

if (SelectedPlatform != "")
errs() << "Warning: --platform argument ignored as "
"OFFLOAD_UNITTEST_PLATFORM env var overrides it.\n";
SelectedPlatform = EnvStr;
}

if (!Device) {
if (SelectedPlatform != "") {
olIterateDevices(
[](ol_device_handle_t D, void *Data) {
ol_platform_handle_t Platform;
olGetDeviceInfo(D, OL_DEVICE_INFO_PLATFORM, sizeof(Platform),
&Platform);

ol_platform_backend_t Backend;
olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND,
sizeof(Backend), &Backend);
std::string PlatformName;
raw_string_ostream S(PlatformName);
S << Platform;

if (PlatformName == SelectedPlatform) {
*(static_cast<ol_device_handle_t *>(Data)) = D;
return false;
if (PlatformName == SelectedPlatform &&
Backend != OL_PLATFORM_BACKEND_HOST) {
std::string Name;
raw_string_ostream NameStr(Name);
NameStr << PlatformName << "_" << D;
static_cast<std::vector<TestEnvironment::Device> *>(Data)
->push_back({D, Name});
}

return true;
},
&Device);

if (Device == nullptr) {
errs() << "No device found with the platform \"" << SelectedPlatform
<< "\". Choose from:"
<< "\n";
printPlatforms();
std::exit(1);
}
&Devices);
} else {
// No platform specified, discover every device that isn't the host.
olIterateDevices(
[](ol_device_handle_t D, void *Data) {
*(static_cast<ol_device_handle_t *>(Data)) = D;
return false;
ol_platform_handle_t Platform;
olGetDeviceInfo(D, OL_DEVICE_INFO_PLATFORM, sizeof(Platform),
&Platform);
ol_platform_backend_t Backend;
olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND,
sizeof(Backend), &Backend);
if (Backend != OL_PLATFORM_BACKEND_HOST) {
std::string Name;
raw_string_ostream NameStr(Name);
NameStr << Platform << "_" << D;
static_cast<std::vector<TestEnvironment::Device> *>(Data)
->push_back({D, Name});
}
return true;
},
&Device);
&Devices);
}
}

return Device;
return Devices;
}

ol_device_handle_t TestEnvironment::getHostDevice() {
Expand Down
8 changes: 7 additions & 1 deletion offload/unittests/OffloadAPI/common/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
#include <gtest/gtest.h>

namespace TestEnvironment {
ol_device_handle_t getDevice();

struct Device {
ol_device_handle_t Handle;
std::string Name;
};

const std::vector<Device> &getDevices();
ol_device_handle_t getHostDevice();
bool loadDeviceBinary(const std::string &BinaryName, ol_device_handle_t Device,
std::unique_ptr<llvm::MemoryBuffer> &BinaryOut);
Expand Down
22 changes: 20 additions & 2 deletions offload/unittests/OffloadAPI/common/Fixtures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,26 @@
} \
(void)0

inline std::string SanitizeString(const std::string &Str) {
auto NewStr = Str;
std::replace_if(
NewStr.begin(), NewStr.end(), [](char C) { return !std::isalnum(C); },
'_');
return NewStr;
}

struct OffloadTest : ::testing::Test {
ol_device_handle_t Host = TestEnvironment::getHostDevice();
};

struct OffloadDeviceTest : OffloadTest {
struct OffloadDeviceTest
: OffloadTest,
::testing::WithParamInterface<TestEnvironment::Device> {
void SetUp() override {
RETURN_ON_FATAL_FAILURE(OffloadTest::SetUp());

Device = TestEnvironment::getDevice();
auto DeviceParam = GetParam();
Device = DeviceParam.Handle;
if (Device == nullptr)
GTEST_SKIP() << "No available devices.";
}
Expand Down Expand Up @@ -120,3 +131,10 @@ struct OffloadQueueTest : OffloadDeviceTest {

ol_queue_handle_t Queue = nullptr;
};

#define OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(FIXTURE) \
INSTANTIATE_TEST_SUITE_P( \
, FIXTURE, ::testing::ValuesIn(TestEnvironment::getDevices()), \
[](const ::testing::TestParamInfo<TestEnvironment::Device> &info) { \
return SanitizeString(info.param.Name); \
})
21 changes: 0 additions & 21 deletions offload/unittests/OffloadAPI/device/olDeviceInfo.hpp

This file was deleted.

Loading
Loading