Skip to content

[SYCL] piextQueueCreateWithNativeHandle should not be followed by a piQueueRetain #4988

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 5 commits into from
Dec 1, 2021
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/plugins/opencl/pi_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ pi_result piextQueueCreateWithNativeHandle(pi_native_handle nativeHandle,
(void)ownNativeHandle;
assert(piQueue != nullptr);
*piQueue = reinterpret_cast<pi_queue>(nativeHandle);
clRetainCommandQueue(cast<cl_command_queue>(nativeHandle));
return PI_SUCCESS;
}

Expand Down
3 changes: 0 additions & 3 deletions sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PiApiKind::piQueueRetain>(MQueues[0]);
}

~queue_impl() {
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/pi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_sycl_unittest(PiTests OBJECT
PiMock.cpp
PlatformTest.cpp
pi_arguments_handler.cpp
piInteropRetain.cpp
)

add_dependencies(PiTests sycl)
Expand Down
63 changes: 63 additions & 0 deletions sycl/unittests/pi/piInteropRetain.cpp
Original file line number Diff line number Diff line change
@@ -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 <CL/opencl.h>
#include <CL/sycl.hpp>
#include <CL/sycl/backend/opencl.hpp>

#include <detail/queue_impl.hpp>
#include <gtest/gtest.h>
#include <helpers/PiMock.hpp>

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<detail::PiApiKind::piQueueRetain>(redefinedQueueRetain);
queue Q{Ctx, default_selector()};
EXPECT_TRUE(QueueRetainCalled == 0);

cl_command_queue OCLQ = get_native<backend::opencl>(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<backend::opencl>(OCLQ, Ctx);
EXPECT_TRUE(QueueRetainCalled == 1);
}

} // namespace