Skip to content

Commit df817a8

Browse files
jhuber6ronlieb
authored andcommitted
[Libomptarget][NFCI] Remove caching of created ELF files (llvm#76080)
Cherry pick from trunk. It seems this was missed in a merge and now a patch is dependent on it. 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. Change-Id: I3e09037a9daea30d886bf6c764b3433260ff2cc2
1 parent ef295e2 commit df817a8

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
@@ -86,9 +86,6 @@ template <typename Ty> struct StaticGlobalTy : public GlobalTy {
8686
/// global metadata (size, addr) from the device.
8787
/// \see getGlobalMetadataFromDevice
8888
class GenericGlobalHandlerTy {
89-
/// Map to store the ELF object files that have been loaded.
90-
llvm::DenseMap<int32_t, ELF64LEObjectFile> ELFObjectFiles;
91-
9289
/// Actually move memory between host and device. See readGlobalFromDevice and
9390
/// writeGlobalToDevice for the interface description.
9491
Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,
@@ -106,10 +103,8 @@ class GenericGlobalHandlerTy {
106103
public:
107104
virtual ~GenericGlobalHandlerTy() {}
108105

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

114109
/// Returns whether the symbol named \p SymName is present in the given \p
115110
/// 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
@@ -1175,15 +1175,13 @@ struct CUDADeviceTy : public GenericDeviceTy {
11751175
// automatically so we must create it ourselves. The backend will emit
11761176
// several globals that contain function pointers we can call. These are
11771177
// prefixed with a known name due to Nvidia's lack of section support.
1178-
const ELF64LEObjectFile *ELFObj =
1179-
Handler.getOrCreateELFObjectFile(*this, Image);
1180-
if (!ELFObj)
1181-
return Plugin::error("Unable to create ELF object for image %p",
1182-
Image.getStart());
1178+
auto ELFObjOrErr = Handler.getELFObjectFile(Image);
1179+
if (!ELFObjOrErr)
1180+
return ELFObjOrErr.takeError();
11831181

11841182
// Search for all symbols that contain a constructor or destructor.
11851183
SmallVector<std::pair<StringRef, uint16_t>> Funcs;
1186-
for (ELFSymbolRef Sym : ELFObj->symbols()) {
1184+
for (ELFSymbolRef Sym : ELFObjOrErr->symbols()) {
11871185
auto NameOrErr = Sym.getName();
11881186
if (!NameOrErr)
11891187
return NameOrErr.takeError();

0 commit comments

Comments
 (0)