Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,18 @@ Error initPlugins(OffloadContext &Context) {
pluginNameToBackend(#Name)}); \
} while (false);
#include "Shared/Targets.def"
// Host platform must be added before initializing plugin devices because
// devices contain a pointer back to the owning platform, and modifying the
// Platforms vector risks reallocating the underlying storage, thus invalidating
// all the platform pointers.
Context.Platforms.emplace_back(
ol_platform_impl_t{nullptr, OL_PLATFORM_BACKEND_HOST});

// Preemptively initialize all devices in the plugin
for (auto &Platform : Context.Platforms) {
if (Platform.BackendType == OL_PLATFORM_BACKEND_HOST)
continue;

auto Err = Platform.Plugin->init();
[[maybe_unused]] std::string InfoMsg = toString(std::move(Err));
for (auto DevNum = 0; DevNum < Platform.Plugin->number_of_devices();
Expand All @@ -273,10 +282,11 @@ Error initPlugins(OffloadContext &Context) {
}
}
}
// The Context.Platforms cannot be modified after this point without updating
// all the Device.Platform pointers.

// Add the special host device
auto &HostPlatform = Context.Platforms.emplace_back(
ol_platform_impl_t{nullptr, OL_PLATFORM_BACKEND_HOST});
auto &HostPlatform = Context.Platforms.back();
HostPlatform.Devices.emplace_back(
std::make_unique<ol_device_impl_t>(-1, nullptr, nullptr, InfoTreeNode{}));
Context.HostDevice()->Platform = &HostPlatform;
Expand Down