Skip to content

Commit 97d9178

Browse files
jhuber6ronlieb
authored andcommitted
[Offload][AMDGPU] Only allow memory pool access to valid agents (llvm#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. Change-Id: I4761963f82a2c8ddcf152ba254f6d662c495dd4a
1 parent 4c61253 commit 97d9178

File tree

1 file changed

+29
-9
lines changed
  • openmp/libomptarget/plugins-nextgen/amdgpu/src

1 file changed

+29
-9
lines changed

openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ struct AMDGPUMemoryPoolTy {
414414
return Plugin::check(Status, "Error in hsa_amd_memory_pool_free: %s");
415415
}
416416

417+
/// Returns if the \p Agent can access the memory pool.
418+
bool canAccess(hsa_agent_t Agent) {
419+
hsa_amd_memory_pool_access_t Access;
420+
if (hsa_amd_agent_memory_pool_get_info(
421+
Agent, MemoryPool, HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS, &Access))
422+
return false;
423+
return Access != HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED;
424+
}
425+
417426
/// Allow the device to access a specific allocation.
418427
Error enableAccess(void *Ptr, int64_t Size,
419428
const llvm::SmallVector<hsa_agent_t> &Agents) const {
@@ -4726,10 +4735,15 @@ void *AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
47264735
}
47274736
assert(Ptr && "Invalid pointer");
47284737

4729-
auto &KernelAgents = Plugin::get<AMDGPUPluginTy>().getKernelAgents();
4738+
// Get a list of agents that can access this memory pool.
4739+
llvm::SmallVector<hsa_agent_t> Agents;
4740+
llvm::copy_if(
4741+
Plugin::get<AMDGPUPluginTy>().getKernelAgents(),
4742+
std::back_inserter(Agents),
4743+
[&](hsa_agent_t Agent) { return MemoryPool->canAccess(Agent); });
47304744

4731-
// Allow all kernel agents to access the allocation.
4732-
if (auto Err = MemoryPool->enableAccess(Ptr, Size, KernelAgents)) {
4745+
// Allow all valid kernel agents to access the allocation.
4746+
if (auto Err = MemoryPool->enableAccess(Ptr, Size, Agents)) {
47334747
REPORT("%s\n", toString(std::move(Err)).data());
47344748
return nullptr;
47354749
}
@@ -4769,12 +4783,18 @@ void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
47694783
}
47704784

47714785
if (Alloc) {
4772-
auto &KernelAgents = Plugin::get<AMDGPUPluginTy>().getKernelAgents();
4773-
// Inherently necessary for host or shared allocations
4774-
// Also enabled for device memory to allow device to device memcpy
4775-
4776-
// Enable all kernel agents to access the buffer.
4777-
if (auto Err = MemoryPool->enableAccess(Alloc, Size, KernelAgents)) {
4786+
// Get a list of agents that can access this memory pool. Inherently
4787+
// necessary for host or shared allocations Also enabled for device memory
4788+
// to allow device to device memcpy
4789+
llvm::SmallVector<hsa_agent_t> Agents;
4790+
llvm::copy_if(static_cast<AMDGPUPluginTy &>
4791+
(Plugin::get<AMDGPUPluginTy>()).getKernelAgents(),
4792+
std::back_inserter(Agents), [&](hsa_agent_t Agent) {
4793+
return MemoryPool->canAccess(Agent);
4794+
});
4795+
4796+
// Enable all valid kernel agents to access the buffer.
4797+
if (auto Err = MemoryPool->enableAccess(Alloc, Size, Agents)) {
47784798
REPORT("%s\n", toString(std::move(Err)).data());
47794799
return nullptr;
47804800
}

0 commit comments

Comments
 (0)