xref: /llvm-project/offload/plugins-nextgen/amdgpu/utils/UtilitiesRTL.h (revision 08533a3ee8f3a09a59cf6ac3be59198b26b7f739)
1 //===----RTLs/amdgpu/utils/UtilitiesRTL.h ------------------------- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // RTL Utilities for AMDGPU plugins
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include <cstdint>
14 
15 #include "Shared/Debug.h"
16 #include "Utils/ELF.h"
17 
18 #include "omptarget.h"
19 
20 #include "llvm/Frontend/Offloading/Utility.h"
21 
22 namespace llvm {
23 namespace omp {
24 namespace target {
25 namespace plugin {
26 namespace hsa_utils {
27 
28 // The implicit arguments of COV5 AMDGPU kernels.
29 struct AMDGPUImplicitArgsTy {
30   uint32_t BlockCountX;
31   uint32_t BlockCountY;
32   uint32_t BlockCountZ;
33   uint16_t GroupSizeX;
34   uint16_t GroupSizeY;
35   uint16_t GroupSizeZ;
36   uint8_t Unused0[46]; // 46 byte offset.
37   uint16_t GridDims;
38   uint8_t Unused1[54]; // 54 byte offset.
39   uint32_t DynamicLdsSize;
40   uint8_t Unused2[132]; // 132 byte offset.
41 };
42 
43 // Dummy struct for COV4 implicitargs.
44 struct AMDGPUImplicitArgsTyCOV4 {
45   uint8_t Unused[56];
46 };
47 
48 /// Returns the size in bytes of the implicit arguments of AMDGPU kernels.
49 /// `Version` is the ELF ABI version, e.g. COV5.
50 inline uint32_t getImplicitArgsSize(uint16_t Version) {
51   return Version < ELF::ELFABIVERSION_AMDGPU_HSA_V5
52              ? sizeof(AMDGPUImplicitArgsTyCOV4)
53              : sizeof(AMDGPUImplicitArgsTy);
54 }
55 
56 /// Reads the AMDGPU specific metadata from the ELF file and propagates the
57 /// KernelInfoMap
58 inline Error readAMDGPUMetaDataFromImage(
59     MemoryBufferRef MemBuffer,
60     StringMap<offloading::amdgpu::AMDGPUKernelMetaData> &KernelInfoMap,
61     uint16_t &ELFABIVersion) {
62   Error Err = llvm::offloading::amdgpu::getAMDGPUMetaDataFromImage(
63       MemBuffer, KernelInfoMap, ELFABIVersion);
64   if (!Err)
65     return Err;
66   DP("ELFABIVERSION Version: %u\n", ELFABIVersion);
67   return Err;
68 }
69 
70 } // namespace hsa_utils
71 } // namespace plugin
72 } // namespace target
73 } // namespace omp
74 } // namespace llvm
75