Skip to content

Commit fb0e7ae

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 69f62a0 commit fb0e7ae

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
@@ -437,6 +437,15 @@ struct AMDGPUMemoryPoolTy {
437437
return Plugin::check(Status, "Error in hsa_amd_memory_pool_free: %s");
438438
}
439439

440+
/// Returns if the \p Agent can access the memory pool.
441+
bool canAccess(hsa_agent_t Agent) {
442+
hsa_amd_memory_pool_access_t Access;
443+
if (hsa_amd_agent_memory_pool_get_info(
444+
Agent, MemoryPool, HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS, &Access))
445+
return false;
446+
return Access != HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED;
447+
}
448+
440449
/// Allow the device to access a specific allocation.
441450
Error enableAccess(void *Ptr, int64_t Size,
442451
const llvm::SmallVector<hsa_agent_t> &Agents) const {
@@ -4702,10 +4711,14 @@ void *AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
47024711
}
47034712
assert(Ptr && "Invalid pointer");
47044713

4705-
auto &KernelAgents = Plugin.getKernelAgents();
4714+
// Get a list of agents that can access this memory pool.
4715+
llvm::SmallVector<hsa_agent_t> Agents;
4716+
llvm::copy_if(
4717+
Plugin.getKernelAgents(), std::back_inserter(Agents),
4718+
[&](hsa_agent_t Agent) { return MemoryPool->canAccess(Agent); });
47064719

4707-
// Allow all kernel agents to access the allocation.
4708-
if (auto Err = MemoryPool->enableAccess(Ptr, Size, KernelAgents)) {
4720+
// Allow all valid kernel agents to access the allocation.
4721+
if (auto Err = MemoryPool->enableAccess(Ptr, Size, Agents)) {
47094722
REPORT("%s\n", toString(std::move(Err)).data());
47104723
return nullptr;
47114724
}
@@ -4745,13 +4758,17 @@ void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
47454758
}
47464759

47474760
if (Alloc) {
4748-
auto &KernelAgents =
4749-
static_cast<AMDGPUPluginTy &>(Plugin).getKernelAgents();
4750-
// Inherently necessary for host or shared allocations
4751-
// Also enabled for device memory to allow device to device memcpy
4752-
4753-
// Enable all kernel agents to access the buffer.
4754-
if (auto Err = MemoryPool->enableAccess(Alloc, Size, KernelAgents)) {
4761+
// Get a list of agents that can access this memory pool. Inherently
4762+
// necessary for host or shared allocations Also enabled for device memory
4763+
// to allow device to device memcpy
4764+
llvm::SmallVector<hsa_agent_t> Agents;
4765+
llvm::copy_if(static_cast<AMDGPUPluginTy &>(Plugin).getKernelAgents(),
4766+
std::back_inserter(Agents), [&](hsa_agent_t Agent) {
4767+
return MemoryPool->canAccess(Agent);
4768+
});
4769+
4770+
// Enable all valid kernel agents to access the buffer.
4771+
if (auto Err = MemoryPool->enableAccess(Alloc, Size, Agents)) {
47554772
REPORT("%s\n", toString(std::move(Err)).data());
47564773
return nullptr;
47574774
}

0 commit comments

Comments
 (0)