Skip to content

Commit e3938f4

Browse files
authored
[Offload] Detect native ELF machine from preprocessor (#91282)
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 5526c8a commit e3938f4

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,27 +52,22 @@ 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
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
5756
"powerpc64le-ibm-linux-gnu" "powerpc64le-ibm-linux-gnu-LTO")
5857
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
5958
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64$")
60-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_PPC64)
6159
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
6260
"powerpc64-ibm-linux-gnu" "powerpc64-ibm-linux-gnu-LTO")
6361
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
6462
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64$")
65-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_X86_64)
6663
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
6764
"x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu-LTO")
6865
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
6966
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64$")
70-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_AARCH64)
7167
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
7268
"aarch64-unknown-linux-gnu" "aarch64-unknown-linux-gnu-LTO")
7369
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
7470
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x$")
75-
target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_S390)
7671
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
7772
"s390x-ibm-linux-gnu" "s390x-ibm-linux-gnu-LTO")
7873
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)

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"
@@ -44,11 +45,6 @@
4445
// The number of devices in this plugin.
4546
#define NUM_DEVICES 4
4647

47-
// The ELF ID should be defined at compile-time by the build system.
48-
#ifndef TARGET_ELF_ID
49-
#define TARGET_ELF_ID EM_NONE
50-
#endif
51-
5248
namespace llvm {
5349
namespace omp {
5450
namespace target {
@@ -416,7 +412,9 @@ struct GenELF64PluginTy final : public GenericPluginTy {
416412
}
417413

418414
/// Get the ELF code to recognize the compatible binary images.
419-
uint16_t getMagicElfBits() const override { return ELF::TARGET_ELF_ID; }
415+
uint16_t getMagicElfBits() const override {
416+
return utils::elf::getTargetMachine();
417+
}
420418

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

0 commit comments

Comments
 (0)