Skip to content

Commit 731a7d8

Browse files
committed
Make kernel handles a simple pointer, no create/destroy functions
1 parent e8e7423 commit 731a7d8

File tree

13 files changed

+59
-183
lines changed

13 files changed

+59
-183
lines changed

offload/liboffload/API/Common.td

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

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

8586
def : Enum {

offload/liboffload/API/Kernel.td

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,15 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
def : Function {
14-
let name = "olCreateKernel";
15-
let desc = "Create a kernel from the function identified by `KernelName` in the given program.";
16-
let details = [];
14+
let name = "olGetKernel";
15+
let desc = "Get a kernel from the function identified by `KernelName` in the given program.";
16+
let details = [
17+
"The kernel handle returned is owned by the device so does not need to be destroyed."
18+
];
1719
let params = [
1820
Param<"ol_program_handle_t", "Program", "handle of the program", PARAM_IN>,
1921
Param<"const char*", "KernelName", "name of the kernel entry point in the program", PARAM_IN>,
20-
Param<"ol_kernel_handle_t*", "Kernel", "output pointer for the created kernel", PARAM_OUT>
21-
];
22-
let returns = [];
23-
}
24-
25-
def : Function {
26-
let name = "olDestroyKernel";
27-
let desc = "Destroy the kernel and free all underlying resources.";
28-
let details = [];
29-
let params = [
30-
Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel", PARAM_IN>
22+
Param<"ol_kernel_handle_t*", "Kernel", "output pointer for the fetched kernel", PARAM_OUT>
3123
];
3224
let returns = [];
3325
}

offload/liboffload/include/generated/OffloadAPI.h

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ typedef struct ol_program_impl_t *ol_program_handle_t;
9999

100100
///////////////////////////////////////////////////////////////////////////////
101101
/// @brief Handle of kernel object
102-
typedef struct ol_kernel_impl_t *ol_kernel_handle_t;
102+
typedef void *ol_kernel_handle_t;
103103

104104
///////////////////////////////////////////////////////////////////////////////
105105
/// @brief Defines Return/Error codes
@@ -757,10 +757,12 @@ OL_APIEXPORT ol_result_t OL_APICALL olDestroyProgram(
757757
ol_program_handle_t Program);
758758

759759
///////////////////////////////////////////////////////////////////////////////
760-
/// @brief Create a kernel from the function identified by `KernelName` in the
760+
/// @brief Get a kernel from the function identified by `KernelName` in the
761761
/// given program.
762762
///
763763
/// @details
764+
/// - The kernel handle returned is owned by the device so does not need to
765+
/// be destroyed.
764766
///
765767
/// @returns
766768
/// - ::OL_RESULT_SUCCESS
@@ -771,30 +773,14 @@ OL_APIEXPORT ol_result_t OL_APICALL olDestroyProgram(
771773
/// - ::OL_ERRC_INVALID_NULL_POINTER
772774
/// + `NULL == KernelName`
773775
/// + `NULL == Kernel`
774-
OL_APIEXPORT ol_result_t OL_APICALL olCreateKernel(
776+
OL_APIEXPORT ol_result_t OL_APICALL olGetKernel(
775777
// [in] handle of the program
776778
ol_program_handle_t Program,
777779
// [in] name of the kernel entry point in the program
778780
const char *KernelName,
779-
// [out] output pointer for the created kernel
781+
// [out] output pointer for the fetched kernel
780782
ol_kernel_handle_t *Kernel);
781783

782-
///////////////////////////////////////////////////////////////////////////////
783-
/// @brief Destroy the kernel and free all underlying resources.
784-
///
785-
/// @details
786-
///
787-
/// @returns
788-
/// - ::OL_RESULT_SUCCESS
789-
/// - ::OL_ERRC_UNINITIALIZED
790-
/// - ::OL_ERRC_DEVICE_LOST
791-
/// - ::OL_ERRC_INVALID_NULL_HANDLE
792-
/// + `NULL == Kernel`
793-
/// - ::OL_ERRC_INVALID_NULL_POINTER
794-
OL_APIEXPORT ol_result_t OL_APICALL olDestroyKernel(
795-
// [in] handle of the kernel
796-
ol_kernel_handle_t Kernel);
797-
798784
///////////////////////////////////////////////////////////////////////////////
799785
/// @brief Function parameters for olGetPlatform
800786
/// @details Each entry is a pointer to the parameter passed to the function;
@@ -969,20 +955,13 @@ typedef struct ol_destroy_program_params_t {
969955
} ol_destroy_program_params_t;
970956

971957
///////////////////////////////////////////////////////////////////////////////
972-
/// @brief Function parameters for olCreateKernel
958+
/// @brief Function parameters for olGetKernel
973959
/// @details Each entry is a pointer to the parameter passed to the function;
974-
typedef struct ol_create_kernel_params_t {
960+
typedef struct ol_get_kernel_params_t {
975961
ol_program_handle_t *pProgram;
976962
const char **pKernelName;
977963
ol_kernel_handle_t **pKernel;
978-
} ol_create_kernel_params_t;
979-
980-
///////////////////////////////////////////////////////////////////////////////
981-
/// @brief Function parameters for olDestroyKernel
982-
/// @details Each entry is a pointer to the parameter passed to the function;
983-
typedef struct ol_destroy_kernel_params_t {
984-
ol_kernel_handle_t *pKernel;
985-
} ol_destroy_kernel_params_t;
964+
} ol_get_kernel_params_t;
986965

987966
///////////////////////////////////////////////////////////////////////////////
988967
/// @brief Variant of olInit that also sets source code location information
@@ -1149,20 +1128,13 @@ OL_APIEXPORT ol_result_t OL_APICALL olDestroyProgramWithCodeLoc(
11491128
ol_program_handle_t Program, ol_code_location_t *CodeLocation);
11501129

11511130
///////////////////////////////////////////////////////////////////////////////
1152-
/// @brief Variant of olCreateKernel that also sets source code location
1131+
/// @brief Variant of olGetKernel that also sets source code location
11531132
/// information
1154-
/// @details See also ::olCreateKernel
1155-
OL_APIEXPORT ol_result_t OL_APICALL olCreateKernelWithCodeLoc(
1133+
/// @details See also ::olGetKernel
1134+
OL_APIEXPORT ol_result_t OL_APICALL olGetKernelWithCodeLoc(
11561135
ol_program_handle_t Program, const char *KernelName,
11571136
ol_kernel_handle_t *Kernel, ol_code_location_t *CodeLocation);
11581137

1159-
///////////////////////////////////////////////////////////////////////////////
1160-
/// @brief Variant of olDestroyKernel that also sets source code location
1161-
/// information
1162-
/// @details See also ::olDestroyKernel
1163-
OL_APIEXPORT ol_result_t OL_APICALL olDestroyKernelWithCodeLoc(
1164-
ol_kernel_handle_t Kernel, ol_code_location_t *CodeLocation);
1165-
11661138
#if defined(__cplusplus)
11671139
} // extern "C"
11681140
#endif

offload/liboffload/include/generated/OffloadEntryPoints.inc

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,9 @@ ol_result_t olDestroyProgramWithCodeLoc(ol_program_handle_t Program,
983983
}
984984

985985
///////////////////////////////////////////////////////////////////////////////
986-
ol_impl_result_t olCreateKernel_val(ol_program_handle_t Program,
987-
const char *KernelName,
988-
ol_kernel_handle_t *Kernel) {
986+
ol_impl_result_t olGetKernel_val(ol_program_handle_t Program,
987+
const char *KernelName,
988+
ol_kernel_handle_t *Kernel) {
989989
if (offloadConfig().ValidationEnabled) {
990990
if (NULL == Program) {
991991
return OL_ERRC_INVALID_NULL_HANDLE;
@@ -1000,19 +1000,19 @@ ol_impl_result_t olCreateKernel_val(ol_program_handle_t Program,
10001000
}
10011001
}
10021002

1003-
return llvm::offload::olCreateKernel_impl(Program, KernelName, Kernel);
1003+
return llvm::offload::olGetKernel_impl(Program, KernelName, Kernel);
10041004
}
1005-
OL_APIEXPORT ol_result_t OL_APICALL olCreateKernel(ol_program_handle_t Program,
1006-
const char *KernelName,
1007-
ol_kernel_handle_t *Kernel) {
1005+
OL_APIEXPORT ol_result_t OL_APICALL olGetKernel(ol_program_handle_t Program,
1006+
const char *KernelName,
1007+
ol_kernel_handle_t *Kernel) {
10081008
if (offloadConfig().TracingEnabled) {
1009-
llvm::errs() << "---> olCreateKernel";
1009+
llvm::errs() << "---> olGetKernel";
10101010
}
10111011

1012-
ol_result_t Result = olCreateKernel_val(Program, KernelName, Kernel);
1012+
ol_result_t Result = olGetKernel_val(Program, KernelName, Kernel);
10131013

10141014
if (offloadConfig().TracingEnabled) {
1015-
ol_create_kernel_params_t Params = {&Program, &KernelName, &Kernel};
1015+
ol_get_kernel_params_t Params = {&Program, &KernelName, &Kernel};
10161016
llvm::errs() << "(" << &Params << ")";
10171017
llvm::errs() << "-> " << Result << "\n";
10181018
if (Result && Result->Details) {
@@ -1021,48 +1021,12 @@ OL_APIEXPORT ol_result_t OL_APICALL olCreateKernel(ol_program_handle_t Program,
10211021
}
10221022
return Result;
10231023
}
1024-
ol_result_t olCreateKernelWithCodeLoc(ol_program_handle_t Program,
1025-
const char *KernelName,
1026-
ol_kernel_handle_t *Kernel,
1027-
ol_code_location_t *CodeLocation) {
1028-
currentCodeLocation() = CodeLocation;
1029-
ol_result_t Result = ::olCreateKernel(Program, KernelName, Kernel);
1030-
1031-
currentCodeLocation() = nullptr;
1032-
return Result;
1033-
}
1034-
1035-
///////////////////////////////////////////////////////////////////////////////
1036-
ol_impl_result_t olDestroyKernel_val(ol_kernel_handle_t Kernel) {
1037-
if (offloadConfig().ValidationEnabled) {
1038-
if (NULL == Kernel) {
1039-
return OL_ERRC_INVALID_NULL_HANDLE;
1040-
}
1041-
}
1042-
1043-
return llvm::offload::olDestroyKernel_impl(Kernel);
1044-
}
1045-
OL_APIEXPORT ol_result_t OL_APICALL olDestroyKernel(ol_kernel_handle_t Kernel) {
1046-
if (offloadConfig().TracingEnabled) {
1047-
llvm::errs() << "---> olDestroyKernel";
1048-
}
1049-
1050-
ol_result_t Result = olDestroyKernel_val(Kernel);
1051-
1052-
if (offloadConfig().TracingEnabled) {
1053-
ol_destroy_kernel_params_t Params = {&Kernel};
1054-
llvm::errs() << "(" << &Params << ")";
1055-
llvm::errs() << "-> " << Result << "\n";
1056-
if (Result && Result->Details) {
1057-
llvm::errs() << " *Error Details* " << Result->Details << " \n";
1058-
}
1059-
}
1060-
return Result;
1061-
}
1062-
ol_result_t olDestroyKernelWithCodeLoc(ol_kernel_handle_t Kernel,
1063-
ol_code_location_t *CodeLocation) {
1024+
ol_result_t olGetKernelWithCodeLoc(ol_program_handle_t Program,
1025+
const char *KernelName,
1026+
ol_kernel_handle_t *Kernel,
1027+
ol_code_location_t *CodeLocation) {
10641028
currentCodeLocation() = CodeLocation;
1065-
ol_result_t Result = ::olDestroyKernel(Kernel);
1029+
ol_result_t Result = ::olGetKernel(Program, KernelName, Kernel);
10661030

10671031
currentCodeLocation() = nullptr;
10681032
return Result;

offload/liboffload/include/generated/OffloadFuncs.inc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ OFFLOAD_FUNC(olMemcpy)
3232
OFFLOAD_FUNC(olLaunchKernel)
3333
OFFLOAD_FUNC(olCreateProgram)
3434
OFFLOAD_FUNC(olDestroyProgram)
35-
OFFLOAD_FUNC(olCreateKernel)
36-
OFFLOAD_FUNC(olDestroyKernel)
35+
OFFLOAD_FUNC(olGetKernel)
3736
OFFLOAD_FUNC(olInitWithCodeLoc)
3837
OFFLOAD_FUNC(olShutDownWithCodeLoc)
3938
OFFLOAD_FUNC(olGetPlatformWithCodeLoc)
@@ -56,7 +55,6 @@ OFFLOAD_FUNC(olMemcpyWithCodeLoc)
5655
OFFLOAD_FUNC(olLaunchKernelWithCodeLoc)
5756
OFFLOAD_FUNC(olCreateProgramWithCodeLoc)
5857
OFFLOAD_FUNC(olDestroyProgramWithCodeLoc)
59-
OFFLOAD_FUNC(olCreateKernelWithCodeLoc)
60-
OFFLOAD_FUNC(olDestroyKernelWithCodeLoc)
58+
OFFLOAD_FUNC(olGetKernelWithCodeLoc)
6159

6260
#undef OFFLOAD_FUNC

offload/liboffload/include/generated/OffloadImplFuncDecls.inc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ ol_impl_result_t olCreateProgram_impl(ol_device_handle_t Device,
7474

7575
ol_impl_result_t olDestroyProgram_impl(ol_program_handle_t Program);
7676

77-
ol_impl_result_t olCreateKernel_impl(ol_program_handle_t Program,
78-
const char *KernelName,
79-
ol_kernel_handle_t *Kernel);
80-
81-
ol_impl_result_t olDestroyKernel_impl(ol_kernel_handle_t Kernel);
77+
ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,
78+
const char *KernelName,
79+
ol_kernel_handle_t *Kernel);

offload/liboffload/include/generated/OffloadPrint.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ template <> struct is_handle<ol_context_handle_t> : std::true_type {};
2525
template <> struct is_handle<ol_queue_handle_t> : std::true_type {};
2626
template <> struct is_handle<ol_event_handle_t> : std::true_type {};
2727
template <> struct is_handle<ol_program_handle_t> : std::true_type {};
28-
template <> struct is_handle<ol_kernel_handle_t> : std::true_type {};
2928
template <typename T> inline constexpr bool is_handle_v = is_handle<T>::value;
3029

3130
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
@@ -635,8 +634,7 @@ operator<<(llvm::raw_ostream &os,
635634
}
636635

637636
inline llvm::raw_ostream &
638-
operator<<(llvm::raw_ostream &os,
639-
const struct ol_create_kernel_params_t *params) {
637+
operator<<(llvm::raw_ostream &os, const struct ol_get_kernel_params_t *params) {
640638
os << ".Program = ";
641639
printPtr(os, *params->pProgram);
642640
os << ", ";
@@ -648,14 +646,6 @@ operator<<(llvm::raw_ostream &os,
648646
return os;
649647
}
650648

651-
inline llvm::raw_ostream &
652-
operator<<(llvm::raw_ostream &os,
653-
const struct ol_destroy_kernel_params_t *params) {
654-
os << ".Kernel = ";
655-
printPtr(os, *params->pKernel);
656-
return os;
657-
}
658-
659649
///////////////////////////////////////////////////////////////////////////////
660650
// @brief Print pointer value
661651
template <typename T>

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ struct ol_program_impl_t {
8383
__tgt_device_image DeviceImage;
8484
};
8585

86-
struct ol_kernel_impl_t {
87-
GenericKernelTy *KernelImpl;
88-
};
89-
9086
namespace llvm {
9187
namespace offload {
9288

@@ -490,9 +486,9 @@ ol_impl_result_t olDestroyProgram_impl(ol_program_handle_t Program) {
490486
return olDestroy(Program);
491487
}
492488

493-
ol_impl_result_t olCreateKernel_impl(ol_program_handle_t Program,
494-
const char *KernelName,
495-
ol_kernel_handle_t *Kernel) {
489+
ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,
490+
const char *KernelName,
491+
ol_kernel_handle_t *Kernel) {
496492

497493
auto &Device = Program->Image->getDevice();
498494
auto KernelImpl = Device.constructKernel(KernelName);
@@ -503,17 +499,11 @@ ol_impl_result_t olCreateKernel_impl(ol_program_handle_t Program,
503499
if (Err)
504500
return {OL_ERRC_UNKNOWN, "Could not initialize the kernel"};
505501

506-
ol_kernel_handle_t CreatedKernel = new ol_kernel_impl_t();
507-
CreatedKernel->KernelImpl = &*KernelImpl;
508-
*Kernel = CreatedKernel;
502+
*Kernel = &*KernelImpl;
509503

510504
return OL_SUCCESS;
511505
}
512506

513-
ol_impl_result_t olDestroyKernel_impl(ol_kernel_handle_t Kernel) {
514-
return olDestroy(Kernel);
515-
}
516-
517507
ol_impl_result_t
518508
olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
519509
ol_kernel_handle_t Kernel, const void *ArgumentsData,
@@ -543,8 +533,9 @@ olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
543533
// Don't do anything with pointer indirection; use arg data as-is
544534
LaunchArgs.Flags.IsCUDA = true;
545535

546-
auto Err = Kernel->KernelImpl->launch(*DeviceImpl, LaunchArgs.ArgPtrs,
547-
nullptr, LaunchArgs, AsyncInfoWrapper);
536+
auto *KernelImpl = reinterpret_cast<GenericKernelTy *>(Kernel);
537+
auto Err = KernelImpl->launch(*DeviceImpl, LaunchArgs.ArgPtrs, nullptr,
538+
LaunchArgs, AsyncInfoWrapper);
548539

549540
AsyncInfoWrapper.finalize(Err);
550541
if (Err)

offload/unittests/OffloadAPI/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ add_libompt_unittest("offload.unittests"
2222
${CMAKE_CURRENT_SOURCE_DIR}/memory/olMemcpy.cpp
2323
${CMAKE_CURRENT_SOURCE_DIR}/program/olCreateProgram.cpp
2424
${CMAKE_CURRENT_SOURCE_DIR}/program/olDestroyProgram.cpp
25-
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olCreateKernel.cpp
26-
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olDestroyKernel.cpp
25+
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olGetKernel.cpp
2726
${CMAKE_CURRENT_SOURCE_DIR}/kernel/olLaunchKernel.cpp
2827
)
2928
add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON} LibomptUnitTestsDeviceBins)

offload/unittests/OffloadAPI/common/Fixtures.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,10 @@ struct OffloadProgramTest : OffloadDeviceTest {
9696
struct OffloadKernelTest : OffloadProgramTest {
9797
void SetUp() override {
9898
RETURN_ON_FATAL_FAILURE(OffloadProgramTest::SetUp());
99-
ASSERT_SUCCESS(olCreateKernel(Program, "foo", &Kernel));
99+
ASSERT_SUCCESS(olGetKernel(Program, "foo", &Kernel));
100100
}
101101

102102
void TearDown() override {
103-
if (Kernel) {
104-
olDestroyKernel(Kernel);
105-
}
106103
RETURN_ON_FATAL_FAILURE(OffloadProgramTest::TearDown());
107104
}
108105

0 commit comments

Comments
 (0)