Skip to content

Commit 8e104d6

Browse files
authored
[Offload] Provide proper memory management for Images on host device (#146066)
The `unloadBinaryImpl` method on the host plugin is now implemented properly (rather than just being a stub). When an image is unloaded, it is deallocated and the library associated with it is closed.
1 parent d1fe7a2 commit 8e104d6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,8 @@ struct GenericPluginTy {
11981198
return reinterpret_cast<Ty *>(Allocator.Allocate(sizeof(Ty), alignof(Ty)));
11991199
}
12001200

1201+
template <typename Ty> void free(Ty *Mem) { Allocator.Deallocate(Mem); }
1202+
12011203
/// Get the reference to the global handler of this plugin.
12021204
GenericGlobalHandlerTy &getGlobalHandler() {
12031205
assert(GlobalHandler && "Global handler not initialized");

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ struct GenELF64DeviceTy : public GenericDeviceTy {
151151
///
152152
/// TODO: This currently does nothing, and should be implemented as part of
153153
/// broader memory handling logic for this plugin
154-
Error unloadBinaryImpl(DeviceImageTy *) override { return Plugin::success(); }
154+
Error unloadBinaryImpl(DeviceImageTy *Image) override {
155+
auto Elf = reinterpret_cast<GenELF64DeviceImageTy *>(Image);
156+
DynamicLibrary::closeLibrary(Elf->getDynamicLibrary());
157+
Plugin.free(Elf);
158+
return Plugin::success();
159+
}
155160

156161
/// Deinitialize the device, which is a no-op
157162
Error deinitImpl() override { return Plugin::success(); }
@@ -212,8 +217,7 @@ struct GenELF64DeviceTy : public GenericDeviceTy {
212217

213218
// Load the temporary file as a dynamic library.
214219
std::string ErrMsg;
215-
DynamicLibrary DynLib =
216-
DynamicLibrary::getPermanentLibrary(TmpFileName, &ErrMsg);
220+
DynamicLibrary DynLib = DynamicLibrary::getLibrary(TmpFileName, &ErrMsg);
217221

218222
// Check if the loaded library is valid.
219223
if (!DynLib.isValid())

0 commit comments

Comments
 (0)