Skip to content

Commit 57ea703

Browse files
committed
Revert "[Offload] Use Error for allocating/deallocating in plugins (llvm#160811)"
This reverts commit 01d761a.
1 parent 6dc3b90 commit 57ea703

File tree

6 files changed

+126
-158
lines changed

6 files changed

+126
-158
lines changed

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,7 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
623623
assert(MemoryManager && "Invalid memory manager");
624624
assert(PtrStorage && "Invalid pointer storage");
625625

626-
auto PtrStorageOrErr = MemoryManager->allocate(Size, nullptr);
627-
if (!PtrStorageOrErr)
628-
return PtrStorageOrErr.takeError();
629-
630-
*PtrStorage = *PtrStorageOrErr;
626+
*PtrStorage = MemoryManager->allocate(Size, nullptr);
631627
if (Size && *PtrStorage == nullptr)
632628
return Plugin::error(ErrorCode::OUT_OF_RESOURCES,
633629
"failure to allocate from AMDGPU memory manager");
@@ -647,12 +643,15 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
647643
private:
648644
/// Allocation callback that will be called once the memory manager does not
649645
/// have more previously allocated buffers.
650-
Expected<void *> allocate(size_t Size, void *HstPtr,
651-
TargetAllocTy Kind) override;
646+
void *allocate(size_t Size, void *HstPtr, TargetAllocTy Kind) override;
652647

653648
/// Deallocation callback that will be called by the memory manager.
654-
Error free(void *TgtPtr, TargetAllocTy Kind) override {
655-
return MemoryPool->deallocate(TgtPtr);
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;
656655
}
657656

658657
/// The underlying plugin that owns this memory manager.
@@ -3624,12 +3623,12 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
36243623
}
36253624

36263625
/// Allocate memory on the device or related to the device.
3627-
Expected<void *> allocate(size_t Size, void *, TargetAllocTy Kind) override;
3626+
void *allocate(size_t Size, void *, TargetAllocTy Kind) override;
36283627

36293628
/// Deallocate memory on the device or related to the device.
3630-
Error free(void *TgtPtr, TargetAllocTy Kind) override {
3629+
int free(void *TgtPtr, TargetAllocTy Kind) override {
36313630
if (TgtPtr == nullptr)
3632-
return Plugin::success();
3631+
return OFFLOAD_SUCCESS;
36333632

36343633
AMDGPUMemoryPoolTy *MemoryPool = nullptr;
36353634
switch (Kind) {
@@ -3645,14 +3644,17 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
36453644
break;
36463645
}
36473646

3648-
if (!MemoryPool)
3649-
return Plugin::error(ErrorCode::OUT_OF_RESOURCES,
3650-
"no memory pool for the specified allocation kind");
3647+
if (!MemoryPool) {
3648+
REPORT("No memory pool for the specified allocation kind\n");
3649+
return OFFLOAD_FAIL;
3650+
}
36513651

3652-
if (auto Err = MemoryPool->deallocate(TgtPtr))
3653-
return Err;
3652+
if (Error Err = MemoryPool->deallocate(TgtPtr)) {
3653+
REPORT("%s\n", toString(std::move(Err)).data());
3654+
return OFFLOAD_FAIL;
3655+
}
36543656

3655-
return Plugin::success();
3657+
return OFFLOAD_SUCCESS;
36563658
}
36573659

36583660
/// Synchronize current thread with the pending operations on the async info.
@@ -5739,13 +5741,14 @@ static Error Plugin::check(int32_t Code, const char *ErrFmt, ArgsTy... Args) {
57395741
return Plugin::error(OffloadErrCode, ErrFmt, Args..., Desc);
57405742
}
57415743

5742-
Expected<void *> AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
5743-
TargetAllocTy Kind) {
5744+
void *AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
5745+
TargetAllocTy Kind) {
57445746
// Allocate memory from the pool.
57455747
void *Ptr = nullptr;
5746-
if (auto Err = MemoryPool->allocate(Size, &Ptr))
5747-
return std::move(Err);
5748-
5748+
if (auto Err = MemoryPool->allocate(Size, &Ptr)) {
5749+
consumeError(std::move(Err));
5750+
return nullptr;
5751+
}
57495752
assert(Ptr && "Invalid pointer");
57505753

57515754
// Get a list of agents that can access this memory pool.
@@ -5755,13 +5758,14 @@ Expected<void *> AMDGPUMemoryManagerTy::allocate(size_t Size, void *HstPtr,
57555758
[&](hsa_agent_t Agent) { return MemoryPool->canAccess(Agent); });
57565759

57575760
// Allow all valid kernel agents to access the allocation.
5758-
if (auto Err = MemoryPool->enableAccess(Ptr, Size, Agents))
5759-
return std::move(Err);
5761+
if (auto Err = MemoryPool->enableAccess(Ptr, Size, Agents)) {
5762+
REPORT("%s\n", toString(std::move(Err)).data());
5763+
return nullptr;
5764+
}
57605765
return Ptr;
57615766
}
57625767

5763-
Expected<void *> AMDGPUDeviceTy::allocate(size_t Size, void *,
5764-
TargetAllocTy Kind) {
5768+
void *AMDGPUDeviceTy::allocate(size_t Size, void *, TargetAllocTy Kind) {
57655769
if (Size == 0)
57665770
return nullptr;
57675771

@@ -5780,14 +5784,17 @@ Expected<void *> AMDGPUDeviceTy::allocate(size_t Size, void *,
57805784
break;
57815785
}
57825786

5783-
if (!MemoryPool)
5784-
return Plugin::error(ErrorCode::UNSUPPORTED,
5785-
"no memory pool for the specified allocation kind");
5787+
if (!MemoryPool) {
5788+
REPORT("No memory pool for the specified allocation kind\n");
5789+
return nullptr;
5790+
}
57865791

57875792
// Allocate from the corresponding memory pool.
57885793
void *Alloc = nullptr;
5789-
if (auto Err = MemoryPool->allocate(Size, &Alloc))
5790-
return std::move(Err);
5794+
if (Error Err = MemoryPool->allocate(Size, &Alloc)) {
5795+
REPORT("%s\n", toString(std::move(Err)).data());
5796+
return nullptr;
5797+
}
57915798

57925799
if (Alloc && (Kind == TARGET_ALLOC_HOST || Kind == TARGET_ALLOC_SHARED ||
57935800
OMPX_EnableDevice2DeviceMemAccess)) {
@@ -5801,8 +5808,10 @@ Expected<void *> AMDGPUDeviceTy::allocate(size_t Size, void *,
58015808
});
58025809

58035810
// Enable all valid kernel agents to access the buffer.
5804-
if (auto Err = MemoryPool->enableAccess(Alloc, Size, Agents))
5805-
return std::move(Err);
5811+
if (auto Err = MemoryPool->enableAccess(Alloc, Size, Agents)) {
5812+
REPORT("%s\n", toString(std::move(Err)).data());
5813+
return nullptr;
5814+
}
58065815
}
58075816

58085817
return Alloc;

offload/plugins-nextgen/common/include/MemoryManager.h

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,18 @@
2525
#include "Shared/Utils.h"
2626
#include "omptarget.h"
2727

28-
#include "llvm/Support/Error.h"
29-
30-
namespace llvm {
31-
3228
/// Base class of per-device allocator.
3329
class DeviceAllocatorTy {
3430
public:
3531
virtual ~DeviceAllocatorTy() = default;
3632

3733
/// Allocate a memory of size \p Size . \p HstPtr is used to assist the
3834
/// allocation.
39-
virtual Expected<void *>
40-
allocate(size_t Size, void *HstPtr,
41-
TargetAllocTy Kind = TARGET_ALLOC_DEFAULT) = 0;
35+
virtual void *allocate(size_t Size, void *HstPtr,
36+
TargetAllocTy Kind = TARGET_ALLOC_DEFAULT) = 0;
4237

4338
/// Delete the pointer \p TgtPtr on the device
44-
virtual Error free(void *TgtPtr,
45-
TargetAllocTy Kind = TARGET_ALLOC_DEFAULT) = 0;
39+
virtual int free(void *TgtPtr, TargetAllocTy Kind = TARGET_ALLOC_DEFAULT) = 0;
4640
};
4741

4842
/// Class of memory manager. The memory manager is per-device by using
@@ -140,17 +134,17 @@ class MemoryManagerTy {
140134
size_t SizeThreshold = 1U << 13;
141135

142136
/// Request memory from target device
143-
Expected<void *> allocateOnDevice(size_t Size, void *HstPtr) const {
137+
void *allocateOnDevice(size_t Size, void *HstPtr) const {
144138
return DeviceAllocator.allocate(Size, HstPtr, TARGET_ALLOC_DEVICE);
145139
}
146140

147141
/// Deallocate data on device
148-
Error deleteOnDevice(void *Ptr) const { return DeviceAllocator.free(Ptr); }
142+
int deleteOnDevice(void *Ptr) const { return DeviceAllocator.free(Ptr); }
149143

150144
/// This function is called when it tries to allocate memory on device but the
151145
/// device returns out of memory. It will first free all memory in the
152146
/// FreeList and try to allocate again.
153-
Expected<void *> freeAndAllocate(size_t Size, void *HstPtr) {
147+
void *freeAndAllocate(size_t Size, void *HstPtr) {
154148
std::vector<void *> RemoveList;
155149

156150
// Deallocate all memory in FreeList
@@ -160,8 +154,7 @@ class MemoryManagerTy {
160154
if (List.empty())
161155
continue;
162156
for (const NodeTy &N : List) {
163-
if (auto Err = deleteOnDevice(N.Ptr))
164-
return Err;
157+
deleteOnDevice(N.Ptr);
165158
RemoveList.push_back(N.Ptr);
166159
}
167160
FreeLists[I].clear();
@@ -182,22 +175,14 @@ class MemoryManagerTy {
182175
/// allocate directly on the device. If a \p nullptr is returned, it might
183176
/// be because the device is OOM. In that case, it will free all unused
184177
/// memory and then try again.
185-
Expected<void *> allocateOrFreeAndAllocateOnDevice(size_t Size,
186-
void *HstPtr) {
187-
auto TgtPtrOrErr = allocateOnDevice(Size, HstPtr);
188-
if (!TgtPtrOrErr)
189-
return TgtPtrOrErr.takeError();
190-
191-
void *TgtPtr = *TgtPtrOrErr;
178+
void *allocateOrFreeAndAllocateOnDevice(size_t Size, void *HstPtr) {
179+
void *TgtPtr = allocateOnDevice(Size, HstPtr);
192180
// We cannot get memory from the device. It might be due to OOM. Let's
193181
// free all memory in FreeLists and try again.
194182
if (TgtPtr == nullptr) {
195183
DP("Failed to get memory on device. Free all memory in FreeLists and "
196184
"try again.\n");
197-
TgtPtrOrErr = freeAndAllocate(Size, HstPtr);
198-
if (!TgtPtrOrErr)
199-
return TgtPtrOrErr.takeError();
200-
TgtPtr = *TgtPtrOrErr;
185+
TgtPtr = freeAndAllocate(Size, HstPtr);
201186
}
202187

203188
if (TgtPtr == nullptr)
@@ -219,17 +204,16 @@ class MemoryManagerTy {
219204

220205
/// Destructor
221206
~MemoryManagerTy() {
222-
for (auto &PtrToNode : PtrToNodeTable) {
223-
assert(PtrToNode.second.Ptr && "nullptr in map table");
224-
if (auto Err = deleteOnDevice(PtrToNode.second.Ptr))
225-
REPORT("Failure to delete memory: %s\n",
226-
toString(std::move(Err)).data());
207+
for (auto Itr = PtrToNodeTable.begin(); Itr != PtrToNodeTable.end();
208+
++Itr) {
209+
assert(Itr->second.Ptr && "nullptr in map table");
210+
deleteOnDevice(Itr->second.Ptr);
227211
}
228212
}
229213

230214
/// Allocate memory of size \p Size from target device. \p HstPtr is used to
231215
/// assist the allocation.
232-
Expected<void *> allocate(size_t Size, void *HstPtr) {
216+
void *allocate(size_t Size, void *HstPtr) {
233217
// If the size is zero, we will not bother the target device. Just return
234218
// nullptr directly.
235219
if (Size == 0)
@@ -244,14 +228,11 @@ class MemoryManagerTy {
244228
DP("%zu is greater than the threshold %zu. Allocate it directly from "
245229
"device\n",
246230
Size, SizeThreshold);
247-
auto TgtPtrOrErr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
248-
if (!TgtPtrOrErr)
249-
return TgtPtrOrErr.takeError();
231+
void *TgtPtr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
250232

251-
DP("Got target pointer " DPxMOD ". Return directly.\n",
252-
DPxPTR(*TgtPtrOrErr));
233+
DP("Got target pointer " DPxMOD ". Return directly.\n", DPxPTR(TgtPtr));
253234

254-
return *TgtPtrOrErr;
235+
return TgtPtr;
255236
}
256237

257238
NodeTy *NodePtr = nullptr;
@@ -279,11 +260,8 @@ class MemoryManagerTy {
279260
if (NodePtr == nullptr) {
280261
DP("Cannot find a node in the FreeLists. Allocate on device.\n");
281262
// Allocate one on device
282-
auto TgtPtrOrErr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
283-
if (!TgtPtrOrErr)
284-
return TgtPtrOrErr.takeError();
263+
void *TgtPtr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
285264

286-
void *TgtPtr = *TgtPtrOrErr;
287265
if (TgtPtr == nullptr)
288266
return nullptr;
289267

@@ -304,7 +282,7 @@ class MemoryManagerTy {
304282
}
305283

306284
/// Deallocate memory pointed by \p TgtPtr
307-
Error free(void *TgtPtr) {
285+
int free(void *TgtPtr) {
308286
DP("MemoryManagerTy::free: target memory " DPxMOD ".\n", DPxPTR(TgtPtr));
309287

310288
NodeTy *P = nullptr;
@@ -336,7 +314,7 @@ class MemoryManagerTy {
336314
FreeLists[B].insert(*P);
337315
}
338316

339-
return Error::success();
317+
return OFFLOAD_SUCCESS;
340318
}
341319

342320
/// Get the size threshold from the environment variable
@@ -366,6 +344,4 @@ class MemoryManagerTy {
366344
constexpr const size_t MemoryManagerTy::BucketSize[];
367345
constexpr const int MemoryManagerTy::NumBuckets;
368346

369-
} // namespace llvm
370-
371347
#endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_COMMON_MEMORYMANAGER_H

0 commit comments

Comments
 (0)