@@ -241,6 +241,9 @@ void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind) {
241241// / @param ptr is the USM pointer to query
242242// / @param ctxt is the sycl context the ptr was allocated in
243243alloc get_pointer_type (const void *Ptr, const context &Ctxt) {
244+ if (!Ptr)
245+ return alloc::unknown;
246+
244247 // Everything on a host device is just system malloc so call it host
245248 if (Ctxt.is_host ())
246249 return alloc::host;
@@ -251,8 +254,18 @@ alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
251254
252255 // query type using PI function
253256 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+ }
256269
257270 alloc ResultAlloc;
258271 switch (AllocTy) {
@@ -278,6 +291,10 @@ alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
278291// / @param ptr is the USM pointer to query
279292// / @param ctxt is the sycl context the ptr was allocated in
280293device 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+
281298 // Just return the host device in the host context
282299 if (Ctxt.is_host ())
283300 return Ctxt.get_devices ()[0 ];
0 commit comments