Skip to content
Merged
Changes from 1 commit
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
20 changes: 10 additions & 10 deletions offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ constexpr ol_platform_backend_t pluginNameToBackend(StringRef Name) {
#include "Shared/Targets.def"

Error initPlugins() {
auto *Context = new OffloadContext{};
auto &Context = OffloadContext::get();

// Attempt to create an instance of each supported plugin.
#define PLUGIN_TARGET(Name) \
do { \
Context->Platforms.emplace_back(ol_platform_impl_t{ \
Context.Platforms.emplace_back(ol_platform_impl_t{ \
std::unique_ptr<GenericPluginTy>(createPlugin_##Name()), \
pluginNameToBackend(#Name)}); \
} while (false);
#include "Shared/Targets.def"

// Preemptively initialize all devices in the plugin
for (auto &Platform : Context->Platforms) {
for (auto &Platform : Context.Platforms) {
// Do not use the host plugin - it isn't supported.
if (Platform.BackendType == OL_PLATFORM_BACKEND_UNKNOWN)
continue;
Expand All @@ -180,15 +180,13 @@ Error initPlugins() {
}

// Add the special host device
auto &HostPlatform = Context->Platforms.emplace_back(
auto &HostPlatform = Context.Platforms.emplace_back(
ol_platform_impl_t{nullptr, OL_PLATFORM_BACKEND_HOST});
HostPlatform.Devices.emplace_back(-1, nullptr, nullptr, InfoTreeNode{});
Context->HostDevice()->Platform = &HostPlatform;
Context.HostDevice()->Platform = &HostPlatform;

Context->TracingEnabled = std::getenv("OFFLOAD_TRACE");
Context->ValidationEnabled = !std::getenv("OFFLOAD_DISABLE_VALIDATION");

OffloadContextVal = Context;
Context.TracingEnabled = std::getenv("OFFLOAD_TRACE");
Context.ValidationEnabled = !std::getenv("OFFLOAD_DISABLE_VALIDATION");

return Plugin::success();
}
Expand All @@ -197,8 +195,10 @@ Error olInit_impl() {
std::lock_guard<std::mutex> Lock{OffloadContextValMutex};

std::optional<Error> InitResult;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A std::optional<Error> is weird here. The result of Error::success() is supposed to be the optional result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to work around the fact that all errors must be checked. But it turns out it's simple enough to just shift the order logic around to avoid it.

if (!isOffloadInitialized())
if (!isOffloadInitialized()) {
OffloadContextVal = new OffloadContext{};
InitResult = initPlugins();
}

OffloadContext::get().RefCount++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be calling new and delete on this if it's null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context is new'd in initPlugins(), but perhaps it makes sense doing it in olInit_impl, so I'll move it here.


Expand Down
Loading