1313#include "iree/base/tracing.h"
1414#include "iree/hal/drivers/hip/dynamic_symbols.h"
1515#include "iree/hal/drivers/hip/hip_buffer.h"
16+ #include "iree/hal/drivers/hip/hip_device.h"
1617#include "iree/hal/drivers/hip/per_device_information.h"
1718#include "iree/hal/drivers/hip/status_util.h"
1819#include "iree/hal/drivers/hip/util/queue.h"
@@ -569,31 +570,67 @@ static void iree_hal_hip_allocator_deallocate_buffer(
569570 iree_hal_buffer_destroy (base_buffer );
570571}
571572
572- static void iree_hal_hip_buffer_release_callback (void * user_data ,
573- iree_hal_buffer_t * buffer ) {
574- iree_hal_hip_allocator_t * allocator = (iree_hal_hip_allocator_t * )user_data ;
575-
576- const iree_hal_hip_buffer_type_t buffer_type =
577- iree_hal_hip_buffer_type (buffer );
578-
579- iree_hal_hip_buffer_free (allocator -> symbols , buffer_type ,
580- iree_hal_hip_buffer_device_pointer (buffer ),
581- iree_hal_hip_buffer_host_pointer (buffer ));
582-
583- switch (buffer_type ) {
573+ typedef struct iree_hal_hip_release_async_data_t {
574+ iree_hal_hip_allocator_t * allocator ;
575+ iree_hal_hip_buffer_type_t buffer_type ;
576+ hipDeviceptr_t device_pointer ;
577+ void * host_pointer ;
578+ IREE_STATISTICS (iree_hal_memory_type_t memory_type ;
579+ iree_device_size_t allocation_size ;)
580+ } iree_hal_hip_release_async_data_t ;
581+
582+ static iree_status_t iree_hal_hip_buffer_release_callback_async (
583+ void * user_data , iree_hal_hip_event_t * event , iree_status_t status ) {
584+ iree_hal_hip_release_async_data_t * async_data =
585+ (iree_hal_hip_release_async_data_t * )user_data ;
586+
587+ iree_hal_hip_buffer_free (async_data -> allocator -> symbols ,
588+ async_data -> buffer_type , async_data -> device_pointer ,
589+ async_data -> host_pointer );
590+
591+ switch (async_data -> buffer_type ) {
584592 case IREE_HAL_HIP_BUFFER_TYPE_DEVICE :
585593 case IREE_HAL_HIP_BUFFER_TYPE_HOST : {
586594 IREE_TRACE_FREE_NAMED (IREE_HAL_HIP_ALLOCATOR_ID ,
587- (void * )iree_hal_hip_buffer_device_pointer ( buffer ) );
595+ (void * )async_data -> device_pointer );
588596 IREE_STATISTICS (iree_hal_allocator_statistics_record_free (
589- & allocator -> statistics , iree_hal_buffer_memory_type ( buffer ) ,
590- iree_hal_buffer_allocation_size ( buffer ) ));
597+ & async_data -> allocator -> statistics , async_data -> memory_type ,
598+ async_data -> allocation_size ));
591599 break ;
592600 }
593601 default :
594602 // Buffer type not tracked.
595603 break ;
596604 }
605+ iree_allocator_free (async_data -> allocator -> host_allocator , async_data );
606+ return status ;
607+ }
608+
609+ static void iree_hal_hip_buffer_release_callback (void * user_data ,
610+ iree_hal_buffer_t * buffer ) {
611+ iree_hal_hip_allocator_t * allocator = (iree_hal_hip_allocator_t * )user_data ;
612+
613+ iree_hal_hip_release_async_data_t * release_async_data = NULL ;
614+
615+ iree_status_t status = iree_allocator_malloc (allocator -> host_allocator ,
616+ sizeof (* release_async_data ),
617+ (void * * )& release_async_data );
618+ if (iree_status_is_ok (status )) {
619+ release_async_data -> allocator = allocator ;
620+ release_async_data -> device_pointer =
621+ iree_hal_hip_buffer_device_pointer (buffer );
622+ release_async_data -> host_pointer = iree_hal_hip_buffer_host_pointer (buffer );
623+ release_async_data -> buffer_type = iree_hal_hip_buffer_type (buffer );
624+ IREE_STATISTICS ({
625+ release_async_data -> memory_type = iree_hal_buffer_memory_type (buffer );
626+ release_async_data -> allocation_size =
627+ iree_hal_buffer_allocation_size (buffer );
628+ })
629+ status = iree_hal_hip_device_add_asynchronous_cleanup (
630+ allocator -> parent_device , & iree_hal_hip_buffer_release_callback_async ,
631+ (void * )release_async_data );
632+ }
633+ iree_status_ignore (status );
597634}
598635
599636static iree_status_t iree_hal_hip_allocator_import_buffer (
@@ -766,6 +803,7 @@ iree_status_t iree_hal_hip_allocator_alloc_async(
766803 iree_hal_buffer_allocation_size (buffer ),
767804 IREE_HOST_SIZE_MAX );
768805 }
806+ IREE_TRACE_ZONE_BEGIN (z0 );
769807
770808 int device_ordinal = 0 ;
771809 device_ordinal =
@@ -837,6 +875,8 @@ iree_status_t iree_hal_hip_allocator_alloc_async(
837875 iree_hal_hip_buffer_set_allocation_empty (buffer );
838876 }
839877
878+ IREE_TRACE_ZONE_END (z0 );
879+
840880 return status ;
841881}
842882
@@ -849,6 +889,7 @@ iree_status_t iree_hal_hip_allocator_free_async(
849889 return iree_ok_status ();
850890 }
851891
892+ IREE_TRACE_ZONE_BEGIN (z0 );
852893 IREE_TRACE_FREE_NAMED (IREE_HAL_HIP_ALLOCATOR_ID , (void * )device_ptr );
853894 IREE_STATISTICS (iree_hal_allocator_statistics_record_free (
854895 & allocator -> statistics , iree_hal_buffer_memory_type (buffer ),
@@ -888,6 +929,7 @@ iree_status_t iree_hal_hip_allocator_free_async(
888929 if (iree_status_is_ok (status )) {
889930 iree_hal_hip_buffer_set_allocation_empty (buffer );
890931 }
932+ IREE_TRACE_ZONE_END (z0 );
891933
892934 return status ;
893935}
0 commit comments