Skip to content

Commit b0fabb3

Browse files
committed
[Offload] Enable querying a kernel's program
Added new entrypoints GetKernelInfo and GetKernelInfoSize for querying info about a kernel. Currently, the only supported info is the program handle which was used to create the program. As part of implementing this, `ol_kernel_handle_t` has been "promoted" to a real handle type rather than a bitcast.
1 parent b78bc35 commit b0fabb3

File tree

6 files changed

+166
-5
lines changed

6 files changed

+166
-5
lines changed

offload/liboffload/API/Common.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ def : Handle {
7777
let desc = "Handle of program object";
7878
}
7979

80-
def : Typedef {
80+
def : Handle {
8181
let name = "ol_kernel_handle_t";
8282
let desc = "Handle of kernel object";
83-
let value = "void *";
8483
}
8584

8685
def ErrorCode : Enum {

offload/liboffload/API/Kernel.td

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

13+
def : Enum {
14+
let name = "ol_kernel_info_t";
15+
let desc = "Supported kernel info.";
16+
let is_typed = 1;
17+
let etors =[
18+
TaggedEtor<"PROGRAM", "ol_program_handle_t", "the program associated with this kernel">,
19+
];
20+
}
21+
1322
def : Function {
1423
let name = "olGetKernel";
1524
let desc = "Get a kernel from the function identified by `KernelName` in the given program.";
@@ -59,3 +68,44 @@ def : Function {
5968
Return<"OL_ERRC_INVALID_DEVICE", ["If Queue is non-null but does not belong to Device"]>,
6069
];
6170
}
71+
72+
def : Function {
73+
let name = "olGetKernelInfo";
74+
let desc = "Queries the given property of the device.";
75+
let details = [];
76+
let params = [
77+
Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel instance", PARAM_IN>,
78+
Param<"ol_kernel_info_t", "PropName", "type of the info to retrieve", PARAM_IN>,
79+
Param<"size_t", "PropSize", "the number of bytes pointed to by PropValue.", PARAM_IN>,
80+
TypeTaggedParam<"void*", "PropValue", "array of bytes holding the info. If PropSize is not equal to or greater than the real "
81+
"number of bytes needed to return the info then the OL_ERRC_INVALID_SIZE error is returned and "
82+
"PropValue is not used.", PARAM_OUT, TypeInfo<"PropName" , "PropSize">>
83+
];
84+
let returns = [
85+
Return<"OL_ERRC_UNSUPPORTED_ENUMERATION", [
86+
"If `PropName` is not supported by the kernel."
87+
]>,
88+
Return<"OL_ERRC_INVALID_SIZE", [
89+
"`PropSize == 0`",
90+
"If `PropSize` is less than the real number of bytes needed to return the info."
91+
]>,
92+
Return<"OL_ERRC_INVALID_DEVICE">
93+
];
94+
}
95+
96+
def : Function {
97+
let name = "olGetKernelInfoSize";
98+
let desc = "Returns the storage size of the given device query.";
99+
let details = [];
100+
let params = [
101+
Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel instance", PARAM_IN>,
102+
Param<"ol_kernel_info_t", "PropName", "type of the info to retrieve", PARAM_IN>,
103+
Param<"size_t*", "PropSizeRet", "pointer to the number of bytes required to store the query", PARAM_OUT>
104+
];
105+
let returns = [
106+
Return<"OL_ERRC_UNSUPPORTED_ENUMERATION", [
107+
"If `PropName` is not supported by the kernel."
108+
]>,
109+
Return<"OL_ERRC_INVALID_DEVICE">
110+
];
111+
}

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ struct ol_program_impl_t {
8585
__tgt_device_image DeviceImage;
8686
};
8787

88+
struct ol_kernel_impl_t {
89+
ol_kernel_impl_t(plugin::GenericKernelTy *Kernel, ol_program_handle_t Program)
90+
: Kernel(Kernel), Program(Program) {}
91+
plugin::GenericKernelTy *Kernel;
92+
ol_program_handle_t Program;
93+
};
94+
8895
namespace llvm {
8996
namespace offload {
9097

@@ -286,6 +293,34 @@ Error olGetDeviceInfoSize_impl(ol_device_handle_t Device,
286293
return olGetDeviceInfoImplDetail(Device, PropName, 0, nullptr, PropSizeRet);
287294
}
288295

296+
Error olGetKernelInfoImplDetail(ol_kernel_handle_t Kernel,
297+
ol_kernel_info_t PropName, size_t PropSize,
298+
void *PropValue, size_t *PropSizeRet) {
299+
300+
ReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
301+
302+
switch (PropName) {
303+
case OL_KERNEL_INFO_PROGRAM:
304+
return ReturnValue(Kernel->Program);
305+
default:
306+
return createOffloadError(ErrorCode::INVALID_ENUMERATION,
307+
"getKernelInfo enum '%i' is invalid", PropName);
308+
}
309+
310+
return Error::success();
311+
}
312+
313+
Error olGetKernelInfo_impl(ol_kernel_handle_t Kernel, ol_kernel_info_t PropName,
314+
size_t PropSize, void *PropValue) {
315+
return olGetKernelInfoImplDetail(Kernel, PropName, PropSize, PropValue,
316+
nullptr);
317+
}
318+
319+
Error olGetKernelInfoSize_impl(ol_kernel_handle_t Kernel,
320+
ol_kernel_info_t PropName, size_t *PropSizeRet) {
321+
return olGetKernelInfoImplDetail(Kernel, PropName, 0, nullptr, PropSizeRet);
322+
}
323+
289324
Error olIterateDevices_impl(ol_device_iterate_cb_t Callback, void *UserData) {
290325
for (auto &Platform : Platforms()) {
291326
for (auto &Device : Platform.Devices) {
@@ -479,7 +514,7 @@ Error olGetKernel_impl(ol_program_handle_t Program, const char *KernelName,
479514
if (auto Err = KernelImpl->init(Device, *Program->Image))
480515
return Err;
481516

482-
*Kernel = &*KernelImpl;
517+
*Kernel = new ol_kernel_impl_t(std::move(&*KernelImpl), Program);
483518

484519
return Error::success();
485520
}
@@ -514,7 +549,7 @@ Error olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
514549
// Don't do anything with pointer indirection; use arg data as-is
515550
LaunchArgs.Flags.IsCUDA = true;
516551

517-
auto *KernelImpl = reinterpret_cast<GenericKernelTy *>(Kernel);
552+
auto *KernelImpl = Kernel->Kernel;
518553
auto Err = KernelImpl->launch(*DeviceImpl, LaunchArgs.ArgPtrs, nullptr,
519554
LaunchArgs, AsyncInfoWrapper);
520555

offload/unittests/OffloadAPI/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ add_offload_unittest("event"
1414

1515
add_offload_unittest("kernel"
1616
kernel/olGetKernel.cpp
17-
kernel/olLaunchKernel.cpp)
17+
kernel/olLaunchKernel.cpp
18+
kernel/olGetKernelInfo.cpp
19+
kernel/olGetKernelInfoSize.cpp)
1820

1921
add_offload_unittest("memory"
2022
memory/olMemAlloc.cpp
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===------- Offload API tests - olGetKernelInfo --------------------------===//
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 <OffloadAPI.h>
10+
11+
#include "../common/Fixtures.hpp"
12+
13+
using olGetKernelInfoTest = OffloadKernelTest;
14+
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olGetKernelInfoTest);
15+
16+
TEST_P(olGetKernelInfoTest, SuccessProgram) {
17+
ol_program_handle_t ReadProgram;
18+
ASSERT_SUCCESS(olGetKernelInfo(Kernel, OL_KERNEL_INFO_PROGRAM,
19+
sizeof(ol_program_handle_t), &ReadProgram));
20+
ASSERT_EQ(Program, ReadProgram);
21+
}
22+
23+
TEST_P(olGetKernelInfoTest, InvalidNullHandle) {
24+
ol_program_handle_t Program;
25+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE,
26+
olGetKernelInfo(nullptr, OL_KERNEL_INFO_PROGRAM, sizeof(Program),
27+
&Program));
28+
}
29+
30+
TEST_P(olGetKernelInfoTest, InvalidKernelInfoEnumeration) {
31+
ol_program_handle_t Program;
32+
ASSERT_ERROR(OL_ERRC_INVALID_ENUMERATION,
33+
olGetKernelInfo(Kernel, OL_KERNEL_INFO_FORCE_UINT32,
34+
sizeof(Program), &Program));
35+
}
36+
37+
TEST_P(olGetKernelInfoTest, InvalidSizeZero) {
38+
ol_program_handle_t Program;
39+
ASSERT_ERROR(OL_ERRC_INVALID_SIZE,
40+
olGetKernelInfo(Kernel, OL_KERNEL_INFO_PROGRAM, 0, &Program));
41+
}
42+
43+
TEST_P(olGetKernelInfoTest, InvalidSizeSmall) {
44+
ol_program_handle_t Program;
45+
ASSERT_ERROR(OL_ERRC_INVALID_SIZE,
46+
olGetKernelInfo(Kernel, OL_KERNEL_INFO_PROGRAM,
47+
sizeof(Program) - 1, &Program));
48+
}
49+
50+
TEST_P(olGetKernelInfoTest, InvalidNullPointerPropValue) {
51+
ol_program_handle_t Program;
52+
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
53+
olGetKernelInfo(Kernel, OL_KERNEL_INFO_PROGRAM, sizeof(Program),
54+
nullptr));
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===------- Offload API tests - olGetKernelInfoSize ----------------------===//
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 <OffloadAPI.h>
10+
11+
#include "../common/Fixtures.hpp"
12+
13+
using olGetKernelInfoSizeTest = OffloadKernelTest;
14+
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olGetKernelInfoSizeTest);
15+
16+
TEST_P(olGetKernelInfoSizeTest, SuccessProgram) {
17+
size_t Size = 0;
18+
ASSERT_SUCCESS(olGetKernelInfoSize(Kernel, OL_KERNEL_INFO_PROGRAM, &Size));
19+
ASSERT_EQ(Size, sizeof(ol_program_handle_t));
20+
}

0 commit comments

Comments
 (0)