Skip to content

Commit bd44019

Browse files
committed
bring in [Libomptarget][NFCI] Move logic out of PluginAdaptorTy (llvm#86971)
Change-Id: If41e44d435e53663cbc6baa27322345b9102b540
1 parent fb3f431 commit bd44019

File tree

2 files changed

+55
-71
lines changed

2 files changed

+55
-71
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: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Error PluginAdaptorTy::init() {
8080
}
8181

8282
// No devices are supported by this RTL?
83-
NumberOfPluginDevices = number_of_devices();
83+
int32_t NumberOfPluginDevices = number_of_devices();
8484
if (!NumberOfPluginDevices) {
8585
return createStringError(inconvertibleErrorCode(),
8686
"No devices supported in this RTL\n");
@@ -112,26 +112,27 @@ void PluginManager::init() {
112112
DP("RTLs loaded!\n");
113113
}
114114

115-
void PluginAdaptorTy::initDevices(PluginManager &PM) {
116-
if (isUsed())
115+
void PluginManager::initDevices(PluginAdaptorTy &RTL) {
116+
// If this RTL has already been initialized.
117+
if (PM->DeviceOffsets.contains(&RTL))
117118
return;
118119
TIMESCOPE();
119120

120121
// If this RTL is not already in use, initialize it.
121-
assert(getNumberOfPluginDevices() > 0 &&
122+
assert(RTL.number_of_devices() > 0 &&
122123
"Tried to initialize useless plugin adaptor");
123124

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

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

130-
// If possible, set the device identifier offset in the plugin.
131-
if (set_device_offset)
132-
set_device_offset(DeviceOffset);
131+
// Set the device identifier offset in the plugin.
132+
RTL.set_device_offset(DeviceOffset);
133133

134-
int32_t NumPD = getNumberOfPluginDevices();
134+
int32_t NumberOfUserDevices = 0;
135+
int32_t NumPD = RTL.number_of_devices();
135136
ExclusiveDevicesAccessor->reserve(DeviceOffset + NumPD);
136137
// Auto zero-copy is a per-device property. We need to ensure
137138
// that all devices are suggesting to use it.
@@ -140,7 +141,7 @@ void PluginAdaptorTy::initDevices(PluginManager &PM) {
140141
// They are surfaced per-device because the related properties
141142
// are computed as such in the plugins.
142143
for (int32_t PDevI = 0, UserDevId = DeviceOffset; PDevI < NumPD; PDevI++) {
143-
auto Device = std::make_unique<DeviceTy>(this, UserDevId, PDevI);
144+
auto Device = std::make_unique<DeviceTy>(&RTL, UserDevId, PDevI);
144145
if (auto Err = Device->init()) {
145146
DP("Skip plugin known device %d: %s\n", PDevI,
146147
toString(std::move(Err)).c_str());
@@ -158,11 +159,9 @@ void PluginAdaptorTy::initDevices(PluginManager &PM) {
158159
// We do not mix APUs with discrete GPUs. Eager maps is set by a host
159160
// environment variable.
160161
bool IsAPU = false;
161-
bool SupportsUnifiedMemory = false;
162162
if (ExclusiveDevicesAccessor->size() > 0) {
163163
auto &Device = *(*ExclusiveDevicesAccessor)[0];
164164
IsAPU = Device.checkIfAPU();
165-
SupportsUnifiedMemory = Device.supportsUnifiedMemory();
166165
}
167166
bool EagerMapsRequested = BoolEnvar("OMPX_EAGER_ZERO_COPY_MAPS", false).get();
168167

@@ -171,43 +170,46 @@ void PluginAdaptorTy::initDevices(PluginManager &PM) {
171170
// If all devices suggest to use it, change requirment flags to trigger
172171
// zero-copy behavior when mapping memory.
173172
if (UseAutoZeroCopy)
174-
PM.addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
173+
addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
175174

176175
// Eager Zero-Copy Maps makes a "copy" execution turn into
177176
// an automatic zero-copy. It also applies to unified_shared_memory.
178177
// It is only available on APUs.
179178
if (IsAPU && EagerMapsRequested) {
180-
PM.addRequirements(OMPX_REQ_EAGER_ZERO_COPY_MAPS);
181-
if (!(PM.getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY))
182-
PM.addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
179+
addRequirements(OMPX_REQ_EAGER_ZERO_COPY_MAPS);
180+
if (!(getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY))
181+
addRequirements(OMPX_REQ_AUTO_ZERO_COPY);
183182
}
184183

185184
// sanity checks for zero-copy depend on specific devices: request it here
186185
if ((ExclusiveDevicesAccessor->size() > 0) &&
187-
((PM.getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY) ||
188-
(PM.getRequirements() & OMPX_REQ_AUTO_ZERO_COPY))) {
186+
((getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY) ||
187+
(getRequirements() & OMPX_REQ_AUTO_ZERO_COPY))) {
189188
// APUs are assumed to be a homogeneous set of GPUs: ask
190189
// the first device in the system to run a sanity check.
191190
auto &Device = *(*ExclusiveDevicesAccessor)[0];
192191
// just skip checks if no devices are found in the system
193192
Device.zeroCopySanityChecksAndDiag(
194-
(PM.getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY),
195-
(PM.getRequirements() & OMPX_REQ_AUTO_ZERO_COPY),
196-
(PM.getRequirements() & OMPX_REQ_EAGER_ZERO_COPY_MAPS));
193+
(getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY),
194+
(getRequirements() & OMPX_REQ_AUTO_ZERO_COPY),
195+
(getRequirements() & OMPX_REQ_EAGER_ZERO_COPY_MAPS));
197196
}
198197

198+
DeviceOffsets[&RTL] = DeviceOffset;
199+
DeviceUsed[&RTL] = NumberOfUserDevices;
199200
DP("Plugin adaptor " DPxMOD " has index %d, exposes %d out of %d devices!\n",
200-
DPxPTR(LibraryHandler.get()), DeviceOffset, NumberOfUserDevices,
201-
NumberOfPluginDevices);
201+
DPxPTR(RTL.LibraryHandler.get()), DeviceOffset, NumberOfUserDevices,
202+
RTL.number_of_devices());
202203
}
203204

204205
void PluginManager::initAllPlugins() {
205206
for (auto &R : PluginAdaptors)
206-
R->initDevices(*this);
207+
initDevices(*R);
207208
}
208209

209210
static void registerImageIntoTranslationTable(TranslationTable &TT,
210-
PluginAdaptorTy &RTL,
211+
int32_t DeviceOffset,
212+
int32_t NumberOfUserDevices,
211213
__tgt_device_image *Image) {
212214

213215
// same size, as when we increase one, we also increase the other.
@@ -216,8 +218,7 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
216218

217219
// Resize the Targets Table and Images to accommodate the new targets if
218220
// required
219-
unsigned TargetsTableMinimumSize =
220-
RTL.DeviceOffset + RTL.getNumberOfUserDevices();
221+
unsigned TargetsTableMinimumSize = DeviceOffset + NumberOfUserDevices;
221222

222223
if (TT.TargetsTable.size() < TargetsTableMinimumSize) {
223224
TT.DeviceTables.resize(TargetsTableMinimumSize, {});
@@ -227,11 +228,11 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
227228
}
228229

229230
// Register the image in all devices for this target type.
230-
for (int32_t I = 0; I < RTL.getNumberOfUserDevices(); ++I) {
231+
for (int32_t I = 0; I < NumberOfUserDevices; ++I) {
231232
// If we are changing the image we are also invalidating the target table.
232-
if (TT.TargetsImages[RTL.DeviceOffset + I] != Image) {
233-
TT.TargetsImages[RTL.DeviceOffset + I] = Image;
234-
TT.TargetsTable[RTL.DeviceOffset + I] =
233+
if (TT.TargetsImages[DeviceOffset + I] != Image) {
234+
TT.TargetsImages[DeviceOffset + I] = Image;
235+
TT.TargetsTable[DeviceOffset + I] =
235236
0; // lazy initialization of target table.
236237
}
237238
}
@@ -270,7 +271,7 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
270271
DP("Image " DPxMOD " is compatible with RTL %s!\n",
271272
DPxPTR(Img->ImageStart), R.Name.c_str());
272273

273-
R.initDevices(*this);
274+
PM->initDevices(R);
274275

275276
// Initialize (if necessary) translation table for this library.
276277
PM->TrlTblMtx.lock();
@@ -288,8 +289,10 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
288289

289290
DP("Registering image " DPxMOD " with RTL %s!\n", DPxPTR(Img->ImageStart),
290291
R.Name.c_str());
291-
registerImageIntoTranslationTable(TransTable, R, Img);
292-
R.UsedImages.insert(Img);
292+
293+
registerImageIntoTranslationTable(TransTable, PM->DeviceOffsets[&R],
294+
PM->DeviceUsed[&R], Img);
295+
PM->UsedImages.insert(Img);
293296

294297
PM->TrlTblMtx.unlock();
295298
FoundRTL = &R;
@@ -344,11 +347,11 @@ void PluginManager::unregisterLib(__tgt_bin_desc *Desc) {
344347
// Scan the RTLs that have associated images until we find one that supports
345348
// the current image. We only need to scan RTLs that are already being used.
346349
for (auto &R : PM->pluginAdaptors()) {
347-
if (!R.isUsed())
350+
if (!DeviceOffsets.contains(&R))
348351
continue;
349352

350353
// Ensure that we do not use any unused images associated with this RTL.
351-
if (!R.UsedImages.contains(Img))
354+
if (!UsedImages.contains(Img))
352355
continue;
353356

354357
FoundRTL = &R;

0 commit comments

Comments
 (0)