Skip to content

Commit fc56481

Browse files
committed
[Libomptarget][NFCI] Move logic out of PluginAdaptorTy
Summary: This patch removes most of the special handling from the `PluginAdaptorTy` in preparation for changing this to be the `GenericPluginTy`. Doing this requires that the OpenMP specific handling of stuff like device offsets be contained within the OpenMP plugin manager. Generally this was uninvasive expect for the change to tracking the offset and size of the used devices. The eaiest way I could think to do this was to use some maps, which double as indicators for which plugins have devices active. This should not affect the logic.
1 parent b7059c7 commit fc56481

File tree

2 files changed

+47
-61
lines changed

2 files changed

+47
-61
lines changed

openmp/libomptarget/include/PluginManager.h

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@ struct PluginAdaptorTy {
4545
static llvm::Expected<std::unique_ptr<PluginAdaptorTy>>
4646
create(const std::string &Name);
4747

48-
/// Initialize as many devices as possible for this plugin adaptor. Devices
49-
/// that fail to initialize are ignored.
50-
void initDevices(PluginManager &PM);
51-
52-
bool isUsed() const { return DeviceOffset >= 0; }
53-
54-
/// Return the number of devices visible to the underlying plugin.
55-
int32_t getNumberOfPluginDevices() const { return NumberOfPluginDevices; }
56-
57-
/// Return the number of devices successfully initialized and visible to the
58-
/// user.
59-
int32_t getNumberOfUserDevices() const { return NumberOfUserDevices; }
60-
61-
/// RTL index, index is the number of devices of other RTLs that were
62-
/// registered before, i.e. the OpenMP index of the first device to be
63-
/// registered with this RTL.
64-
int32_t DeviceOffset = -1;
65-
6648
/// Name of the shared object file representing the plugin.
6749
std::string Name;
6850

@@ -76,16 +58,6 @@ struct PluginAdaptorTy {
7658
#include "Shared/PluginAPI.inc"
7759
#undef PLUGIN_API_HANDLE
7860

79-
llvm::DenseSet<const __tgt_device_image *> UsedImages;
80-
81-
private:
82-
/// Number of devices the underling plugins sees.
83-
int32_t NumberOfPluginDevices = -1;
84-
85-
/// Number of devices exposed to the user. This can be less than the number of
86-
/// devices for the plugin if some failed to initialize.
87-
int32_t NumberOfUserDevices = 0;
88-
8961
/// Create a plugin adaptor for filename \p Name with a dynamic library \p DL.
9062
PluginAdaptorTy(const std::string &Name,
9163
std::unique_ptr<llvm::sys::DynamicLibrary> DL);
@@ -120,6 +92,11 @@ struct PluginManager {
12092
std::make_unique<DeviceImageTy>(TgtBinDesc, TgtDeviceImage));
12193
}
12294

95+
/// Initialize as many devices as possible for this plugin adaptor. Devices
96+
/// that fail to initialize are ignored. Returns the offset the devices were
97+
/// registered at.
98+
void initDevices(PluginAdaptorTy &RTL);
99+
123100
/// Return the device presented to the user as device \p DeviceNo if it is
124101
/// initialized and ready. Otherwise return an error explaining the problem.
125102
llvm::Expected<DeviceTy &> getDevice(uint32_t DeviceNo);
@@ -169,12 +146,7 @@ struct PluginManager {
169146
return Devices.getExclusiveAccessor();
170147
}
171148

172-
int getNumUsedPlugins() const {
173-
int NCI = 0;
174-
for (auto &P : PluginAdaptors)
175-
NCI += P->isUsed();
176-
return NCI;
177-
}
149+
int getNumUsedPlugins() const { return DeviceOffsets.size(); }
178150

179151
// Initialize all plugins.
180152
void initAllPlugins();
@@ -195,6 +167,15 @@ struct PluginManager {
195167
// List of all plugin adaptors, in use or not.
196168
llvm::SmallVector<std::unique_ptr<PluginAdaptorTy>> PluginAdaptors;
197169

170+
// Mapping of plugin adaptors to offsets in the device table.
171+
llvm::DenseMap<const PluginAdaptorTy *, int32_t> DeviceOffsets;
172+
173+
// Mapping of plugin adaptors to the number of used devices.
174+
llvm::DenseMap<const PluginAdaptorTy *, int32_t> DeviceUsed;
175+
176+
// Set of all device images currently in use.
177+
llvm::DenseSet<const __tgt_device_image *> UsedImages;
178+
198179
/// Executable images and information extracted from the input images passed
199180
/// to the runtime.
200181
llvm::SmallVector<std::unique_ptr<DeviceImageTy>> DeviceImages;

openmp/libomptarget/src/PluginManager.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Error PluginAdaptorTy::init() {
7575
}
7676

7777
// No devices are supported by this RTL?
78-
NumberOfPluginDevices = number_of_devices();
78+
int32_t NumberOfPluginDevices = number_of_devices();
7979
if (!NumberOfPluginDevices) {
8080
return createStringError(inconvertibleErrorCode(),
8181
"No devices supported in this RTL\n");
@@ -109,32 +109,33 @@ void PluginManager::init() {
109109
DP("RTLs loaded!\n");
110110
}
111111

112-
void PluginAdaptorTy::initDevices(PluginManager &PM) {
113-
if (isUsed())
112+
void PluginManager::initDevices(PluginAdaptorTy &RTL) {
113+
// If this RTL has already been initialized.
114+
if (PM->DeviceOffsets.contains(&RTL))
114115
return;
115116
TIMESCOPE();
116117

117118
// If this RTL is not already in use, initialize it.
118-
assert(getNumberOfPluginDevices() > 0 &&
119+
assert(RTL.number_of_devices() > 0 &&
119120
"Tried to initialize useless plugin adaptor");
120121

121122
// Initialize the device information for the RTL we are about to use.
122-
auto ExclusiveDevicesAccessor = PM.getExclusiveDevicesAccessor();
123+
auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor();
123124

124125
// Initialize the index of this RTL and save it in the used RTLs.
125-
DeviceOffset = ExclusiveDevicesAccessor->size();
126+
int32_t DeviceOffset = ExclusiveDevicesAccessor->size();
126127

127-
// If possible, set the device identifier offset in the plugin.
128-
if (set_device_offset)
129-
set_device_offset(DeviceOffset);
128+
// Set the device identifier offset in the plugin.
129+
RTL.set_device_offset(DeviceOffset);
130130

131-
int32_t NumPD = getNumberOfPluginDevices();
131+
int32_t NumberOfUserDevices = 0;
132+
int32_t NumPD = RTL.number_of_devices();
132133
ExclusiveDevicesAccessor->reserve(DeviceOffset + NumPD);
133134
// Auto zero-copy is a per-device property. We need to ensure
134135
// that all devices are suggesting to use it.
135136
bool UseAutoZeroCopy = !(NumPD == 0);
136137
for (int32_t PDevI = 0, UserDevId = DeviceOffset; PDevI < NumPD; PDevI++) {
137-
auto Device = std::make_unique<DeviceTy>(this, UserDevId, PDevI);
138+
auto Device = std::make_unique<DeviceTy>(&RTL, UserDevId, PDevI);
138139
if (auto Err = Device->init()) {
139140
DP("Skip plugin known device %d: %s\n", PDevI,
140141
toString(std::move(Err)).c_str());
@@ -152,20 +153,23 @@ void PluginAdaptorTy::initDevices(PluginManager &PM) {
152153
// If all devices suggest to use it, change requirment flags to trigger
153154
// zero-copy behavior when mapping memory.
154155
if (UseAutoZeroCopy)
155-
PM.addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
156+
addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
156157

158+
DeviceOffsets[&RTL] = DeviceOffset;
159+
DeviceUsed[&RTL] = NumberOfUserDevices;
157160
DP("Plugin adaptor " DPxMOD " has index %d, exposes %d out of %d devices!\n",
158-
DPxPTR(LibraryHandler.get()), DeviceOffset, NumberOfUserDevices,
159-
NumberOfPluginDevices);
161+
DPxPTR(RTL.LibraryHandler.get()), DeviceOffset, NumberOfUserDevices,
162+
RTL.number_of_devices());
160163
}
161164

162165
void PluginManager::initAllPlugins() {
163166
for (auto &R : PluginAdaptors)
164-
R->initDevices(*this);
167+
initDevices(*R);
165168
}
166169

167170
static void registerImageIntoTranslationTable(TranslationTable &TT,
168-
PluginAdaptorTy &RTL,
171+
int32_t DeviceOffset,
172+
int32_t NumberOfUserDevices,
169173
__tgt_device_image *Image) {
170174

171175
// same size, as when we increase one, we also increase the other.
@@ -174,8 +178,7 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
174178

175179
// Resize the Targets Table and Images to accommodate the new targets if
176180
// required
177-
unsigned TargetsTableMinimumSize =
178-
RTL.DeviceOffset + RTL.getNumberOfUserDevices();
181+
unsigned TargetsTableMinimumSize = DeviceOffset + NumberOfUserDevices;
179182

180183
if (TT.TargetsTable.size() < TargetsTableMinimumSize) {
181184
TT.DeviceTables.resize(TargetsTableMinimumSize, {});
@@ -185,11 +188,11 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
185188
}
186189

187190
// Register the image in all devices for this target type.
188-
for (int32_t I = 0; I < RTL.getNumberOfUserDevices(); ++I) {
191+
for (int32_t I = 0; I < NumberOfUserDevices; ++I) {
189192
// If we are changing the image we are also invalidating the target table.
190-
if (TT.TargetsImages[RTL.DeviceOffset + I] != Image) {
191-
TT.TargetsImages[RTL.DeviceOffset + I] = Image;
192-
TT.TargetsTable[RTL.DeviceOffset + I] =
193+
if (TT.TargetsImages[DeviceOffset + I] != Image) {
194+
TT.TargetsImages[DeviceOffset + I] = Image;
195+
TT.TargetsTable[DeviceOffset + I] =
193196
0; // lazy initialization of target table.
194197
}
195198
}
@@ -227,7 +230,7 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
227230
DP("Image " DPxMOD " is compatible with RTL %s!\n",
228231
DPxPTR(Img->ImageStart), R.Name.c_str());
229232

230-
R.initDevices(*this);
233+
PM->initDevices(R);
231234

232235
// Initialize (if necessary) translation table for this library.
233236
PM->TrlTblMtx.lock();
@@ -245,8 +248,10 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
245248

246249
DP("Registering image " DPxMOD " with RTL %s!\n", DPxPTR(Img->ImageStart),
247250
R.Name.c_str());
248-
registerImageIntoTranslationTable(TransTable, R, Img);
249-
R.UsedImages.insert(Img);
251+
252+
registerImageIntoTranslationTable(TransTable, PM->DeviceOffsets[&R],
253+
PM->DeviceUsed[&R], Img);
254+
PM->UsedImages.insert(Img);
250255

251256
PM->TrlTblMtx.unlock();
252257
FoundRTL = &R;
@@ -282,11 +287,11 @@ void PluginManager::unregisterLib(__tgt_bin_desc *Desc) {
282287
// Scan the RTLs that have associated images until we find one that supports
283288
// the current image. We only need to scan RTLs that are already being used.
284289
for (auto &R : PM->pluginAdaptors()) {
285-
if (!R.isUsed())
290+
if (!DeviceOffsets.contains(&R))
286291
continue;
287292

288293
// Ensure that we do not use any unused images associated with this RTL.
289-
if (!R.UsedImages.contains(Img))
294+
if (!UsedImages.contains(Img))
290295
continue;
291296

292297
FoundRTL = &R;

0 commit comments

Comments
 (0)