Skip to content

Conversation

@jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Nov 27, 2024

Summary:
We can simply include this header from the shared directory now and do
not need to have this level of indirection. Simply stash it with the
other libc opcode handlers.

If we were able to move the printf handlers to the shared directory then
this could just be a header as well, which would HEAVILY simplify the
mess associated with building the RPC server first in the projects
build, then copying it to the runtimes build.

Summary:
We can simply include this header from the shared directory now and do
not need to have this level of indirection. Simply stash it with the
other libc opcode handlers.

If we were able to move the printf handlers to the shared directory then
this could just be a header as well, which would HEAVILY simplify the
mess associated with building the RPC server first in the projects
build, then copying it to the runtimes build.
@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2024

@llvm/pr-subscribers-offload

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

Summary:
We can simply include this header from the shared directory now and do
not need to have this level of indirection. Simply stash it with the
other libc opcode handlers.

If we were able to move the printf handlers to the shared directory then
this could just be a header as well, which would HEAVILY simplify the
mess associated with building the RPC server first in the projects
build, then copying it to the runtimes build.


Full diff: https://github.com/llvm/llvm-project/pull/117908.diff

7 Files Affected:

  • (modified) libc/shared/rpc_opcodes.h (+10)
  • (modified) libc/utils/gpu/loader/Loader.h (+1-3)
  • (modified) libc/utils/gpu/server/CMakeLists.txt (-3)
  • (removed) libc/utils/gpu/server/llvmlibc_rpc_server.h (-24)
  • (modified) libc/utils/gpu/server/rpc_server.cpp (+8-6)
  • (modified) offload/plugins-nextgen/common/CMakeLists.txt (+1-4)
  • (modified) offload/plugins-nextgen/common/src/RPC.cpp (+7-8)
diff --git a/libc/shared/rpc_opcodes.h b/libc/shared/rpc_opcodes.h
index 430b53aa1870ca..e7ec0e739a7787 100644
--- a/libc/shared/rpc_opcodes.h
+++ b/libc/shared/rpc_opcodes.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_SHARED_RPC_OPCODES_H
 #define LLVM_LIBC_SHARED_RPC_OPCODES_H
 
+#include "rpc.h"
+
 #define LLVM_LIBC_RPC_BASE 'c'
 #define LLVM_LIBC_OPCODE(n) (LLVM_LIBC_RPC_BASE << 24 | n)
 
@@ -46,4 +48,12 @@ typedef enum {
   RPC_LAST = 0xFFFFFFFF,
 } rpc_opcode_t;
 
+#undef LLVM_LIBC_OPCODE
+
+namespace rpc {
+// The implementation of this function currently lives in the utility directory
+// at 'utils/gpu/server/rpc_server.cpp'.
+rpc::Status handle_libc_opcodes(rpc::Server::Port &port, uint32_t num_lanes);
+} // namespace rpc
+
 #endif // LLVM_LIBC_SHARED_RPC_OPCODES_H
diff --git a/libc/utils/gpu/loader/Loader.h b/libc/utils/gpu/loader/Loader.h
index 497f8114728bf9..6c4434a1b63887 100644
--- a/libc/utils/gpu/loader/Loader.h
+++ b/libc/utils/gpu/loader/Loader.h
@@ -9,8 +9,6 @@
 #ifndef LLVM_LIBC_UTILS_GPU_LOADER_LOADER_H
 #define LLVM_LIBC_UTILS_GPU_LOADER_LOADER_H
 
-#include "utils/gpu/server/llvmlibc_rpc_server.h"
-
 #include "include/llvm-libc-types/test_rpc_opcodes_t.h"
 
 #include "shared/rpc.h"
@@ -183,7 +181,7 @@ inline uint32_t handle_server(rpc::Server &server, uint32_t index,
     break;
   }
   default:
-    status = libc_handle_rpc_port(&*port, num_lanes);
+    status = handle_libc_opcodes(*port, num_lanes);
     break;
   }
 
diff --git a/libc/utils/gpu/server/CMakeLists.txt b/libc/utils/gpu/server/CMakeLists.txt
index 36a4c2a8b051ec..b1cada44cd3279 100644
--- a/libc/utils/gpu/server/CMakeLists.txt
+++ b/libc/utils/gpu/server/CMakeLists.txt
@@ -23,9 +23,6 @@ target_compile_definitions(llvmlibc_rpc_server PUBLIC
                            LIBC_NAMESPACE=${LIBC_NAMESPACE})
 
 # Install the server and associated header.
-install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/llvmlibc_rpc_server.h
-        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
-        COMPONENT libc-headers)
 install(TARGETS llvmlibc_rpc_server
         ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}"
         COMPONENT libc)
diff --git a/libc/utils/gpu/server/llvmlibc_rpc_server.h b/libc/utils/gpu/server/llvmlibc_rpc_server.h
deleted file mode 100644
index b7f173734345c0..00000000000000
--- a/libc/utils/gpu/server/llvmlibc_rpc_server.h
+++ /dev/null
@@ -1,24 +0,0 @@
-//===-- Shared memory RPC server instantiation ------------------*- 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_UTILS_GPU_SERVER_RPC_SERVER_H
-#define LLVM_LIBC_UTILS_GPU_SERVER_RPC_SERVER_H
-
-#include <stdint.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int libc_handle_rpc_port(void *port, uint32_t num_lanes);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/libc/utils/gpu/server/rpc_server.cpp b/libc/utils/gpu/server/rpc_server.cpp
index 21af7ad7d5f1fd..559e0f06ff0cf1 100644
--- a/libc/utils/gpu/server/rpc_server.cpp
+++ b/libc/utils/gpu/server/rpc_server.cpp
@@ -17,8 +17,6 @@
 #include "shared/rpc.h"
 #include "shared/rpc_opcodes.h"
 
-#include "llvmlibc_rpc_server.h"
-
 #include "src/__support/arg_list.h"
 #include "src/stdio/printf_core/converter.h"
 #include "src/stdio/printf_core/parser.h"
@@ -445,15 +443,19 @@ rpc::Status handle_port_impl(rpc::Server::Port &port) {
   return rpc::SUCCESS;
 }
 
-int libc_handle_rpc_port(void *port, uint32_t num_lanes) {
+namespace rpc {
+// The implementation of this function currently lives in the utility directory
+// at 'utils/gpu/server/rpc_server.cpp'.
+rpc::Status handle_libc_opcodes(rpc::Server::Port &port, uint32_t num_lanes) {
   switch (num_lanes) {
   case 1:
-    return handle_port_impl<1>(*reinterpret_cast<rpc::Server::Port *>(port));
+    return handle_port_impl<1>(port);
   case 32:
-    return handle_port_impl<32>(*reinterpret_cast<rpc::Server::Port *>(port));
+    return handle_port_impl<32>(port);
   case 64:
-    return handle_port_impl<64>(*reinterpret_cast<rpc::Server::Port *>(port));
+    return handle_port_impl<64>(port);
   default:
     return rpc::ERROR;
   }
 }
+} // namespace rpc
diff --git a/offload/plugins-nextgen/common/CMakeLists.txt b/offload/plugins-nextgen/common/CMakeLists.txt
index 3ed5c02ed4a3bb..93cf42c89f321e 100644
--- a/offload/plugins-nextgen/common/CMakeLists.txt
+++ b/offload/plugins-nextgen/common/CMakeLists.txt
@@ -32,10 +32,7 @@ elseif(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
 		target_link_libraries(PluginCommon PRIVATE ${llvmlibc_rpc_server})
 		target_compile_definitions(PluginCommon PRIVATE LIBOMPTARGET_RPC_SUPPORT)
     # We may need to get the headers directly from the 'libc' source directory.
-    target_include_directories(PluginCommon PRIVATE
-                               ${CMAKE_SOURCE_DIR}/../libc/utils/gpu/server
-                               ${CMAKE_SOURCE_DIR}/../libc/
-                               ${CMAKE_SOURCE_DIR}/../libc/include)
+    target_include_directories(PluginCommon PRIVATE ${CMAKE_SOURCE_DIR}/../libc/)
   endif()
 endif()
 
diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
index be41928111da44..bcfe32d5eafe7e 100644
--- a/offload/plugins-nextgen/common/src/RPC.cpp
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -14,7 +14,6 @@
 
 // TODO: This should be included unconditionally and cleaned up.
 #if defined(LIBOMPTARGET_RPC_SUPPORT)
-#include "llvmlibc_rpc_server.h"
 #include "shared/rpc.h"
 #include "shared/rpc_opcodes.h"
 #endif
@@ -79,21 +78,21 @@ Error RPCServerTy::runServer(plugin::GenericDeviceTy &Device) {
       std::min(Device.requestedRPCPortCount(), rpc::MAX_PORT_COUNT);
   rpc::Server Server(NumPorts, Buffers[Device.getDeviceId()]);
 
-  auto port = Server.try_open(Device.getWarpSize());
-  if (!port)
+  auto Port = Server.try_open(Device.getWarpSize());
+  if (!Port)
     return Error::success();
 
   int Status = rpc::SUCCESS;
-  switch (port->get_opcode()) {
+  switch (Port->get_opcode()) {
   case RPC_MALLOC: {
-    port->recv_and_send([&](rpc::Buffer *Buffer, uint32_t) {
+    Port->recv_and_send([&](rpc::Buffer *Buffer, uint32_t) {
       Buffer->data[0] = reinterpret_cast<uintptr_t>(Device.allocate(
           Buffer->data[0], nullptr, TARGET_ALLOC_DEVICE_NON_BLOCKING));
     });
     break;
   }
   case RPC_FREE: {
-    port->recv([&](rpc::Buffer *Buffer, uint32_t) {
+    Port->recv([&](rpc::Buffer *Buffer, uint32_t) {
       Device.free(reinterpret_cast<void *>(Buffer->data[0]),
                   TARGET_ALLOC_DEVICE_NON_BLOCKING);
     });
@@ -101,10 +100,10 @@ Error RPCServerTy::runServer(plugin::GenericDeviceTy &Device) {
   }
   default:
     // Let the `libc` library handle any other unhandled opcodes.
-    Status = libc_handle_rpc_port(&*port, Device.getWarpSize());
+    Status = handle_libc_opcodes(*Port, Device.getWarpSize());
     break;
   }
-  port->close();
+  Port->close();
 
   if (Status != rpc::SUCCESS)
     return createStringError("RPC server given invalid opcode!");

switch (num_lanes) {
case 1:
return handle_port_impl<1>(*reinterpret_cast<rpc::Server::Port *>(port));
return handle_port_impl<1>(port);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it confusing looking at the variable name of libc project. what is the rule here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code that libc exports to handle the opcodes offload doesn't know how to handle.

break;
}
port->close();
Port->close();
Copy link
Contributor

@shiltian shiltian Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then this becomes Port?

NVM. This is offload.

${CMAKE_SOURCE_DIR}/../libc/utils/gpu/server
${CMAKE_SOURCE_DIR}/../libc/
${CMAKE_SOURCE_DIR}/../libc/include)
target_include_directories(PluginCommon PRIVATE ${CMAKE_SOURCE_DIR}/../libc/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, but I wonder why don't we just export libc's rpc as a target and then just link against the target, such that CMake will handle all the interface directories.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just described the next patch I'm going to do once this lands. We already have something called include(FindLibcCommonUtils) that I will use after this removes the need for the extra directories.

@jhuber6 jhuber6 merged commit 1d810ec into llvm:main Nov 27, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants