Skip to content

Commit

Permalink
[Libomptarget] Remove extra cache for offloading entries (#77012)
Browse files Browse the repository at this point in the history
Summary:
The offloading entries right now are assumed to be baked into the binary
itself, and thus always valid whenever the library is executing. This
means that we don't need to copy them to additional storage and can
instead simply pass around references to it.

This is not likely to change in the expected operation of the OpenMP
library. Additionally, the indirection for the offload entry struct is
simply two pointers, so moving it by value is trivial.
  • Loading branch information
jhuber6 committed Jan 8, 2024
1 parent d5f84e6 commit 0fe86f9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
5 changes: 3 additions & 2 deletions openmp/libomptarget/include/DeviceImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
class DeviceImageTy {

std::unique_ptr<llvm::object::OffloadBinary> Binary;
llvm::SmallVector<std::unique_ptr<OffloadEntryTy>> OffloadEntries;

__tgt_bin_desc *BinaryDesc;
__tgt_device_image Image;
Expand All @@ -37,7 +36,9 @@ class DeviceImageTy {
__tgt_device_image &getExecutableImage() { return Image; }
__tgt_bin_desc &getBinaryDesc() { return *BinaryDesc; }

auto entries() { return llvm::make_pointee_range(OffloadEntries); }
auto entries() {
return llvm::make_range(Image.EntriesBegin, Image.EntriesEnd);
}
};

#endif // OMPTARGET_DEVICE_IMAGE_H
8 changes: 4 additions & 4 deletions openmp/libomptarget/include/OffloadEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class OffloadEntryTy {
const char *getNameAsCStr() const { return OffloadEntry.name; }
__tgt_bin_desc *getBinaryDescription() const;

bool isCTor() { return hasFlags(OMP_DECLARE_TARGET_CTOR); }
bool isDTor() { return hasFlags(OMP_DECLARE_TARGET_DTOR); }
bool isLink() { return hasFlags(OMP_DECLARE_TARGET_LINK); }
bool isCTor() const { return hasFlags(OMP_DECLARE_TARGET_CTOR); }
bool isDTor() const { return hasFlags(OMP_DECLARE_TARGET_DTOR); }
bool isLink() const { return hasFlags(OMP_DECLARE_TARGET_LINK); }

bool hasFlags(OpenMPOffloadingDeclareTargetFlags Flags) {
bool hasFlags(OpenMPOffloadingDeclareTargetFlags Flags) const {
return Flags & OffloadEntry.flags;
}
};
Expand Down
4 changes: 2 additions & 2 deletions openmp/libomptarget/include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct DeviceTy {
/// }

/// Register \p Entry as an offload entry that is avalable on this device.
void addOffloadEntry(OffloadEntryTy &Entry);
void addOffloadEntry(const OffloadEntryTy &Entry);

/// Print all offload entries to stderr.
void dumpOffloadEntries();
Expand All @@ -170,7 +170,7 @@ struct DeviceTy {

/// All offload entries available on this device.
using DeviceOffloadEntriesMapTy =
llvm::DenseMap<llvm::StringRef, OffloadEntryTy *>;
llvm::DenseMap<llvm::StringRef, OffloadEntryTy>;
ProtectedObj<DeviceOffloadEntriesMapTy> DeviceOffloadEntries;

/// Handler to collect and organize host-2-device mapping information.
Expand Down
4 changes: 0 additions & 4 deletions openmp/libomptarget/src/DeviceImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ DeviceImageTy::DeviceImageTy(__tgt_bin_desc &BinaryDesc,
__tgt_device_image &TgtDeviceImage)
: BinaryDesc(&BinaryDesc), Image(TgtDeviceImage) {

for (__tgt_offload_entry &Entry :
llvm::make_range(Image.EntriesBegin, Image.EntriesEnd))
OffloadEntries.emplace_back(std::make_unique<OffloadEntryTy>(*this, Entry));

llvm::StringRef ImageStr(
static_cast<char *>(Image.ImageStart),
llvm::omp::target::getPtrDiff(Image.ImageEnd, Image.ImageStart));
Expand Down
4 changes: 2 additions & 2 deletions openmp/libomptarget/src/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void PluginAdaptorTy::addOffloadEntries(DeviceImageTy &DI) {
toString(DeviceOrErr.takeError()).c_str());

DeviceTy &Device = *DeviceOrErr;
for (OffloadEntryTy &Entry : DI.entries())
Device.addOffloadEntry(Entry);
for (__tgt_offload_entry &Entry : DI.entries())
Device.addOffloadEntry(OffloadEntryTy(DI, Entry));
}
}

Expand Down
15 changes: 7 additions & 8 deletions openmp/libomptarget/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,9 @@ int32_t DeviceTy::destroyEvent(void *Event) {
return OFFLOAD_SUCCESS;
}

void DeviceTy::addOffloadEntry(OffloadEntryTy &Entry) {
void DeviceTy::addOffloadEntry(const OffloadEntryTy &Entry) {
std::lock_guard<decltype(PendingGlobalsMtx)> Lock(PendingGlobalsMtx);
DeviceOffloadEntries.getExclusiveAccessor()->insert(
{Entry.getName(), &Entry});
DeviceOffloadEntries.getExclusiveAccessor()->insert({Entry.getName(), Entry});
if (Entry.isGlobal())
return;

Expand Down Expand Up @@ -329,14 +328,14 @@ void DeviceTy::dumpOffloadEntries() {
fprintf(stderr, "Device %i offload entries:\n", DeviceID);
for (auto &It : *DeviceOffloadEntries.getExclusiveAccessor()) {
const char *Kind = "kernel";
if (It.second->isCTor())
if (It.second.isCTor())
Kind = "constructor";
else if (It.second->isDTor())
else if (It.second.isDTor())
Kind = "destructor";
else if (It.second->isLink())
else if (It.second.isLink())
Kind = "link";
else if (It.second->isGlobal())
else if (It.second.isGlobal())
Kind = "global var.";
fprintf(stderr, " %11s: %s\n", Kind, It.second->getNameAsCStr());
fprintf(stderr, " %11s: %s\n", Kind, It.second.getNameAsCStr());
}
}

0 comments on commit 0fe86f9

Please sign in to comment.