Skip to content

Commit 690c3ee

Browse files
authored
[Offload] Replace "EventOut" parameters with olCreateEvent (#150217)
Rather than having every "enqueue"-type function have an output pointer specifically for an output event, just provide an `olCreateEvent` entrypoint which pushes an event to the queue. For example, replace: ```cpp olMemcpy(Queue, ..., EventOut); ``` with ```cpp olMemcpy(Queue, ...); olCreateEvent(Queue, EventOut); ```
1 parent 675d7e1 commit 690c3ee

File tree

11 files changed

+100
-110
lines changed

11 files changed

+100
-110
lines changed

offload/liboffload/API/Event.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
def : Function {
14+
let name = "olCreateEvent";
15+
let desc = "Enqueue an event to `Queue` and return it.";
16+
let details = [
17+
"This event can be used with `olSyncEvent` and `olWaitEvents` and will be complete once all enqueued work prior to the `olCreateEvent` call is complete.",
18+
];
19+
let params = [
20+
Param<"ol_queue_handle_t", "Queue", "queue to create the event for", PARAM_IN>,
21+
Param<"ol_event_handle_t*", "Event", "output pointer for the created event", PARAM_OUT>
22+
];
23+
let returns = [];
24+
}
25+
1326
def : Function {
1427
let name = "olDestroyEvent";
1528
let desc = "Destroy the event and free all underlying resources.";

offload/liboffload/API/Kernel.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ def : Function {
3535
Param<"const void*", "ArgumentsData", "pointer to the kernel argument struct", PARAM_IN_OPTIONAL>,
3636
Param<"size_t", "ArgumentsSize", "size of the kernel argument struct", PARAM_IN>,
3737
Param<"const ol_kernel_launch_size_args_t*", "LaunchSizeArgs", "pointer to the struct containing launch size parameters", PARAM_IN>,
38-
Param<"ol_event_handle_t*", "EventOut", "optional recorded event for the enqueued operation", PARAM_OUT_OPTIONAL>
3938
];
4039
let returns = [
41-
Return<"OL_ERRC_INVALID_ARGUMENT", ["`Queue == NULL && EventOut != NULL`"]>,
4240
Return<"OL_ERRC_INVALID_ARGUMENT", ["`ArgumentsSize > 0 && ArgumentsData == NULL`"]>,
4341
Return<"OL_ERRC_INVALID_DEVICE", ["If Queue is non-null but does not belong to Device"]>,
4442
Return<"OL_ERRC_SYMBOL_KIND", ["The provided symbol is not a kernel"]>,

offload/liboffload/API/Memory.td

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ def : Function {
6060
Param<"const void*", "SrcPtr", "pointer to copy from", PARAM_IN>,
6161
Param<"ol_device_handle_t", "SrcDevice", "device that SrcPtr belongs to", PARAM_IN>,
6262
Param<"size_t", "Size", "size in bytes of data to copy", PARAM_IN>,
63-
Param<"ol_event_handle_t*", "EventOut", "optional recorded event for the enqueued operation", PARAM_OUT_OPTIONAL>
64-
];
65-
let returns = [
66-
Return<"OL_ERRC_INVALID_ARGUMENT", ["`Queue == NULL && EventOut != NULL`"]>
6763
];
64+
let returns = [];
6865
}

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -591,26 +591,21 @@ Error olGetEventInfoSize_impl(ol_event_handle_t Event, ol_event_info_t PropName,
591591
return olGetEventInfoImplDetail(Event, PropName, 0, nullptr, PropSizeRet);
592592
}
593593

594-
ol_event_handle_t makeEvent(ol_queue_handle_t Queue) {
595-
auto EventImpl = std::make_unique<ol_event_impl_t>(nullptr, Queue);
596-
if (auto Res = Queue->Device->Device->createEvent(&EventImpl->EventInfo)) {
597-
llvm::consumeError(std::move(Res));
598-
return nullptr;
599-
}
594+
Error olCreateEvent_impl(ol_queue_handle_t Queue, ol_event_handle_t *EventOut) {
595+
*EventOut = new ol_event_impl_t(nullptr, Queue);
596+
if (auto Res = Queue->Device->Device->createEvent(&(*EventOut)->EventInfo))
597+
return Res;
600598

601-
if (auto Res = Queue->Device->Device->recordEvent(EventImpl->EventInfo,
602-
Queue->AsyncInfo)) {
603-
llvm::consumeError(std::move(Res));
604-
return nullptr;
605-
}
599+
if (auto Res = Queue->Device->Device->recordEvent((*EventOut)->EventInfo,
600+
Queue->AsyncInfo))
601+
return Res;
606602

607-
return EventImpl.release();
603+
return Plugin::success();
608604
}
609605

610606
Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
611607
ol_device_handle_t DstDevice, const void *SrcPtr,
612-
ol_device_handle_t SrcDevice, size_t Size,
613-
ol_event_handle_t *EventOut) {
608+
ol_device_handle_t SrcDevice, size_t Size) {
614609
auto Host = OffloadContext::get().HostDevice();
615610
if (DstDevice == Host && SrcDevice == Host) {
616611
if (!Queue) {
@@ -641,9 +636,6 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
641636
return Res;
642637
}
643638

644-
if (EventOut)
645-
*EventOut = makeEvent(Queue);
646-
647639
return Error::success();
648640
}
649641

@@ -690,8 +682,7 @@ Error olDestroyProgram_impl(ol_program_handle_t Program) {
690682
Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
691683
ol_symbol_handle_t Kernel, const void *ArgumentsData,
692684
size_t ArgumentsSize,
693-
const ol_kernel_launch_size_args_t *LaunchSizeArgs,
694-
ol_event_handle_t *EventOut) {
685+
const ol_kernel_launch_size_args_t *LaunchSizeArgs) {
695686
auto *DeviceImpl = Device->Device;
696687
if (Queue && Device != Queue->Device) {
697688
return createOffloadError(
@@ -729,9 +720,6 @@ Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
729720
if (Err)
730721
return Err;
731722

732-
if (EventOut)
733-
*EventOut = makeEvent(Queue);
734-
735723
return Error::success();
736724
}
737725

offload/unittests/OffloadAPI/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_offload_unittest("device"
99
device/olGetDeviceInfoSize.cpp)
1010

1111
add_offload_unittest("event"
12+
event/olCreateEvent.cpp
1213
event/olDestroyEvent.cpp
1314
event/olSyncEvent.cpp
1415
event/olGetEventInfo.cpp

offload/unittests/OffloadAPI/common/Fixtures.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,8 @@ struct OffloadQueueTest : OffloadDeviceTest {
171171
struct OffloadEventTest : OffloadQueueTest {
172172
void SetUp() override {
173173
RETURN_ON_FATAL_FAILURE(OffloadQueueTest::SetUp());
174-
// Get an event from a memcpy. We can still use it in olGetEventInfo etc
175-
// after it has been waited on.
176-
void *Alloc;
177-
uint32_t Value = 42;
178-
ASSERT_SUCCESS(
179-
olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(Value), &Alloc));
180-
ASSERT_SUCCESS(
181-
olMemcpy(Queue, Alloc, Device, &Value, Host, sizeof(Value), &Event));
182-
ASSERT_SUCCESS(olSyncEvent(Event));
183-
ASSERT_SUCCESS(olMemFree(Alloc));
174+
ASSERT_SUCCESS(olCreateEvent(Queue, &Event));
175+
ASSERT_SUCCESS(olSyncQueue(Queue));
184176
}
185177

186178
void TearDown() override {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===------- Offload API tests - olCreateEvent ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../common/Fixtures.hpp"
10+
#include <OffloadAPI.h>
11+
#include <gtest/gtest.h>
12+
13+
using olCreateEventTest = OffloadQueueTest;
14+
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olCreateEventTest);
15+
16+
TEST_P(olCreateEventTest, Success) {
17+
ol_event_handle_t Event = nullptr;
18+
ASSERT_SUCCESS(olCreateEvent(Queue, &Event));
19+
ASSERT_NE(Event, nullptr);
20+
}
21+
22+
TEST_P(olCreateEventTest, InvalidNullQueue) {
23+
ol_event_handle_t Event;
24+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olCreateEvent(nullptr, &Event));
25+
}
26+
27+
TEST_P(olCreateEventTest, InvalidNullDest) {
28+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, olCreateEvent(Queue, nullptr));
29+
}

offload/unittests/OffloadAPI/event/olDestroyEvent.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ using olDestroyEventTest = OffloadQueueTest;
1414
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olDestroyEventTest);
1515

1616
TEST_P(olDestroyEventTest, Success) {
17-
uint32_t Src = 42;
18-
void *DstPtr;
19-
2017
ol_event_handle_t Event = nullptr;
21-
ASSERT_SUCCESS(
22-
olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(uint32_t), &DstPtr));
23-
ASSERT_SUCCESS(
24-
olMemcpy(Queue, DstPtr, Device, &Src, Host, sizeof(Src), &Event));
18+
ASSERT_SUCCESS(olCreateEvent(Queue, &Event));
2519
ASSERT_NE(Event, nullptr);
2620
ASSERT_SUCCESS(olSyncQueue(Queue));
2721
ASSERT_SUCCESS(olDestroyEvent(Event));

offload/unittests/OffloadAPI/event/olSyncEvent.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ using olSyncEventTest = OffloadQueueTest;
1414
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olSyncEventTest);
1515

1616
TEST_P(olSyncEventTest, Success) {
17-
uint32_t Src = 42;
18-
void *DstPtr;
19-
2017
ol_event_handle_t Event = nullptr;
21-
ASSERT_SUCCESS(
22-
olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(uint32_t), &DstPtr));
23-
ASSERT_SUCCESS(
24-
olMemcpy(Queue, DstPtr, Device, &Src, Host, sizeof(Src), &Event));
18+
ASSERT_SUCCESS(olCreateEvent(Queue, &Event));
2519
ASSERT_NE(Event, nullptr);
2620
ASSERT_SUCCESS(olSyncEvent(Event));
2721
ASSERT_SUCCESS(olDestroyEvent(Event));
@@ -31,15 +25,9 @@ TEST_P(olSyncEventTest, InvalidNullEvent) {
3125
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olSyncEvent(nullptr));
3226
}
3327

34-
TEST_P(olSyncEventTest, SuccessMultipleWait) {
35-
uint32_t Src = 42;
36-
void *DstPtr;
37-
28+
TEST_P(olSyncEventTest, SuccessMultipleSync) {
3829
ol_event_handle_t Event = nullptr;
39-
ASSERT_SUCCESS(
40-
olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(uint32_t), &DstPtr));
41-
ASSERT_SUCCESS(
42-
olMemcpy(Queue, DstPtr, Device, &Src, Host, sizeof(Src), &Event));
30+
ASSERT_SUCCESS(olCreateEvent(Queue, &Event));
4331
ASSERT_NE(Event, nullptr);
4432

4533
for (size_t I = 0; I < 10; I++)

offload/unittests/OffloadAPI/kernel/olLaunchKernel.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ TEST_P(olLaunchKernelFooTest, Success) {
9191
void *Mem;
9292
} Args{Mem};
9393

94-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args),
95-
&LaunchArgs, nullptr));
94+
ASSERT_SUCCESS(
95+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
9696

9797
ASSERT_SUCCESS(olSyncQueue(Queue));
9898

@@ -106,7 +106,7 @@ TEST_P(olLaunchKernelFooTest, Success) {
106106

107107
TEST_P(olLaunchKernelNoArgsTest, Success) {
108108
ASSERT_SUCCESS(
109-
olLaunchKernel(Queue, Device, Kernel, nullptr, 0, &LaunchArgs, nullptr));
109+
olLaunchKernel(Queue, Device, Kernel, nullptr, 0, &LaunchArgs));
110110

111111
ASSERT_SUCCESS(olSyncQueue(Queue));
112112
}
@@ -121,7 +121,7 @@ TEST_P(olLaunchKernelFooTest, SuccessSynchronous) {
121121
} Args{Mem};
122122

123123
ASSERT_SUCCESS(olLaunchKernel(nullptr, Device, Kernel, &Args, sizeof(Args),
124-
&LaunchArgs, nullptr));
124+
&LaunchArgs));
125125

126126
uint32_t *Data = (uint32_t *)Mem;
127127
for (uint32_t i = 0; i < 64; i++) {
@@ -144,8 +144,8 @@ TEST_P(olLaunchKernelLocalMemTest, Success) {
144144
void *Mem;
145145
} Args{Mem};
146146

147-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args),
148-
&LaunchArgs, nullptr));
147+
ASSERT_SUCCESS(
148+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
149149

150150
ASSERT_SUCCESS(olSyncQueue(Queue));
151151

@@ -167,8 +167,8 @@ TEST_P(olLaunchKernelLocalMemReductionTest, Success) {
167167
void *Mem;
168168
} Args{Mem};
169169

170-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args),
171-
&LaunchArgs, nullptr));
170+
ASSERT_SUCCESS(
171+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
172172

173173
ASSERT_SUCCESS(olSyncQueue(Queue));
174174

@@ -190,8 +190,8 @@ TEST_P(olLaunchKernelLocalMemStaticTest, Success) {
190190
void *Mem;
191191
} Args{Mem};
192192

193-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args),
194-
&LaunchArgs, nullptr));
193+
ASSERT_SUCCESS(
194+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
195195

196196
ASSERT_SUCCESS(olSyncQueue(Queue));
197197

@@ -210,11 +210,11 @@ TEST_P(olLaunchKernelGlobalTest, Success) {
210210
void *Mem;
211211
} Args{Mem};
212212

213-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernels[0], nullptr, 0,
214-
&LaunchArgs, nullptr));
213+
ASSERT_SUCCESS(
214+
olLaunchKernel(Queue, Device, Kernels[0], nullptr, 0, &LaunchArgs));
215215
ASSERT_SUCCESS(olSyncQueue(Queue));
216216
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernels[1], &Args, sizeof(Args),
217-
&LaunchArgs, nullptr));
217+
&LaunchArgs));
218218
ASSERT_SUCCESS(olSyncQueue(Queue));
219219

220220
uint32_t *Data = (uint32_t *)Mem;
@@ -229,9 +229,8 @@ TEST_P(olLaunchKernelGlobalTest, InvalidNotAKernel) {
229229
ol_symbol_handle_t Global = nullptr;
230230
ASSERT_SUCCESS(
231231
olGetSymbol(Program, "global", OL_SYMBOL_KIND_GLOBAL_VARIABLE, &Global));
232-
ASSERT_ERROR(
233-
OL_ERRC_SYMBOL_KIND,
234-
olLaunchKernel(Queue, Device, Global, nullptr, 0, &LaunchArgs, nullptr));
232+
ASSERT_ERROR(OL_ERRC_SYMBOL_KIND,
233+
olLaunchKernel(Queue, Device, Global, nullptr, 0, &LaunchArgs));
235234
}
236235

237236
TEST_P(olLaunchKernelGlobalCtorTest, Success) {
@@ -242,8 +241,8 @@ TEST_P(olLaunchKernelGlobalCtorTest, Success) {
242241
void *Mem;
243242
} Args{Mem};
244243

245-
ASSERT_SUCCESS(olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args),
246-
&LaunchArgs, nullptr));
244+
ASSERT_SUCCESS(
245+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
247246
ASSERT_SUCCESS(olSyncQueue(Queue));
248247

249248
uint32_t *Data = (uint32_t *)Mem;
@@ -259,6 +258,6 @@ TEST_P(olLaunchKernelGlobalDtorTest, Success) {
259258
// find/implement a way, update this test. For now we just check that nothing
260259
// crashes
261260
ASSERT_SUCCESS(
262-
olLaunchKernel(Queue, Device, Kernel, nullptr, 0, &LaunchArgs, nullptr));
261+
olLaunchKernel(Queue, Device, Kernel, nullptr, 0, &LaunchArgs));
263262
ASSERT_SUCCESS(olSyncQueue(Queue));
264263
}

0 commit comments

Comments
 (0)