Skip to content

[SYCL] Add Unified Runtime plugin and route to it with SYCL_PREFER_UR #9232

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 21 commits into from
May 15, 2023
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
1 change: 1 addition & 0 deletions sycl/doc/EnvironmentVariables.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ variables in production code.</span>

| Environment variable | Values | Description |
| -------------------- | ------ | ----------- |
| `SYCL_PREFER_UR` | Integer | If non-0 then run through Unified Runtime if desired backend is supported there. Default is 0. |
| `SYCL_PI_TRACE` | Described [below](#sycl_pi_trace-options) | Enable specified level of tracing for PI. |
| `SYCL_QUEUE_THREAD_POOL_SIZE` | Positive integer | Number of threads in thread pool of queue. |
| `SYCL_DEVICELIB_NO_FALLBACK` | Any(\*) | Disable loading and linking of device library images |
Expand Down
9 changes: 7 additions & 2 deletions sycl/include/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum class PiApiKind {
#include <sycl/detail/pi.def>
};
class plugin;
using PluginPtr = std::shared_ptr<plugin>;

template <sycl::backend BE>
__SYCL_EXPORT void *getPluginOpaqueData(void *opaquedata_arg);
Expand All @@ -70,25 +71,29 @@ bool trace(TraceLevel level);
#define __SYCL_CUDA_PLUGIN_NAME "pi_cuda.dll"
#define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "pi_esimd_emulator.dll"
#define __SYCL_HIP_PLUGIN_NAME "libpi_hip.dll"
#define __SYCL_UR_PLUGIN_NAME "pi_unified_runtime.dll"
#else
#define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.dll"
#define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.dll"
#define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.dll"
#define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.dll"
#define __SYCL_HIP_PLUGIN_NAME "libpi_hip.dll"
#define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.dll"
#endif
#elif defined(__SYCL_RT_OS_LINUX)
#define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.so"
#define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.so"
#define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.so"
#define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.so"
#define __SYCL_HIP_PLUGIN_NAME "libpi_hip.so"
#define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.so"
#elif defined(__SYCL_RT_OS_DARWIN)
#define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.dylib"
#define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.dylib"
#define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.dylib"
#define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.dylib"
#define __SYCL_HIP_PLUGIN_NAME "libpi_hip.dylib"
#define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.dylib"
#else
#error "Unsupported OS"
#endif
Expand Down Expand Up @@ -176,10 +181,10 @@ template <class To, class From> To cast(From value);
extern std::shared_ptr<plugin> GlobalPlugin;

// Performs PI one-time initialization.
std::vector<plugin> &initialize();
std::vector<PluginPtr> &initialize();

// Get the plugin serving given backend.
template <backend BE> __SYCL_EXPORT const plugin &getPlugin();
template <backend BE> __SYCL_EXPORT const PluginPtr &getPlugin();

// Utility Functions to get Function Name for a PI Api.
template <PiApiKind PiApiOffset> struct PiFuncInfo {};
Expand Down
16 changes: 16 additions & 0 deletions sycl/plugins/unified_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,20 @@ set_target_properties("ur_adapter_level_zero" PROPERTIES

if (TARGET UnifiedRuntimeLoader)
set_target_properties(hello_world PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
# Install the UR loader.
# TODO: this is piggy-backing on the existing target component level-zero-sycl-dev
# When UR is moved to its separate repo perhaps we should introduce new component,
# e.g. unified-runtime-sycl-dev.
install(TARGETS loader
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT level-zero-sycl-dev
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT level-zero-sycl-dev
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT level-zero-sycl-dev
)
endif()

# Install the UR adapters too
install(TARGETS ur_adapter_level_zero
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT level-zero-sycl-dev
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT level-zero-sycl-dev
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT level-zero-sycl-dev
)
44 changes: 22 additions & 22 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
namespace detail {

static const plugin &getPlugin(backend Backend) {
static const PluginPtr &getPlugin(backend Backend) {
switch (Backend) {
case backend::opencl:
return pi::getPlugin<backend::opencl>();
Expand Down Expand Up @@ -67,8 +67,8 @@ platform make_platform(pi_native_handle NativeHandle, backend Backend) {

// Create PI platform first.
pi::PiPlatform PiPlatform = nullptr;
Plugin.call<PiApiKind::piextPlatformCreateWithNativeHandle>(NativeHandle,
&PiPlatform);
Plugin->call<PiApiKind::piextPlatformCreateWithNativeHandle>(NativeHandle,
&PiPlatform);

return detail::createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(PiPlatform, Plugin));
Expand All @@ -79,8 +79,8 @@ __SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
const auto &Plugin = getPlugin(Backend);

pi::PiDevice PiDevice = nullptr;
Plugin.call<PiApiKind::piextDeviceCreateWithNativeHandle>(NativeHandle,
nullptr, &PiDevice);
Plugin->call<PiApiKind::piextDeviceCreateWithNativeHandle>(
NativeHandle, nullptr, &PiDevice);
// Construct the SYCL device from PI device.
return detail::createSyclObjFromImpl<device>(
std::make_shared<device_impl>(PiDevice, Plugin));
Expand All @@ -92,7 +92,7 @@ __SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
const auto &Plugin = getPlugin(Backend);

pi::PiContext PiContext = nullptr;
Plugin.call<PiApiKind::piextContextCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextContextCreateWithNativeHandle>(
NativeHandle, 0, nullptr, false, &PiContext);
// Construct the SYCL context from PI context.
return detail::createSyclObjFromImpl<context>(
Expand All @@ -106,7 +106,7 @@ queue make_queue_impl(pi_native_handle NativeHandle, const context &Context,
const auto &ContextImpl = getSyclObjImpl(Context);
// Create PI queue first.
pi::PiQueue PiQueue = nullptr;
Plugin.call<PiApiKind::piextQueueCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), Device, !KeepOwnership,
&PiQueue);
// Construct the SYCL queue from PI queue.
Expand Down Expand Up @@ -155,7 +155,7 @@ __SYCL_EXPORT queue make_queue2(pi_native_handle NativeHandle,

// Create PI queue first.
pi::PiQueue PiQueue = nullptr;
Plugin.call<PiApiKind::piextQueueCreateWithNativeHandle2>(
Plugin->call<PiApiKind::piextQueueCreateWithNativeHandle2>(
NativeHandle, NativeHandleDesc, ContextImpl->getHandleRef(), PiDevice,
!KeepOwnership, Properties, &PiQueue);
// Construct the SYCL queue from PI queue.
Expand All @@ -175,14 +175,14 @@ __SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
const auto &ContextImpl = getSyclObjImpl(Context);

pi::PiEvent PiEvent = nullptr;
Plugin.call<PiApiKind::piextEventCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextEventCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), !KeepOwnership, &PiEvent);

event Event = detail::createSyclObjFromImpl<event>(
std::make_shared<event_impl>(PiEvent, Context));

if (Backend == backend::opencl)
Plugin.call<PiApiKind::piEventRetain>(PiEvent);
Plugin->call<PiApiKind::piEventRetain>(PiEvent);
return Event;
}

Expand All @@ -193,34 +193,34 @@ make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
const auto &ContextImpl = getSyclObjImpl(TargetContext);

pi::PiProgram PiProgram = nullptr;
Plugin.call<PiApiKind::piextProgramCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextProgramCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), !KeepOwnership, &PiProgram);
if (ContextImpl->getBackend() == backend::opencl)
Plugin.call<PiApiKind::piProgramRetain>(PiProgram);
Plugin->call<PiApiKind::piProgramRetain>(PiProgram);

std::vector<pi::PiDevice> ProgramDevices;
size_t NumDevices = 0;

Plugin.call<PiApiKind::piProgramGetInfo>(
Plugin->call<PiApiKind::piProgramGetInfo>(
PiProgram, PI_PROGRAM_INFO_NUM_DEVICES, sizeof(size_t), &NumDevices,
nullptr);
ProgramDevices.resize(NumDevices);
Plugin.call<PiApiKind::piProgramGetInfo>(PiProgram, PI_PROGRAM_INFO_DEVICES,
sizeof(pi::PiDevice) * NumDevices,
ProgramDevices.data(), nullptr);
Plugin->call<PiApiKind::piProgramGetInfo>(PiProgram, PI_PROGRAM_INFO_DEVICES,
sizeof(pi::PiDevice) * NumDevices,
ProgramDevices.data(), nullptr);

for (const auto &Dev : ProgramDevices) {
size_t BinaryType = 0;
Plugin.call<PiApiKind::piProgramGetBuildInfo>(
Plugin->call<PiApiKind::piProgramGetBuildInfo>(
PiProgram, Dev, PI_PROGRAM_BUILD_INFO_BINARY_TYPE, sizeof(size_t),
&BinaryType, nullptr);
switch (BinaryType) {
case (PI_PROGRAM_BINARY_TYPE_NONE):
if (State == bundle_state::object)
Plugin.call<errc::build, PiApiKind::piProgramCompile>(
Plugin->call<errc::build, PiApiKind::piProgramCompile>(
PiProgram, 1, &Dev, nullptr, 0, nullptr, nullptr, nullptr, nullptr);
else if (State == bundle_state::executable)
Plugin.call<errc::build, PiApiKind::piProgramBuild>(
Plugin->call<errc::build, PiApiKind::piProgramBuild>(
PiProgram, 1, &Dev, nullptr, nullptr, nullptr);
break;
case (PI_PROGRAM_BINARY_TYPE_COMPILED_OBJECT):
Expand All @@ -231,7 +231,7 @@ make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
"Program and kernel_bundle state mismatch",
PI_ERROR_INVALID_VALUE);
if (State == bundle_state::executable)
Plugin.call<errc::build, PiApiKind::piProgramLink>(
Plugin->call<errc::build, PiApiKind::piProgramLink>(
ContextImpl->getHandleRef(), 1, &Dev, nullptr, 1, &PiProgram,
nullptr, nullptr, &PiProgram);
break;
Expand Down Expand Up @@ -306,12 +306,12 @@ kernel make_kernel(const context &TargetContext,

// Create PI kernel first.
pi::PiKernel PiKernel = nullptr;
Plugin.call<PiApiKind::piextKernelCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextKernelCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), PiProgram, !KeepOwnership,
&PiKernel);

if (Backend == backend::opencl)
Plugin.call<PiApiKind::piKernelRetain>(PiKernel);
Plugin->call<PiApiKind::piKernelRetain>(PiKernel);

// Construct the SYCL queue from PI queue.
return detail::createSyclObjFromImpl<kernel>(
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/backend/level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ __SYCL_EXPORT device make_device(const platform &Platform,
const auto &PlatformImpl = getSyclObjImpl(Platform);
// Create PI device first.
pi::PiDevice PiDevice;
Plugin.call<PiApiKind::piextDeviceCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextDeviceCreateWithNativeHandle>(
NativeHandle, PlatformImpl->getHandleRef(), &PiDevice);

return detail::createSyclObjFromImpl<device>(
Expand All @@ -51,7 +51,7 @@ __SYCL_EXPORT context make_context(const std::vector<device> &DeviceList,
for (auto Dev : DeviceList) {
DeviceHandles.push_back(detail::getSyclObjImpl(Dev)->getHandleRef());
}
Plugin.call<PiApiKind::piextContextCreateWithNativeHandle>(
Plugin->call<PiApiKind::piextContextCreateWithNativeHandle>(
NativeHandle, DeviceHandles.size(), DeviceHandles.data(), !KeepOwnership,
&PiContext);
// Construct the SYCL context from PI context.
Expand Down
20 changes: 10 additions & 10 deletions sycl/source/backend/opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ __SYCL_EXPORT bool has_extension(const sycl::platform &SyclPlatform,
std::shared_ptr<sycl::detail::platform_impl> PlatformImpl =
getSyclObjImpl(SyclPlatform);
detail::RT::PiPlatform PluginPlatform = PlatformImpl->getHandleRef();
const plugin &Plugin = PlatformImpl->getPlugin();
const PluginPtr &Plugin = PlatformImpl->getPlugin();

// Manual invocation of plugin API to avoid using deprecated
// info::platform::extensions call.
size_t ResultSize = 0;
Plugin.call<PiApiKind::piPlatformGetInfo>(
Plugin->call<PiApiKind::piPlatformGetInfo>(
PluginPlatform, PI_PLATFORM_INFO_EXTENSIONS, /*param_value_size=*/0,
/*param_value_size=*/nullptr, &ResultSize);
if (ResultSize == 0)
return false;

std::unique_ptr<char[]> Result(new char[ResultSize]);
Plugin.call<PiApiKind::piPlatformGetInfo>(PluginPlatform,
PI_PLATFORM_INFO_EXTENSIONS,
ResultSize, Result.get(), nullptr);
Plugin->call<PiApiKind::piPlatformGetInfo>(PluginPlatform,
PI_PLATFORM_INFO_EXTENSIONS,
ResultSize, Result.get(), nullptr);

std::string_view ExtensionsString(Result.get());
return ExtensionsString.find(Extension) != std::string::npos;
Expand All @@ -93,21 +93,21 @@ __SYCL_EXPORT bool has_extension(const sycl::device &SyclDevice,
std::shared_ptr<sycl::detail::device_impl> DeviceImpl =
getSyclObjImpl(SyclDevice);
detail::RT::PiDevice PluginDevice = DeviceImpl->getHandleRef();
const plugin &Plugin = DeviceImpl->getPlugin();
const PluginPtr &Plugin = DeviceImpl->getPlugin();

// Manual invocation of plugin API to avoid using deprecated
// info::device::extensions call.
size_t ResultSize = 0;
Plugin.call<PiApiKind::piDeviceGetInfo>(
Plugin->call<PiApiKind::piDeviceGetInfo>(
PluginDevice, PI_DEVICE_INFO_EXTENSIONS, /*param_value_size=*/0,
/*param_value_size=*/nullptr, &ResultSize);
if (ResultSize == 0)
return false;

std::unique_ptr<char[]> Result(new char[ResultSize]);
Plugin.call<PiApiKind::piDeviceGetInfo>(PluginDevice,
PI_DEVICE_INFO_EXTENSIONS, ResultSize,
Result.get(), nullptr);
Plugin->call<PiApiKind::piDeviceGetInfo>(PluginDevice,
PI_DEVICE_INFO_EXTENSIONS,
ResultSize, Result.get(), nullptr);

std::string_view ExtensionsString(Result.get());
return ExtensionsString.find(Extension) != std::string::npos;
Expand Down
8 changes: 4 additions & 4 deletions sycl/source/detail/allowlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ bool deviceIsAllowed(const DeviceDescT &DeviceDesc,
}

void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
RT::PiPlatform PiPlatform, const plugin &Plugin) {
RT::PiPlatform PiPlatform, const PluginPtr &Plugin) {

AllowListParsedT AllowListParsed =
parseAllowList(SYCLConfig<SYCL_DEVICE_ALLOWLIST>::get());
Expand Down Expand Up @@ -367,9 +367,9 @@ void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
auto DeviceImpl = PlatformImpl->getOrMakeDeviceImpl(Device, PlatformImpl);
// get DeviceType value and put it to DeviceDesc
RT::PiDeviceType PiDevType;
Plugin.call<PiApiKind::piDeviceGetInfo>(Device, PI_DEVICE_INFO_TYPE,
sizeof(RT::PiDeviceType),
&PiDevType, nullptr);
Plugin->call<PiApiKind::piDeviceGetInfo>(Device, PI_DEVICE_INFO_TYPE,
sizeof(RT::PiDeviceType),
&PiDevType, nullptr);
sycl::info::device_type DeviceType = pi::cast<info::device_type>(PiDevType);
for (const auto &SyclDeviceType : getSyclDeviceTypeMap()) {
if (SyclDeviceType.second == DeviceType) {
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/allowlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool deviceIsAllowed(const DeviceDescT &DeviceDesc,
const AllowListParsedT &AllowListParsed);

void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
RT::PiPlatform PiPlatform, const plugin &Plugin);
RT::PiPlatform PiPlatform, const PluginPtr &Plugin);

} // namespace detail
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
Expand Down
8 changes: 4 additions & 4 deletions sycl/source/detail/buffer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void buffer_impl::addInteropObject(
if (std::find(Handles.begin(), Handles.end(),
pi::cast<pi_native_handle>(MInteropMemObject)) ==
Handles.end()) {
const plugin &Plugin = getPlugin();
Plugin.call<PiApiKind::piMemRetain>(
const PluginPtr &Plugin = getPlugin();
Plugin->call<PiApiKind::piMemRetain>(
pi::cast<RT::PiMem>(MInteropMemObject));
Handles.push_back(pi::cast<pi_native_handle>(MInteropMemObject));
}
Expand Down Expand Up @@ -79,11 +79,11 @@ buffer_impl::getNativeVector(backend BackendName) const {
if (Platform->getBackend() != BackendName)
continue;
if (Platform->getBackend() == backend::opencl) {
Plugin.call<PiApiKind::piMemRetain>(NativeMem);
Plugin->call<PiApiKind::piMemRetain>(NativeMem);
}

pi_native_handle Handle;
Plugin.call<PiApiKind::piextMemGetNativeHandle>(NativeMem, &Handle);
Plugin->call<PiApiKind::piextMemGetNativeHandle>(NativeMem, &Handle);
Handles.push_back(Handle);
}

Expand Down
Loading