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.
3329class DeviceAllocatorTy {
3430public:
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 {
366344constexpr const size_t MemoryManagerTy::BucketSize[];
367345constexpr const int MemoryManagerTy::NumBuckets;
368346
369- } // namespace llvm
370-
371347#endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_COMMON_MEMORYMANAGER_H
0 commit comments