Skip to content

Commit 38bd941

Browse files
authored
Merge pull request #1587 from callumfare/callum/native_handle_uintptr_t
Change ur_native_handle_t to be uintptr_t
2 parents fa06e95 + 36ca9f1 commit 38bd941

37 files changed

+93
-86
lines changed

include/ur_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ typedef struct ur_queue_handle_t_ *ur_queue_handle_t;
394394

395395
///////////////////////////////////////////////////////////////////////////////
396396
/// @brief Handle of a native object
397-
typedef struct ur_native_handle_t_ *ur_native_handle_t;
397+
typedef uintptr_t ur_native_handle_t;
398398

399399
///////////////////////////////////////////////////////////////////////////////
400400
/// @brief Handle of a Sampler object

include/ur_print.hpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10391,8 +10391,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1039110391

1039210392
os << ".hNativePlatform = ";
1039310393

10394-
ur::details::printPtr(os,
10395-
*(params->phNativePlatform));
10394+
ur::details::printPtr(os, reinterpret_cast<void *>(
10395+
*(params->phNativePlatform)));
1039610396

1039710397
os << ", ";
1039810398
os << ".hAdapter = ";
@@ -10588,8 +10588,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1058810588

1058910589
os << ".hNativeContext = ";
1059010590

10591-
ur::details::printPtr(os,
10592-
*(params->phNativeContext));
10591+
ur::details::printPtr(os, reinterpret_cast<void *>(
10592+
*(params->phNativeContext)));
1059310593

1059410594
os << ", ";
1059510595
os << ".numDevices = ";
@@ -10798,8 +10798,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1079810798

1079910799
os << ".hNativeEvent = ";
1080010800

10801-
ur::details::printPtr(os,
10802-
*(params->phNativeEvent));
10801+
ur::details::printPtr(os, reinterpret_cast<void *>(
10802+
*(params->phNativeEvent)));
1080310803

1080410804
os << ", ";
1080510805
os << ".hContext = ";
@@ -11392,8 +11392,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1139211392

1139311393
os << ".hNativeProgram = ";
1139411394

11395-
ur::details::printPtr(os,
11396-
*(params->phNativeProgram));
11395+
ur::details::printPtr(os, reinterpret_cast<void *>(
11396+
*(params->phNativeProgram)));
1139711397

1139811398
os << ", ";
1139911399
os << ".hContext = ";
@@ -11612,8 +11612,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1161211612

1161311613
os << ".hNativeKernel = ";
1161411614

11615-
ur::details::printPtr(os,
11616-
*(params->phNativeKernel));
11615+
ur::details::printPtr(os, reinterpret_cast<void *>(
11616+
*(params->phNativeKernel)));
1161711617

1161811618
os << ", ";
1161911619
os << ".hContext = ";
@@ -12061,8 +12061,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1206112061

1206212062
os << ".hNativeQueue = ";
1206312063

12064-
ur::details::printPtr(os,
12065-
*(params->phNativeQueue));
12064+
ur::details::printPtr(os, reinterpret_cast<void *>(
12065+
*(params->phNativeQueue)));
1206612066

1206712067
os << ", ";
1206812068
os << ".hContext = ";
@@ -12235,8 +12235,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1223512235

1223612236
os << ".hNativeSampler = ";
1223712237

12238-
ur::details::printPtr(os,
12239-
*(params->phNativeSampler));
12238+
ur::details::printPtr(os, reinterpret_cast<void *>(
12239+
*(params->phNativeSampler)));
1224012240

1224112241
os << ", ";
1224212242
os << ".hContext = ";
@@ -12439,8 +12439,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1243912439

1244012440
os << ".hNativeMem = ";
1244112441

12442-
ur::details::printPtr(os,
12443-
*(params->phNativeMem));
12442+
ur::details::printPtr(os, reinterpret_cast<void *>(
12443+
*(params->phNativeMem)));
1244412444

1244512445
os << ", ";
1244612446
os << ".hContext = ";
@@ -12471,8 +12471,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1247112471

1247212472
os << ".hNativeMem = ";
1247312473

12474-
ur::details::printPtr(os,
12475-
*(params->phNativeMem));
12474+
ur::details::printPtr(os, reinterpret_cast<void *>(
12475+
*(params->phNativeMem)));
1247612476

1247712477
os << ", ";
1247812478
os << ".hContext = ";
@@ -17221,8 +17221,8 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1722117221

1722217222
os << ".hNativeDevice = ";
1722317223

17224-
ur::details::printPtr(os,
17225-
*(params->phNativeDevice));
17224+
ur::details::printPtr(os, reinterpret_cast<void *>(
17225+
*(params->phNativeDevice)));
1722617226

1722717227
os << ", ";
1722817228
os << ".hPlatform = ";

scripts/templates/api.h.mako

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ ${th.make_func_name(n, tags, obj)}(
121121
);
122122
## HANDLE #####################################################################
123123
%elif re.match(r"handle", obj['type']):
124-
%if 'alias' in obj:
124+
%if th.type_traits.is_native_handle(obj['name']):
125+
typedef uintptr_t ${th.subt(n, tags, obj['name'])};
126+
%elif 'alias' in obj:
125127
typedef ${th.subt(n, tags, obj['alias'])} ${th.subt(n, tags, obj['name'])};
126128
%else:
127129
typedef struct ${th.subt(n, tags, obj['name'])}_ *${th.subt(n, tags, obj['name'])};

scripts/templates/helper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def get_handle(item, meta):
109109
"""
110110
class type_traits:
111111
RE_HANDLE = r"(.*)handle_t"
112+
RE_NATIVE_HANDLE = r"(.*)native_handle_t"
112113
RE_IPC = r"(.*)ipc(.*)handle_t"
113114
RE_POINTER = r"(.*\w+)\*+"
114115
RE_PPOINTER = r"(.*\w+)\*{2,}"
@@ -128,6 +129,13 @@ def is_handle(cls, name):
128129
except:
129130
return False
130131

132+
@classmethod
133+
def is_native_handle(cls, name):
134+
try:
135+
return True if re.match(cls.RE_NATIVE_HANDLE, name) else False
136+
except:
137+
return False
138+
131139
@classmethod
132140
def is_pointer_to_pointer(cls, name):
133141
try:

scripts/templates/print.hpp.mako

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ from templates import helper as th
3636
${x}::details::printPtr(os, ${caller.body()});
3737
%elif loop and th.type_traits.is_pointer_to_pointer(itype):
3838
${x}::details::printPtr(os, ${caller.body()});
39+
%elif th.type_traits.is_native_handle(itype):
40+
${x}::details::printPtr(os, reinterpret_cast<void*>(${caller.body()}));
3941
%elif th.type_traits.is_handle(itype):
4042
${x}::details::printPtr(os, ${caller.body()});
4143
%elif iname and iname.startswith("pfn"):

source/adapters/cuda/device.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
11871187
ur_device_handle_t *phDevice) {
11881188
std::ignore = pProperties;
11891189

1190-
// We can't cast between ur_native_handle_t and CUdevice, so memcpy the bits
1191-
// instead
1192-
CUdevice CuDevice = 0;
1193-
memcpy(&CuDevice, &hNativeDevice, sizeof(CUdevice));
1190+
CUdevice CuDevice = static_cast<CUdevice>(hNativeDevice);
11941191

11951192
auto IsDevice = [=](std::unique_ptr<ur_device_handle_t_> &Dev) {
11961193
return Dev->get() == CuDevice;

source/adapters/cuda/memory.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ urMemGetNativeHandle(ur_mem_handle_t hMem, ur_device_handle_t Device,
132132
ur_native_handle_t *phNativeMem) {
133133
UR_ASSERT(Device != nullptr, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
134134
try {
135-
*phNativeMem = reinterpret_cast<ur_native_handle_t>(
136-
std::get<BufferMem>(hMem->Mem).getPtr(Device));
135+
*phNativeMem = std::get<BufferMem>(hMem->Mem).getPtr(Device);
137136
} catch (ur_result_t Err) {
138137
return Err;
139138
} catch (...) {

source/adapters/hip/device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet(ur_platform_handle_t hPlatform,
982982
/// \return UR_RESULT_SUCCESS
983983
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle(
984984
ur_device_handle_t hDevice, ur_native_handle_t *phNativeHandle) {
985-
*phNativeHandle = reinterpret_cast<ur_native_handle_t>(hDevice->get());
985+
*phNativeHandle = hDevice->get();
986986
return UR_RESULT_SUCCESS;
987987
}
988988

source/adapters/level_zero/event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
926926

927927
// we dont have urEventCreate, so use this check for now to know that
928928
// the call comes from urEventCreate()
929-
if (NativeEvent == nullptr) {
929+
if (reinterpret_cast<ze_event_handle_t>(NativeEvent) == nullptr) {
930930
UR_CALL(EventCreate(Context, nullptr, false, true, Event));
931931

932932
(*Event)->RefCountExternal++;

source/adapters/opencl/queue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
167167

168168
*phQueue = reinterpret_cast<ur_queue_handle_t>(hNativeQueue);
169169
cl_int RetErr =
170-
clRetainCommandQueue(cl_adapter::cast<cl_command_queue>(hNativeQueue));
170+
clRetainCommandQueue(cl_adapter::cast<cl_command_queue>(*phQueue));
171171
CL_RETURN_ON_FAILURE(RetErr);
172172
return UR_RESULT_SUCCESS;
173173
}

0 commit comments

Comments
 (0)