Skip to content

Commit 719ebae

Browse files
RossBruntongithub-actions[bot]
authored andcommitted
Automerge: [Offload] Change ol_kernel_handle_t -> ol_symbol_handle_t (#147943)
In the future, we want `ol_symbol_handle_t` to represent both kernels and global variables The first step in this process is a rename and promotion to a "typed handle".
2 parents 98ee147 + 466357a commit 719ebae

File tree

8 files changed

+54
-18
lines changed

8 files changed

+54
-18
lines changed

offload/liboffload/API/Common.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ def : Handle {
7474
let desc = "Handle of program object";
7575
}
7676

77-
def : Typedef {
78-
let name = "ol_kernel_handle_t";
79-
let desc = "Handle of kernel object";
80-
let value = "void *";
77+
def : Handle {
78+
let name = "ol_symbol_handle_t";
79+
let desc = "Handle of an object in a device's memory for a specific program";
8180
}
8281

8382
def ErrorCode : Enum {
@@ -112,6 +111,7 @@ def ErrorCode : Enum {
112111
Etor<"INVALID_DEVICE", "invalid device">,
113112
Etor<"INVALID_QUEUE", "invalid queue">,
114113
Etor<"INVALID_EVENT", "invalid event">,
114+
Etor<"SYMBOL_KIND", "the operation does not support this symbol kind">,
115115
];
116116
}
117117

offload/liboffload/API/Kernel.td

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file contains Offload API definitions related to the kernel handle
9+
// This file contains Offload API definitions related to loading and launching
10+
// kernels
1011
//
1112
//===----------------------------------------------------------------------===//
1213

1314
def : Function {
1415
let name = "olGetKernel";
1516
let desc = "Get a kernel from the function identified by `KernelName` in the given program.";
1617
let details = [
17-
"The kernel handle returned is owned by the device so does not need to be destroyed."
18+
"Symbol handles are owned by the program and do not need to be manually destroyed."
1819
];
1920
let params = [
2021
Param<"ol_program_handle_t", "Program", "handle of the program", PARAM_IN>,
2122
Param<"const char*", "KernelName", "name of the kernel entry point in the program", PARAM_IN>,
22-
Param<"ol_kernel_handle_t*", "Kernel", "output pointer for the fetched kernel", PARAM_OUT>
23+
Param<"ol_symbol_handle_t*", "Kernel", "output pointer for the fetched kernel", PARAM_OUT>
2324
];
2425
let returns = [];
2526
}
@@ -45,7 +46,7 @@ def : Function {
4546
let params = [
4647
Param<"ol_queue_handle_t", "Queue", "handle of the queue", PARAM_IN_OPTIONAL>,
4748
Param<"ol_device_handle_t", "Device", "handle of the device to execute on", PARAM_IN>,
48-
Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel", PARAM_IN>,
49+
Param<"ol_symbol_handle_t", "Kernel", "handle of the kernel", PARAM_IN>,
4950
Param<"const void*", "ArgumentsData", "pointer to the kernel argument struct", PARAM_IN_OPTIONAL>,
5051
Param<"size_t", "ArgumentsSize", "size of the kernel argument struct", PARAM_IN>,
5152
Param<"const ol_kernel_launch_size_args_t*", "LaunchSizeArgs", "pointer to the struct containing launch size parameters", PARAM_IN>,
@@ -55,5 +56,6 @@ def : Function {
5556
Return<"OL_ERRC_INVALID_ARGUMENT", ["`Queue == NULL && EventOut != NULL`"]>,
5657
Return<"OL_ERRC_INVALID_ARGUMENT", ["`ArgumentsSize > 0 && ArgumentsData == NULL`"]>,
5758
Return<"OL_ERRC_INVALID_DEVICE", ["If Queue is non-null but does not belong to Device"]>,
59+
Return<"OL_ERRC_SYMBOL_KIND", ["The provided symbol is not a kernel"]>,
5860
];
5961
}

offload/liboffload/API/OffloadAPI.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ include "Queue.td"
1818
include "Event.td"
1919
include "Program.td"
2020
include "Kernel.td"
21+
include "Symbol.td"

offload/liboffload/API/Symbol.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Symbol.td - Symbol definitions for Offload ---------*- tablegen -*-===//
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+
// This file contains Offload API definitions related to the symbol handle.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
def : Enum {
14+
let name = "ol_symbol_kind_t";
15+
let desc = "The kind of a symbol";
16+
let etors =[
17+
Etor<"KERNEL", "a kernel object">,
18+
];
19+
}

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,17 @@ struct ol_program_impl_t {
8484
DeviceImage(DeviceImage) {}
8585
plugin::DeviceImageTy *Image;
8686
std::unique_ptr<llvm::MemoryBuffer> ImageData;
87+
std::vector<std::unique_ptr<ol_symbol_impl_t>> Symbols;
8788
__tgt_device_image DeviceImage;
8889
};
8990

91+
struct ol_symbol_impl_t {
92+
ol_symbol_impl_t(GenericKernelTy *Kernel)
93+
: PluginImpl(Kernel), Kind(OL_SYMBOL_KIND_KERNEL) {}
94+
std::variant<GenericKernelTy *> PluginImpl;
95+
ol_symbol_kind_t Kind;
96+
};
97+
9098
namespace llvm {
9199
namespace offload {
92100

@@ -653,7 +661,7 @@ Error olDestroyProgram_impl(ol_program_handle_t Program) {
653661
}
654662

655663
Error olGetKernel_impl(ol_program_handle_t Program, const char *KernelName,
656-
ol_kernel_handle_t *Kernel) {
664+
ol_symbol_handle_t *Kernel) {
657665

658666
auto &Device = Program->Image->getDevice();
659667
auto KernelImpl = Device.constructKernel(KernelName);
@@ -663,13 +671,15 @@ Error olGetKernel_impl(ol_program_handle_t Program, const char *KernelName,
663671
if (auto Err = KernelImpl->init(Device, *Program->Image))
664672
return Err;
665673

666-
*Kernel = &*KernelImpl;
674+
*Kernel = Program->Symbols
675+
.emplace_back(std::make_unique<ol_symbol_impl_t>(&*KernelImpl))
676+
.get();
667677

668678
return Error::success();
669679
}
670680

671681
Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
672-
ol_kernel_handle_t Kernel, const void *ArgumentsData,
682+
ol_symbol_handle_t Kernel, const void *ArgumentsData,
673683
size_t ArgumentsSize,
674684
const ol_kernel_launch_size_args_t *LaunchSizeArgs,
675685
ol_event_handle_t *EventOut) {
@@ -680,6 +690,10 @@ Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
680690
"device specified does not match the device of the given queue");
681691
}
682692

693+
if (Kernel->Kind != OL_SYMBOL_KIND_KERNEL)
694+
return createOffloadError(ErrorCode::SYMBOL_KIND,
695+
"provided symbol is not a kernel");
696+
683697
auto *QueueImpl = Queue ? Queue->AsyncInfo : nullptr;
684698
AsyncInfoWrapperTy AsyncInfoWrapper(*DeviceImpl, QueueImpl);
685699
KernelArgsTy LaunchArgs{};
@@ -698,7 +712,7 @@ Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
698712
// Don't do anything with pointer indirection; use arg data as-is
699713
LaunchArgs.Flags.IsCUDA = true;
700714

701-
auto *KernelImpl = reinterpret_cast<GenericKernelTy *>(Kernel);
715+
auto *KernelImpl = std::get<GenericKernelTy *>(Kernel->PluginImpl);
702716
auto Err = KernelImpl->launch(*DeviceImpl, LaunchArgs.ArgPtrs, nullptr,
703717
LaunchArgs, AsyncInfoWrapper);
704718

offload/unittests/OffloadAPI/common/Fixtures.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct OffloadKernelTest : OffloadProgramTest {
120120
RETURN_ON_FATAL_FAILURE(OffloadProgramTest::TearDown());
121121
}
122122

123-
ol_kernel_handle_t Kernel = nullptr;
123+
ol_symbol_handle_t Kernel = nullptr;
124124
};
125125

126126
struct OffloadQueueTest : OffloadDeviceTest {

offload/unittests/OffloadAPI/kernel/olGetKernel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ using olGetKernelTest = OffloadProgramTest;
1414
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olGetKernelTest);
1515

1616
TEST_P(olGetKernelTest, Success) {
17-
ol_kernel_handle_t Kernel = nullptr;
17+
ol_symbol_handle_t Kernel = nullptr;
1818
ASSERT_SUCCESS(olGetKernel(Program, "foo", &Kernel));
1919
ASSERT_NE(Kernel, nullptr);
2020
}
2121

2222
TEST_P(olGetKernelTest, InvalidNullProgram) {
23-
ol_kernel_handle_t Kernel = nullptr;
23+
ol_symbol_handle_t Kernel = nullptr;
2424
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE,
2525
olGetKernel(nullptr, "foo", &Kernel));
2626
}
@@ -32,7 +32,7 @@ TEST_P(olGetKernelTest, InvalidNullKernelPointer) {
3232

3333
// Error code returning from plugin interface not yet supported
3434
TEST_P(olGetKernelTest, InvalidKernelName) {
35-
ol_kernel_handle_t Kernel = nullptr;
35+
ol_symbol_handle_t Kernel = nullptr;
3636
ASSERT_ERROR(OL_ERRC_NOT_FOUND,
3737
olGetKernel(Program, "invalid_kernel_name", &Kernel));
3838
}

offload/unittests/OffloadAPI/kernel/olLaunchKernel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct LaunchSingleKernelTestBase : LaunchKernelTestBase {
4343
ASSERT_SUCCESS(olGetKernel(Program, kernel, &Kernel));
4444
}
4545

46-
ol_kernel_handle_t Kernel = nullptr;
46+
ol_symbol_handle_t Kernel = nullptr;
4747
};
4848

4949
#define KERNEL_TEST(NAME, KERNEL) \
@@ -70,7 +70,7 @@ struct LaunchMultipleKernelTestBase : LaunchKernelTestBase {
7070
ASSERT_SUCCESS(olGetKernel(Program, K, &Kernels[I++]));
7171
}
7272

73-
std::vector<ol_kernel_handle_t> Kernels;
73+
std::vector<ol_symbol_handle_t> Kernels;
7474
};
7575

7676
#define KERNEL_MULTI_TEST(NAME, PROGRAM, ...) \

0 commit comments

Comments
 (0)