Skip to content

Commit 6ec68e7

Browse files
committed
Remove legacy error checking Pt 4
1 parent 45d38a2 commit 6ec68e7

File tree

4 files changed

+73
-59
lines changed

4 files changed

+73
-59
lines changed

offload/plugins-nextgen/level_zero/include/L0Memory.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,28 @@ class MemAllocatorTy {
205205

206206
public:
207207
MemPoolTy() = default;
208-
209-
/// Construct pool with allocation kind, allocator, and user options.
210-
MemPoolTy(int32_t Kind, MemAllocatorTy *_Allocator,
211-
const L0OptionsTy &Option);
212-
// Used for reduction pool
213-
MemPoolTy(MemAllocatorTy *_Allocator, const L0OptionsTy &Option);
214-
// Used for small memory pool with fixed parameters
215-
MemPoolTy(MemAllocatorTy *_Allocator);
216-
217208
MemPoolTy(const MemPoolTy &) = delete;
218209
MemPoolTy(MemPoolTy &&) = delete;
219210
MemPoolTy &operator=(const MemPoolTy &) = delete;
220211
MemPoolTy &operator=(const MemPoolTy &&) = delete;
221212
~MemPoolTy() {}
222213

223214
void printUsage();
215+
216+
/// Initialize pool with allocation kind, allocator, and user options.
217+
Error init(int32_t Kind, MemAllocatorTy *_Allocator,
218+
const L0OptionsTy &Option);
219+
// Initialize pool used for reduction pool
220+
Error init(MemAllocatorTy *_Allocator, const L0OptionsTy &Option);
221+
// Initialize pool used for small memory pool with fixed parameters
222+
Error init(MemAllocatorTy *_Allocator);
223+
224224
/// Release resources used in the pool.
225225
Error deinit();
226226

227227
/// Allocate the requested size of memory from this pool.
228228
/// AllocSize is the chunk size internally used for the returned memory.
229-
void *alloc(size_t Size, size_t &AllocSize);
229+
Expected<void *> alloc(size_t Size, size_t &AllocSize);
230230
/// Deallocate the specified memory and returns block size deallocated.
231231
size_t dealloc(void *Ptr);
232232
}; // MemPoolTy
@@ -322,8 +322,8 @@ class MemAllocatorTy {
322322
/// Allocator only supports host memory
323323
bool supportsHostMem() { return IsHostMem; }
324324

325-
void initDevicePools(L0DeviceTy &L0Device, const L0OptionsTy &Option);
326-
void initHostPool(L0ContextTy &Driver, const L0OptionsTy &Option);
325+
Error initDevicePools(L0DeviceTy &L0Device, const L0OptionsTy &Option);
326+
Error initHostPool(L0ContextTy &Driver, const L0OptionsTy &Option);
327327
void updateMaxAllocSize(L0DeviceTy &L0Device);
328328

329329
/// Allocate memory from L0 GPU RT. We use over-allocation workaround

offload/plugins-nextgen/level_zero/src/L0Context.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Error L0ContextTy::init() {
2323
CALL_ZE_RET_ERROR(zeContextCreate, zeDriver, &Desc, &zeContext);
2424
if (auto Err = EventPool.init(zeContext, 0))
2525
return Err;
26-
HostMemAllocator.initHostPool(*this, Plugin.getOptions());
26+
if (auto Err = HostMemAllocator.initHostPool(*this, Plugin.getOptions()))
27+
return Err;
2728
return Plugin::success();
2829
}
2930

offload/plugins-nextgen/level_zero/src/L0Device.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ Error L0DeviceTy::initImpl(GenericPluginTy &Plugin) {
214214
LinkCopyOrdinal = findCopyOrdinal(true);
215215
IsAsyncEnabled =
216216
isDiscreteDevice() && Options.CommandMode != CommandModeTy::Sync;
217-
MemAllocator.initDevicePools(*this, getPlugin().getOptions());
217+
if (auto Err = MemAllocator.initDevicePools(*this, getPlugin().getOptions()))
218+
return Err;
218219
l0Context.getHostMemAllocator().updateMaxAllocSize(*this);
219220
return Plugin::success();
220221
}

offload/plugins-nextgen/level_zero/src/L0Memory.cpp

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void MemAllocatorTy::MemPoolTy::BlockTy::dealloc(void *Mem) {
6363
FreeSlot = Slot;
6464
}
6565

66-
MemAllocatorTy::MemPoolTy::MemPoolTy(int32_t Kind, MemAllocatorTy *_Allocator,
67-
const L0OptionsTy &Option) {
66+
Error MemAllocatorTy::MemPoolTy::init(int32_t Kind, MemAllocatorTy *AllocatorIn,
67+
const L0OptionsTy &Option) {
6868
AllocKind = Kind;
69-
Allocator = _Allocator;
69+
Allocator = AllocatorIn;
7070

7171
// Read user-defined options
7272
const auto &UserOptions = Option.MemPoolInfo.at(AllocKind);
@@ -85,22 +85,23 @@ MemAllocatorTy::MemPoolTy::MemPoolTy(int32_t Kind, MemAllocatorTy *_Allocator,
8585
// allocation size when allocating from L0.
8686
auto MemOrErr = Allocator->allocL0(8, 0, AllocKind);
8787
if (!MemOrErr)
88-
FATAL_MESSAGE0(0, "Failed to allocate memory pool\n");
88+
return MemOrErr.takeError();
8989
void *Mem = *MemOrErr;
9090
ze_memory_allocation_properties_t AP{
9191
ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES, nullptr,
9292
ZE_MEMORY_TYPE_UNKNOWN, 0, 0};
93-
CALL_ZE_RET_VOID(zeMemGetAllocProperties, Context, Mem, &AP, nullptr);
93+
CALL_ZE_RET_ERROR(zeMemGetAllocProperties, Context, Mem, &AP, nullptr);
9494
AllocUnit = (std::max)(AP.pageSize, AllocUnit);
95-
CALL_ZE_RET_VOID(zeMemFree, Context, Mem);
95+
CALL_ZE_RET_ERROR(zeMemFree, Context, Mem);
9696

9797
bool IsDiscrete = false;
9898
if (Device) {
9999
ze_device_properties_t Properties{};
100100
Properties.deviceId = 0;
101101
Properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
102102
Properties.pNext = nullptr;
103-
CALL_ZE_RET_VOID(zeDeviceGetProperties, Device->getZeDevice(), &Properties);
103+
CALL_ZE_RET_ERROR(zeDeviceGetProperties, Device->getZeDevice(),
104+
&Properties);
104105
IsDiscrete = Device->isDiscreteDevice();
105106

106107
if (AllocKind == TARGET_ALLOC_SHARED && IsDiscrete) {
@@ -153,13 +154,14 @@ MemAllocatorTy::MemPoolTy::MemPoolTy(int32_t Kind, MemAllocatorTy *_Allocator,
153154
"Capacity = %" PRIu32 ", PoolSizeMax = %zu\n",
154155
AllocKindToStr(AllocKind), DPxPTR(Device), AllocUnit, AllocMax,
155156
BlockCapacity, PoolSizeMax);
157+
return Plugin::success();
156158
}
157159

158160
// Used for reduction pool
159-
MemAllocatorTy::MemPoolTy::MemPoolTy(MemAllocatorTy *_Allocator,
160-
const L0OptionsTy &Option) {
161+
Error MemAllocatorTy::MemPoolTy::init(MemAllocatorTy *AllocatorIn,
162+
const L0OptionsTy &Option) {
161163
AllocKind = TARGET_ALLOC_DEVICE;
162-
Allocator = _Allocator;
164+
Allocator = AllocatorIn;
163165
AllocMin = AllocUnit = 1024 << 6; // 64KB
164166
AllocMax = Option.ReductionPoolInfo[0] << 20;
165167
BlockCapacity = Option.ReductionPoolInfo[1];
@@ -178,12 +180,13 @@ MemAllocatorTy::MemPoolTy::MemPoolTy(MemAllocatorTy *_Allocator,
178180
DP("Initialized reduction scratch pool for device " DPxMOD
179181
": AllocMin = %zu, AllocMax = %zu, PoolSizeMax = %zu\n",
180182
DPxPTR(Allocator->Device), AllocMin, AllocMax, PoolSizeMax);
183+
return Plugin::success();
181184
}
182185

183186
// Used for small memory pool with fixed parameters
184-
MemAllocatorTy::MemPoolTy::MemPoolTy(MemAllocatorTy *_Allocator) {
187+
Error MemAllocatorTy::MemPoolTy::init(MemAllocatorTy *AllocatorIn) {
185188
AllocKind = TARGET_ALLOC_DEVICE;
186-
Allocator = _Allocator;
189+
Allocator = AllocatorIn;
187190
AllocMax = AllocMin;
188191
BlockCapacity = AllocUnit / AllocMax;
189192
PoolSize = 0;
@@ -195,6 +198,7 @@ MemAllocatorTy::MemPoolTy::MemPoolTy(MemAllocatorTy *_Allocator) {
195198
DP("Initialized zero-initialized reduction counter pool for "
196199
"device " DPxMOD ": AllocMin = %zu, AllocMax = %zu, PoolSizeMax = %zu\n",
197200
DPxPTR(Allocator->Device), AllocMin, AllocMax, PoolSizeMax);
201+
return Plugin::success();
198202
}
199203

200204
void MemAllocatorTy::MemPoolTy::printUsage() {
@@ -254,7 +258,8 @@ Error MemAllocatorTy::MemPoolTy::deinit() {
254258

255259
/// Allocate the requested size of memory from this pool.
256260
/// AllocSize is the chunk size internally used for the returned memory.
257-
void *MemAllocatorTy::MemPoolTy::alloc(size_t Size, size_t &AllocSize) {
261+
Expected<void *> MemAllocatorTy::MemPoolTy::alloc(size_t Size,
262+
size_t &AllocSize) {
258263
if (Size == 0 || Size > AllocMax)
259264
return nullptr;
260265

@@ -281,21 +286,15 @@ void *MemAllocatorTy::MemPoolTy::alloc(size_t Size, size_t &AllocSize) {
281286
const auto ChunkSize = BucketParams[BucketId].first;
282287
const auto BlockSize = BucketParams[BucketId].second;
283288
auto BaseOrErr = Allocator->allocL0(BlockSize, 0, AllocKind);
284-
if (!BaseOrErr) {
285-
consumeError(BaseOrErr.takeError());
286-
DP("Failed to allocate new block for %s pool\n",
287-
AllocKindToStr(AllocKind));
288-
return nullptr;
289-
}
289+
if (!BaseOrErr)
290+
return BaseOrErr.takeError();
291+
290292
void *Base = *BaseOrErr;
291293

292294
if (ZeroInit) {
293295
auto Err = Allocator->enqueueMemSet(Base, 0, BlockSize);
294-
if (Err) {
295-
consumeError(std::move(Err));
296-
DP("Failed to zero-initialize pool memory\n");
297-
return nullptr;
298-
}
296+
if (Err)
297+
return Err;
299298
}
300299

301300
BlockTy *Block = new BlockTy(Base, BlockSize, ChunkSize);
@@ -367,54 +366,60 @@ bool MemAllocatorTy::MemAllocInfoMapTy::remove(void *Ptr,
367366
return true;
368367
}
369368

370-
void MemAllocatorTy::initDevicePools(L0DeviceTy &L0Device,
371-
const L0OptionsTy &Option) {
369+
Error MemAllocatorTy::initDevicePools(L0DeviceTy &L0Device,
370+
const L0OptionsTy &Options) {
372371
SupportsLargeMem = L0Device.supportsLargeMem();
373372
IsHostMem = false;
374373
Device = &L0Device;
375374
L0Context = &L0Device.getL0Context();
376375
for (auto Kind : {TARGET_ALLOC_DEVICE, TARGET_ALLOC_SHARED}) {
377-
if (Option.MemPoolInfo.count(Kind) > 0) {
376+
if (Options.MemPoolInfo.count(Kind) > 0) {
378377
std::lock_guard<std::mutex> Lock(Mtx);
379-
Pools[Kind] = std::make_unique<MemPoolTy>(Kind, this, Option);
378+
Pools[Kind] = std::make_unique<MemPoolTy>();
379+
if (auto Err = Pools[Kind]->init(Kind, this, Options))
380+
return Err;
380381
}
381382
}
382-
ReductionPool = std::make_unique<MemPoolTy>(this, Option);
383-
CounterPool = std::make_unique<MemPoolTy>(this);
383+
ReductionPool = std::make_unique<MemPoolTy>();
384+
if (auto Err = ReductionPool->init(this, Options))
385+
return Err;
386+
CounterPool = std::make_unique<MemPoolTy>();
387+
if (auto Err = CounterPool->init(this))
388+
return Err;
384389
updateMaxAllocSize(L0Device);
390+
return Plugin::success();
385391
}
386392

387-
void MemAllocatorTy::initHostPool(L0ContextTy &Driver,
388-
const L0OptionsTy &Option) {
393+
Error MemAllocatorTy::initHostPool(L0ContextTy &Driver,
394+
const L0OptionsTy &Option) {
389395
SupportsLargeMem = Driver.supportsLargeMem();
390396
IsHostMem = true;
391397
this->L0Context = &Driver;
392398
if (Option.MemPoolInfo.count(TARGET_ALLOC_HOST) > 0) {
393399
std::lock_guard<std::mutex> Lock(Mtx);
394-
Pools[TARGET_ALLOC_HOST] =
395-
std::make_unique<MemPoolTy>(TARGET_ALLOC_HOST, this, Option);
400+
Pools[TARGET_ALLOC_HOST] = std::make_unique<MemPoolTy>();
401+
if (auto Err =
402+
Pools[TARGET_ALLOC_HOST]->init(TARGET_ALLOC_HOST, this, Option))
403+
return Err;
396404
}
405+
return Plugin::success();
397406
}
398407

399408
void MemAllocatorTy::updateMaxAllocSize(L0DeviceTy &L0Device) {
400409
// Update the maximum allocation size for this Allocator
401-
ze_device_properties_t P;
402-
P.maxMemAllocSize = 0;
403-
P.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
404-
P.pNext = nullptr;
405-
CALL_ZE_RET_VOID(zeDeviceGetProperties, L0Device.getZeDevice(), &P);
410+
auto maxMemAllocSize = L0Device.getMaxMemAllocSize();
406411

407412
if (IsHostMem) {
408413
// MaxAllocSize should be the minimum of all devices from the driver
409-
if (MaxAllocSize > P.maxMemAllocSize) {
410-
MaxAllocSize = P.maxMemAllocSize;
414+
if (MaxAllocSize > maxMemAllocSize) {
415+
MaxAllocSize = maxMemAllocSize;
411416
DP("Updated MaxAllocSize for driver " DPxMOD " to %zu\n",
412417
DPxPTR(L0Context), MaxAllocSize);
413418
}
414419
return;
415420
}
416421

417-
MaxAllocSize = P.maxMemAllocSize;
422+
MaxAllocSize = maxMemAllocSize;
418423
DP("Updated MaxAllocSize for device " DPxMOD " to %zu\n", DPxPTR(Device),
419424
MaxAllocSize);
420425
}
@@ -502,12 +507,19 @@ Expected<void *> MemAllocatorTy::alloc(size_t Size, size_t Align, int32_t Kind,
502507
if (Align > 0)
503508
AllocSize += (Align - 1);
504509
size_t PoolAllocSize = 0;
510+
MemPoolTy *Pool = nullptr;
511+
505512
if (UseScratchPool)
506-
AllocBase = ReductionPool->alloc(AllocSize, PoolAllocSize);
513+
AllocBase = &ReductionPool;
507514
else if (UseZeroInitPool)
508-
AllocBase = CounterPool->alloc(AllocSize, PoolAllocSize);
515+
AllocBase = &CounterPool;
509516
else
510-
AllocBase = Pools[Kind]->alloc(AllocSize, PoolAllocSize);
517+
AllocBase = Pools[Kind].get();
518+
519+
auto PtrOrErr = Pool->alloc(AllocSize, PoolAllocSize);
520+
if (!PtrOrErr)
521+
return PtrOrErr.takeError();
522+
AllocBase = *PtrOrErr;
511523
if (AllocBase) {
512524
uintptr_t Base = (uintptr_t)AllocBase;
513525
if (Align > 0)

0 commit comments

Comments
 (0)