From e7a7eb737fb5bfbe65c27f318a7fd5144ffd2733 Mon Sep 17 00:00:00 2001 From: Jon Chesterfield Date: Fri, 4 Dec 2020 14:32:54 +0000 Subject: [PATCH] Refactor elfutil --- openmp/libomptarget/include/elf_common.h | 16 ---- .../plugins/amdgpu/CMakeLists.txt | 2 +- .../plugins/amdgpu/impl/elf_amd.h | 15 ---- .../plugins/amdgpu/impl/elfutil.cpp | 75 ------------------- .../amdgpu/impl/get_elf_mach_gfx_name.cpp | 43 +++++++++++ .../amdgpu/impl/get_elf_mach_gfx_name.h | 8 ++ .../libomptarget/plugins/amdgpu/src/rtl.cpp | 39 +++++++++- .../libomptarget/plugins/common/elf_common.c | 38 ---------- 8 files changed, 88 insertions(+), 148 deletions(-) delete mode 100644 openmp/libomptarget/include/elf_common.h delete mode 100644 openmp/libomptarget/plugins/amdgpu/impl/elf_amd.h delete mode 100644 openmp/libomptarget/plugins/amdgpu/impl/elfutil.cpp create mode 100644 openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.cpp create mode 100644 openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.h diff --git a/openmp/libomptarget/include/elf_common.h b/openmp/libomptarget/include/elf_common.h deleted file mode 100644 index 841e0ae3b95c..000000000000 --- a/openmp/libomptarget/include/elf_common.h +++ /dev/null @@ -1,16 +0,0 @@ -//===-- elf_common.h - Elf functions needed from elf_common.cpp ---*- C -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// Get the e_rflags from ethe elf imange -uint32_t elf_e_flags(__tgt_device_image *image); - -// Verifies that the target_id for the system matches the image -int32_t elf_check_machine(__tgt_device_image *image, - uint16_t target_id); - - diff --git a/openmp/libomptarget/plugins/amdgpu/CMakeLists.txt b/openmp/libomptarget/plugins/amdgpu/CMakeLists.txt index 161f3f397eba..7d108f9ba73b 100644 --- a/openmp/libomptarget/plugins/amdgpu/CMakeLists.txt +++ b/openmp/libomptarget/plugins/amdgpu/CMakeLists.txt @@ -58,7 +58,7 @@ add_library(omptarget.rtl.amdgpu SHARED impl/atmi.cpp impl/atmi_interop_hsa.cpp impl/data.cpp - impl/elfutil.cpp + impl/get_elf_mach_gfx_name.cpp impl/machine.cpp impl/system.cpp impl/utils.cpp diff --git a/openmp/libomptarget/plugins/amdgpu/impl/elf_amd.h b/openmp/libomptarget/plugins/amdgpu/impl/elf_amd.h deleted file mode 100644 index 697eceb368ef..000000000000 --- a/openmp/libomptarget/plugins/amdgpu/impl/elf_amd.h +++ /dev/null @@ -1,15 +0,0 @@ -//===-- elf_amd.h - Elf functions needed by amd plugin rtl.cpp ----*- C -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// Convenience functions for rtl.cpp to query various AMD Elf attributes. - -extern int get_elf_mach_gfx(__tgt_device_image *image); - -extern const char *get_elf_mach_gfx_name(__tgt_device_image *image); - -extern bool elf_machine_id_is_amdgcn(__tgt_device_image *image); diff --git a/openmp/libomptarget/plugins/amdgpu/impl/elfutil.cpp b/openmp/libomptarget/plugins/amdgpu/impl/elfutil.cpp deleted file mode 100644 index 8026abfd89c1..000000000000 --- a/openmp/libomptarget/plugins/amdgpu/impl/elfutil.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/*===-------------------------------------------------------------------------- - * ATMI (Asynchronous Task and Memory Interface) - * - * This file is distributed under the MIT License. See LICENSE.txt for details. - *===------------------------------------------------------------------------*/ - -#include -#include -#include -#include - -#include "Debug.h" -#include "omptarget.h" -#include "llvm/BinaryFormat/ELF.h" - -#ifndef TARGET_NAME -#define TARGET_NAME AMDHSA -#endif -#define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL" - -#include "../../common/elf_common.c" - -using namespace llvm; -using namespace ELF; - -int get_elf_mach_gfx(__tgt_device_image *image) { - uint32_t EFlags = elf_e_flags(image); - uint32_t Gfx = (EFlags & EF_AMDGPU_MACH); - return Gfx; -} - -const char *get_elf_mach_gfx_name(__tgt_device_image *image) { - uint32_t Gfx = get_elf_mach_gfx(image); - switch (Gfx) { - case EF_AMDGPU_MACH_AMDGCN_GFX801: - return "gfx801"; - case EF_AMDGPU_MACH_AMDGCN_GFX802: - return "gfx802"; - case EF_AMDGPU_MACH_AMDGCN_GFX803: - return "gfx803"; - case EF_AMDGPU_MACH_AMDGCN_GFX810: - return "gfx810"; - case EF_AMDGPU_MACH_AMDGCN_GFX900: - return "gfx900"; - case EF_AMDGPU_MACH_AMDGCN_GFX902: - return "gfx902"; - case EF_AMDGPU_MACH_AMDGCN_GFX904: - return "gfx904"; - case EF_AMDGPU_MACH_AMDGCN_GFX906: - return "gfx906"; - case EF_AMDGPU_MACH_AMDGCN_GFX908: - return "gfx908"; - case EF_AMDGPU_MACH_AMDGCN_GFX909: - return "gfx909"; - case EF_AMDGPU_MACH_AMDGCN_GFX1010: - return "gfx1010"; - case EF_AMDGPU_MACH_AMDGCN_GFX1011: - return "gfx1011"; - case EF_AMDGPU_MACH_AMDGCN_GFX1012: - return "gfx1012"; - case EF_AMDGPU_MACH_AMDGCN_GFX1030: - return "gfx1030"; - default: - return "--unknown gfx"; - } -} - -bool elf_machine_id_is_amdgcn(__tgt_device_image *image) { - const uint16_t amdgcnMachineID = EM_AMDGPU; - int32_t r = elf_check_machine(image, amdgcnMachineID); - if (!r) { - DP("Supported machine ID not found\n"); - } - return r; -} diff --git a/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.cpp b/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.cpp new file mode 100644 index 000000000000..64af1a462a43 --- /dev/null +++ b/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.cpp @@ -0,0 +1,43 @@ +#include "get_elf_mach_gfx_name.h" + +// This header conflicts with the system elf.h (macros vs enums of the same +// identifier) and contains more up to date values for the enum checked here. +// rtl.cpp uses the system elf.h. +#include "llvm/BinaryFormat/ELF.h" + +const char *get_elf_mach_gfx_name(uint32_t EFlags) { + using namespace llvm::ELF; + uint32_t Gfx = (EFlags & EF_AMDGPU_MACH); + switch (Gfx) { + case EF_AMDGPU_MACH_AMDGCN_GFX801: + return "gfx801"; + case EF_AMDGPU_MACH_AMDGCN_GFX802: + return "gfx802"; + case EF_AMDGPU_MACH_AMDGCN_GFX803: + return "gfx803"; + case EF_AMDGPU_MACH_AMDGCN_GFX810: + return "gfx810"; + case EF_AMDGPU_MACH_AMDGCN_GFX900: + return "gfx900"; + case EF_AMDGPU_MACH_AMDGCN_GFX902: + return "gfx902"; + case EF_AMDGPU_MACH_AMDGCN_GFX904: + return "gfx904"; + case EF_AMDGPU_MACH_AMDGCN_GFX906: + return "gfx906"; + case EF_AMDGPU_MACH_AMDGCN_GFX908: + return "gfx908"; + case EF_AMDGPU_MACH_AMDGCN_GFX909: + return "gfx909"; + case EF_AMDGPU_MACH_AMDGCN_GFX1010: + return "gfx1010"; + case EF_AMDGPU_MACH_AMDGCN_GFX1011: + return "gfx1011"; + case EF_AMDGPU_MACH_AMDGCN_GFX1012: + return "gfx1012"; + case EF_AMDGPU_MACH_AMDGCN_GFX1030: + return "gfx1030"; + default: + return "--unknown gfx"; + } +} diff --git a/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.h b/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.h new file mode 100644 index 000000000000..b1be90dc29d5 --- /dev/null +++ b/openmp/libomptarget/plugins/amdgpu/impl/get_elf_mach_gfx_name.h @@ -0,0 +1,8 @@ +#ifndef GET_ELF_MACH_GFX_NAME_H_INCLUDED +#define GET_ELF_MACH_GFX_NAME_H_INCLUDED + +#include + +const char *get_elf_mach_gfx_name(uint32_t EFlags); + +#endif diff --git a/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp b/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp index 1f3ba128a40b..b5379bddd2de 100644 --- a/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp +++ b/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp @@ -41,7 +41,7 @@ extern "C" hsa_status_t hostrpc_init(); extern "C" hsa_status_t hostrpc_terminate(); #include "internal.h" - +#include "get_elf_mach_gfx_name.h" #include "Debug.h" #include "omptargetplugin.h" @@ -79,7 +79,6 @@ uint32_t TgtStackItemSize = 0; #endif #include "../../common/elf_common.c" -#include "../impl/elf_amd.h" /// Keep entries table per device struct FuncOrGblEntryTy { @@ -619,6 +618,40 @@ void finiAsyncInfoPtr(__tgt_async_info *async_info_ptr) { assert(async_info_ptr->Queue); async_info_ptr->Queue = 0; } + +bool elf_machine_id_is_amdgcn(__tgt_device_image *image) { + const uint16_t amdgcnMachineID = EM_AMDGPU; + int32_t r = elf_check_machine(image, amdgcnMachineID); + if (!r) { + DP("Supported machine ID not found\n"); + } + return r; +} + +uint32_t elf_e_flags(__tgt_device_image *image) { + char *img_begin = (char *)image->ImageStart; + size_t img_size = (char *)image->ImageEnd - img_begin; + + Elf *e = elf_memory(img_begin, img_size); + if (!e) { + DP("Unable to get ELF handle: %s!\n", elf_errmsg(-1)); + return 0; + } + + Elf64_Ehdr *eh64 = elf64_getehdr(e); + + if (!eh64) { + DP("Unable to get machine ID from ELF file!\n"); + elf_end(e); + return 0; + } + + uint32_t Flags = eh64->e_flags; + + elf_end(e); + DP("ELF Flags: 0x%x\n", Flags); + return Flags; +} } // namespace int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *image) { @@ -1044,7 +1077,7 @@ __tgt_target_table *__tgt_rtl_load_binary_locked(int32_t device_id, "Possible gpu arch mismatch: device:%s, image:%s please check" " compiler flag: -march=\n", DeviceInfo.GPUName[device_id].c_str(), - get_elf_mach_gfx_name(image)); + get_elf_mach_gfx_name(elf_e_flags(image))); return NULL; } } diff --git a/openmp/libomptarget/plugins/common/elf_common.c b/openmp/libomptarget/plugins/common/elf_common.c index ebcbe1c9c0ce..8cbf8aadca28 100644 --- a/openmp/libomptarget/plugins/common/elf_common.c +++ b/openmp/libomptarget/plugins/common/elf_common.c @@ -109,41 +109,3 @@ static inline int32_t elf_is_dynamic(__tgt_device_image *image) { DP("ELF Type: %d\n", Type); return Type == ET_DYN; } - -static inline uint32_t elf_e_flags(__tgt_device_image *image) { - - char *img_begin = (char *)image->ImageStart; - char *img_end = (char *)image->ImageEnd; - size_t img_size = img_end - img_begin; - - // Obtain elf handler - Elf *e = elf_memory(img_begin, img_size); - if (!e) { - DP("Unable to get ELF handle: %s!\n", elf_errmsg(-1)); - return 0; - } - - Elf64_Ehdr *eh64 = elf64_getehdr(e); - Elf32_Ehdr *eh32 = elf32_getehdr(e); - - if (!eh64 && !eh32) { - DP("Unable to get machine ID from ELF file!\n"); - elf_end(e); - return 0; - } - - uint32_t Flags; - if (eh64 && !eh32) - Flags = eh64->e_flags; - else if (eh32 && !eh64) - Flags = eh32->e_flags; - else { - DP("Ambiguous ELF header!\n"); - elf_end(e); - return 0; - } - - elf_end(e); - DP("ELF Flags: 0x%x\n", Flags); - return Flags; -}