[llvm] [Offload] Detect native ELF machine from preprocessor (PR #91282)
Joseph Huber via llvm-commits
llvm-commits at lists.llvm.org
Mon May 6 15:59:14 PDT 2024
https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/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.
>From 34177bb7a83d587ccca6d4322f7895b67484b2bf Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 6 May 2024 17:57:35 -0500
Subject: [PATCH] [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.
---
.../plugins-nextgen/common/include/Utils/ELF.h | 3 +++
offload/plugins-nextgen/common/src/Utils/ELF.cpp | 15 +++++++++++++++
offload/plugins-nextgen/host/CMakeLists.txt | 5 -----
offload/plugins-nextgen/host/src/rtl.cpp | 10 ++++------
4 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/offload/plugins-nextgen/common/include/Utils/ELF.h b/offload/plugins-nextgen/common/include/Utils/ELF.h
index 88c83d39b68ceb..f87e0a5ed02b4c 100644
--- a/offload/plugins-nextgen/common/include/Utils/ELF.h
+++ b/offload/plugins-nextgen/common/include/Utils/ELF.h
@@ -24,6 +24,9 @@ namespace elf {
/// Returns true or false if the \p Buffer is an ELF file.
bool isELF(llvm::StringRef Buffer);
+/// Returns the ELF e_machine value of the current compilation target.
+uint16_t getTargetMachine();
+
/// Checks if the given \p Object is a valid ELF matching the e_machine value.
llvm::Expected<bool> checkMachine(llvm::StringRef Object, uint16_t EMachine);
diff --git a/offload/plugins-nextgen/common/src/Utils/ELF.cpp b/offload/plugins-nextgen/common/src/Utils/ELF.cpp
index 2ae97f0f258925..90d6950b83e5ad 100644
--- a/offload/plugins-nextgen/common/src/Utils/ELF.cpp
+++ b/offload/plugins-nextgen/common/src/Utils/ELF.cpp
@@ -36,6 +36,21 @@ bool utils::elf::isELF(StringRef Buffer) {
}
}
+uint16_t utils::elf::getTargetMachine() {
+#if defined(__x86_64__)
+ return EM_X86_64;
+#elif defined(__s390x__)
+ return EM_S390;
+#elif defined(__aarch64__)
+ return EM_AARCH64;
+#elif defined(__powerpc64__)
+ return EM_PPC64;
+#else
+#warning "Unknown ELF compilation target architecture"
+ return EM_NONE;
+#endif
+}
+
template <class ELFT>
static Expected<bool>
checkMachineImpl(const object::ELFObjectFile<ELFT> &ELFObj, uint16_t EMachine) {
diff --git a/offload/plugins-nextgen/host/CMakeLists.txt b/offload/plugins-nextgen/host/CMakeLists.txt
index 6407f72e8db0a2..8a35bbd8cffe42 100644
--- a/offload/plugins-nextgen/host/CMakeLists.txt
+++ b/offload/plugins-nextgen/host/CMakeLists.txt
@@ -52,35 +52,30 @@ endif()
# Define the target specific triples and ELF machine values.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le$")
- target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_PPC64)
target_compile_definitions(omptarget.rtl.host PRIVATE
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="powerpc64le-ibm-linux-gnu")
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
"powerpc64le-ibm-linux-gnu" "powerpc64le-ibm-linux-gnu-LTO")
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64$")
- target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_PPC64)
target_compile_definitions(omptarget.rtl.host PRIVATE
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="powerpc64-ibm-linux-gnu")
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
"powerpc64-ibm-linux-gnu" "powerpc64-ibm-linux-gnu-LTO")
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64$")
- target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_X86_64)
target_compile_definitions(omptarget.rtl.host PRIVATE
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="x86_64-pc-linux-gnu")
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
"x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu-LTO")
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64$")
- target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_AARCH64)
target_compile_definitions(omptarget.rtl.host PRIVATE
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="aarch64-unknown-linux-gnu")
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
"aarch64-unknown-linux-gnu" "aarch64-unknown-linux-gnu-LTO")
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x$")
- target_compile_definitions(omptarget.rtl.host PRIVATE TARGET_ELF_ID=EM_S390)
target_compile_definitions(omptarget.rtl.host PRIVATE
LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE="s390x-ibm-linux-gnu")
list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
diff --git a/offload/plugins-nextgen/host/src/rtl.cpp b/offload/plugins-nextgen/host/src/rtl.cpp
index f0ce24249301af..85b5711bfe3860 100644
--- a/offload/plugins-nextgen/host/src/rtl.cpp
+++ b/offload/plugins-nextgen/host/src/rtl.cpp
@@ -18,6 +18,7 @@
#include "Shared/Debug.h"
#include "Shared/Environment.h"
+#include "Utils/ELF.h"
#include "GlobalHandler.h"
#include "OpenMP/OMPT/Callback.h"
@@ -33,11 +34,6 @@
// The number of devices in this plugin.
#define NUM_DEVICES 4
-// The ELF ID should be defined at compile-time by the build system.
-#ifndef TARGET_ELF_ID
-#define TARGET_ELF_ID EM_NONE
-#endif
-
// The target triple should be defined at compile-time by the build system.
#ifndef LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE
#define LIBOMPTARGET_NEXTGEN_GENERIC_PLUGIN_TRIPLE ""
@@ -410,7 +406,9 @@ struct GenELF64PluginTy final : public GenericPluginTy {
}
/// Get the ELF code to recognize the compatible binary images.
- uint16_t getMagicElfBits() const override { return ELF::TARGET_ELF_ID; }
+ uint16_t getMagicElfBits() const override {
+ return utils::elf::getTargetMachine();
+ }
/// This plugin does not support exchanging data between two devices.
bool isDataExchangable(int32_t SrcDeviceId, int32_t DstDeviceId) override {
More information about the llvm-commits
mailing list