Skip to content

Commit f324584

Browse files
authored
[Libomptarget][NFCI] Remove caching of created ELF files (#76080)
Summary: We currently keep a cache of created ELF files from the relevant images. This shouldn't be necessary as the entire ELF interface is generally trivially constructable and extremely cheap. The cost of constructing one of these objects is simply a size check and writing a pointer to the underlying data. Given that, keeping a cache of these images should not be necessary overall.
1 parent b37c048 commit f324584

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

openmp/libomptarget/plugins-nextgen/common/include/GlobalHandler.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ template <typename Ty> class StaticGlobalTy : public GlobalTy {
8989
/// global metadata (size, addr) from the device.
9090
/// \see getGlobalMetadataFromDevice
9191
class GenericGlobalHandlerTy {
92-
/// Map to store the ELF object files that have been loaded.
93-
llvm::DenseMap<int32_t, ELF64LEObjectFile> ELFObjectFiles;
94-
9592
/// Actually move memory between host and device. See readGlobalFromDevice and
9693
/// writeGlobalToDevice for the interface description.
9794
Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,
@@ -109,10 +106,8 @@ class GenericGlobalHandlerTy {
109106
public:
110107
virtual ~GenericGlobalHandlerTy() {}
111108

112-
/// Get the cached ELF64LEObjectFile previosuly created for a specific
113-
/// device image or create it if did not exist.
114-
const ELF64LEObjectFile *
115-
getOrCreateELFObjectFile(const GenericDeviceTy &Device, DeviceImageTy &Image);
109+
/// Helper function for getting an ELF from a device image.
110+
Expected<ELF64LEObjectFile> getELFObjectFile(DeviceImageTy &Image);
116111

117112
/// Returns whether the symbol named \p SymName is present in the given \p
118113
/// Image.

openmp/libomptarget/plugins-nextgen/common/src/GlobalHandler.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,14 @@ using namespace omp;
2525
using namespace target;
2626
using namespace plugin;
2727

28-
const ELF64LEObjectFile *
29-
GenericGlobalHandlerTy::getOrCreateELFObjectFile(const GenericDeviceTy &Device,
30-
DeviceImageTy &Image) {
28+
Expected<ELF64LEObjectFile>
29+
GenericGlobalHandlerTy::getELFObjectFile(DeviceImageTy &Image) {
30+
assert(utils::elf::isELF(Image.getMemoryBuffer().getBuffer()) &&
31+
"Input is not an ELF file");
3132

32-
auto Search = ELFObjectFiles.find(Image.getId());
33-
if (Search != ELFObjectFiles.end())
34-
// The ELF object file was already there.
35-
return &Search->second;
36-
37-
// The ELF object file we are checking is not created yet.
3833
Expected<ELF64LEObjectFile> ElfOrErr =
3934
ELF64LEObjectFile::create(Image.getMemoryBuffer());
40-
if (!ElfOrErr) {
41-
consumeError(ElfOrErr.takeError());
42-
return nullptr;
43-
}
44-
45-
auto Result =
46-
ELFObjectFiles.try_emplace(Image.getId(), std::move(ElfOrErr.get()));
47-
assert(Result.second && "Map insertion failed");
48-
assert(Result.first != ELFObjectFiles.end() && "Map insertion failed");
49-
50-
return &Result.first->second;
35+
return ElfOrErr;
5136
}
5237

5338
Error GenericGlobalHandlerTy::moveGlobalBetweenDeviceAndHost(
@@ -83,7 +68,8 @@ Error GenericGlobalHandlerTy::moveGlobalBetweenDeviceAndHost(
8368
return Err;
8469
}
8570

86-
DP("Succesfully %s %u bytes associated with global symbol '%s' %s the device "
71+
DP("Succesfully %s %u bytes associated with global symbol '%s' %s the "
72+
"device "
8773
"(%p -> %p).\n",
8874
Device2Host ? "read" : "write", HostGlobal.getSize(),
8975
HostGlobal.getName().data(), Device2Host ? "from" : "to",
@@ -98,12 +84,14 @@ bool GenericGlobalHandlerTy::isSymbolInImage(GenericDeviceTy &Device,
9884
// Get the ELF object file for the image. Notice the ELF object may already
9985
// be created in previous calls, so we can reuse it. If this is unsuccessful
10086
// just return false as we couldn't find it.
101-
const ELF64LEObjectFile *ELFObj = getOrCreateELFObjectFile(Device, Image);
102-
if (!ELFObj)
87+
auto ELFObjOrErr = getELFObjectFile(Image);
88+
if (!ELFObjOrErr) {
89+
consumeError(ELFObjOrErr.takeError());
10390
return false;
91+
}
10492

10593
// Search the ELF symbol using the symbol name.
106-
auto SymOrErr = utils::elf::getSymbol(*ELFObj, SymName);
94+
auto SymOrErr = utils::elf::getSymbol(*ELFObjOrErr, SymName);
10795
if (!SymOrErr) {
10896
consumeError(SymOrErr.takeError());
10997
return false;
@@ -117,10 +105,9 @@ Error GenericGlobalHandlerTy::getGlobalMetadataFromImage(
117105

118106
// Get the ELF object file for the image. Notice the ELF object may already
119107
// be created in previous calls, so we can reuse it.
120-
const ELF64LEObjectFile *ELFObj = getOrCreateELFObjectFile(Device, Image);
108+
auto ELFObj = getELFObjectFile(Image);
121109
if (!ELFObj)
122-
return Plugin::error("Unable to create ELF object for image %p",
123-
Image.getStart());
110+
return ELFObj.takeError();
124111

125112
// Search the ELF symbol using the symbol name.
126113
auto SymOrErr = utils::elf::getSymbol(*ELFObj, ImageGlobal.getName());

openmp/libomptarget/plugins-nextgen/cuda/src/rtl.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,15 +1063,13 @@ struct CUDADeviceTy : public GenericDeviceTy {
10631063
// automatically so we must create it ourselves. The backend will emit
10641064
// several globals that contain function pointers we can call. These are
10651065
// prefixed with a known name due to Nvidia's lack of section support.
1066-
const ELF64LEObjectFile *ELFObj =
1067-
Handler.getOrCreateELFObjectFile(*this, Image);
1068-
if (!ELFObj)
1069-
return Plugin::error("Unable to create ELF object for image %p",
1070-
Image.getStart());
1066+
auto ELFObjOrErr = Handler.getELFObjectFile(Image);
1067+
if (!ELFObjOrErr)
1068+
return ELFObjOrErr.takeError();
10711069

10721070
// Search for all symbols that contain a constructor or destructor.
10731071
SmallVector<std::pair<StringRef, uint16_t>> Funcs;
1074-
for (ELFSymbolRef Sym : ELFObj->symbols()) {
1072+
for (ELFSymbolRef Sym : ELFObjOrErr->symbols()) {
10751073
auto NameOrErr = Sym.getName();
10761074
if (!NameOrErr)
10771075
return NameOrErr.takeError();

0 commit comments

Comments
 (0)