Skip to content

Commit e19565c

Browse files
authored
[Offload][AMDGPU] Only allow memory pool access to valid agents (#93969)
Summary: The logic since the next-gen plugins was added was that every single agent would get access to a memory pool we allocated. This is necessary for things like fine-grained memory and to faciliate d2d copied. However, there are cases where an agent cannot legally access a memory pool. We have a debug check for this, but it would always be triggered in these situations because both uses of the function simply passed every agent. This patch changes the behavior by only enabling memory pool access for agents that can access the memory pool.
1 parent a966440 commit e19565c

File tree

1 file changed

+27
-10
lines changed
  • offload/plugins-nextgen/amdgpu/src

1 file changed

+27
-10
lines changed

offload/plugins-nextgen/amdgpu/src/rtl.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ struct AMDGPUMemoryPoolTy {
307307
return Plugin::check(Status, "Error in hsa_amd_memory_pool_free: %s");
308308
}
309309

310+
/// Returns if the \p Agent can access the memory pool.
311+
bool canAccess(hsa_agent_t Agent) {
312+
hsa_amd_memory_pool_access_t Access;
313+
if (hsa_amd_agent_memory_pool_get_info(
314+
Agent, MemoryPool, HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS, &Access))
315+
return false;
316+
return Access != HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED;
317+
}
318+
310319
/// Allow the device to access a specific allocation.
311320
Error enableAccess(void *Ptr, int64_t Size,
312321
const llvm::SmallVector<hsa_agent_t> &Agents) const {
@@ -3407,10 +3416,14 @@ void *AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
34073416
}
34083417
assert(Ptr && "Invalid pointer");
34093418

3410-
auto &KernelAgents = Plugin.getKernelAgents();
3419+
// Get a list of agents that can access this memory pool.
3420+
llvm::SmallVector<hsa_agent_t> Agents;
3421+
llvm::copy_if(
3422+
Plugin.getKernelAgents(), std::back_inserter(Agents),
3423+
[&](hsa_agent_t Agent) { return MemoryPool->canAccess(Agent); });
34113424

3412-
// Allow all kernel agents to access the allocation.
3413-
if (auto Err = MemoryPool->enableAccess(Ptr, Size, KernelAgents)) {
3425+
// Allow all valid kernel agents to access the allocation.
3426+
if (auto Err = MemoryPool->enableAccess(Ptr, Size, Agents)) {
34143427
REPORT("%s\n", toString(std::move(Err)).data());
34153428
return nullptr;
34163429
}
@@ -3450,13 +3463,17 @@ void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
34503463
}
34513464

34523465
if (Alloc) {
3453-
auto &KernelAgents =
3454-
static_cast<AMDGPUPluginTy &>(Plugin).getKernelAgents();
3455-
// Inherently necessary for host or shared allocations
3456-
// Also enabled for device memory to allow device to device memcpy
3457-
3458-
// Enable all kernel agents to access the buffer.
3459-
if (auto Err = MemoryPool->enableAccess(Alloc, Size, KernelAgents)) {
3466+
// Get a list of agents that can access this memory pool. Inherently
3467+
// necessary for host or shared allocations Also enabled for device memory
3468+
// to allow device to device memcpy
3469+
llvm::SmallVector<hsa_agent_t> Agents;
3470+
llvm::copy_if(static_cast<AMDGPUPluginTy &>(Plugin).getKernelAgents(),
3471+
std::back_inserter(Agents), [&](hsa_agent_t Agent) {
3472+
return MemoryPool->canAccess(Agent);
3473+
});
3474+
3475+
// Enable all valid kernel agents to access the buffer.
3476+
if (auto Err = MemoryPool->enableAccess(Alloc, Size, Agents)) {
34603477
REPORT("%s\n", toString(std::move(Err)).data());
34613478
return nullptr;
34623479
}

0 commit comments

Comments
 (0)