Skip to content

Commit 4667dd6

Browse files
authored
[OpenMP][NFC] Merge elf_common into PluginInterface (#73677)
The overhead of a library and 4 files seems high without benefit. This simply tries to consolidate our structure.
1 parent 21361bb commit 4667dd6

File tree

13 files changed

+93
-173
lines changed

13 files changed

+93
-173
lines changed

openmp/libomptarget/plugins-nextgen/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "${tmachine}$")
4747

4848
LINK_LIBS
4949
PRIVATE
50-
elf_common
5150
MemoryManager
5251
PluginInterface
5352
${LIBOMPTARGET_DEP_LIBFFI_LIBRARIES}

openmp/libomptarget/plugins-nextgen/amdgpu/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ add_llvm_library(omptarget.rtl.amdgpu SHARED
8080

8181
LINK_LIBS
8282
PRIVATE
83-
elf_common
8483
MemoryManager
8584
PluginInterface
8685
${LIBOMPTARGET_DEP_LIBRARIES}

openmp/libomptarget/plugins-nextgen/common/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
add_subdirectory(OMPT)
1414
add_subdirectory(PluginInterface)
1515
add_subdirectory(MemoryManager)
16-
add_subdirectory(elf_common)

openmp/libomptarget/plugins-nextgen/common/OMPT/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ endif()
5252
target_link_libraries(OMPT
5353
PUBLIC
5454
${llvm_libs}
55-
elf_common
5655
MemoryManager
5756
)
5857

openmp/libomptarget/plugins-nextgen/common/PluginInterface/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
# NOTE: Don't try to build `PluginInterface` using `add_llvm_library` because we
1414
# don't want to export `PluginInterface` while `add_llvm_library` requires that.
1515
add_library(PluginInterface OBJECT
16-
PluginInterface.cpp GlobalHandler.cpp JIT.cpp RPC.cpp)
16+
PluginInterface.cpp
17+
GlobalHandler.cpp
18+
JIT.cpp
19+
RPC.cpp
20+
Utils/ELF.cpp
21+
)
1722

1823
# Only enable JIT for those targets that LLVM can support.
1924
string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" TargetsSupported)
@@ -58,7 +63,6 @@ endif()
5863
target_link_libraries(PluginInterface
5964
PUBLIC
6065
${llvm_libs}
61-
elf_common
6266
MemoryManager
6367
)
6468

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "GlobalHandler.h"
14-
#include "ELFSymbols.h"
1514
#include "PluginInterface.h"
1615
#include "Utilities.h"
16+
#include "Utils/ELF.h"
1717

1818
#include <cstring>
1919

@@ -115,7 +115,7 @@ Error GenericGlobalHandlerTy::getGlobalMetadataFromImage(
115115
Image.getStart());
116116

117117
// Search the ELF symbol using the symbol name.
118-
auto SymOrErr = getELFSymbol(*ELFObj, ImageGlobal.getName());
118+
auto SymOrErr = utils::elf::getSymbol(*ELFObj, ImageGlobal.getName());
119119
if (!SymOrErr)
120120
return Plugin::error("Failed ELF lookup of global '%s': %s",
121121
ImageGlobal.getName().data(),

openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "Environment.h"
1414
#include "GlobalHandler.h"
1515
#include "JIT.h"
16-
#include "elf_common.h"
16+
#include "Utils/ELF.h"
1717
#include "omptarget.h"
1818
#include "omptargetplugin.h"
1919

@@ -1636,7 +1636,7 @@ int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *TgtImage) {
16361636
if (!Plugin::isActive())
16371637
return false;
16381638

1639-
if (elf_check_machine(TgtImage, Plugin::get().getMagicElfBits()))
1639+
if (utils::elf::checkMachine(TgtImage, Plugin::get().getMagicElfBits()))
16401640
return true;
16411641

16421642
return Plugin::get().getJIT().checkBitcodeImage(*TgtImage);

openmp/libomptarget/plugins-nextgen/common/elf_common/ELFSymbols.cpp renamed to openmp/libomptarget/plugins-nextgen/common/PluginInterface/Utils/ELF.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,76 @@
1-
//===-- ELFSymbols.cpp - ELF Symbol look-up functionality -------*- C++ -*-===//
1+
//===-- Utils/ELF.cpp - Common ELF functionality --------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// Common ELF functionality for target plugins.
10+
//
11+
//===----------------------------------------------------------------------===//
812

9-
#include "ELFSymbols.h"
13+
#include "ELF.h"
14+
#include "Debug.h"
15+
16+
#include "llvm/BinaryFormat/Magic.h"
17+
#include "llvm/Object/Binary.h"
18+
#include "llvm/Object/ELFObjectFile.h"
19+
#include "llvm/Object/ELFTypes.h"
20+
#include "llvm/Object/ObjectFile.h"
21+
#include "llvm/Support/MemoryBuffer.h"
1022

1123
using namespace llvm;
12-
using namespace llvm::object;
1324
using namespace llvm::ELF;
25+
using namespace llvm::object;
26+
27+
/// If the given range of bytes [\p BytesBegin, \p BytesEnd) represents
28+
/// a valid ELF, then invoke \p Callback on the ELFObjectFileBase
29+
/// created from this range, otherwise, return 0.
30+
/// If \p Callback is invoked, then return whatever value \p Callback returns.
31+
template <typename F>
32+
static int32_t withBytesAsElf(char *BytesBegin, char *BytesEnd, F Callback) {
33+
size_t Size = BytesEnd - BytesBegin;
34+
StringRef StrBuf(BytesBegin, Size);
35+
36+
auto Magic = identify_magic(StrBuf);
37+
if (Magic != file_magic::elf && Magic != file_magic::elf_relocatable &&
38+
Magic != file_magic::elf_executable &&
39+
Magic != file_magic::elf_shared_object && Magic != file_magic::elf_core) {
40+
DP("Not an ELF image!\n");
41+
return 0;
42+
}
43+
44+
std::unique_ptr<MemoryBuffer> MemBuf =
45+
MemoryBuffer::getMemBuffer(StrBuf, "", false);
46+
Expected<std::unique_ptr<ObjectFile>> BinOrErr =
47+
ObjectFile::createELFObjectFile(MemBuf->getMemBufferRef(),
48+
/*InitContent=*/false);
49+
if (!BinOrErr) {
50+
DP("Unable to get ELF handle: %s!\n",
51+
toString(BinOrErr.takeError()).c_str());
52+
return 0;
53+
}
54+
55+
auto *Object = dyn_cast<const ELFObjectFileBase>(BinOrErr->get());
56+
57+
if (!Object) {
58+
DP("Unknown ELF format!\n");
59+
return 0;
60+
}
61+
62+
return Callback(Object);
63+
}
64+
65+
// Check whether an image is valid for execution on target_id
66+
int32_t utils::elf::checkMachine(__tgt_device_image *Image, uint16_t TargetId) {
67+
auto CheckMachine = [TargetId](const ELFObjectFileBase *Object) {
68+
return TargetId == Object->getEMachine();
69+
};
70+
return withBytesAsElf(reinterpret_cast<char *>(Image->ImageStart),
71+
reinterpret_cast<char *>(Image->ImageEnd),
72+
CheckMachine);
73+
}
1474

1575
template <class ELFT>
1676
static Expected<const typename ELFT::Sym *>
@@ -173,7 +233,7 @@ getSymTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec,
173233
}
174234

175235
Expected<const typename ELF64LE::Sym *>
176-
getELFSymbol(const ELFObjectFile<ELF64LE> &ELFObj, StringRef Name) {
236+
utils::elf::getSymbol(const ELFObjectFile<ELF64LE> &ELFObj, StringRef Name) {
177237
// First try to look up the symbol via the hash table.
178238
for (ELFSectionRef Sec : ELFObj.sections()) {
179239
if (Sec.getType() != SHT_HASH && Sec.getType() != SHT_GNU_HASH)
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
1-
//===-- ELFSymbols.h - ELF Symbol look-up functionality ---------*- C++ -*-===//
1+
//===-- Utils/ELF.h - Common ELF functionality ------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// ELF routines for obtaining a symbol from an Elf file without loading it.
9+
// Common ELF functionality for target plugins.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_COMMON_ELF_COMMON_ELF_SYMBOLS_H
14-
#define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_COMMON_ELF_COMMON_ELF_SYMBOLS_H
13+
#ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H
14+
#define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H
15+
16+
#include "omptargetplugin.h"
1517

1618
#include "llvm/Object/ELF.h"
1719
#include "llvm/Object/ELFObjectFile.h"
1820

21+
namespace utils {
22+
namespace elf {
23+
24+
/// Return non-zero, if the given \p image is an ELF object, which
25+
/// e_machine matches \p target_id; return zero otherwise.
26+
EXTERN int32_t checkMachine(__tgt_device_image *Image, uint16_t TargetId);
27+
1928
/// Returns the symbol associated with the \p Name in the \p ELFObj. It will
2029
/// first search for the hash sections to identify symbols from the hash table.
2130
/// If that fails it will fall back to a linear search in the case of an
2231
/// executable file without a hash table.
2332
llvm::Expected<const typename llvm::object::ELF64LE::Sym *>
24-
getELFSymbol(const llvm::object::ELFObjectFile<llvm::object::ELF64LE> &ELFObj,
25-
llvm::StringRef Name);
33+
getSymbol(const llvm::object::ELFObjectFile<llvm::object::ELF64LE> &ELFObj,
34+
llvm::StringRef Name);
35+
36+
} // namespace elf
37+
} // namespace utils
2638

27-
#endif
39+
#endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_ELF_UTILS_H

openmp/libomptarget/plugins-nextgen/common/elf_common/CMakeLists.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

openmp/libomptarget/plugins-nextgen/common/elf_common/elf_common.cpp

Lines changed: 0 additions & 88 deletions
This file was deleted.

openmp/libomptarget/plugins-nextgen/common/elf_common/elf_common.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

openmp/libomptarget/plugins-nextgen/cuda/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ add_llvm_library(omptarget.rtl.cuda SHARED
3434
Object
3535

3636
LINK_LIBS PRIVATE
37-
elf_common
3837
MemoryManager
3938
PluginInterface
4039
${OPENMP_PTHREAD_LIB}

0 commit comments

Comments
 (0)