-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Offload][NFC] use unique ptrs for platforms #160888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-offload Author: Piotr Balcer (pbalcer) ChangesCurrently, devices store a raw pointer to back to their owning Platform. Platforms are stored directly inside of a vector. Modifying this vector risks invalidating all the platform pointers stored in devices. This patch allocates platforms individually, and changes devices to store a reference to its platform instead of a pointer. This is safe, because devices are guaranteed to outlive the platform they are from. Full diff: https://github.com/llvm/llvm-project/pull/160888.diff 1 Files Affected:
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 08a2e25b97d85..70fc97c15511e 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -39,12 +39,28 @@ using namespace llvm::omp::target;
using namespace llvm::omp::target::plugin;
using namespace error;
+struct ol_platform_impl_t {
+ ol_platform_impl_t(std::unique_ptr<GenericPluginTy> Plugin,
+ ol_platform_backend_t BackendType)
+ : Plugin(std::move(Plugin)), BackendType(BackendType) {}
+ std::unique_ptr<GenericPluginTy> Plugin;
+ llvm::SmallVector<std::unique_ptr<ol_device_impl_t>> Devices;
+ ol_platform_backend_t BackendType;
+
+ /// Complete all pending work for this platform and perform any needed
+ /// cleanup.
+ ///
+ /// After calling this function, no liboffload functions should be called with
+ /// this platform handle.
+ llvm::Error destroy();
+};
+
// Handle type definitions. Ideally these would be 1:1 with the plugins, but
// we add some additional data here for now to avoid churn in the plugin
// interface.
struct ol_device_impl_t {
ol_device_impl_t(int DeviceNum, GenericDeviceTy *Device,
- ol_platform_handle_t Platform, InfoTreeNode &&DevInfo)
+ ol_platform_impl_t &Platform, InfoTreeNode &&DevInfo)
: DeviceNum(DeviceNum), Device(Device), Platform(Platform),
Info(std::forward<InfoTreeNode>(DevInfo)) {}
@@ -55,7 +71,7 @@ struct ol_device_impl_t {
int DeviceNum;
GenericDeviceTy *Device;
- ol_platform_handle_t Platform;
+ ol_platform_impl_t &Platform;
InfoTreeNode Info;
llvm::SmallVector<__tgt_async_info *> OutstandingQueues;
@@ -102,20 +118,8 @@ struct ol_device_impl_t {
}
};
-struct ol_platform_impl_t {
- ol_platform_impl_t(std::unique_ptr<GenericPluginTy> Plugin,
- ol_platform_backend_t BackendType)
- : Plugin(std::move(Plugin)), BackendType(BackendType) {}
- std::unique_ptr<GenericPluginTy> Plugin;
- llvm::SmallVector<std::unique_ptr<ol_device_impl_t>> Devices;
- ol_platform_backend_t BackendType;
- /// Complete all pending work for this platform and perform any needed
- /// cleanup.
- ///
- /// After calling this function, no liboffload functions should be called with
- /// this platform handle.
- llvm::Error destroy() {
+llvm::Error ol_platform_impl_t::destroy() {
llvm::Error Result = Plugin::success();
for (auto &D : Devices)
if (auto Err = D->destroy())
@@ -125,8 +129,7 @@ struct ol_platform_impl_t {
Result = llvm::joinErrors(std::move(Result), std::move(Res));
return Result;
- }
-};
+}
struct ol_queue_impl_t {
ol_queue_impl_t(__tgt_async_info *AsyncInfo, ol_device_handle_t Device)
@@ -206,12 +209,12 @@ struct OffloadContext {
// Partitioned list of memory base addresses. Each element in this list is a
// key in AllocInfoMap
llvm::SmallVector<void *> AllocBases{};
- SmallVector<ol_platform_impl_t, 4> Platforms{};
+ SmallVector<std::unique_ptr<ol_platform_impl_t>, 4> Platforms{};
size_t RefCount;
ol_device_handle_t HostDevice() {
// The host platform is always inserted last
- return Platforms.back().Devices[0].get();
+ return Platforms.back()->Devices[0].get();
}
static OffloadContext &get() {
@@ -251,35 +254,34 @@ Error initPlugins(OffloadContext &Context) {
#define PLUGIN_TARGET(Name) \
do { \
if (StringRef(#Name) != "host") \
- Context.Platforms.emplace_back(ol_platform_impl_t{ \
+ Context.Platforms.emplace_back(std::make_unique<ol_platform_impl_t>( \
std::unique_ptr<GenericPluginTy>(createPlugin_##Name()), \
- pluginNameToBackend(#Name)}); \
+ pluginNameToBackend(#Name))); \
} while (false);
#include "Shared/Targets.def"
// Preemptively initialize all devices in the plugin
for (auto &Platform : Context.Platforms) {
- auto Err = Platform.Plugin->init();
+ auto Err = Platform->Plugin->init();
[[maybe_unused]] std::string InfoMsg = toString(std::move(Err));
- for (auto DevNum = 0; DevNum < Platform.Plugin->number_of_devices();
+ for (auto DevNum = 0; DevNum < Platform->Plugin->number_of_devices();
DevNum++) {
- if (Platform.Plugin->init_device(DevNum) == OFFLOAD_SUCCESS) {
- auto Device = &Platform.Plugin->getDevice(DevNum);
+ if (Platform->Plugin->init_device(DevNum) == OFFLOAD_SUCCESS) {
+ auto Device = &Platform->Plugin->getDevice(DevNum);
auto Info = Device->obtainInfoImpl();
if (auto Err = Info.takeError())
return Err;
- Platform.Devices.emplace_back(std::make_unique<ol_device_impl_t>(
- DevNum, Device, &Platform, std::move(*Info)));
+ Platform->Devices.emplace_back(std::make_unique<ol_device_impl_t>(
+ DevNum, Device, *Platform, std::move(*Info)));
}
}
}
// Add the special host device
auto &HostPlatform = Context.Platforms.emplace_back(
- ol_platform_impl_t{nullptr, OL_PLATFORM_BACKEND_HOST});
- HostPlatform.Devices.emplace_back(
- std::make_unique<ol_device_impl_t>(-1, nullptr, nullptr, InfoTreeNode{}));
- Context.HostDevice()->Platform = &HostPlatform;
+ std::make_unique<ol_platform_impl_t>(nullptr, OL_PLATFORM_BACKEND_HOST));
+ HostPlatform->Devices.emplace_back(
+ std::make_unique<ol_device_impl_t>(-1, nullptr, *HostPlatform, InfoTreeNode{}));
Context.TracingEnabled = std::getenv("OFFLOAD_TRACE");
Context.ValidationEnabled = !std::getenv("OFFLOAD_DISABLE_VALIDATION");
@@ -316,10 +318,10 @@ Error olShutDown_impl() {
for (auto &P : OldContext->Platforms) {
// Host plugin is nullptr and has no deinit
- if (!P.Plugin || !P.Plugin->is_initialized())
+ if (!P->Plugin || !P->Plugin->is_initialized())
continue;
- if (auto Res = P.destroy())
+ if (auto Res = P->destroy())
Result = llvm::joinErrors(std::move(Result), std::move(Res));
}
@@ -384,7 +386,7 @@ Error olGetDeviceInfoImplDetail(ol_device_handle_t Device,
// These are not implemented by the plugin interface
switch (PropName) {
case OL_DEVICE_INFO_PLATFORM:
- return Info.write<void *>(Device->Platform);
+ return Info.write<void *>(&Device->Platform);
case OL_DEVICE_INFO_TYPE:
return Info.write<ol_device_type_t>(OL_DEVICE_TYPE_GPU);
@@ -517,7 +519,7 @@ Error olGetDeviceInfoImplDetailHost(ol_device_handle_t Device,
switch (PropName) {
case OL_DEVICE_INFO_PLATFORM:
- return Info.write<void *>(Device->Platform);
+ return Info.write<void *>(&Device->Platform);
case OL_DEVICE_INFO_TYPE:
return Info.write<ol_device_type_t>(OL_DEVICE_TYPE_HOST);
case OL_DEVICE_INFO_NAME:
@@ -595,7 +597,7 @@ Error olGetDeviceInfoSize_impl(ol_device_handle_t Device,
Error olIterateDevices_impl(ol_device_iterate_cb_t Callback, void *UserData) {
for (auto &Platform : OffloadContext::get().Platforms) {
- for (auto &Device : Platform.Devices) {
+ for (auto &Device : Platform->Devices) {
if (!Callback(Device.get(), UserData)) {
break;
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me assuming it passes the unit tests, but gives @rossburton a chance to look through it.
✅ With the latest revision this PR passed the C/C++ code formatter. |
d5c8f2d
to
19919be
Compare
Did you mean me? |
Haha, sorry about that. I meant @RossBrunton, hopefully you can see how that typo happened. |
Currently, devices store a raw pointer to back to their owning Platform. Platforms are stored directly inside of a vector. Modifying this vector risks invalidating all the platform pointers stored in devices. This patch allocates platforms individually, and changes devices to store a reference to its platform instead of a pointer. This is safe, because devices are guaranteed to outlive the platform they are from.
@pbalcer Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Currently, devices store a raw pointer to back to their owning Platform. Platforms are stored directly inside of a vector. Modifying this vector risks invalidating all the platform pointers stored in devices. This patch allocates platforms individually, and changes devices to store a reference to its platform instead of a pointer. This is safe, because platforms are guaranteed to outlive the devices they contain.
Currently, devices store a raw pointer to back to their owning Platform. Platforms are stored directly inside of a vector. Modifying this vector risks invalidating all the platform pointers stored in devices.
This patch allocates platforms individually, and changes devices to store a reference to its platform instead of a pointer. This is safe, because platforms are guaranteed to outlive the devices they contain.