@@ -43,11 +43,21 @@ static const size_t kPageSize_ = 4096;
4343static atomic_uint8_t amdgpu_runtime_shutdown{0 };
4444static atomic_uint8_t amdgpu_event_registered{0 };
4545
46- bool AmdgpuMemFuncs::GetAmdgpuRuntimeShutdown () {
46+ // Check if AMDGPU runtime shutdown state
47+ bool AmdgpuMemFuncs::IsAmdgpuRuntimeShutdown () {
4748 return static_cast <bool >(
4849 atomic_load (&amdgpu_runtime_shutdown, memory_order_acquire));
4950}
5051
52+ // Notify AMDGPU runtime shutdown to allocator
53+ void AmdgpuMemFuncs::NotifyAmdgpuRuntimeShutdown () {
54+ uint8_t shutdown = 0 ;
55+ if (atomic_compare_exchange_strong (&amdgpu_runtime_shutdown, &shutdown, 1 ,
56+ memory_order_acq_rel)) {
57+ VReport (1 , " Amdgpu Allocator: AMDGPU runtime shutdown detected\n " );
58+ }
59+ }
60+
5161bool AmdgpuMemFuncs::Init () {
5262 hsa_amd.memory_pool_allocate =
5363 (decltype (hsa_amd.memory_pool_allocate ))dlsym (
@@ -73,7 +83,8 @@ bool AmdgpuMemFuncs::Init() {
7383
7484void *AmdgpuMemFuncs::Allocate (uptr size, uptr alignment,
7585 DeviceAllocationInfo *da_info) {
76- if (atomic_load (&amdgpu_runtime_shutdown, memory_order_acquire))
86+ // Do not allocate if AMDGPU runtime is shutdown
87+ if (IsAmdgpuRuntimeShutdown ())
7788 return nullptr ;
7889 AmdgpuAllocationInfo *aa_info =
7990 reinterpret_cast <AmdgpuAllocationInfo *>(da_info);
@@ -92,7 +103,8 @@ void *AmdgpuMemFuncs::Allocate(uptr size, uptr alignment,
92103}
93104
94105void AmdgpuMemFuncs::Deallocate (void *p) {
95- if (atomic_load (&amdgpu_runtime_shutdown, memory_order_acquire))
106+ // Deallocate does nothing after AMDGPU runtime shutdown
107+ if (IsAmdgpuRuntimeShutdown ())
96108 return ;
97109 DevicePointerInfo DevPtrInfo;
98110 if (AmdgpuMemFuncs::GetPointerInfo (reinterpret_cast <uptr>(p), &DevPtrInfo)) {
@@ -123,26 +135,24 @@ bool AmdgpuMemFuncs::GetPointerInfo(uptr ptr, DevicePointerInfo* ptr_info) {
123135
124136 return true ;
125137}
126-
138+ // Register shutdown system event handler only once
139+ // TODO: Register multiple event handlers if needed in future
127140void AmdgpuMemFuncs::RegisterSystemEventHandlers () {
128- // Register shutdown system event handler only once
141+ // Check if already registered
129142 if (atomic_load (&amdgpu_event_registered, memory_order_acquire) == 0 ) {
130143 // Callback to just detect runtime shutdown
131144 hsa_amd_system_event_callback_t callback = [](const hsa_amd_event_t * event,
132145 void * data) {
133146 if (!event)
134147 return HSA_STATUS_ERROR_INVALID_ARGUMENT;
135- if (event->event_type == HSA_AMD_SYSTEM_SHUTDOWN_EVENT) {
136- uint8_t shutdown = 0 ;
137- if (atomic_compare_exchange_strong (&amdgpu_runtime_shutdown, &shutdown,
138- 1 , memory_order_acq_rel)) {
139- // Evict all allocations (add purge logic here).
140- }
141- }
148+ if (event->event_type == HSA_AMD_SYSTEM_SHUTDOWN_EVENT)
149+ AmdgpuMemFuncs::NotifyAmdgpuRuntimeShutdown ();
142150 return HSA_STATUS_SUCCESS;
143151 };
152+ // Register the callback
144153 hsa_status_t status =
145154 hsa_amd.register_system_event_handler (callback, nullptr );
155+ // Mark as registered if successful
146156 if (status == HSA_STATUS_SUCCESS)
147157 atomic_store (&amdgpu_event_registered, 1 , memory_order_release);
148158 }
0 commit comments