Skip to content

Commit a1295ba

Browse files
authored
Merge pull request #1791 from aarongreig/aaron/makeKernelNativeProgramOpt
Make urKernelCreateWithNativeHandle's program param optional.
2 parents 8d8eb06 + edfa3ff commit a1295ba

File tree

12 files changed

+69
-24
lines changed

12 files changed

+69
-24
lines changed

include/ur_api.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5278,6 +5278,8 @@ typedef struct ur_kernel_native_properties_t {
52785278
/// - The application may call this function from simultaneous threads for
52795279
/// the same context.
52805280
/// - The implementation of this function should be thread-safe.
5281+
/// - The implementation may require a valid program handle to return the
5282+
/// native kernel handle
52815283
///
52825284
/// @returns
52835285
/// - ::UR_RESULT_SUCCESS
@@ -5286,7 +5288,7 @@ typedef struct ur_kernel_native_properties_t {
52865288
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
52875289
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
52885290
/// + `NULL == hContext`
5289-
/// + `NULL == hProgram`
5291+
/// + If `hProgram == NULL` and the implementation requires a valid program.
52905292
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
52915293
/// + `NULL == phKernel`
52925294
/// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE
@@ -5295,7 +5297,7 @@ UR_APIEXPORT ur_result_t UR_APICALL
52955297
urKernelCreateWithNativeHandle(
52965298
ur_native_handle_t hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
52975299
ur_context_handle_t hContext, ///< [in] handle of the context object
5298-
ur_program_handle_t hProgram, ///< [in] handle of the program associated with the kernel
5300+
ur_program_handle_t hProgram, ///< [in][optional] handle of the program associated with the kernel
52995301
const ur_kernel_native_properties_t *pProperties, ///< [in][optional] pointer to native kernel properties struct
53005302
ur_kernel_handle_t *phKernel ///< [out] pointer to the handle of the kernel object created.
53015303
);

scripts/core/kernel.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ details:
513513
- "Creates runtime kernel handle from native driver kernel handle."
514514
- "The application may call this function from simultaneous threads for the same context."
515515
- "The implementation of this function should be thread-safe."
516+
- "The implementation may require a valid program handle to return the native kernel handle"
516517
params:
517518
- type: $x_native_handle_t
518519
name: hNativeKernel
@@ -523,7 +524,7 @@ params:
523524
desc: "[in] handle of the context object"
524525
- type: $x_program_handle_t
525526
name: hProgram
526-
desc: "[in] handle of the program associated with the kernel"
527+
desc: "[in][optional] handle of the program associated with the kernel"
527528
- type: "const $x_kernel_native_properties_t*"
528529
name: pProperties
529530
desc: "[in][optional] pointer to native kernel properties struct"
@@ -534,6 +535,8 @@ params:
534535
returns:
535536
- $X_RESULT_ERROR_UNSUPPORTED_FEATURE:
536537
- "If the adapter has no underlying equivalent handle."
538+
- $X_RESULT_ERROR_INVALID_NULL_HANDLE:
539+
- "If `hProgram == NULL` and the implementation requires a valid program."
537540
--- #--------------------------------------------------------------------------
538541
type: function
539542
desc: "Get the suggested local work size for a kernel."

source/adapters/level_zero/kernel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
11421142
ur_kernel_handle_t *
11431143
RetKernel ///< [out] pointer to the handle of the kernel object created.
11441144
) {
1145+
if (!Program) {
1146+
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
1147+
}
11451148
ze_kernel_handle_t ZeKernel = ur_cast<ze_kernel_handle_t>(NativeKernel);
11461149
ur_kernel_handle_t_ *Kernel = nullptr;
11471150
try {

source/adapters/mock/ur_mockddi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4489,7 +4489,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
44894489
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
44904490
ur_context_handle_t hContext, ///< [in] handle of the context object
44914491
ur_program_handle_t
4492-
hProgram, ///< [in] handle of the program associated with the kernel
4492+
hProgram, ///< [in][optional] handle of the program associated with the kernel
44934493
const ur_kernel_native_properties_t *
44944494
pProperties, ///< [in][optional] pointer to native kernel properties struct
44954495
ur_kernel_handle_t

source/loader/layers/tracing/ur_trcddi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3428,7 +3428,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
34283428
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
34293429
ur_context_handle_t hContext, ///< [in] handle of the context object
34303430
ur_program_handle_t
3431-
hProgram, ///< [in] handle of the program associated with the kernel
3431+
hProgram, ///< [in][optional] handle of the program associated with the kernel
34323432
const ur_kernel_native_properties_t *
34333433
pProperties, ///< [in][optional] pointer to native kernel properties struct
34343434
ur_kernel_handle_t

source/loader/layers/validation/ur_valddi.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,7 +3855,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
38553855
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
38563856
ur_context_handle_t hContext, ///< [in] handle of the context object
38573857
ur_program_handle_t
3858-
hProgram, ///< [in] handle of the program associated with the kernel
3858+
hProgram, ///< [in][optional] handle of the program associated with the kernel
38593859
const ur_kernel_native_properties_t *
38603860
pProperties, ///< [in][optional] pointer to native kernel properties struct
38613861
ur_kernel_handle_t
@@ -3873,10 +3873,6 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
38733873
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
38743874
}
38753875

3876-
if (NULL == hProgram) {
3877-
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
3878-
}
3879-
38803876
if (NULL == phKernel) {
38813877
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
38823878
}

source/loader/ur_ldrddi.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
35923592
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
35933593
ur_context_handle_t hContext, ///< [in] handle of the context object
35943594
ur_program_handle_t
3595-
hProgram, ///< [in] handle of the program associated with the kernel
3595+
hProgram, ///< [in][optional] handle of the program associated with the kernel
35963596
const ur_kernel_native_properties_t *
35973597
pProperties, ///< [in][optional] pointer to native kernel properties struct
35983598
ur_kernel_handle_t
@@ -3614,7 +3614,9 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
36143614
hContext = reinterpret_cast<ur_context_object_t *>(hContext)->handle;
36153615

36163616
// convert loader handle to platform handle
3617-
hProgram = reinterpret_cast<ur_program_object_t *>(hProgram)->handle;
3617+
hProgram = (hProgram)
3618+
? reinterpret_cast<ur_program_object_t *>(hProgram)->handle
3619+
: nullptr;
36183620

36193621
// forward to device-platform
36203622
result = pfnCreateWithNativeHandle(hNativeKernel, hContext, hProgram,

source/loader/ur_libapi.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4139,6 +4139,8 @@ ur_result_t UR_APICALL urKernelGetNativeHandle(
41394139
/// - The application may call this function from simultaneous threads for
41404140
/// the same context.
41414141
/// - The implementation of this function should be thread-safe.
4142+
/// - The implementation may require a valid program handle to return the
4143+
/// native kernel handle
41424144
///
41434145
/// @returns
41444146
/// - ::UR_RESULT_SUCCESS
@@ -4147,7 +4149,7 @@ ur_result_t UR_APICALL urKernelGetNativeHandle(
41474149
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
41484150
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
41494151
/// + `NULL == hContext`
4150-
/// + `NULL == hProgram`
4152+
/// + If `hProgram == NULL` and the implementation requires a valid program.
41514153
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
41524154
/// + `NULL == phKernel`
41534155
/// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE
@@ -4157,7 +4159,7 @@ ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
41574159
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
41584160
ur_context_handle_t hContext, ///< [in] handle of the context object
41594161
ur_program_handle_t
4160-
hProgram, ///< [in] handle of the program associated with the kernel
4162+
hProgram, ///< [in][optional] handle of the program associated with the kernel
41614163
const ur_kernel_native_properties_t *
41624164
pProperties, ///< [in][optional] pointer to native kernel properties struct
41634165
ur_kernel_handle_t

source/ur_api.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,6 +3516,8 @@ ur_result_t UR_APICALL urKernelGetNativeHandle(
35163516
/// - The application may call this function from simultaneous threads for
35173517
/// the same context.
35183518
/// - The implementation of this function should be thread-safe.
3519+
/// - The implementation may require a valid program handle to return the
3520+
/// native kernel handle
35193521
///
35203522
/// @returns
35213523
/// - ::UR_RESULT_SUCCESS
@@ -3524,7 +3526,7 @@ ur_result_t UR_APICALL urKernelGetNativeHandle(
35243526
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
35253527
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
35263528
/// + `NULL == hContext`
3527-
/// + `NULL == hProgram`
3529+
/// + If `hProgram == NULL` and the implementation requires a valid program.
35283530
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
35293531
/// + `NULL == phKernel`
35303532
/// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE
@@ -3534,7 +3536,7 @@ ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
35343536
hNativeKernel, ///< [in][nocheck] the native handle of the kernel.
35353537
ur_context_handle_t hContext, ///< [in] handle of the context object
35363538
ur_program_handle_t
3537-
hProgram, ///< [in] handle of the program associated with the kernel
3539+
hProgram, ///< [in][optional] handle of the program associated with the kernel
35383540
const ur_kernel_native_properties_t *
35393541
pProperties, ///< [in][optional] pointer to native kernel properties struct
35403542
ur_kernel_handle_t

test/adapters/level_zero/urKernelCreateWithNativeHandle.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,46 @@ TEST_P(urLevelZeroKernelNativeHandleTest, OwnedHandleRelease) {
6161
ASSERT_SUCCESS(urKernelRelease(kernel));
6262
ASSERT_SUCCESS(urProgramRelease(program));
6363
}
64+
65+
TEST_P(urLevelZeroKernelNativeHandleTest, NullProgram) {
66+
ze_context_handle_t native_context;
67+
urContextGetNativeHandle(context, (ur_native_handle_t *)&native_context);
68+
69+
ze_device_handle_t native_device;
70+
urDeviceGetNativeHandle(device, (ur_native_handle_t *)&native_device);
71+
72+
std::shared_ptr<std::vector<char>> il_binary;
73+
uur::KernelsEnvironment::instance->LoadSource("foo", il_binary);
74+
75+
auto kernel_name =
76+
uur::KernelsEnvironment::instance->GetEntryPointNames("foo")[0];
77+
78+
ze_module_desc_t moduleDesc = {ZE_STRUCTURE_TYPE_MODULE_DESC};
79+
moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
80+
moduleDesc.inputSize = il_binary->size();
81+
moduleDesc.pInputModule =
82+
reinterpret_cast<const uint8_t *>(il_binary->data());
83+
moduleDesc.pBuildFlags = "";
84+
ze_module_handle_t module;
85+
86+
ASSERT_EQ(zeModuleCreate(native_context, native_device, &moduleDesc,
87+
&module, NULL),
88+
ZE_RESULT_SUCCESS);
89+
90+
ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC};
91+
kernelDesc.pKernelName = kernel_name.c_str();
92+
93+
ze_kernel_handle_t native_kernel;
94+
95+
ASSERT_EQ(zeKernelCreate(module, &kernelDesc, &native_kernel),
96+
ZE_RESULT_SUCCESS);
97+
98+
ur_kernel_native_properties_t kprops = {
99+
UR_STRUCTURE_TYPE_KERNEL_NATIVE_PROPERTIES, nullptr, 1};
100+
101+
ur_kernel_handle_t kernel;
102+
EXPECT_EQ(urKernelCreateWithNativeHandle((ur_native_handle_t)native_kernel,
103+
context, nullptr, &kprops,
104+
&kernel),
105+
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
106+
}

0 commit comments

Comments
 (0)