Skip to content

Commit 34177bb

Browse files
committed
[Offload] Detect native ELF machine from preprocessor
Summary: This gets the target's corresponding ELF value from the preprocessor. We use this to detect if a given ELF is compatible with the CPU offloading impolementation for OpenMP. Previously we used defitions from CMake, but this is easier for people to understand as there may be new users of this in the future.
1 parent 1b22eca commit 34177bb

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

offload/plugins-nextgen/common/include/Utils/ELF.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace elf {
2424
/// Returns true or false if the \p Buffer is an ELF file.
2525
bool isELF(llvm::StringRef Buffer);
2626

27+
/// Returns the ELF e_machine value of the current compilation target.
28+
uint16_t getTargetMachine();
29+
2730
/// Checks if the given \p Object is a valid ELF matching the e_machine value.
2831
llvm::Expected<bool> checkMachine(llvm::StringRef Object, uint16_t EMachine);
2932

offload/plugins-nextgen/common/src/Utils/ELF.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ bool utils::elf::isELF(StringRef Buffer) {
3636
}
3737
}
3838

39+
uint16_t utils::elf::getTargetMachine() {
40+
#if defined(__x86_64__)
41+
return EM_X86_64;
42+
#elif defined(__s390x__)
43+
return EM_S390;
44+
#elif defined(__aarch64__)
45+
return EM_AARCH64;
46+
#elif defined(__powerpc64__)
47+
return EM_PPC64;
48+
#else
49+
#warning "Unknown ELF compilation target architecture"
50+
return EM_NONE;
51+
#endif
52+
}
53+
3954
template <class ELFT>
4055
static Expected<bool>
4156
checkMachineImpl(const object::ELFObjectFile<ELFT> &ELFObj, uint16_t EMachine) {

offload/plugins-nextgen/host/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,30 @@ endif()
5252

5353
# Define the target specific triples and ELF machine values.
5454
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le$")
55-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_PPC64)
5655
target_compile_definitions(omptarget.rtl.host PRIVATE
5756
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="powerpc64le-ibm-linux-gnu")
5857
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
5958
"powerpc64le-ibm-linux-gnu" "powerpc64le-ibm-linux-gnu-LTO")
6059
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
6160
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64$")
62-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_PPC64)
6361
target_compile_definitions(omptarget.rtl.host PRIVATE
6462
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="powerpc64-ibm-linux-gnu")
6563
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
6664
"powerpc64-ibm-linux-gnu" "powerpc64-ibm-linux-gnu-LTO")
6765
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
6866
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64$")
69-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_X86_64)
7067
target_compile_definitions(omptarget.rtl.host PRIVATE
7168
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="x86_64-pc-linux-gnu")
7269
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
7370
"x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu-LTO")
7471
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
7572
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64$")
76-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_AARCH64)
7773
target_compile_definitions(omptarget.rtl.host PRIVATE
7874
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="aarch64-unknown-linux-gnu")
7975
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
8076
"aarch64-unknown-linux-gnu" "aarch64-unknown-linux-gnu-LTO")
8177
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
8278
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x$")
83-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_S390)
8479
target_compile_definitions(omptarget.rtl.host PRIVATE
8580
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="s390x-ibm-linux-gnu")
8681
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS

offload/plugins-nextgen/host/src/rtl.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "Shared/Debug.h"
2020
#include "Shared/Environment.h"
21+
#include "Utils/ELF.h"
2122

2223
#include "GlobalHandler.h"
2324
#include "OpenMP/OMPT/Callback.h"
@@ -33,11 +34,6 @@
3334
// The number of devices in this plugin.
3435
#define NUM_DEVICES 4
3536

36-
// The ELF ID should be defined at compile-time by the build system.
37-
#ifndef TARGET_ELF_ID
38-
#define TARGET_ELF_ID EM_NONE
39-
#endif
40-
4137
// The target triple should be defined at compile-time by the build system.
4238
#ifndef LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE
4339
#define LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE ""
@@ -410,7 +406,9 @@ struct GenELF64PluginTy final : public GenericPluginTy {
410406
}
411407

412408
/// Get the ELF code to recognize the compatible binary images.
413-
uint16_t getMagicElfBits() const override { return ELF::TARGET_ELF_ID; }
409+
uint16_t getMagicElfBits() const override {
410+
return utils::elf::getTargetMachine();
411+
}
414412

415413
/// This plugin does not support exchanging data between two devices.
416414
bool isDataExchangable(int32_t SrcDeviceId, int32_t DstDeviceId) override {

0 commit comments

Comments
 (0)