brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · da73cc0 Raw
56 lines · c
1//===-- Common RPC server handler -----------------------------------------===//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_SERVER_H10#define LLVM_TOOLS_LLVM_GPU_LOADER_SERVER_H11 12#include <cstddef>13#include <cstdint>14 15#include "shared/rpc.h"16#include "shared/rpc_opcodes.h"17#include "shared/rpc_server.h"18 19template <uint32_t num_lanes, typename Alloc, typename Free>20inline uint32_t handle_server(rpc::Server &server, uint32_t index,21                              Alloc &&alloc, Free &&free) {22  auto port = server.try_open(num_lanes, index);23  if (!port)24    return 0;25  index = port->get_index() + 1;26 27  int status = rpc::RPC_SUCCESS;28  switch (port->get_opcode()) {29  case LIBC_MALLOC: {30    port->recv_and_send([&](rpc::Buffer *buffer, uint32_t) {31      buffer->data[0] = reinterpret_cast<uintptr_t>(alloc(buffer->data[0]));32    });33    break;34  }35  case LIBC_FREE: {36    port->recv([&](rpc::Buffer *buffer, uint32_t) {37      free(reinterpret_cast<void *>(buffer->data[0]));38    });39    break;40  }41  default:42    status = LIBC_NAMESPACE::shared::handle_libc_opcodes(*port, num_lanes);43    break;44  }45 46  // Handle all of the `libc` specific opcodes.47  if (status != rpc::RPC_SUCCESS)48    handle_error("Error handling RPC server");49 50  port->close();51 52  return index;53}54 55#endif // LLVM_TOOLS_LLVM_GPU_LOADER_SERVER_H56