[HIP] Keep allocation completion ordered with its signal stream (#25291) Queue allocations run on the async memory stream and record their signal-semaphore events there. The cleanup event that advances those semaphores on the host, however, was always recorded on the dispatch stream. Allocation completion was therefore observed independently of the stream that performed it. `iree_hal_hip_device_stream_add_cleanup` now records on the stream it is given, and semaphore signalling passes the stream its events were recorded on. Queue reads and submissions keep using the dispatch stream. Assisted-by: Codex
diff --git a/runtime/src/iree/hal/drivers/hip/hip_device.c b/runtime/src/iree/hal/drivers/hip/hip_device.c index 6034fbb..5c60dd5 100644 --- a/runtime/src/iree/hal/drivers/hip/hip_device.c +++ b/runtime/src/iree/hal/drivers/hip/hip_device.c
@@ -1383,10 +1383,11 @@ IREE_HAL_HIP_DEVICE_SEMAPHORE_OPERATION_ASYNC_DEALLOC, } iree_hal_hip_device_semaphore_buffer_operation_type_t; +// Runs |callback| once the work submitted to |stream| so far completes. static iree_status_t iree_hal_hip_device_stream_add_cleanup( - iree_hal_hip_device_t* device, iree_hal_hip_cleanup_thread_t* thread, - iree_host_size_t device_ordinal, iree_hal_hip_cleanup_callback_t callback, - void* user_data) { + iree_hal_hip_device_t* device, hipStream_t stream, + iree_hal_hip_cleanup_thread_t* thread, iree_host_size_t device_ordinal, + iree_hal_hip_cleanup_callback_t callback, void* user_data) { iree_hal_hip_event_t* event = NULL; iree_status_t status = iree_hal_hip_event_pool_acquire( device->devices[device_ordinal].device_event_pool, 1, &event); @@ -1394,8 +1395,7 @@ if (iree_status_is_ok(status)) { status = IREE_HIP_CALL_TO_STATUS( device->hip_symbols, - hipEventRecord(iree_hal_hip_event_handle(event), - device->devices[device_ordinal].hip_dispatch_stream)); + hipEventRecord(iree_hal_hip_event_handle(event), stream)); } if (iree_status_is_ok(status)) { @@ -1450,8 +1450,10 @@ } if (iree_status_is_ok(status)) { + // The callback advances the signal semaphores, whose events were just + // recorded on |stream|; its own event must follow them on that stream. status = iree_hal_hip_device_stream_add_cleanup( - device, thread, device_ordinal, callback, user_data); + device, stream, thread, device_ordinal, callback, user_data); } IREE_TRACE_ZONE_END(z0); return status; @@ -2281,7 +2283,8 @@ break; } else { status = iree_hal_hip_device_stream_add_cleanup( - device, device->cleanup_thread, device_ordinal, + device, device->devices[device_ordinal].hip_dispatch_stream, + device->cleanup_thread, device_ordinal, &iree_hal_hip_device_complete_queue_read_operation, data); } }