Skip to content

Commit 0fc432c

Browse files
committed
[SPEC] Add urProgramGetGlobalVariablePointer entrypoint
1 parent c63ad9b commit 0fc432c

30 files changed

+601
-10
lines changed

include/ur_api.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ typedef enum ur_function_t {
215215
UR_FUNCTION_COMMAND_BUFFER_APPEND_USM_ADVISE_EXP = 213, ///< Enumerator for ::urCommandBufferAppendUSMAdviseExp
216216
UR_FUNCTION_ENQUEUE_COOPERATIVE_KERNEL_LAUNCH_EXP = 214, ///< Enumerator for ::urEnqueueCooperativeKernelLaunchExp
217217
UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP = 215, ///< Enumerator for ::urKernelSuggestMaxCooperativeGroupCountExp
218+
UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER = 216, ///< Enumerator for ::urProgramGetGlobalVariablePointer
218219
/// @cond
219220
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
220221
/// @endcond
@@ -4262,6 +4263,42 @@ urProgramGetFunctionPointer(
42624263
void **ppFunctionPointer ///< [out] Returns the pointer to the function if it is found in the program.
42634264
);
42644265

4266+
///////////////////////////////////////////////////////////////////////////////
4267+
/// @brief Retrieves a pointer to a device global variable.
4268+
///
4269+
/// @details
4270+
/// - Retrieves a pointer to a device global variable.
4271+
/// - The application may call this function from simultaneous threads for
4272+
/// the same device.
4273+
/// - The implementation of this function should be thread-safe.
4274+
///
4275+
/// @remarks
4276+
/// _Analogues_
4277+
/// - **clGetDeviceGlobalVariablePointerINTEL**
4278+
///
4279+
/// @returns
4280+
/// - ::UR_RESULT_SUCCESS
4281+
/// - ::UR_RESULT_ERROR_UNINITIALIZED
4282+
/// - ::UR_RESULT_ERROR_DEVICE_LOST
4283+
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
4284+
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
4285+
/// + `NULL == hDevice`
4286+
/// + `NULL == hProgram`
4287+
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
4288+
/// + `NULL == pGlobalVariableName`
4289+
/// + `NULL == ppGlobalVariablePointerRet`
4290+
/// - ::UR_RESULT_ERROR_INVALID_SIZE
4291+
/// + `name` is not a valid variable in the program.
4292+
UR_APIEXPORT ur_result_t UR_APICALL
4293+
urProgramGetGlobalVariablePointer(
4294+
ur_device_handle_t hDevice, ///< [in] handle of the device to retrieve the pointer for.
4295+
ur_program_handle_t hProgram, ///< [in] handle of the program where the global variable is.
4296+
const char *pGlobalVariableName, ///< [in] mangled name of the global variable to retrieve the pointer for.
4297+
size_t *pGlobalVariableSizeRet, ///< [out][optional] Returns the size of the global variable if it is found
4298+
///< in the program.
4299+
void **ppGlobalVariablePointerRet ///< [out] Returns the pointer to the global variable if it is found in the program.
4300+
);
4301+
42654302
///////////////////////////////////////////////////////////////////////////////
42664303
/// @brief Get Program object information
42674304
typedef enum ur_program_info_t {
@@ -9144,6 +9181,18 @@ typedef struct ur_program_get_function_pointer_params_t {
91449181
void ***pppFunctionPointer;
91459182
} ur_program_get_function_pointer_params_t;
91469183

9184+
///////////////////////////////////////////////////////////////////////////////
9185+
/// @brief Function parameters for urProgramGetGlobalVariablePointer
9186+
/// @details Each entry is a pointer to the parameter passed to the function;
9187+
/// allowing the callback the ability to modify the parameter's value
9188+
typedef struct ur_program_get_global_variable_pointer_params_t {
9189+
ur_device_handle_t *phDevice;
9190+
ur_program_handle_t *phProgram;
9191+
const char **ppGlobalVariableName;
9192+
size_t **ppGlobalVariableSizeRet;
9193+
void ***pppGlobalVariablePointerRet;
9194+
} ur_program_get_global_variable_pointer_params_t;
9195+
91479196
///////////////////////////////////////////////////////////////////////////////
91489197
/// @brief Function parameters for urProgramGetInfo
91499198
/// @details Each entry is a pointer to the parameter passed to the function;

include/ur_ddi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ typedef ur_result_t(UR_APICALL *ur_pfnProgramGetFunctionPointer_t)(
329329
const char *,
330330
void **);
331331

332+
///////////////////////////////////////////////////////////////////////////////
333+
/// @brief Function-pointer for urProgramGetGlobalVariablePointer
334+
typedef ur_result_t(UR_APICALL *ur_pfnProgramGetGlobalVariablePointer_t)(
335+
ur_device_handle_t,
336+
ur_program_handle_t,
337+
const char *,
338+
size_t *,
339+
void **);
340+
332341
///////////////////////////////////////////////////////////////////////////////
333342
/// @brief Function-pointer for urProgramGetInfo
334343
typedef ur_result_t(UR_APICALL *ur_pfnProgramGetInfo_t)(
@@ -380,6 +389,7 @@ typedef struct ur_program_dditable_t {
380389
ur_pfnProgramRetain_t pfnRetain;
381390
ur_pfnProgramRelease_t pfnRelease;
382391
ur_pfnProgramGetFunctionPointer_t pfnGetFunctionPointer;
392+
ur_pfnProgramGetGlobalVariablePointer_t pfnGetGlobalVariablePointer;
383393
ur_pfnProgramGetInfo_t pfnGetInfo;
384394
ur_pfnProgramGetBuildInfo_t pfnGetBuildInfo;
385395
ur_pfnProgramSetSpecializationConstants_t pfnSetSpecializationConstants;

include/ur_print.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ inline std::ostream &operator<<(std::ostream &os, ur_function_t value) {
879879
case UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP:
880880
os << "UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP";
881881
break;
882+
case UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER:
883+
os << "UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER";
884+
break;
882885
default:
883886
os << "unknown enumerator";
884887
break;
@@ -10257,6 +10260,44 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1025710260
return os;
1025810261
}
1025910262

10263+
///////////////////////////////////////////////////////////////////////////////
10264+
/// @brief Print operator for the ur_program_get_global_variable_pointer_params_t type
10265+
/// @returns
10266+
/// std::ostream &
10267+
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_program_get_global_variable_pointer_params_t *params) {
10268+
10269+
os << ".hDevice = ";
10270+
10271+
ur::details::printPtr(os,
10272+
*(params->phDevice));
10273+
10274+
os << ", ";
10275+
os << ".hProgram = ";
10276+
10277+
ur::details::printPtr(os,
10278+
*(params->phProgram));
10279+
10280+
os << ", ";
10281+
os << ".pGlobalVariableName = ";
10282+
10283+
ur::details::printPtr(os,
10284+
*(params->ppGlobalVariableName));
10285+
10286+
os << ", ";
10287+
os << ".pGlobalVariableSizeRet = ";
10288+
10289+
ur::details::printPtr(os,
10290+
*(params->ppGlobalVariableSizeRet));
10291+
10292+
os << ", ";
10293+
os << ".ppGlobalVariablePointerRet = ";
10294+
10295+
ur::details::printPtr(os,
10296+
*(params->pppGlobalVariablePointerRet));
10297+
10298+
return os;
10299+
}
10300+
1026010301
///////////////////////////////////////////////////////////////////////////////
1026110302
/// @brief Print operator for the ur_program_get_info_params_t type
1026210303
/// @returns
@@ -15992,6 +16033,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
1599216033
case UR_FUNCTION_PROGRAM_GET_FUNCTION_POINTER: {
1599316034
os << (const struct ur_program_get_function_pointer_params_t *)params;
1599416035
} break;
16036+
case UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER: {
16037+
os << (const struct ur_program_get_global_variable_pointer_params_t *)params;
16038+
} break;
1599516039
case UR_FUNCTION_PROGRAM_GET_INFO: {
1599616040
os << (const struct ur_program_get_info_params_t *)params;
1599716041
} break;

scripts/core/program.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,43 @@ params:
311311
desc: |
312312
[out] Returns the pointer to the function if it is found in the program.
313313
--- #--------------------------------------------------------------------------
314+
type: function
315+
desc: "Retrieves a pointer to a device global variable."
316+
class: $xProgram
317+
name: GetGlobalVariablePointer
318+
decl: static
319+
ordinal: "7"
320+
analogue:
321+
- "**clGetDeviceGlobalVariablePointerINTEL**"
322+
details:
323+
- "Retrieves a pointer to a device global variable."
324+
- "The application may call this function from simultaneous threads for the same device."
325+
- "The implementation of this function should be thread-safe."
326+
params:
327+
- type: "$x_device_handle_t"
328+
name: hDevice
329+
desc: |
330+
[in] handle of the device to retrieve the pointer for.
331+
- type: "$x_program_handle_t"
332+
name: hProgram
333+
desc: |
334+
[in] handle of the program where the global variable is.
335+
- type: "const char*"
336+
name: pGlobalVariableName
337+
desc: |
338+
[in] mangled name of the global variable to retrieve the pointer for.
339+
- type: "size_t*"
340+
name: pGlobalVariableSizeRet
341+
desc: |
342+
[out][optional] Returns the size of the global variable if it is found in the program.
343+
- type: "void**"
344+
name: ppGlobalVariablePointerRet
345+
desc: |
346+
[out] Returns the pointer to the global variable if it is found in the program.
347+
returns:
348+
- $X_RESULT_ERROR_INVALID_SIZE:
349+
- "`name` is not a valid variable in the program."
350+
--- #--------------------------------------------------------------------------
314351
type: enum
315352
desc: "Get Program object information"
316353
class: $xProgram

scripts/core/registry.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ etors:
559559
- name: KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP
560560
desc: Enumerator for $xKernelSuggestMaxCooperativeGroupCountExp
561561
value: '215'
562+
- name: PROGRAM_GET_GLOBAL_VARIABLE_POINTER
563+
desc: Enumerator for $xProgramGetGlobalVariablePointer
564+
value: '216'
562565
---
563566
type: enum
564567
desc: Defines structure types

source/adapters/cuda/program.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,34 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
489489

490490
return Result;
491491
}
492+
493+
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
494+
ur_device_handle_t, ur_program_handle_t hProgram,
495+
const char *pGlobalVariableName, size_t *pGlobalVariableSizeRet,
496+
void **ppGlobalVariablePointerRet) {
497+
498+
/* Since CUDA requires a global variable to be referenced by name, we use
499+
* metadata to find the correct name to access it by. */
500+
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(pGlobalVariableName);
501+
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
502+
return UR_RESULT_ERROR_INVALID_VALUE;
503+
std::string DeviceGlobalName = DeviceGlobalNameIt->second;
504+
505+
ur_result_t Result = UR_RESULT_SUCCESS;
506+
try {
507+
CUdeviceptr DeviceGlobal = 0;
508+
size_t DeviceGlobalSize = 0;
509+
UR_CHECK_ERROR(cuModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
510+
hProgram->get(),
511+
DeviceGlobalName.c_str()));
512+
513+
if (pGlobalVariableSizeRet) {
514+
*pGlobalVariableSizeRet = DeviceGlobalSize;
515+
}
516+
*ppGlobalVariablePointerRet = reinterpret_cast<void *>(DeviceGlobal);
517+
518+
} catch (ur_result_t Err) {
519+
Result = Err;
520+
}
521+
return Result;
522+
}

source/adapters/cuda/ur_interface_loader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable(
9393
pDdiTable->pfnCreateWithNativeHandle = urProgramCreateWithNativeHandle;
9494
pDdiTable->pfnGetBuildInfo = urProgramGetBuildInfo;
9595
pDdiTable->pfnGetFunctionPointer = urProgramGetFunctionPointer;
96+
pDdiTable->pfnGetGlobalVariablePointer = urProgramGetGlobalVariablePointer;
9697
pDdiTable->pfnGetInfo = urProgramGetInfo;
9798
pDdiTable->pfnGetNativeHandle = urProgramGetNativeHandle;
9899
pDdiTable->pfnLink = urProgramLink;

source/adapters/hip/program.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
495495

496496
return Result;
497497
}
498+
499+
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
500+
ur_device_handle_t, ur_program_handle_t, const char *, size_t *, void **) {
501+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
502+
}

source/adapters/hip/ur_interface_loader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable(
9393
pDdiTable->pfnCreateWithNativeHandle = urProgramCreateWithNativeHandle;
9494
pDdiTable->pfnGetBuildInfo = urProgramGetBuildInfo;
9595
pDdiTable->pfnGetFunctionPointer = urProgramGetFunctionPointer;
96+
pDdiTable->pfnGetGlobalVariablePointer = urProgramGetGlobalVariablePointer;
9697
pDdiTable->pfnGetInfo = urProgramGetInfo;
9798
pDdiTable->pfnGetNativeHandle = urProgramGetNativeHandle;
9899
pDdiTable->pfnLink = urProgramLink;

source/adapters/level_zero/program.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
558558
return ze2urResult(ZeResult);
559559
}
560560

561+
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
562+
ur_device_handle_t, ur_program_handle_t, const char *, size_t *, void **) {
563+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
564+
}
565+
561566
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetInfo(
562567
ur_program_handle_t Program, ///< [in] handle of the Program object
563568
ur_program_info_t PropName, ///< [in] name of the Program property to query

source/adapters/level_zero/ur_interface_loader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable(
215215
pDdiTable->pfnRetain = urProgramRetain;
216216
pDdiTable->pfnRelease = urProgramRelease;
217217
pDdiTable->pfnGetFunctionPointer = urProgramGetFunctionPointer;
218+
pDdiTable->pfnGetGlobalVariablePointer = urProgramGetGlobalVariablePointer;
218219
pDdiTable->pfnGetInfo = urProgramGetInfo;
219220
pDdiTable->pfnGetBuildInfo = urProgramGetBuildInfo;
220221
pDdiTable->pfnSetSpecializationConstants =

source/adapters/null/ur_nullddi.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,39 @@ __urdlllocal ur_result_t UR_APICALL urProgramGetFunctionPointer(
18721872
return exceptionToResult(std::current_exception());
18731873
}
18741874

1875+
///////////////////////////////////////////////////////////////////////////////
1876+
/// @brief Intercept function for urProgramGetGlobalVariablePointer
1877+
__urdlllocal ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
1878+
ur_device_handle_t
1879+
hDevice, ///< [in] handle of the device to retrieve the pointer for.
1880+
ur_program_handle_t
1881+
hProgram, ///< [in] handle of the program where the global variable is.
1882+
const char *
1883+
pGlobalVariableName, ///< [in] mangled name of the global variable to retrieve the pointer for.
1884+
size_t *
1885+
pGlobalVariableSizeRet, ///< [out][optional] Returns the size of the global variable if it is found
1886+
///< in the program.
1887+
void **
1888+
ppGlobalVariablePointerRet ///< [out] Returns the pointer to the global variable if it is found in the program.
1889+
) try {
1890+
ur_result_t result = UR_RESULT_SUCCESS;
1891+
1892+
// if the driver has created a custom function, then call it instead of using the generic path
1893+
auto pfnGetGlobalVariablePointer =
1894+
d_context.urDdiTable.Program.pfnGetGlobalVariablePointer;
1895+
if (nullptr != pfnGetGlobalVariablePointer) {
1896+
result = pfnGetGlobalVariablePointer(
1897+
hDevice, hProgram, pGlobalVariableName, pGlobalVariableSizeRet,
1898+
ppGlobalVariablePointerRet);
1899+
} else {
1900+
// generic implementation
1901+
}
1902+
1903+
return result;
1904+
} catch (...) {
1905+
return exceptionToResult(std::current_exception());
1906+
}
1907+
18751908
///////////////////////////////////////////////////////////////////////////////
18761909
/// @brief Intercept function for urProgramGetInfo
18771910
__urdlllocal ur_result_t UR_APICALL urProgramGetInfo(
@@ -5941,6 +5974,9 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable(
59415974

59425975
pDdiTable->pfnGetFunctionPointer = driver::urProgramGetFunctionPointer;
59435976

5977+
pDdiTable->pfnGetGlobalVariablePointer =
5978+
driver::urProgramGetGlobalVariablePointer;
5979+
59445980
pDdiTable->pfnGetInfo = driver::urProgramGetInfo;
59455981

59465982
pDdiTable->pfnGetBuildInfo = driver::urProgramGetBuildInfo;

source/adapters/opencl/common.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* error is mapped to UR
4040
*/
4141
#define CL_RETURN_ON_FAILURE_AND_SET_NULL(clCall, outPtr) \
42-
if (const cl_int cl_result_macro = clCall != CL_SUCCESS) { \
42+
if (const cl_int cl_result_macro = clCall; cl_result_macro != CL_SUCCESS) { \
4343
if (outPtr != nullptr) { \
4444
*outPtr = nullptr; \
4545
} \
@@ -197,6 +197,8 @@ CONSTFIX char SetProgramSpecializationConstantName[] =
197197
"clSetProgramSpecializationConstant";
198198
CONSTFIX char GetDeviceFunctionPointerName[] =
199199
"clGetDeviceFunctionPointerINTEL";
200+
CONSTFIX char GetDeviceGlobalVariablePointerName[] =
201+
"clGetDeviceGlobalVariablePointerINTEL";
200202
CONSTFIX char EnqueueWriteGlobalVariableName[] =
201203
"clEnqueueWriteGlobalVariableINTEL";
202204
CONSTFIX char EnqueueReadGlobalVariableName[] =
@@ -221,6 +223,10 @@ using clGetDeviceFunctionPointer_fn = CL_API_ENTRY
221223
cl_int(CL_API_CALL *)(cl_device_id device, cl_program program,
222224
const char *FuncName, cl_ulong *ret_ptr);
223225

226+
using clGetDeviceGlobalVariablePointer_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
227+
cl_device_id device, cl_program program, const char *globalVariableName,
228+
size_t *globalVariableSizeRet, void **globalVariablePointerRet);
229+
224230
using clEnqueueWriteGlobalVariable_fn = CL_API_ENTRY
225231
cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool,
226232
size_t, size_t, const void *, cl_uint, const cl_event *,
@@ -314,6 +320,8 @@ struct ExtFuncPtrCacheT {
314320
FuncPtrCache<clDeviceMemAllocINTEL_fn> clDeviceMemAllocINTELCache;
315321
FuncPtrCache<clSharedMemAllocINTEL_fn> clSharedMemAllocINTELCache;
316322
FuncPtrCache<clGetDeviceFunctionPointer_fn> clGetDeviceFunctionPointerCache;
323+
FuncPtrCache<clGetDeviceGlobalVariablePointer_fn>
324+
clGetDeviceGlobalVariablePointerCache;
317325
FuncPtrCache<clCreateBufferWithPropertiesINTEL_fn>
318326
clCreateBufferWithPropertiesINTELCache;
319327
FuncPtrCache<clMemBlockingFreeINTEL_fn> clMemBlockingFreeINTELCache;

0 commit comments

Comments
 (0)