[llvm] [Clang] Prevent `mlink-builtin-bitcode` from internalizing the RPC client (PR #118661)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 07:12:23 PST 2025


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/118661

>From 8ca093d1f115f0d11d9d2f4689752c73e13840e2 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 4 Dec 2024 10:10:11 -0600
Subject: [PATCH] [Clang] Prevent `mlink-builtin-bitcode` from internalizing
 the RPC client

Summary:
Currently, we only use `-mmlink-builtin-bitcode` for non-LTO NVIDIA
compiliations. THis has the problem that it will internalize the RPC
client symbol which needs to be visible to the host. To counteract that,
I put `retain` on it, but this also prevents optimizations on the global
itself, so the passes we have that remove the symbol don't work on
OpenMP anymore. This patch does the dumbest solution, adding a special
string check for it in clang. Not the best solution, the runner up would
be to habe a clang attribute for `externally_inititliazed` because those
can't be internalized, but that might have some unfortunate
side-effects. Alternatively we could make NVIDIA compilations do LTO all
the time, but that would affect some users and it's harder than I
thought.
---
 llvm/lib/Transforms/IPO/Internalize.cpp |  4 ++++
 offload/DeviceRTL/src/Misc.cpp          | 10 ++--------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/Internalize.cpp b/llvm/lib/Transforms/IPO/Internalize.cpp
index 4cdd1fa6110627..9316de96405e06 100644
--- a/llvm/lib/Transforms/IPO/Internalize.cpp
+++ b/llvm/lib/Transforms/IPO/Internalize.cpp
@@ -233,6 +233,10 @@ bool InternalizePass::internalizeModule(Module &M) {
   else
     AlwaysPreserved.insert("__stack_chk_guard");
 
+  // For GPU host callbacks.
+  if (Triple(M.getTargetTriple()).isNVPTX())
+    AlwaysPreserved.insert("__llvm_rpc_server");
+
   // Mark all functions not in the api as internal.
   IsWasm = Triple(M.getTargetTriple()).isOSBinFormatWasm();
   for (Function &I : M) {
diff --git a/offload/DeviceRTL/src/Misc.cpp b/offload/DeviceRTL/src/Misc.cpp
index ba6fbf5d5c7e3c..a6660d6853e477 100644
--- a/offload/DeviceRTL/src/Misc.cpp
+++ b/offload/DeviceRTL/src/Misc.cpp
@@ -105,14 +105,8 @@ void *indirectCallLookup(void *HstPtr) {
 }
 
 /// The openmp client instance used to communicate with the server.
-/// FIXME: This is marked as 'retain' so that it is not removed via
-/// `-mlink-builtin-bitcode`
-#ifdef __NVPTX__
-[[gnu::visibility("protected"), gnu::weak,
-  gnu::retain]] rpc::Client Client asm("__llvm_rpc_client");
-#else
-[[gnu::visibility("protected"), gnu::weak]] rpc::Client Client asm("__llvm_rpc_client");
-#endif
+[[gnu::visibility("protected"),
+  gnu::weak]] rpc::Client Client asm("__llvm_rpc_client");
 
 } // namespace impl
 } // namespace ompx



More information about the llvm-commits mailing list