Skip to content

[Offload] Stop using global ctor/dtors in Offload unit tests #149299

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 15 additions & 4 deletions offload/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,24 @@ function(add_offload_unittest test_dirname)
set(target_name "${test_dirname}.unittests")

list(TRANSFORM ARGN PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/" OUTPUT_VARIABLE files)
list(APPEND files ${CMAKE_CURRENT_SOURCE_DIR}/common/Environment.cpp)

add_unittest(OffloadUnitTests "${target_name}"
${CMAKE_CURRENT_SOURCE_DIR}/common/Environment.cpp
${files})
if( NOT LLVM_BUILD_TESTS )
set(EXCLUDE_FROM_ALL ON)
endif()
add_llvm_executable(${target_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${files})
get_subproject_title(subproject_title)
set_target_properties(${target_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit")

target_link_options(${target_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")

set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
set_output_directory(${target_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
target_link_libraries(${target_name} PRIVATE llvm_gtest ${LLVM_PTHREAD_LIB} ${PLUGINS_TEST_COMMON})

add_dependencies(OffloadUnitTests ${target_name})
add_dependencies(${target_name} ${PLUGINS_TEST_COMMON} offload_device_binaries)
target_compile_definitions(${target_name} PRIVATE DEVICE_CODE_PATH="${OFFLOAD_TEST_DEVICE_CODE_PATH}")
target_link_libraries(${target_name} PRIVATE ${PLUGINS_TEST_COMMON})
target_include_directories(${target_name} PRIVATE ${PLUGINS_TEST_INCLUDE})
endfunction()

Expand Down
2 changes: 1 addition & 1 deletion offload/unittests/OffloadAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_offload_unittest("event"

add_offload_unittest("init"
init/olInit.cpp)
target_compile_definitions("init.unittests" PRIVATE DISABLE_WRAPPER)
target_compile_definitions("init.unittests" PRIVATE DISABLE_OL_INIT)

add_offload_unittest("kernel"
kernel/olLaunchKernel.cpp)
Expand Down
32 changes: 22 additions & 10 deletions offload/unittests/OffloadAPI/common/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include <OffloadAPI.h>
#include <OffloadPrint.hpp>
#include <fstream>

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
#ifndef DISABLE_WRAPPER
struct OffloadInitWrapper {
OffloadInitWrapper() { olInit(); }
~OffloadInitWrapper() { olShutDown(); }
};
static OffloadInitWrapper Wrapper{};
#endif

static cl::opt<std::string>
SelectedPlatform("platform", cl::desc("Only test the specified platform"),
cl::value_desc("platform"));
Expand Down Expand Up @@ -193,3 +184,24 @@ bool TestEnvironment::loadDeviceBinary(
BinaryOut = std::move(SourceFile.get());
return true;
}

int main(int argc, char **argv) {
#ifndef DISABLE_OL_INIT
if (auto Err = olInit()) {
errs() << "Failed to initialize liboffload: " << Err->Code << "\n";
return -1;
}
#endif

::testing::InitGoogleTest(&argc, argv);
int Result = RUN_ALL_TESTS();

#ifndef DISABLE_OL_INIT
if (auto Err = olShutDown()) {
errs() << "Failed to shut down liboffload: " << Err->Code << "\n";
return -1;
}
#endif

return Result;
}
Loading