Skip to content

Commit 879fc9e

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 daa755b commit 879fc9e

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
@@ -78,7 +78,7 @@ Error PluginAdaptorTy::init() {
7878
}
7979

8080
// No devices are supported by this RTL?
81-
NumberOfPluginDevices = number_of_devices();
81+
int32_t NumberOfPluginDevices = number_of_devices();
8282
if (!NumberOfPluginDevices) {
8383
return createStringError(inconvertibleErrorCode(),
8484
"No devices supported in this RTL\n");
@@ -110,32 +110,33 @@ void PluginManager::init() {
110110
DP("RTLs loaded!\n");
111111
}
112112

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

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

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

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

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

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

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

163166
void PluginManager::initAllPlugins() {
164167
for (auto &R : PluginAdaptors)
165-
R->initDevices(*this);
168+
initDevices(*R);
166169
}
167170

168171
static void registerImageIntoTranslationTable(TranslationTable &TT,
169-
PluginAdaptorTy &RTL,
172+
int32_t DeviceOffset,
173+
int32_t NumberOfUserDevices,
170174
__tgt_device_image *Image) {
171175

172176
// same size, as when we increase one, we also increase the other.
@@ -175,8 +179,7 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
175179

176180
// Resize the Targets Table and Images to accommodate the new targets if
177181
// required
178-
unsigned TargetsTableMinimumSize =
179-
RTL.DeviceOffset + RTL.getNumberOfUserDevices();
182+
unsigned TargetsTableMinimumSize = DeviceOffset + NumberOfUserDevices;
180183

181184
if (TT.TargetsTable.size() < TargetsTableMinimumSize) {
182185
TT.DeviceTables.resize(TargetsTableMinimumSize, {});
@@ -186,11 +189,11 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
186189
}
187190

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

231-
R.initDevices(*this);
234+
PM->initDevices(R);
232235

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

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

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

289294
// Ensure that we do not use any unused images associated with this RTL.
290-
if (!R.UsedImages.contains(Img))
295+
if (!UsedImages.contains(Img))
291296
continue;
292297

293298
FoundRTL = &R;

0 commit comments

Comments
 (0)