Skip to content

Commit 1b4a8b8

Browse files
Merge pull request #1399 from GeorgeWeb/georgi/get-event-info-from-native-fix
[CUDA][HIP] Return error on silently failing urEventGetInfo queries for interop created events
2 parents 47ab963 + df6e9d2 commit 1b4a8b8

File tree

8 files changed

+137
-27
lines changed

8 files changed

+137
-27
lines changed

source/adapters/cuda/event.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
164164
UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropValueSizeRet);
165165

166166
switch (propName) {
167-
case UR_EVENT_INFO_COMMAND_QUEUE:
167+
case UR_EVENT_INFO_COMMAND_QUEUE: {
168+
// If the runtime owns the native handle, we have reference to the queue.
169+
// Otherwise, the event handle comes from an interop API with no RT refs.
170+
if (!hEvent->getQueue()) {
171+
setErrorMessage("Command queue info cannot be queried for the event. The "
172+
"event object was created from a native event and has no "
173+
"valid reference to a command queue.",
174+
UR_RESULT_ERROR_INVALID_VALUE);
175+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
176+
}
168177
return ReturnValue(hEvent->getQueue());
178+
}
169179
case UR_EVENT_INFO_COMMAND_TYPE:
170180
return ReturnValue(hEvent->getCommandType());
171181
case UR_EVENT_INFO_REFERENCE_COUNT:
@@ -280,17 +290,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
280290

281291
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};
282292

283-
try {
284-
EventPtr =
285-
std::unique_ptr<ur_event_handle_t_>(ur_event_handle_t_::makeWithNative(
286-
hContext, reinterpret_cast<CUevent>(hNativeEvent)));
287-
} catch (const std::bad_alloc &) {
288-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
289-
} catch (...) {
290-
return UR_RESULT_ERROR_UNKNOWN;
291-
}
292-
293-
*phEvent = EventPtr.release();
293+
*phEvent = ur_event_handle_t_::makeWithNative(
294+
hContext, reinterpret_cast<CUevent>(hNativeEvent));
294295

295296
return UR_RESULT_SUCCESS;
296297
}

source/adapters/hip/event.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
178178

179179
UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropValueSizeRet);
180180
switch (propName) {
181-
case UR_EVENT_INFO_COMMAND_QUEUE:
181+
case UR_EVENT_INFO_COMMAND_QUEUE: {
182+
// If the runtime owns the native handle, we have reference to the queue.
183+
// Otherwise, the event handle comes from an interop API with no RT refs.
184+
if (!hEvent->getQueue()) {
185+
setErrorMessage("Command queue info cannot be queried for the event. The "
186+
"event object was created from a native event and has no "
187+
"valid reference to a command queue.",
188+
UR_RESULT_ERROR_INVALID_VALUE);
189+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
190+
}
182191
return ReturnValue(hEvent->getQueue());
192+
}
183193
case UR_EVENT_INFO_COMMAND_TYPE:
184194
return ReturnValue(hEvent->getCommandType());
185195
case UR_EVENT_INFO_REFERENCE_COUNT:

test/adapters/cuda/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_adapter_test(cuda
1616
urQueueGetNativeHandle.cpp
1717
kernel_tests.cpp
1818
memory_tests.cpp
19+
event_tests.cpp
1920
#FIXME: make this cleaner
2021
${CMAKE_CURRENT_SOURCE_DIR}/../../../source/adapters/cuda/queue.cpp
2122
${CMAKE_CURRENT_SOURCE_DIR}/../../../source/adapters/cuda/common.cpp

test/adapters/cuda/event_tests.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2022-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#include "device.hpp"
7+
#include "event.hpp"
8+
#include "fixtures.h"
9+
#include "raii.h"
10+
11+
using cudaEventTest = uur::urContextTest;
12+
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(cudaEventTest);
13+
14+
// Testing the urEventGetInfo behaviour for natively constructed (Cuda) events.
15+
// Backend interop APIs can lead to creating event objects that are not fully
16+
// initialized. In the Cuda adapter, an event can have nullptr command queue
17+
// because the interop API does not associate a UR-owned queue with the event.
18+
TEST_P(cudaEventTest, GetQueueFromEventCreatedWithNativeHandle) {
19+
CUcontext cuda_ctx = device->getNativeContext();
20+
EXPECT_NE(cuda_ctx, nullptr);
21+
RAIICUevent cuda_event;
22+
ASSERT_SUCCESS_CUDA(cuCtxSetCurrent(cuda_ctx));
23+
ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT));
24+
25+
auto native_event = reinterpret_cast<ur_native_handle_t>(cuda_event.get());
26+
uur::raii::Event event{nullptr};
27+
ASSERT_SUCCESS(urEventCreateWithNativeHandle(native_event, context, nullptr,
28+
event.ptr()));
29+
EXPECT_NE(event, nullptr);
30+
31+
size_t ret_size{};
32+
ur_queue_handle_t q{};
33+
ASSERT_EQ_RESULT(urEventGetInfo(event, UR_EVENT_INFO_COMMAND_QUEUE,
34+
sizeof(ur_queue_handle_t), &q, &ret_size),
35+
UR_RESULT_ERROR_ADAPTER_SPECIFIC);
36+
}

test/adapters/cuda/raii.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2022-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#ifndef UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED
7+
#define UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED
8+
9+
#include "uur/raii.h"
10+
#include <cuda.h>
11+
12+
struct RAIICUevent {
13+
CUevent handle = nullptr;
14+
15+
~RAIICUevent() {
16+
if (handle) {
17+
cuEventDestroy(handle);
18+
}
19+
}
20+
21+
CUevent *ptr() { return &handle; }
22+
CUevent get() { return handle; }
23+
};
24+
25+
#endif // UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED

test/adapters/cuda/urEventCreateWithNativeHandle.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

66
#include "fixtures.h"
7-
#include "uur/raii.h"
7+
#include "raii.h"
88

99
using urCudaEventCreateWithNativeHandleTest = uur::urQueueTest;
1010
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEventCreateWithNativeHandleTest);
1111

12-
struct RAIICUevent {
13-
CUevent handle = nullptr;
14-
15-
~RAIICUevent() {
16-
if (handle) {
17-
cuEventDestroy(handle);
18-
}
19-
}
20-
21-
CUevent *ptr() { return &handle; }
22-
CUevent get() { return handle; }
23-
};
24-
2512
TEST_P(urCudaEventCreateWithNativeHandleTest, Success) {
2613
RAIICUevent cuda_event;
2714
ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT));

test/adapters/hip/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_adapter_test(hip
1111
urDeviceGetNativeHandle.cpp
1212
urEventGetNativeHandle.cpp
1313
test_context.cpp
14+
test_event.cpp
1415
ENVIRONMENT
1516
"UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_hip>\""
1617
)

test/adapters/hip/test_event.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (C) 2022-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#include "event.hpp"
7+
#include "fixtures.h"
8+
#include "uur/raii.h"
9+
10+
#include <hip/hip_runtime.h>
11+
#include <tuple>
12+
13+
struct RAIIHipEvent {
14+
hipEvent_t handle = nullptr;
15+
16+
~RAIIHipEvent() {
17+
if (handle) {
18+
std::ignore = hipEventDestroy(handle);
19+
}
20+
}
21+
22+
hipEvent_t *ptr() { return &handle; }
23+
hipEvent_t get() { return handle; }
24+
};
25+
26+
using urHipEventTest = uur::urContextTest;
27+
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urHipEventTest);
28+
29+
// Testing the urEventGetInfo behaviour for natively constructed (HIP) events.
30+
// Backend interop APIs can lead to creating event objects that are not fully
31+
// initialized. In the Cuda adapter, an event can have nullptr command queue
32+
// because the interop API does not associate a UR-owned queue with the event.
33+
TEST_P(urHipEventTest, GetQueueFromEventCreatedWithNativeHandle) {
34+
RAIIHipEvent hip_event;
35+
ASSERT_SUCCESS_HIP(
36+
hipEventCreateWithFlags(hip_event.ptr(), hipEventDefault));
37+
38+
auto native_event = reinterpret_cast<ur_native_handle_t>(hip_event.get());
39+
uur::raii::Event event{nullptr};
40+
ASSERT_SUCCESS(urEventCreateWithNativeHandle(native_event, context, nullptr,
41+
event.ptr()));
42+
EXPECT_NE(event, nullptr);
43+
44+
size_t ret_size{};
45+
ur_queue_handle_t q{};
46+
ASSERT_EQ_RESULT(urEventGetInfo(event, UR_EVENT_INFO_COMMAND_QUEUE,
47+
sizeof(ur_queue_handle_t), &q, &ret_size),
48+
UR_RESULT_ERROR_ADAPTER_SPECIFIC);
49+
}

0 commit comments

Comments
 (0)