diff --git a/sycl/plugins/opencl/pi_opencl.cpp b/sycl/plugins/opencl/pi_opencl.cpp index e259ac5e942cf..57d2e15b96317 100644 --- a/sycl/plugins/opencl/pi_opencl.cpp +++ b/sycl/plugins/opencl/pi_opencl.cpp @@ -389,6 +389,7 @@ pi_result piextQueueCreateWithNativeHandle(pi_native_handle nativeHandle, (void)ownNativeHandle; assert(piQueue != nullptr); *piQueue = reinterpret_cast(nativeHandle); + clRetainCommandQueue(cast(nativeHandle)); return PI_SUCCESS; } diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index 1d4a524e12e8f..e9a201e71a990 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -129,9 +129,6 @@ class queue_impl { sizeof(Device), &Device, nullptr); MDevice = DeviceImplPtr(new device_impl(Device, Context->getPlatformImpl())); - - // TODO catch an exception and put it to list of asynchronous exceptions - getPlugin().call(MQueues[0]); } ~queue_impl() { diff --git a/sycl/unittests/pi/CMakeLists.txt b/sycl/unittests/pi/CMakeLists.txt index 53c69f5cae2fa..0d174ac94dbeb 100644 --- a/sycl/unittests/pi/CMakeLists.txt +++ b/sycl/unittests/pi/CMakeLists.txt @@ -7,6 +7,7 @@ add_sycl_unittest(PiTests OBJECT PiMock.cpp PlatformTest.cpp pi_arguments_handler.cpp + piInteropRetain.cpp ) add_dependencies(PiTests sycl) diff --git a/sycl/unittests/pi/piInteropRetain.cpp b/sycl/unittests/pi/piInteropRetain.cpp new file mode 100644 index 0000000000000..d8a56f0f3ad3a --- /dev/null +++ b/sycl/unittests/pi/piInteropRetain.cpp @@ -0,0 +1,63 @@ +//==--------------------- piInteropRetain.cpp -- check proper retain calls -==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include +#include +#include + +#include +#include +#include + +namespace { +using namespace cl::sycl; + +static int QueueRetainCalled = 0; +pi_result redefinedQueueRetain(pi_queue Queue) { + ++QueueRetainCalled; + return PI_SUCCESS; +} + +bool preparePiMock(platform &Plt) { + if (Plt.is_host()) { + std::cout << "Not run on host - no PI events created in that case" + << std::endl; + return false; + } + if (detail::getSyclObjImpl(Plt)->getPlugin().getBackend() != + backend::opencl) { + std::cout << "Not run on non-OpenCL backend" << std::endl; + return false; + } + return true; +} + +TEST(PiInteropTest, CheckRetain) { + platform Plt{default_selector()}; + if (!preparePiMock(Plt)) + return; + context Ctx{Plt.get_devices()[0]}; + + unittest::PiMock Mock{Plt}; + + // The queue construction should not call to piQueueRetain. Instead + // piQueueCreate should return the "retained" queue. + Mock.redefine(redefinedQueueRetain); + queue Q{Ctx, default_selector()}; + EXPECT_TRUE(QueueRetainCalled == 0); + + cl_command_queue OCLQ = get_native(Q); + EXPECT_TRUE(QueueRetainCalled == 1); + + // The make_queue should not call to piQueueRetain. The + // piextCreateQueueWithNative handle should do the "retain" if needed. + queue Q1 = make_queue(OCLQ, Ctx); + EXPECT_TRUE(QueueRetainCalled == 1); +} + +} // namespace