109 lines · c
1//===-- Generic device loader interface -----------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_TOOLS_LLVM_GPU_LOADER_LLVM_GPU_LOADER_H10#define LLVM_TOOLS_LLVM_GPU_LOADER_LLVM_GPU_LOADER_H11 12#include <cstddef>13#include <cstdint>14#include <cstdio>15#include <cstdlib>16#include <cstring>17 18/// Generic launch parameters for configuration the number of blocks / threads.19struct LaunchParameters {20 uint32_t num_threads_x;21 uint32_t num_threads_y;22 uint32_t num_threads_z;23 uint32_t num_blocks_x;24 uint32_t num_blocks_y;25 uint32_t num_blocks_z;26};27 28/// The arguments to the '_begin' kernel.29struct begin_args_t {30 int argc;31 void *argv;32 void *envp;33};34 35/// The arguments to the '_start' kernel.36struct start_args_t {37 int argc;38 void *argv;39 void *envp;40 void *ret;41};42 43/// The arguments to the '_end' kernel.44struct end_args_t {};45 46/// Generic interface to load the \p image and launch execution of the _start47/// kernel on the target device. Copies \p argc and \p argv to the device.48/// Returns the final value of the `main` function on the device.49#ifdef AMDHSA_SUPPORT50int load_amdhsa(int argc, const char **argv, const char **evnp, void *image,51 size_t size, const LaunchParameters ¶ms,52 bool print_resource_usage);53#endif54#ifdef NVPTX_SUPPORT55int load_nvptx(int argc, const char **argv, const char **evnp, void *image,56 size_t size, const LaunchParameters ¶ms,57 bool print_resource_usage);58#endif59 60/// Return \p V aligned "upwards" according to \p Align.61template <typename V, typename A> inline V align_up(V val, A align) {62 return ((val + V(align) - 1) / V(align)) * V(align);63}64 65/// Copy the system's argument vector to GPU memory allocated using \p alloc.66template <typename Allocator>67void *copy_argument_vector(int argc, const char **argv, Allocator alloc) {68 size_t argv_size = sizeof(char *) * (argc + 1);69 size_t str_size = 0;70 for (int i = 0; i < argc; ++i)71 str_size += strlen(argv[i]) + 1;72 73 // We allocate enough space for a null terminated array and all the strings.74 void *dev_argv = alloc(argv_size + str_size);75 if (!dev_argv)76 return nullptr;77 78 // Store the strings linerally in the same memory buffer.79 void *dev_str = reinterpret_cast<uint8_t *>(dev_argv) + argv_size;80 for (int i = 0; i < argc; ++i) {81 size_t size = strlen(argv[i]) + 1;82 std::memcpy(dev_str, argv[i], size);83 static_cast<void **>(dev_argv)[i] = dev_str;84 dev_str = reinterpret_cast<uint8_t *>(dev_str) + size;85 }86 87 // Ensure the vector is null terminated.88 reinterpret_cast<void **>(dev_argv)[argc] = nullptr;89 return dev_argv;90}91 92/// Copy the system's environment to GPU memory allocated using \p alloc.93template <typename Allocator>94void *copy_environment(const char **envp, Allocator alloc) {95 int envc = 0;96 for (const char **env = envp; *env != 0; ++env)97 ++envc;98 99 return copy_argument_vector(envc, envp, alloc);100}101 102inline void handle_error_impl(const char *file, int32_t line, const char *msg) {103 fprintf(stderr, "%s:%d:0: Error: %s\n", file, line, msg);104 exit(EXIT_FAILURE);105}106#define handle_error(X) handle_error_impl(__FILE__, __LINE__, X)107 108#endif // LLVM_TOOLS_LLVM_GPU_LOADER_LLVM_GPU_LOADER_H109