2023-11-30 13:47:47 -08:00
|
|
|
//===-- PluginManager.h - Plugin loading and communication API --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Declarations for managing devices that are handled by RTL plugins.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef OMPTARGET_PLUGIN_MANAGER_H
|
|
|
|
#define OMPTARGET_PLUGIN_MANAGER_H
|
|
|
|
|
2024-05-09 06:35:54 -05:00
|
|
|
#include "PluginInterface.h"
|
|
|
|
|
2023-12-01 14:59:12 -08:00
|
|
|
#include "DeviceImage.h"
|
2023-12-04 17:10:37 -08:00
|
|
|
#include "ExclusiveAccess.h"
|
2023-11-30 13:47:47 -08:00
|
|
|
#include "Shared/APITypes.h"
|
2023-12-01 14:47:00 -08:00
|
|
|
#include "Shared/Requirements.h"
|
2023-11-30 13:47:47 -08:00
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2023-12-01 14:59:12 -08:00
|
|
|
#include "llvm/ADT/iterator.h"
|
2023-12-01 10:36:59 -08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2023-11-30 13:47:47 -08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2023-12-04 17:10:37 -08:00
|
|
|
#include "llvm/Support/Error.h"
|
2023-11-30 13:47:47 -08:00
|
|
|
|
2023-12-01 14:47:00 -08:00
|
|
|
#include <cstdint>
|
2023-11-30 13:47:47 -08:00
|
|
|
#include <list>
|
2023-12-01 14:59:12 -08:00
|
|
|
#include <memory>
|
2023-11-30 13:47:47 -08:00
|
|
|
#include <mutex>
|
2023-12-01 10:36:59 -08:00
|
|
|
#include <string>
|
2023-11-30 13:47:47 -08:00
|
|
|
|
2024-05-09 06:35:54 -05:00
|
|
|
using GenericPluginTy = llvm::omp::target::plugin::GenericPluginTy;
|
2023-11-30 13:47:47 -08:00
|
|
|
|
|
|
|
/// Struct for the data required to handle plugins
|
|
|
|
struct PluginManager {
|
2023-12-04 17:10:37 -08:00
|
|
|
/// Type of the devices container. We hand out DeviceTy& to queries which are
|
|
|
|
/// stable addresses regardless if the container changes.
|
|
|
|
using DeviceContainerTy = llvm::SmallVector<std::unique_ptr<DeviceTy>>;
|
|
|
|
|
|
|
|
/// Exclusive accessor type for the device container.
|
|
|
|
using ExclusiveDevicesAccessorTy = Accessor<DeviceContainerTy>;
|
|
|
|
|
2023-12-01 10:36:59 -08:00
|
|
|
PluginManager() {}
|
|
|
|
|
|
|
|
void init();
|
|
|
|
|
2024-05-09 06:35:54 -05:00
|
|
|
void deinit();
|
|
|
|
|
2023-12-01 14:34:11 -08:00
|
|
|
// Register a shared library with all (compatible) RTLs.
|
|
|
|
void registerLib(__tgt_bin_desc *Desc);
|
|
|
|
|
|
|
|
// Unregister a shared library from all RTLs.
|
|
|
|
void unregisterLib(__tgt_bin_desc *Desc);
|
2023-11-30 13:47:47 -08:00
|
|
|
|
2024-02-22 16:49:21 -06:00
|
|
|
void addDeviceImage(__tgt_bin_desc &TgtBinDesc,
|
|
|
|
__tgt_device_image &TgtDeviceImage) {
|
|
|
|
DeviceImages.emplace_back(
|
|
|
|
std::make_unique<DeviceImageTy>(TgtBinDesc, TgtDeviceImage));
|
2023-12-01 14:59:12 -08:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:10:37 -08:00
|
|
|
/// Return the device presented to the user as device \p DeviceNo if it is
|
|
|
|
/// initialized and ready. Otherwise return an error explaining the problem.
|
|
|
|
llvm::Expected<DeviceTy &> getDevice(uint32_t DeviceNo);
|
|
|
|
|
|
|
|
/// Iterate over all initialized and ready devices registered with this
|
|
|
|
/// plugin.
|
|
|
|
auto devices(ExclusiveDevicesAccessorTy &DevicesAccessor) {
|
|
|
|
return llvm::make_pointee_range(*DevicesAccessor);
|
|
|
|
}
|
|
|
|
|
2023-12-01 14:59:12 -08:00
|
|
|
/// Iterate over all device images registered with this plugin.
|
|
|
|
auto deviceImages() { return llvm::make_pointee_range(DeviceImages); }
|
2023-11-30 13:47:47 -08:00
|
|
|
|
2025-01-31 16:35:29 +01:00
|
|
|
/// Translation table retrieved from the binary
|
2023-11-30 13:47:47 -08:00
|
|
|
HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
|
|
|
|
std::mutex TrlTblMtx; ///< For Translation Table
|
|
|
|
/// Host offload entries in order of image registration
|
2025-01-21 12:05:24 -06:00
|
|
|
llvm::SmallVector<llvm::offloading::EntryTy *>
|
|
|
|
HostEntriesBeginRegistrationOrder;
|
2023-11-30 13:47:47 -08:00
|
|
|
|
|
|
|
/// Map from ptrs on the host to an entry in the Translation Table
|
|
|
|
HostPtrToTableMapTy HostPtrToTableMap;
|
|
|
|
std::mutex TblMapMtx; ///< For HostPtrToTableMap
|
|
|
|
|
2023-12-06 16:04:23 -08:00
|
|
|
// Work around for plugins that call dlopen on shared libraries that call
|
|
|
|
// tgt_register_lib during their initialisation. Stash the pointers in a
|
|
|
|
// vector until the plugins are all initialised and then register them.
|
|
|
|
bool delayRegisterLib(__tgt_bin_desc *Desc) {
|
|
|
|
if (RTLsLoaded)
|
|
|
|
return false;
|
|
|
|
DelayedBinDesc.push_back(Desc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerDelayedLibraries() {
|
|
|
|
// Only called by libomptarget constructor
|
|
|
|
RTLsLoaded = true;
|
|
|
|
for (auto *Desc : DelayedBinDesc)
|
|
|
|
__tgt_register_lib(Desc);
|
|
|
|
DelayedBinDesc.clear();
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:10:37 -08:00
|
|
|
/// Return the number of usable devices.
|
|
|
|
int getNumDevices() { return getExclusiveDevicesAccessor()->size(); }
|
|
|
|
|
|
|
|
/// Return an exclusive handle to access the devices container.
|
|
|
|
ExclusiveDevicesAccessorTy getExclusiveDevicesAccessor() {
|
|
|
|
return Devices.getExclusiveAccessor();
|
2023-11-30 16:44:47 -08:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:35:09 -07:00
|
|
|
/// Initialize \p Plugin. Returns true on success.
|
|
|
|
bool initializePlugin(GenericPluginTy &Plugin);
|
|
|
|
|
|
|
|
/// Initialize device \p DeviceNo of \p Plugin. Returns true on success.
|
|
|
|
bool initializeDevice(GenericPluginTy &Plugin, int32_t DeviceId);
|
|
|
|
|
|
|
|
/// Eagerly initialize all plugins and their devices.
|
|
|
|
void initializeAllDevices();
|
2023-12-01 10:36:59 -08:00
|
|
|
|
2024-05-09 06:35:54 -05:00
|
|
|
/// Iterator range for all plugins (in use or not, but always valid).
|
|
|
|
auto plugins() { return llvm::make_pointee_range(Plugins); }
|
2023-12-01 10:36:59 -08:00
|
|
|
|
2024-06-06 08:10:56 -05:00
|
|
|
/// Iterator range for all plugins (in use or not, but always valid).
|
|
|
|
auto plugins() const { return llvm::make_pointee_range(Plugins); }
|
|
|
|
|
2023-12-01 14:47:00 -08:00
|
|
|
/// Return the user provided requirements.
|
|
|
|
int64_t getRequirements() const { return Requirements.getRequirements(); }
|
|
|
|
|
|
|
|
/// Add \p Flags to the user provided requirements.
|
|
|
|
void addRequirements(int64_t Flags) { Requirements.addRequirements(Flags); }
|
|
|
|
|
2024-06-06 08:10:56 -05:00
|
|
|
/// Returns the number of plugins that are active.
|
|
|
|
int getNumActivePlugins() const {
|
|
|
|
int count = 0;
|
|
|
|
for (auto &R : plugins())
|
|
|
|
if (R.is_initialized())
|
|
|
|
++count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-11-30 13:47:47 -08:00
|
|
|
private:
|
2023-12-06 16:04:23 -08:00
|
|
|
bool RTLsLoaded = false;
|
|
|
|
llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
|
|
|
|
|
2024-05-09 06:35:54 -05:00
|
|
|
// List of all plugins, in use or not.
|
|
|
|
llvm::SmallVector<std::unique_ptr<GenericPluginTy>> Plugins;
|
2023-12-01 14:47:00 -08:00
|
|
|
|
2024-06-06 08:10:56 -05:00
|
|
|
// Mapping of plugins to the OpenMP device identifier.
|
|
|
|
llvm::DenseMap<std::pair<const GenericPluginTy *, int32_t>, int32_t>
|
|
|
|
DeviceIds;
|
2024-03-29 07:19:22 -05:00
|
|
|
|
|
|
|
// Set of all device images currently in use.
|
|
|
|
llvm::DenseSet<const __tgt_device_image *> UsedImages;
|
|
|
|
|
2023-12-01 14:59:12 -08:00
|
|
|
/// Executable images and information extracted from the input images passed
|
|
|
|
/// to the runtime.
|
|
|
|
llvm::SmallVector<std::unique_ptr<DeviceImageTy>> DeviceImages;
|
|
|
|
|
2023-12-01 14:47:00 -08:00
|
|
|
/// The user provided requirements.
|
|
|
|
RequirementCollection Requirements;
|
2023-12-04 17:10:37 -08:00
|
|
|
|
|
|
|
std::mutex RTLsMtx; ///< For RTLs
|
|
|
|
|
|
|
|
/// Devices associated with plugins, accesses to the container are exclusive.
|
|
|
|
ProtectedObj<DeviceContainerTy> Devices;
|
2025-01-28 07:26:13 -06:00
|
|
|
|
2025-01-31 16:35:29 +01:00
|
|
|
/// References to upgraded legacy offloading entries.
|
2025-01-28 07:26:13 -06:00
|
|
|
std::list<llvm::SmallVector<llvm::offloading::EntryTy, 0>> LegacyEntries;
|
|
|
|
std::list<llvm::SmallVector<__tgt_device_image, 0>> LegacyImages;
|
|
|
|
llvm::DenseMap<__tgt_bin_desc *, __tgt_bin_desc> UpgradedDescriptors;
|
|
|
|
__tgt_bin_desc *upgradeLegacyEntries(__tgt_bin_desc *Desc);
|
2023-11-30 13:47:47 -08:00
|
|
|
};
|
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
/// Initialize the plugin manager and OpenMP runtime.
|
|
|
|
void initRuntime();
|
|
|
|
|
|
|
|
/// Deinitialize the plugin and delete it.
|
|
|
|
void deinitRuntime();
|
|
|
|
|
2023-11-30 13:47:47 -08:00
|
|
|
extern PluginManager *PM;
|
|
|
|
|
|
|
|
#endif // OMPTARGET_PLUGIN_MANAGER_H
|