@@ -241,6 +241,9 @@ void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind) {
241
241
// / @param ptr is the USM pointer to query
242
242
// / @param ctxt is the sycl context the ptr was allocated in
243
243
alloc get_pointer_type (const void *Ptr , const context &Ctxt) {
244
+ if (!Ptr )
245
+ return alloc::unknown;
246
+
244
247
// Everything on a host device is just system malloc so call it host
245
248
if (Ctxt.is_host ())
246
249
return alloc::host;
@@ -251,8 +254,18 @@ alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
251
254
252
255
// query type using PI function
253
256
const detail::plugin &Plugin = CtxImpl->getPlugin ();
254
- Plugin.call <detail::PiApiKind::piextUSMGetMemAllocInfo>(
255
- PICtx, Ptr , PI_MEM_ALLOC_TYPE, sizeof (pi_usm_type), &AllocTy, nullptr );
257
+ RT::PiResult Err =
258
+ Plugin.call_nocheck <detail::PiApiKind::piextUSMGetMemAllocInfo>(
259
+ PICtx, Ptr , PI_MEM_ALLOC_TYPE, sizeof (pi_usm_type), &AllocTy,
260
+ nullptr );
261
+
262
+ // PI_INVALID_VALUE means USM doesn't know about this ptr
263
+ if (Err == PI_INVALID_VALUE)
264
+ return alloc::unknown;
265
+ // otherwise PI_SUCCESS is expected
266
+ if (Err != PI_SUCCESS) {
267
+ throw runtime_error (" Error querying USM pointer: " , Err);
268
+ }
256
269
257
270
alloc ResultAlloc;
258
271
switch (AllocTy) {
@@ -278,6 +291,10 @@ alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
278
291
// / @param ptr is the USM pointer to query
279
292
// / @param ctxt is the sycl context the ptr was allocated in
280
293
device get_pointer_device (const void *Ptr , const context &Ctxt) {
294
+ // Check if ptr is a valid USM pointer
295
+ if (get_pointer_type (Ptr , Ctxt) == alloc::unknown)
296
+ throw runtime_error (" Ptr not a valid USM allocation!" );
297
+
281
298
// Just return the host device in the host context
282
299
if (Ctxt.is_host ())
283
300
return Ctxt.get_devices ()[0 ];
0 commit comments