@@ -1459,64 +1459,74 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
1459
1459
ur_queue_handle_t hQueue, const void *pMem, size_t size,
1460
1460
ur_usm_migration_flags_t flags, uint32_t numEventsInWaitList,
1461
1461
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
1462
+ std::ignore = flags;
1463
+
1462
1464
void *HIPDevicePtr = const_cast <void *>(pMem);
1463
1465
ur_device_handle_t Device = hQueue->getDevice ();
1464
1466
1465
- // If the device does not support managed memory access, we can't set
1466
- // mem_advise.
1467
- if (!getAttribute (Device, hipDeviceAttributeManagedMemory)) {
1468
- setErrorMessage (" mem_advise ignored as device does not support "
1469
- " managed memory access" ,
1470
- UR_RESULT_SUCCESS);
1471
- return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1472
- }
1473
-
1474
- hipPointerAttribute_t attribs;
1475
- // TODO: hipPointerGetAttributes will fail if pMem is non-HIP allocated
1476
- // memory, as it is neither registered as host memory, nor into the address
1477
- // space for the current device, meaning the pMem ptr points to a
1478
- // system-allocated memory. This means we may need to check system-alloacted
1479
- // memory and handle the failure more gracefully.
1480
- UR_CHECK_ERROR (hipPointerGetAttributes (&attribs, pMem));
1481
- // async prefetch requires USM pointer (or hip SVM) to work.
1482
- if (!attribs.isManaged ) {
1483
- setErrorMessage (" Prefetch hint ignored as prefetch only works with USM" ,
1484
- UR_RESULT_SUCCESS);
1485
- return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1486
- }
1487
-
1488
- // HIP_POINTER_ATTRIBUTE_RANGE_SIZE is not an attribute in ROCM < 5,
1489
- // so we can't perform this check for such cases.
1467
+ // HIP_POINTER_ATTRIBUTE_RANGE_SIZE is not an attribute in ROCM < 5,
1468
+ // so we can't perform this check for such cases.
1490
1469
#if HIP_VERSION_MAJOR >= 5
1491
1470
unsigned int PointerRangeSize = 0 ;
1492
1471
UR_CHECK_ERROR (hipPointerGetAttribute (&PointerRangeSize,
1493
1472
HIP_POINTER_ATTRIBUTE_RANGE_SIZE,
1494
1473
(hipDeviceptr_t)HIPDevicePtr));
1495
1474
UR_ASSERT (size <= PointerRangeSize, UR_RESULT_ERROR_INVALID_SIZE);
1496
1475
#endif
1497
- // flags is currently unused so fail if set
1498
- if (flags != 0 )
1499
- return UR_RESULT_ERROR_INVALID_VALUE;
1476
+
1500
1477
ur_result_t Result = UR_RESULT_SUCCESS;
1501
- std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1502
1478
1503
1479
try {
1504
1480
ScopedContext Active (hQueue->getDevice ());
1505
1481
hipStream_t HIPStream = hQueue->getNextTransferStream ();
1506
1482
Result = enqueueEventsWait (hQueue, HIPStream, numEventsInWaitList,
1507
1483
phEventWaitList);
1484
+
1485
+ std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1486
+
1508
1487
if (phEvent) {
1509
1488
EventPtr =
1510
1489
std::unique_ptr<ur_event_handle_t_>(ur_event_handle_t_::makeNative (
1511
1490
UR_COMMAND_USM_PREFETCH, hQueue, HIPStream));
1512
1491
UR_CHECK_ERROR (EventPtr->start ());
1513
1492
}
1493
+
1494
+ // Helper to ensure returning a valid event on early exit.
1495
+ auto releaseEvent = [&EventPtr, &phEvent]() -> void {
1496
+ if (phEvent) {
1497
+ UR_CHECK_ERROR (EventPtr->record ());
1498
+ *phEvent = EventPtr.release ();
1499
+ }
1500
+ };
1501
+
1502
+ // If the device does not support managed memory access, we can't set
1503
+ // mem_advise.
1504
+ if (!getAttribute (Device, hipDeviceAttributeManagedMemory)) {
1505
+ releaseEvent ();
1506
+ setErrorMessage (" mem_advise ignored as device does not support "
1507
+ " managed memory access" ,
1508
+ UR_RESULT_SUCCESS);
1509
+ return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1510
+ }
1511
+
1512
+ hipPointerAttribute_t attribs;
1513
+ // TODO: hipPointerGetAttributes will fail if pMem is non-HIP allocated
1514
+ // memory, as it is neither registered as host memory, nor into the address
1515
+ // space for the current device, meaning the pMem ptr points to a
1516
+ // system-allocated memory. This means we may need to check system-alloacted
1517
+ // memory and handle the failure more gracefully.
1518
+ UR_CHECK_ERROR (hipPointerGetAttributes (&attribs, pMem));
1519
+ // async prefetch requires USM pointer (or hip SVM) to work.
1520
+ if (!attribs.isManaged ) {
1521
+ releaseEvent ();
1522
+ setErrorMessage (" Prefetch hint ignored as prefetch only works with USM" ,
1523
+ UR_RESULT_SUCCESS);
1524
+ return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1525
+ }
1526
+
1514
1527
UR_CHECK_ERROR (
1515
1528
hipMemPrefetchAsync (pMem, size, hQueue->getDevice ()->get (), HIPStream));
1516
- if (phEvent) {
1517
- UR_CHECK_ERROR (EventPtr->record ());
1518
- *phEvent = EventPtr.release ();
1519
- }
1529
+ releaseEvent ();
1520
1530
} catch (ur_result_t Err) {
1521
1531
Result = Err;
1522
1532
}
0 commit comments