Skip to content

Commit 78e3a56

Browse files
committed
Refactor atomic variable checking/updation.
Summary: - Added wrapper IsAmdgpuRuntimeShutdown for checking state. - Added wrapper NotifyAmdgpuRuntimeShutdown for updating state.
1 parent 1578d00 commit 78e3a56

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_allocator_amdgpu.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ static const size_t kPageSize_ = 4096;
4343
static atomic_uint8_t amdgpu_runtime_shutdown{0};
4444
static 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+
5161
bool AmdgpuMemFuncs::Init() {
5262
hsa_amd.memory_pool_allocate =
5363
(decltype(hsa_amd.memory_pool_allocate))dlsym(
@@ -73,7 +83,8 @@ bool AmdgpuMemFuncs::Init() {
7383

7484
void *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

94105
void 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
127140
void 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
}

compiler-rt/lib/sanitizer_common/sanitizer_allocator_amdgpu.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class AmdgpuMemFuncs {
2323
static bool GetPointerInfo(uptr ptr, DevicePointerInfo* ptr_info);
2424
static uptr GetPageSize();
2525
static void RegisterSystemEventHandlers();
26-
static bool GetAmdgpuRuntimeShutdown();
26+
static bool IsAmdgpuRuntimeShutdown();
27+
28+
private:
29+
static void NotifyAmdgpuRuntimeShutdown();
2730
};
2831

2932
struct AmdgpuAllocationInfo : public DeviceAllocationInfo {

compiler-rt/lib/sanitizer_common/sanitizer_allocator_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class DeviceAllocatorT {
316316
// If GetPointerInfo() fails, we don't assume the runtime is unloaded yet.
317317
// We just return a conservative single-page header. Here mark/check the
318318
// runtime shutdown state
319-
dev_runtime_unloaded_ = DeviceMemFuncs::GetAmdgpuRuntimeShutdown();
319+
dev_runtime_unloaded_ = DeviceMemFuncs::IsAmdgpuRuntimeShutdown();
320320
}
321321
// If we reach here, device runtime is unloaded.
322322
// Fallback: conservative single-page header

0 commit comments

Comments
 (0)