[clang] [Clang][HIP] Skip internalization for non-LTO device links (PR #225859)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 1 11:01:13 PDT 2026


https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/225859

>From 3d8548d41e65756e9d08e712a93c4809ca6128d5 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 22 Sep 2026 15:25:46 +0000
Subject: [PATCH] [Clang][HIP] Skip internalization for non-LTO device links

A non-LTO HIP device link can combine native relocatable objects with
bitcode libraries. This can happen with both RDC and non-RDC
compilation. LLD must resolve undefined symbols in the native objects
with definitions from the bitcode.

The LTO step processing the bitcode cannot see references from native
objects. Internalizing non-kernel functions can therefore hide
definitions that those objects need. Do not enable AMDGPU
internalization for these mixed non-LTO links.

LTO mode does not have this problem. It links the bitcode modules before
internalization, so the references are visible when symbols are
resolved. Keep internalization for LTO links and when compiling non-RDC
main modules.
---
 clang/lib/Driver/ToolChains/HIPAMD.cpp        | 18 ++++----
 .../test/Driver/hip-profile-rocm-runtime.hip  | 46 ++++++++++++++++++-
 2 files changed, 53 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index ae46e8eb9fcaef5..dd0daac889d96e8 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -53,19 +53,19 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
                                          const llvm::opt::ArgList &Args) const {
   // Construct lld command.
   // The output from ld.lld is an HSA code object file.
-  ArgStringList LldArgs{"-flavor",
-                        "gnu",
-                        "-m",
-                        "elf64_amdgpu",
-                        "--no-undefined",
-                        "-shared",
-                        "-plugin-opt=-amdgpu-internalize-symbols"};
+  const ToolChain &TC = getToolChain();
+  LTOKind LTOMode = TC.getLTOMode(Args, Action::OFK_HIP);
+  ArgStringList LldArgs{"-flavor",        "gnu",    "-m", "elf64_amdgpu",
+                        "--no-undefined", "-shared"};
+  // Native object references are invisible when LTO compiles bitcode libraries
+  // in a non-LTO link, so library definitions must remain external.
+  if (LTOMode != LTOK_None)
+    LldArgs.push_back("-plugin-opt=-amdgpu-internalize-symbols");
   if (Args.hasArg(options::OPT_hipstdpar))
     LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar");
 
-  auto &TC = getToolChain();
   auto &D = TC.getDriver();
-  bool IsThinLTO = TC.getLTOMode(Args, Action::OFK_HIP) == LTOK_Thin;
+  bool IsThinLTO = LTOMode == LTOK_Thin;
   addLTOOptions(TC, Args, LldArgs, Output, Inputs, IsThinLTO);
 
   // Extract all the -m options
diff --git a/clang/test/Driver/hip-profile-rocm-runtime.hip b/clang/test/Driver/hip-profile-rocm-runtime.hip
index b267fa382b2be9a..d59cfabc7526e2a 100644
--- a/clang/test/Driver/hip-profile-rocm-runtime.hip
+++ b/clang/test/Driver/hip-profile-rocm-runtime.hip
@@ -36,5 +36,47 @@
 // RUN: %clang -### -x hip --offload-device-only --target=x86_64-unknown-linux \
 // RUN: --offload-arch=gfx90a -fprofile-generate -resource-dir=%t \
 // RUN: -nogpuinc -nogpulib %s 2>&1 \
-// RUN: | FileCheck -check-prefix=HIP-DEVICE-PGO %s
-// HIP-DEVICE-PGO: "{{.*}}lld" {{.*}}"{{.*}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}libclang_rt.profile.a"
+// RUN: | FileCheck -check-prefix=HIP-DEVICE-PGO --implicit-check-not=-plugin-opt=-amdgpu-internalize-symbols %s
+// HIP-DEVICE-PGO: "-cc1"
+// HIP-DEVICE-PGO-SAME: "-emit-obj"
+// HIP-DEVICE-PGO-SAME: "-mllvm" "-amdgpu-internalize-symbols"
+// HIP-DEVICE-PGO: "{{.*}}lld"
+// HIP-DEVICE-PGO-SAME: "{{.*}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}libclang_rt.profile.a"
+
+// An RDC non-LTO link also keeps bitcode library definitions external.
+// RUN: %clang -x c --target=x86_64-unknown-linux-gnu -c %s -o %t.host.o
+// RUN: %clang -x hip --offload-device-only --target=x86_64-unknown-linux \
+// RUN: --offload-arch=gfx90a -fgpu-rdc -fno-offload-lto -nogpuinc -nogpulib \
+// RUN: -c %s -o %t.device.o
+// RUN: clang-offload-bundler -type=o \
+// RUN: -targets=host-x86_64-unknown-linux-gnu,hip-amdgpu9.0a-amd-amdhsa--gfx90a \
+// RUN: -input=%t.host.o -input=%t.device.o -output=%t.rdc.o
+// RUN: %clang -### --hip-link --target=x86_64-unknown-linux \
+// RUN: --offload-arch=gfx90a -fgpu-rdc -fno-offload-lto -fprofile-generate \
+// RUN: -resource-dir=%t --rocm-path=%S/Inputs/rocm -nogpuinc -nogpulib \
+// RUN: %t.rdc.o 2>&1 \
+// RUN: | FileCheck -check-prefix=HIP-RDC-PGO --implicit-check-not=-plugin-opt=-amdgpu-internalize-symbols %s
+// HIP-RDC-PGO: clang-linker-wrapper
+// HIP-RDC-PGO-SAME: "--device-compiler=amdgpu-amd-amdhsa=-fprofile-generate"
+
+// Full offload LTO still enables AMDGPU internalization.
+// RUN: %clang -### -x hip --offload-device-only --target=x86_64-unknown-linux \
+// RUN: --offload-arch=gfx90a -fprofile-generate -foffload-lto=full \
+// RUN: -resource-dir=%t -nogpuinc -nogpulib %s 2>&1 \
+// RUN: | FileCheck -check-prefix=HIP-DEVICE-LTO-PGO %s
+// HIP-DEVICE-LTO-PGO: "-cc1"
+// HIP-DEVICE-LTO-PGO-SAME: "-emit-llvm-bc"
+// HIP-DEVICE-LTO-PGO: "{{.*}}lld"
+// HIP-DEVICE-LTO-PGO-SAME: "-plugin-opt=-amdgpu-internalize-symbols"
+// HIP-DEVICE-LTO-PGO-SAME: "{{.*}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}libclang_rt.profile.a"
+
+// A normal non-profile device link also internalizes the main module in cc1,
+// not while linking the resulting native object.
+// RUN: %clang -### -x hip --offload-device-only --target=x86_64-unknown-linux \
+// RUN: --offload-arch=gfx90a -resource-dir=%t -nogpuinc -nogpulib %s 2>&1 \
+// RUN: | FileCheck -check-prefix=HIP-DEVICE-NOPGO --implicit-check-not=-plugin-opt=-amdgpu-internalize-symbols %s
+// HIP-DEVICE-NOPGO: "-cc1"
+// HIP-DEVICE-NOPGO-SAME: "-emit-obj"
+// HIP-DEVICE-NOPGO-SAME: "-mllvm" "-amdgpu-internalize-symbols"
+// HIP-DEVICE-NOPGO: "{{.*}}lld"
+// HIP-DEVICE-NOPGO-NOT: libclang_rt.profile.a



More information about the cfe-commits mailing list