Skip to content

[SYCL] Introduce a fully-mocked PI plugin for unit tests #6684

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
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
34 changes: 21 additions & 13 deletions sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ void GlobalHandler::registerDefaultContextReleaseHandler() {
static DefaultContextReleaseHandler handler{};
}

// Note: Split from shutdown so it is available to the unittests for ensuring
// that the mock plugin is the lone plugin.
void GlobalHandler::unloadPlugins() {
// Call to GlobalHandler::instance().getPlugins() initializes plugins. If
// user application has loaded SYCL runtime, and never called any APIs,
// there's no need to load and unload plugins.
if (GlobalHandler::instance().MPlugins.Inst) {
for (plugin &Plugin : GlobalHandler::instance().getPlugins()) {
// PluginParameter is reserved for future use that can control
// some parameters in the plugin tear-down process.
// Currently, it is not used.
void *PluginParameter = nullptr;
Plugin.call<PiApiKind::piTearDown>(PluginParameter);
Plugin.unload();
}
}
}

void shutdown() {
// Ensure neither host task is working so that no default context is accessed
// upon its release
Expand All @@ -134,20 +152,10 @@ void shutdown() {
GlobalHandler::instance().MScheduler.Inst.reset(nullptr);
GlobalHandler::instance().MProgramManager.Inst.reset(nullptr);

// Call to GlobalHandler::instance().getPlugins() initializes plugins. If
// user application has loaded SYCL runtime, and never called any APIs,
// there's no need to load and unload plugins.
if (GlobalHandler::instance().MPlugins.Inst) {
for (plugin &Plugin : GlobalHandler::instance().getPlugins()) {
// PluginParameter is reserved for future use that can control
// some parameters in the plugin tear-down process.
// Currently, it is not used.
void *PluginParameter = nullptr;
Plugin.call<PiApiKind::piTearDown>(PluginParameter);
Plugin.unload();
}
// Clear the plugins and reset the instance if it was there.
GlobalHandler::instance().unloadPlugins();
if (GlobalHandler::instance().MPlugins.Inst)
GlobalHandler::instance().MPlugins.Inst.reset(nullptr);
}

// Release the rest of global resources.
delete &GlobalHandler::instance();
Expand Down
2 changes: 2 additions & 0 deletions sycl/source/detail/global_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class GlobalHandler {

static void registerDefaultContextReleaseHandler();

void unloadPlugins();

private:
friend void releaseDefaultContexts();
friend void shutdown();
Expand Down
8 changes: 7 additions & 1 deletion sycl/source/detail/posix_pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ void *loadOsLibrary(const std::string &PluginPath) {
return so;
}

int unloadOsLibrary(void *Library) { return dlclose(Library); }
int unloadOsLibrary(void *Library) {
// The mock plugin does not have an associated library, so we allow nullptr
// here to avoid it trying to free a non-existent library.
if (!Library)
return 0;
return dlclose(Library);
}

void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) {
return dlsym(Library, FunctionName.c_str());
Expand Down
4 changes: 4 additions & 0 deletions sycl/source/detail/windows_pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ void *loadOsLibrary(const std::string &PluginPath) {
}

int unloadOsLibrary(void *Library) {
// The mock plugin does not have an associated library, so we allow nullptr
// here to avoid it trying to free a non-existent library.
if (!Library)
return 1;
return (int)FreeLibrary((HMODULE)Library);
}

Expand Down
8 changes: 6 additions & 2 deletions sycl/test/Unit/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ def find_shlibpath_var():
lit_config.warning("unable to inject shared library path on '{}'"
.format(platform.system()))

# The mock plugin currently appears as an opencl plugin, but could be changed in
# the future. To avoid it being filtered out we set the filter to use the *
# wildcard.
config.environment['SYCL_DEVICE_FILTER'] = "*"
lit_config.note("Using Mock Plugin.")

config.environment['SYCL_CACHE_DIR'] = config.llvm_obj_root + "/sycl_cache"
config.environment['SYCL_DEVICE_FILTER'] = lit_config.params.get('SYCL_PLUGIN', "opencl") + ",host"
lit_config.note("Backend: {}".format(config.environment['SYCL_DEVICE_FILTER']))
lit_config.note("SYCL cache directory: {}".format(config.environment['SYCL_CACHE_DIR']))
1 change: 1 addition & 0 deletions sycl/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ add_subdirectory(Extensions)
add_subdirectory(windows)
add_subdirectory(event)
add_subdirectory(buffer)
add_subdirectory(context)
24 changes: 6 additions & 18 deletions sycl/unittests/Extensions/DefaultContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <sycl/sycl.hpp>

#include <detail/config.hpp>
#include <helpers/CommonRedefinitions.hpp>
#include <helpers/PiMock.hpp>
#include <helpers/ScopedEnvVar.hpp>

Expand All @@ -25,17 +24,11 @@ TEST(DefaultContextTest, DefaultContextTest) {
ScopedEnvVar var(EnableDefaultContextsName, "1",
SYCLConfig<SYCL_ENABLE_DEFAULT_CONTEXTS>::reset);

sycl::platform Plt1{sycl::default_selector()};
if (Plt1.is_host()) {
std::cout << "Host platform does not support PI mock.\n";
return;
}
sycl::unittest::PiMock Mock1{Plt1};
setupDefaultMockAPIs(Mock1);
sycl::unittest::PiMock Mock1;
sycl::platform Plt1 = Mock1.getPlatform();

sycl::platform Plt2{sycl::default_selector()};
sycl::unittest::PiMock Mock2{Plt2};
setupDefaultMockAPIs(Mock2);
sycl::unittest::PiMock Mock2;
sycl::platform Plt2 = Mock2.getPlatform();

const sycl::device Dev1 = Plt1.get_devices()[0];
const sycl::device Dev2 = Plt2.get_devices()[0];
Expand All @@ -55,13 +48,8 @@ TEST(DefaultContextTest, DefaultContextCanBeDisabled) {
ScopedEnvVar var(EnableDefaultContextsName, "0",
SYCLConfig<SYCL_ENABLE_DEFAULT_CONTEXTS>::reset);

sycl::platform Plt{sycl::default_selector()};
if (Plt.is_host()) {
std::cout << "Host platform does not support PI mock.\n";
return;
}
sycl::unittest::PiMock Mock{Plt};
setupDefaultMockAPIs(Mock);
sycl::unittest::PiMock Mock;
sycl::platform Plt = Mock.getPlatform();

bool catchException = false;
try {
Expand Down
20 changes: 3 additions & 17 deletions sycl/unittests/SYCL2020/GetNativeOpenCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <sycl/backend/opencl.hpp>
#include <sycl/sycl.hpp>

#include <helpers/CommonRedefinitions.hpp>
#include <helpers/PiMock.hpp>
#include <helpers/TestKernel.hpp>

Expand Down Expand Up @@ -84,20 +83,8 @@ static pi_result redefinedUSMEnqueueMemset(pi_queue, void *, pi_int32, size_t,
}

TEST(GetNative, GetNativeHandle) {
platform Plt{default_selector()};
if (Plt.get_backend() != backend::opencl) {
std::cout << "Test is created for opencl only" << std::endl;
return;
}
if (Plt.is_host()) {
std::cout << "Not run on host - no PI events created in that case"
<< std::endl;
return;
}
TestCounter = 0;

unittest::PiMock Mock{Plt};
setupDefaultMockAPIs(Mock);
sycl::unittest::PiMock Mock;
sycl::platform Plt = Mock.getPlatform();

Mock.redefine<detail::PiApiKind::piEventGetInfo>(redefinedEventGetInfo);
Mock.redefine<detail::PiApiKind::piContextRetain>(redefinedContextRetain);
Expand All @@ -111,9 +98,8 @@ TEST(GetNative, GetNativeHandle) {
Mock.redefine<detail::PiApiKind::piextUSMEnqueueMemset>(
redefinedUSMEnqueueMemset);

default_selector Selector;
context Context(Plt);
queue Queue(Context, Selector);
queue Queue(Context, default_selector_v);

auto Device = Queue.get_device();

Expand Down
Loading