@@ -1532,37 +1532,6 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1532
1532
void *HIPDevicePtr = const_cast <void *>(pMem);
1533
1533
ur_device_handle_t Device = hQueue->getContext ()->getDevice ();
1534
1534
1535
- // If the device does not support managed memory access, we can't set
1536
- // mem_advise.
1537
- if (!getAttribute (Device, hipDeviceAttributeManagedMemory)) {
1538
- setErrorMessage (" mem_advise ignored as device does not support "
1539
- " managed memory access" ,
1540
- UR_RESULT_SUCCESS);
1541
- return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1542
- }
1543
-
1544
- // Passing MEM_ADVICE_SET/MEM_ADVICE_CLEAR_PREFERRED_LOCATION to hipMemAdvise
1545
- // on a GPU device requires the GPU device to report a non-zero value for
1546
- // hipDeviceAttributeConcurrentManagedAccess. Therefore, ignore the mem advice
1547
- // if concurrent managed memory access is not available.
1548
- if (advice & (UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION |
1549
- UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION |
1550
- UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE |
1551
- UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE |
1552
- UR_USM_ADVICE_FLAG_DEFAULT)) {
1553
- if (!getAttribute (Device, hipDeviceAttributeConcurrentManagedAccess)) {
1554
- setErrorMessage (" mem_advise ignored as device does not support "
1555
- " concurrent managed access" ,
1556
- UR_RESULT_SUCCESS);
1557
- return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1558
- }
1559
-
1560
- // TODO: If pMem points to valid system-allocated pageable memory, we should
1561
- // check that the device also has the hipDeviceAttributePageableMemoryAccess
1562
- // property, so that a valid read-only copy can be created on the device.
1563
- // This also applies for UR_USM_MEM_ADVICE_SET/MEM_ADVICE_CLEAR_READ_MOSTLY.
1564
- }
1565
-
1566
1535
#if HIP_VERSION_MAJOR >= 5
1567
1536
// NOTE: The hipPointerGetAttribute API is marked as beta, meaning, while this
1568
1537
// is feature complete, it is still open to changes and outstanding issues.
@@ -1574,10 +1543,10 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1574
1543
#endif
1575
1544
1576
1545
ur_result_t Result = UR_RESULT_SUCCESS;
1577
- std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1578
1546
1579
1547
try {
1580
1548
ScopedContext Active (Device);
1549
+ std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1581
1550
1582
1551
if (phEvent) {
1583
1552
EventPtr =
@@ -1586,6 +1555,48 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1586
1555
EventPtr->start ();
1587
1556
}
1588
1557
1558
+ // Helper to ensure returning a valid event on early exit.
1559
+ auto releaseEvent = [&EventPtr, &phEvent]() -> void {
1560
+ if (phEvent) {
1561
+ UR_CHECK_ERROR (EventPtr->record ());
1562
+ *phEvent = EventPtr.release ();
1563
+ }
1564
+ };
1565
+
1566
+ // If the device does not support managed memory access, we can't set
1567
+ // mem_advise.
1568
+ if (!getAttribute (Device, hipDeviceAttributeManagedMemory)) {
1569
+ releaseEvent ();
1570
+ setErrorMessage (" mem_advise ignored as device does not support "
1571
+ " managed memory access" ,
1572
+ UR_RESULT_SUCCESS);
1573
+ return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1574
+ }
1575
+
1576
+ // Passing MEM_ADVICE_SET/MEM_ADVICE_CLEAR_PREFERRED_LOCATION to
1577
+ // hipMemAdvise on a GPU device requires the GPU device to report a non-zero
1578
+ // value for hipDeviceAttributeConcurrentManagedAccess. Therefore, ignore
1579
+ // the mem advice if concurrent managed memory access is not available.
1580
+ if (advice & (UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION |
1581
+ UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION |
1582
+ UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE |
1583
+ UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE |
1584
+ UR_USM_ADVICE_FLAG_DEFAULT)) {
1585
+ if (!getAttribute (Device, hipDeviceAttributeConcurrentManagedAccess)) {
1586
+ releaseEvent ();
1587
+ setErrorMessage (" mem_advise ignored as device does not support "
1588
+ " concurrent managed access" ,
1589
+ UR_RESULT_SUCCESS);
1590
+ return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1591
+ }
1592
+
1593
+ // TODO: If pMem points to valid system-allocated pageable memory, we
1594
+ // should check that the device also has the
1595
+ // hipDeviceAttributePageableMemoryAccess property, so that a valid
1596
+ // read-only copy can be created on the device. This also applies for
1597
+ // UR_USM_MEM_ADVICE_SET/MEM_ADVICE_CLEAR_READ_MOSTLY.
1598
+ }
1599
+
1589
1600
const auto DeviceID = Device->get ();
1590
1601
if (advice & UR_USM_ADVICE_FLAG_DEFAULT) {
1591
1602
UR_CHECK_ERROR (
@@ -1600,17 +1611,15 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1600
1611
// currently unmapped advice arguments as not supported by this platform.
1601
1612
// Therefore, warn the user instead of throwing and aborting the runtime.
1602
1613
if (Result == UR_RESULT_ERROR_INVALID_ENUMERATION) {
1614
+ releaseEvent ();
1603
1615
setErrorMessage (" mem_advise is ignored as the advice argument is not "
1604
1616
" supported by this device" ,
1605
1617
UR_RESULT_SUCCESS);
1606
1618
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1607
1619
}
1608
1620
}
1609
1621
1610
- if (phEvent) {
1611
- Result = EventPtr->record ();
1612
- *phEvent = EventPtr.release ();
1613
- }
1622
+ releaseEvent ();
1614
1623
} catch (ur_result_t err) {
1615
1624
Result = err;
1616
1625
} catch (...) {
0 commit comments