@@ -623,7 +623,11 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
623623 assert (MemoryManager && " Invalid memory manager" );
624624 assert (PtrStorage && " Invalid pointer storage" );
625625
626- *PtrStorage = MemoryManager->allocate (Size, nullptr );
626+ auto PtrStorageOrErr = MemoryManager->allocate (Size, nullptr );
627+ if (!PtrStorageOrErr)
628+ return PtrStorageOrErr.takeError ();
629+
630+ *PtrStorage = *PtrStorageOrErr;
627631 if (Size && *PtrStorage == nullptr )
628632 return Plugin::error (ErrorCode::OUT_OF_RESOURCES,
629633 " failure to allocate from AMDGPU memory manager" );
@@ -643,15 +647,12 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
643647private:
644648 // / Allocation callback that will be called once the memory manager does not
645649 // / have more previously allocated buffers.
646- void *allocate (size_t Size, void *HstPtr, TargetAllocTy Kind) override ;
650+ Expected<void *> allocate (size_t Size, void *HstPtr,
651+ TargetAllocTy Kind) override ;
647652
648653 // / Deallocation callback that will be called by the memory manager.
649- int free (void *TgtPtr, TargetAllocTy Kind) override {
650- if (auto Err = MemoryPool->deallocate (TgtPtr)) {
651- consumeError (std::move (Err));
652- return OFFLOAD_FAIL;
653- }
654- return OFFLOAD_SUCCESS;
654+ Error free (void *TgtPtr, TargetAllocTy Kind) override {
655+ return MemoryPool->deallocate (TgtPtr);
655656 }
656657
657658 // / The underlying plugin that owns this memory manager.
@@ -3623,12 +3624,12 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
36233624 }
36243625
36253626 // / Allocate memory on the device or related to the device.
3626- void *allocate (size_t Size, void *, TargetAllocTy Kind) override ;
3627+ Expected< void *> allocate (size_t Size, void *, TargetAllocTy Kind) override ;
36273628
36283629 // / Deallocate memory on the device or related to the device.
3629- int free (void *TgtPtr, TargetAllocTy Kind) override {
3630+ Error free (void *TgtPtr, TargetAllocTy Kind) override {
36303631 if (TgtPtr == nullptr )
3631- return OFFLOAD_SUCCESS ;
3632+ return Plugin::success () ;
36323633
36333634 AMDGPUMemoryPoolTy *MemoryPool = nullptr ;
36343635 switch (Kind) {
@@ -3644,17 +3645,14 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
36443645 break ;
36453646 }
36463647
3647- if (!MemoryPool) {
3648- REPORT (" No memory pool for the specified allocation kind\n " );
3649- return OFFLOAD_FAIL;
3650- }
3648+ if (!MemoryPool)
3649+ return Plugin::error (ErrorCode::OUT_OF_RESOURCES,
3650+ " no memory pool for the specified allocation kind" );
36513651
3652- if (Error Err = MemoryPool->deallocate (TgtPtr)) {
3653- REPORT (" %s\n " , toString (std::move (Err)).data ());
3654- return OFFLOAD_FAIL;
3655- }
3652+ if (auto Err = MemoryPool->deallocate (TgtPtr))
3653+ return Err;
36563654
3657- return OFFLOAD_SUCCESS ;
3655+ return Plugin::success () ;
36583656 }
36593657
36603658 // / Synchronize current thread with the pending operations on the async info.
@@ -5741,14 +5739,13 @@ static Error Plugin::check(int32_t Code, const char *ErrFmt, ArgsTy... Args) {
57415739 return Plugin::error (OffloadErrCode, ErrFmt, Args..., Desc);
57425740}
57435741
5744- void *AMDGPUMemoryManagerTy::allocate (size_t Size, void *HstPtr,
5745- TargetAllocTy Kind) {
5742+ Expected< void *> AMDGPUMemoryManagerTy::allocate (size_t Size, void *HstPtr,
5743+ TargetAllocTy Kind) {
57465744 // Allocate memory from the pool.
57475745 void *Ptr = nullptr ;
5748- if (auto Err = MemoryPool->allocate (Size, &Ptr)) {
5749- consumeError (std::move (Err));
5750- return nullptr ;
5751- }
5746+ if (auto Err = MemoryPool->allocate (Size, &Ptr))
5747+ return std::move (Err);
5748+
57525749 assert (Ptr && " Invalid pointer" );
57535750
57545751 // Get a list of agents that can access this memory pool.
@@ -5758,14 +5755,13 @@ void *AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
57585755 [&](hsa_agent_t Agent) { return MemoryPool->canAccess (Agent); });
57595756
57605757 // Allow all valid kernel agents to access the allocation.
5761- if (auto Err = MemoryPool->enableAccess (Ptr, Size, Agents)) {
5762- REPORT (" %s\n " , toString (std::move (Err)).data ());
5763- return nullptr ;
5764- }
5758+ if (auto Err = MemoryPool->enableAccess (Ptr, Size, Agents))
5759+ return std::move (Err);
57655760 return Ptr;
57665761}
57675762
5768- void *AMDGPUDeviceTy::allocate (size_t Size, void *, TargetAllocTy Kind) {
5763+ Expected<void *> AMDGPUDeviceTy::allocate (size_t Size, void *,
5764+ TargetAllocTy Kind) {
57695765 if (Size == 0 )
57705766 return nullptr ;
57715767
@@ -5784,31 +5780,14 @@ void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
57845780 break ;
57855781 }
57865782
5787- if (Kind == TARGET_ALLOC_SHARED && IsEquippedWithGFX90A &&
5788- EnableGFX90ACoarseGrainSharedAlloc) {
5789- MemoryPool = CoarseGrainedMemoryPools[0 ];
5790- }
5791-
5792- if (!MemoryPool) {
5793- REPORT (" No memory pool for the specified allocation kind\n " );
5794- return nullptr ;
5795- }
5783+ if (!MemoryPool)
5784+ return Plugin::error (ErrorCode::UNSUPPORTED,
5785+ " no memory pool for the specified allocation kind" );
57965786
57975787 // Allocate from the corresponding memory pool.
57985788 void *Alloc = nullptr ;
5799- if (Error Err = MemoryPool->allocate (Size, &Alloc)) {
5800- REPORT (" %s\n " , toString (std::move (Err)).data ());
5801- return nullptr ;
5802- }
5803- if (MemoryPool == CoarseGrainedMemoryPools[0 ] && IsEquippedWithGFX90A &&
5804- EnableGFX90ACoarseGrainUsmMaps) {
5805- // Need to register in the coarse grain usm map table
5806- // if not already registered.
5807- if (auto Err = setCoarseGrainMemoryImpl (Alloc, Size, /* set_attr=*/ false )) {
5808- REPORT (" %s\n " , toString (std::move (Err)).data ());
5809- return nullptr ;
5810- }
5811- }
5789+ if (auto Err = MemoryPool->allocate (Size, &Alloc))
5790+ return std::move (Err);
58125791
58135792 if (Alloc && (Kind == TARGET_ALLOC_HOST || Kind == TARGET_ALLOC_SHARED ||
58145793 OMPX_EnableDevice2DeviceMemAccess)) {
@@ -5822,10 +5801,8 @@ void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
58225801 });
58235802
58245803 // Enable all valid kernel agents to access the buffer.
5825- if (auto Err = MemoryPool->enableAccess (Alloc, Size, Agents)) {
5826- REPORT (" %s\n " , toString (std::move (Err)).data ());
5827- return nullptr ;
5828- }
5804+ if (auto Err = MemoryPool->enableAccess (Alloc, Size, Agents))
5805+ return std::move (Err);
58295806 }
58305807
58315808 return Alloc;
0 commit comments