Skip to content
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

Testing refactor ext function cache #12662

Draft
wants to merge 2 commits into
base: sycl
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
2 changes: 2 additions & 0 deletions sycl/include/sycl/detail/pi.def
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ _PI_API(piSamplerCreate)
_PI_API(piSamplerGetInfo)
_PI_API(piSamplerRetain)
_PI_API(piSamplerRelease)
_PI_API(piextSamplerGetNativeHandle)
_PI_API(piextSamplerCreateWithNativeHandle)
// Queue commands
_PI_API(piEnqueueKernelLaunch)
_PI_API(piEnqueueEventsWait)
Expand Down
7 changes: 7 additions & 0 deletions sycl/include/sycl/detail/pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,13 @@ __SYCL_EXPORT pi_result piSamplerRetain(pi_sampler sampler);

__SYCL_EXPORT pi_result piSamplerRelease(pi_sampler sampler);

__SYCL_EXPORT pi_result piextSamplerCreateWithNativeHandle(
pi_native_handle nativeHandle, pi_context context,
const bool ownNativeHandle, pi_sampler *sampler);

__SYCL_EXPORT pi_result
piextSamplerGetNativeHandle(pi_sampler sampler, pi_native_handle *nativeHandle);

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/hip/pi_hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/native_cpu/pi_native_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/opencl/pi_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
18 changes: 8 additions & 10 deletions sycl/plugins/unified_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ endif()
if(SYCL_PI_UR_USE_FETCH_CONTENT)
include(FetchContent)

set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git")
# commit 69ac6c7fecac1028024ec1889d3e2cc02565c9e7
# Merge: f11823e1 b3469f1e
# Author: Kenneth Benzie (Benie) <k.benzie@codeplay.com>
# Date: Thu Feb 15 15:28:12 2024 +0100
#
# Merge pull request #1344 from aarongreig/aaron/correctlyMapBufferLocation
#
# Correct mapping of BUFFER_ALLOC_LOCATION_PROPERITES in CL adapter.
set(UNIFIED_RUNTIME_TAG 69ac6c7fecac1028024ec1889d3e2cc02565c9e7)
set(UNIFIED_RUNTIME_REPO "https://github.com/omarahmed1111/unified-runtime.git")
# commit 47af3ee296ae0517213114332ffd3ac54a456b16
# Merge: bd76c510 f2ca7a91
# Author: Omar Ahmed <30423288+omarahmed1111@users.noreply.github.com>
# Date: Thu Nov 30 16:11:56 2023 +0000
# - Merge pull request #1072 from omarahmed1111/merge-some-main-changes-into-adapters-third-patch
# - Merge main into adapters branch
set(UNIFIED_RUNTIME_TAG 9bfbf6e654ed3813926e041f9470689b46569907)

if(SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO)
set(UNIFIED_RUNTIME_REPO "${SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO}")
Expand Down
32 changes: 32 additions & 0 deletions sycl/plugins/unified_runtime/pi2ur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4412,6 +4412,38 @@ inline pi_result piSamplerRelease(pi_sampler Sampler) {
return PI_SUCCESS;
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
auto UrSampler = reinterpret_cast<ur_sampler_handle_t>(sampler);

ur_native_handle_t UrNativeHandle{};
HANDLE_ERRORS(urSamplerGetNativeHandle(UrSampler, &UrNativeHandle));

*nativeHandle = reinterpret_cast<pi_native_handle>(UrNativeHandle);

return PI_SUCCESS;
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
bool ownNativeHandle,
pi_sampler *sampler) {
ur_native_handle_t UrNativeSampler =
reinterpret_cast<ur_native_handle_t>(nativeHandle);

ur_context_handle_t UrContext =
reinterpret_cast<ur_context_handle_t>(context);

ur_sampler_handle_t *URSampler =
reinterpret_cast<ur_sampler_handle_t *>(sampler);
ur_sampler_native_properties_t Properties{};
Properties.isNativeHandleOwned = ownNativeHandle;
HANDLE_ERRORS(urSamplerCreateWithNativeHandle(UrNativeSampler, UrContext,
&Properties, URSampler));

return PI_SUCCESS;
}

// Sampler
///////////////////////////////////////////////////////////////////////////////

Expand Down
14 changes: 14 additions & 0 deletions sycl/plugins/unified_runtime/pi_unified_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,18 @@ __SYCL_EXPORT pi_result piSamplerRetain(pi_sampler Sampler) {
__SYCL_EXPORT pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}
/* clang-format off */
__SYCL_EXPORT pi_result piextSamplerGetNativeHandle(
pi_sampler sampler, pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}
/* clang-format on */
__SYCL_EXPORT pi_result piextSamplerCreateWithNativeHandle(
pi_native_handle nativeHandle, pi_context context,
const bool ownNativeHandle, pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

__SYCL_EXPORT pi_result piMemImageGetInfo(pi_mem Image, pi_image_info ParamName,
size_t ParamValueSize,
Expand Down Expand Up @@ -1484,6 +1496,8 @@ __SYCL_EXPORT pi_result piPluginInit(pi_plugin *PluginInit) {
_PI_API(piSamplerGetInfo)
_PI_API(piSamplerRetain)
_PI_API(piSamplerRelease)
_PI_API(piextSamplerCreateWithNativeHandle)
_PI_API(piextSamplerGetNativeHandle)

// Peer to Peer
_PI_API(piextEnablePeerAccess)
Expand Down
3 changes: 3 additions & 0 deletions sycl/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ function(add_sycl_rt_library LIB_NAME LIB_OBJ_NAME)
target_link_libraries(${LIB_OBJ_NAME}
PRIVATE OpenCL-Headers
)
target_link_libraries(${LIB_NAME}
PRIVATE OpenCL::OpenCL
)

if(SYCL_ENABLE_KERNEL_FUSION)
target_link_libraries(${LIB_NAME} PRIVATE sycl-fusion)
Expand Down
24 changes: 23 additions & 1 deletion sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ __SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
__SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
const async_handler &Handler,
backend Backend) {
if (Backend == backend::opencl) {
sycl::context Context(detail::pi::cast<cl_context>(NativeHandle), Handler);
return Context;
}

const auto &Plugin = getPlugin(Backend);

pi::PiContext PiContext = nullptr;
Expand Down Expand Up @@ -127,7 +132,11 @@ __SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
make_error_code(errc::invalid),
"Queue create using make_queue cannot have compute_index property.");
}

if (Backend == backend::opencl) {
sycl::queue Queue(detail::pi::cast<cl_command_queue>(NativeHandle), Context,
Handler);
return Queue;
}
// Create PI queue first.
pi::PiQueue PiQueue = nullptr;
Plugin->call<PiApiKind::piextQueueCreateWithNativeHandle>(
Expand Down Expand Up @@ -276,6 +285,19 @@ kernel make_kernel(const context &TargetContext,
*KernelBundle.begin();
const auto &DeviceImageImpl = getSyclObjImpl(DeviceImage);
PiProgram = DeviceImageImpl->get_program_ref();
} else if (Backend == backend::opencl) {
cl_kernel CLKernel = detail::pi::cast<cl_kernel>(NativeHandle);
cl_program CLProgram;
size_t Ret = clGetKernelInfo(CLKernel, CL_KERNEL_PROGRAM, sizeof(CLProgram),
&CLProgram, nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve program associated with the kernel",
PI_ERROR_INVALID_KERNEL);
}
Plugin->call<detail::PiApiKind::piextProgramCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(CLProgram),
ContextImpl->getHandleRef(), false, &PiProgram);
}

// Create PI kernel first.
Expand Down
34 changes: 30 additions & 4 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,35 @@ context::context(const std::vector<device> &DeviceList,
PropList);
}
}

context::context(cl_context ClContext, async_handler AsyncHandler) {
const auto &Plugin = sycl::detail::pi::getPlugin<backend::opencl>();
impl = std::make_shared<detail::context_impl>(
detail::pi::cast<sycl::detail::pi::PiContext>(ClContext), AsyncHandler,
Plugin);
uint32_t DeviceCount = 0;
size_t Ret = clGetContextInfo(ClContext, CL_CONTEXT_NUM_DEVICES,
sizeof(DeviceCount), &DeviceCount, nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve device count associated with the context",
PI_ERROR_INVALID_CONTEXT);
}
std::vector<cl_device_id> CLDevices(DeviceCount);
Ret = clGetContextInfo(ClContext, CL_CONTEXT_DEVICES, sizeof(CLDevices),
CLDevices.data(), nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve devices associated with the context",
PI_ERROR_INVALID_CONTEXT);
}
std::vector<sycl::detail::pi::PiDevice> Devices(DeviceCount);
for (uint32_t i = 0; i < DeviceCount; i++) {
Plugin->call<detail::PiApiKind::piextDeviceCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(CLDevices[i]), nullptr, &Devices[i]);
}
sycl::detail::pi::PiContext Context;
Plugin->call<detail::PiApiKind::piextContextCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(ClContext), DeviceCount,
Devices.data(), false, &Context);
impl = std::make_shared<detail::context_impl>(Context, AsyncHandler, Plugin);
}

template <typename Param>
Expand Down Expand Up @@ -122,7 +146,9 @@ context::get_info() const {

#undef __SYCL_PARAM_TRAITS_SPEC

cl_context context::get() const { return impl->get(); }
cl_context context::get() const {
return detail::pi::cast<cl_context>(impl->getNative());
}

bool context::is_host() const {
bool IsHost = impl->is_host();
Expand Down
5 changes: 4 additions & 1 deletion sycl/source/detail/buffer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ void buffer_impl::addInteropObject(
const PluginPtr &Plugin = getPlugin();
Plugin->call<PiApiKind::piMemRetain>(
pi::cast<sycl::detail::pi::PiMem>(MInteropMemObject));
Handles.push_back(pi::cast<pi_native_handle>(MInteropMemObject));
pi_native_handle Handle;
Plugin->call<PiApiKind::piextMemGetNativeHandle>(MInteropMemObject,
nullptr, &Handle);
Handles.push_back(Handle);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ cl_context context_impl::get() const {
"This instance of context doesn't support OpenCL interoperability.",
PI_ERROR_INVALID_CONTEXT);
}
// TODO catch an exception and put it to list of asynchronous exceptions
getPlugin()->call<PiApiKind::piContextRetain>(MContext);
return pi::cast<cl_context>(MContext);
// TODO catch an exception and put it to list of asynchronous exceptions
pi_native_handle NativeContext;
getPlugin()->call<PiApiKind::piextContextGetNativeHandle>(MContext,
&NativeContext);
return pi::cast<cl_context>(NativeContext);
}

bool context_impl::is_host() const { return MHostContext; }
Expand Down
10 changes: 0 additions & 10 deletions sycl/source/detail/kernel_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ kernel_impl::kernel_impl(sycl::detail::pi::PiKernel Kernel,
MCreatedFromSource(IsCreatedFromSource),
MKernelBundleImpl(std::move(KernelBundleImpl)),
MKernelArgMaskPtr{ArgMask} {

sycl::detail::pi::PiContext Context = nullptr;
// Using the plugin from the passed ContextImpl
getPlugin()->call<PiApiKind::piKernelGetInfo>(
MKernel, PI_KERNEL_INFO_CONTEXT, sizeof(Context), &Context, nullptr);
if (ContextImpl->getHandleRef() != Context)
throw sycl::invalid_parameter_error(
"Input context must be the same as the context of cl_kernel",
PI_ERROR_INVALID_CONTEXT);

MIsInterop = MProgramImpl->isInterop();
}

Expand Down
6 changes: 5 additions & 1 deletion sycl/source/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ class kernel_impl {
PI_ERROR_INVALID_KERNEL);
}
getPlugin()->call<PiApiKind::piKernelRetain>(MKernel);
return pi::cast<cl_kernel>(MKernel);
// TODO catch an exception and put it to list of asynchronous exceptions
pi_native_handle NativeKernel;
getPlugin()->call<PiApiKind::piextKernelGetNativeHandle>(MKernel,
&NativeKernel);
return pi::cast<cl_kernel>(NativeKernel);
}

/// Check if the associated SYCL context is a SYCL host context.
Expand Down
Loading
Loading