Skip to content

Commit b7e95ab

Browse files
committed
move unsafe code out of destructors
1 parent 69abec0 commit b7e95ab

File tree

9 files changed

+54
-30
lines changed

9 files changed

+54
-30
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ class L0ContextTy {
7979
L0ContextTy &operator=(const L0ContextTy &&) = delete;
8080

8181
/// Release resources
82-
~L0ContextTy() {
82+
~L0ContextTy() {}
83+
84+
Error deinit() {
8385
EventPool.deinit();
8486
auto Err = HostMemAllocator.deinit();
8587
if (Err)
86-
consumeError(std::move(Err));
88+
return Err;
8789
if (zeContext)
88-
CALL_ZE_RET_VOID(zeContextDestroy, zeContext);
90+
CALL_ZE_RET_ERROR(zeContextDestroy, zeContext);
91+
return Error::success();
8992
}
9093

9194
auto &getPlugin() const { return Plugin; }

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,7 @@ class L0DeviceTy final : public GenericDeviceTy {
306306

307307
Error setContext() override { return Plugin::success(); }
308308
Error initImpl(GenericPluginTy &Plugin) override;
309-
Error deinitImpl() override {
310-
Programs.clear();
311-
return MemAllocator.deinit();
312-
}
313-
309+
Error deinitImpl() override;
314310
auto getZeDevice() const { return zeDevice; }
315311

316312
const L0ContextTy &getL0Context() const { return l0Context; }

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ class L0KernelTy : public GenericKernelTy {
132132
public:
133133
/// Create a L0 kernel with a name and an execution mode.
134134
L0KernelTy(const char *Name) : GenericKernelTy(Name), zeKernel(nullptr) {}
135-
~L0KernelTy() {
136-
if (zeKernel)
137-
CALL_ZE_RET_VOID(zeKernelDestroy, zeKernel);
138-
}
135+
~L0KernelTy() {}
139136
L0KernelTy(const L0KernelTy &) = delete;
140137
L0KernelTy(L0KernelTy &&) = delete;
141138
L0KernelTy &operator=(const L0KernelTy &) = delete;
@@ -150,6 +147,10 @@ class L0KernelTy : public GenericKernelTy {
150147
uint32_t NumBlocks[3], KernelArgsTy &KernelArgs,
151148
KernelLaunchParamsTy LaunchParams,
152149
AsyncInfoWrapperTy &AsyncInfoWrapper) const override;
150+
Error deinit() {
151+
CALL_ZE_RET_ERROR(zeKernelDestroy, zeKernel);
152+
return Plugin::success();
153+
}
153154

154155
Expected<uint64_t> maxGroupSize(GenericDeviceTy &GenericDevice,
155156
uint64_t DynamicMemSize) const override {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ class MemAllocatorTy {
218218
MemPoolTy(MemPoolTy &&) = delete;
219219
MemPoolTy &operator=(const MemPoolTy &) = delete;
220220
MemPoolTy &operator=(const MemPoolTy &&) = delete;
221+
~MemPoolTy() {}
221222

222223
void printUsage();
223224
/// Release resources used in the pool.
224-
~MemPoolTy();
225+
Error deinit();
225226

226227
/// Allocate the requested size of memory from this pool.
227228
/// AllocSize is the chunk size internally used for the returned memory.
@@ -313,7 +314,6 @@ class MemAllocatorTy {
313314
MemAllocatorTy(MemAllocatorTy &&) = delete;
314315
MemAllocatorTy &operator=(const MemAllocatorTy &) = delete;
315316
MemAllocatorTy &operator=(const MemAllocatorTy &&) = delete;
316-
317317
~MemAllocatorTy() {}
318318

319319
/// Release resources and report statistics if requested
@@ -517,10 +517,7 @@ class StagingBufferTy {
517517
StagingBufferTy &operator=(const StagingBufferTy &) = delete;
518518
StagingBufferTy &operator=(const StagingBufferTy &&) = delete;
519519

520-
~StagingBufferTy() {
521-
if (initialized())
522-
clear();
523-
}
520+
~StagingBufferTy() { }
524521

525522
void clear() {
526523
ze_result_t Rc;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ class L0ProgramTy : public DeviceImageTy {
7575
L0ProgramTy(int32_t ImageId, GenericDeviceTy &Device,
7676
std::unique_ptr<MemoryBuffer> Image)
7777
: DeviceImageTy(ImageId, Device, std::move(Image)) {}
78-
79-
~L0ProgramTy();
78+
~L0ProgramTy() {}
8079

8180
L0ProgramTy(const L0ProgramTy &other) = delete;
8281
L0ProgramTy(L0ProgramTy &&) = delete;
8382
L0ProgramTy &operator=(const L0ProgramTy &) = delete;
8483
L0ProgramTy &operator=(const L0ProgramTy &&) = delete;
8584

85+
Error deinit();
86+
8687
static L0ProgramTy &makeL0Program(DeviceImageTy &Device) {
8788
return static_cast<L0ProgramTy &>(Device);
8889
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ Error L0DeviceTy::initImpl(GenericPluginTy &Plugin) {
223223
return Plugin::success();
224224
}
225225

226+
Error L0DeviceTy::deinitImpl() {
227+
for (auto &PGM : Programs)
228+
if (auto Err = PGM.deinit())
229+
return Err;
230+
return MemAllocator.deinit();
231+
}
232+
226233
int32_t L0DeviceTy::synchronize(__tgt_async_info *AsyncInfo,
227234
bool ReleaseQueue) {
228235
bool IsAsync = AsyncInfo && asyncEnabled();

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,20 @@ void MemAllocatorTy::MemPoolTy::printUsage() {
233233
}
234234

235235
/// Release resources used in the pool.
236-
MemAllocatorTy::MemPoolTy::~MemPoolTy() {
236+
Error MemAllocatorTy::MemPoolTy::deinit() {
237237
const int DebugLevel = getDebugLevel();
238238
if (DebugLevel > 0)
239239
printUsage();
240240
for (auto &Bucket : Buckets) {
241241
for (auto *Block : Bucket) {
242242
if (DebugLevel > 0)
243243
Allocator->log(0, Block->Size, AllocKind);
244-
CALL_ZE_RET_VOID(zeMemFree, Allocator->L0Context->getZeContext(),
245-
reinterpret_cast<void *>(Block->Base));
244+
CALL_ZE_RET_ERROR(zeMemFree, Allocator->L0Context->getZeContext(),
245+
reinterpret_cast<void *>(Block->Base));
246246
delete Block;
247247
}
248248
}
249+
return Plugin::success();
249250
}
250251

251252
/// Allocate the requested size of memory from this pool.
@@ -419,8 +420,23 @@ Error MemAllocatorTy::deinit() {
419420
if (Err)
420421
return Err;
421422
}
422-
ReductionPool.reset(nullptr);
423-
CounterPool.reset(nullptr);
423+
for (auto &Pool : Pools) {
424+
if (Pool) {
425+
if (auto Err = Pool->deinit())
426+
return Err;
427+
Pool.reset(nullptr);
428+
}
429+
}
430+
if (ReductionPool) {
431+
if (auto Err = ReductionPool->deinit())
432+
return Err;
433+
ReductionPool.reset(nullptr);
434+
}
435+
if (CounterPool) {
436+
if (auto Err = CounterPool->deinit())
437+
return Err;
438+
CounterPool.reset(nullptr);
439+
}
424440
// Report memory usage if requested
425441
if (getDebugLevel() > 0) {
426442
for (auto &Stat : Stats) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Error LevelZeroPluginTy::deinitImpl() {
149149
ContextTLSTable.clear();
150150
DeviceTLSTable.clear();
151151
ThreadTLSTable.clear();
152+
for (auto &Context : ContextList)
153+
if (auto Err = Context.deinit())
154+
return Err;
152155
ContextList.clear();
153156
DP("Level0 plugin deinitialized successfully\n");
154157
return Plugin::success();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ inline L0DeviceTy &L0ProgramTy::getL0Device() const {
4747
return L0DeviceTy::makeL0Device(getDevice());
4848
}
4949

50-
L0ProgramTy::~L0ProgramTy() {
50+
Error L0ProgramTy::deinit() {
5151
for (auto *Kernel : Kernels) {
52-
// We need explicit destructor and deallocate calls to release the kernels
53-
// created by `GenericDeviceTy::constructKernel()`.
54-
Kernel->~L0KernelTy();
52+
if (auto Err = Kernel->deinit())
53+
return Err;
5554
getL0Device().getPlugin().free(Kernel);
5655
}
5756
for (auto Module : Modules) {
58-
CALL_ZE_RET_VOID(zeModuleDestroy, Module);
57+
CALL_ZE_RET_ERROR(zeModuleDestroy, Module);
5958
}
59+
return Plugin::success();
6060
}
6161

6262
void L0ProgramTy::setLibModule() {

0 commit comments

Comments
 (0)