summaryrefslogtreecommitdiff
path: root/offload/plugins-nextgen/common/src/RPC.cpp
diff options
context:
space:
mode:
authorJohannes Doerfert <johannes@jdoerfert.de>2024-04-22 09:51:33 -0700
committerGitHub <noreply@github.com>2024-04-22 09:51:33 -0700
commit330d8983d25d08580fc1642fea48b2473f47a9da (patch)
tree25994a04a5acd171f41e2ab7dd9780016f22237a /offload/plugins-nextgen/common/src/RPC.cpp
parentb6628c24ef017138b8d6eb288e94c141e7c846b0 (diff)
[Offload] Move `/openmp/libomptarget` to `/offload` (#75125)
In a nutshell, this moves our libomptarget code to populate the offload subproject. With this commit, users need to enable the new LLVM/Offload subproject as a runtime in their cmake configuration. No further changes are expected for downstream code. Tests and other components still depend on OpenMP and have also not been renamed. The results below are for a build in which OpenMP and Offload are enabled runtimes. In addition to the pure `git mv`, we needed to adjust some CMake files. Nothing is intended to change semantics. ``` ninja check-offload ``` Works with the X86 and AMDGPU offload tests ``` ninja check-openmp ``` Still works but doesn't build offload tests anymore. ``` ls install/lib ``` Shows all expected libraries, incl. - `libomptarget.devicertl.a` - `libomptarget-nvptx-sm_90.bc` - `libomptarget.rtl.amdgpu.so` -> `libomptarget.rtl.amdgpu.so.18git` - `libomptarget.so` -> `libomptarget.so.18git` Fixes: https://github.com/llvm/llvm-project/issues/75124 --------- Co-authored-by: Saiyedul Islam <Saiyedul.Islam@amd.com>
Diffstat (limited to 'offload/plugins-nextgen/common/src/RPC.cpp')
-rw-r--r--offload/plugins-nextgen/common/src/RPC.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
new file mode 100644
index 000000000000..faa2cbd4f02f
--- /dev/null
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -0,0 +1,137 @@
+//===- RPC.h - Interface for remote procedure calls from the GPU ----------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "RPC.h"
+
+#include "Shared/Debug.h"
+
+#include "PluginInterface.h"
+
+#if defined(LIBOMPTARGET_RPC_SUPPORT)
+#include "llvm-libc-types/rpc_opcodes_t.h"
+#include "llvmlibc_rpc_server.h"
+#endif
+
+using namespace llvm;
+using namespace omp;
+using namespace target;
+
+RPCServerTy::RPCServerTy(plugin::GenericPluginTy &Plugin)
+ : Handles(Plugin.getNumDevices()) {}
+
+llvm::Expected<bool>
+RPCServerTy::isDeviceUsingRPC(plugin::GenericDeviceTy &Device,
+ plugin::GenericGlobalHandlerTy &Handler,
+ plugin::DeviceImageTy &Image) {
+#ifdef LIBOMPTARGET_RPC_SUPPORT
+ return Handler.isSymbolInImage(Device, Image, rpc_client_symbol_name);
+#else
+ return false;
+#endif
+}
+
+Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
+ plugin::GenericGlobalHandlerTy &Handler,
+ plugin::DeviceImageTy &Image) {
+#ifdef LIBOMPTARGET_RPC_SUPPORT
+ auto Alloc = [](uint64_t Size, void *Data) {
+ plugin::GenericDeviceTy &Device =
+ *reinterpret_cast<plugin::GenericDeviceTy *>(Data);
+ return Device.allocate(Size, nullptr, TARGET_ALLOC_HOST);
+ };
+ uint64_t NumPorts =
+ std::min(Device.requestedRPCPortCount(), RPC_MAXIMUM_PORT_COUNT);
+ rpc_device_t RPCDevice;
+ if (rpc_status_t Err = rpc_server_init(&RPCDevice, NumPorts,
+ Device.getWarpSize(), Alloc, &Device))
+ return plugin::Plugin::error(
+ "Failed to initialize RPC server for device %d: %d",
+ Device.getDeviceId(), Err);
+
+ // Register a custom opcode handler to perform plugin specific allocation.
+ auto MallocHandler = [](rpc_port_t Port, void *Data) {
+ rpc_recv_and_send(
+ Port,
+ [](rpc_buffer_t *Buffer, void *Data) {
+ plugin::GenericDeviceTy &Device =
+ *reinterpret_cast<plugin::GenericDeviceTy *>(Data);
+ Buffer->data[0] = reinterpret_cast<uintptr_t>(Device.allocate(
+ Buffer->data[0], nullptr, TARGET_ALLOC_DEVICE_NON_BLOCKING));
+ },
+ Data);
+ };
+ if (rpc_status_t Err =
+ rpc_register_callback(RPCDevice, RPC_MALLOC, MallocHandler, &Device))
+ return plugin::Plugin::error(
+ "Failed to register RPC malloc handler for device %d: %d\n",
+ Device.getDeviceId(), Err);
+
+ // Register a custom opcode handler to perform plugin specific deallocation.
+ auto FreeHandler = [](rpc_port_t Port, void *Data) {
+ rpc_recv(
+ Port,
+ [](rpc_buffer_t *Buffer, void *Data) {
+ plugin::GenericDeviceTy &Device =
+ *reinterpret_cast<plugin::GenericDeviceTy *>(Data);
+ Device.free(reinterpret_cast<void *>(Buffer->data[0]),
+ TARGET_ALLOC_DEVICE_NON_BLOCKING);
+ },
+ Data);
+ };
+ if (rpc_status_t Err =
+ rpc_register_callback(RPCDevice, RPC_FREE, FreeHandler, &Device))
+ return plugin::Plugin::error(
+ "Failed to register RPC free handler for device %d: %d\n",
+ Device.getDeviceId(), Err);
+
+ // Get the address of the RPC client from the device.
+ void *ClientPtr;
+ plugin::GlobalTy ClientGlobal(rpc_client_symbol_name, sizeof(void *));
+ if (auto Err =
+ Handler.getGlobalMetadataFromDevice(Device, Image, ClientGlobal))
+ return Err;
+
+ if (auto Err = Device.dataRetrieve(&ClientPtr, ClientGlobal.getPtr(),
+ sizeof(void *), nullptr))
+ return Err;
+
+ const void *ClientBuffer = rpc_get_client_buffer(RPCDevice);
+ if (auto Err = Device.dataSubmit(ClientPtr, ClientBuffer,
+ rpc_get_client_size(), nullptr))
+ return Err;
+ Handles[Device.getDeviceId()] = RPCDevice.handle;
+#endif
+ return Error::success();
+}
+
+Error RPCServerTy::runServer(plugin::GenericDeviceTy &Device) {
+#ifdef LIBOMPTARGET_RPC_SUPPORT
+ rpc_device_t RPCDevice{Handles[Device.getDeviceId()]};
+ if (rpc_status_t Err = rpc_handle_server(RPCDevice))
+ return plugin::Plugin::error(
+ "Error while running RPC server on device %d: %d", Device.getDeviceId(),
+ Err);
+#endif
+ return Error::success();
+}
+
+Error RPCServerTy::deinitDevice(plugin::GenericDeviceTy &Device) {
+#ifdef LIBOMPTARGET_RPC_SUPPORT
+ rpc_device_t RPCDevice{Handles[Device.getDeviceId()]};
+ auto Dealloc = [](void *Ptr, void *Data) {
+ plugin::GenericDeviceTy &Device =
+ *reinterpret_cast<plugin::GenericDeviceTy *>(Data);
+ Device.free(Ptr, TARGET_ALLOC_HOST);
+ };
+ if (rpc_status_t Err = rpc_server_shutdown(RPCDevice, Dealloc, &Device))
+ return plugin::Plugin::error(
+ "Failed to shut down RPC server for device %d: %d",
+ Device.getDeviceId(), Err);
+#endif
+ return Error::success();
+}