https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/134476
In HIP, the Clang driver already sets `force-import-all` when ThinLTO is enabled. As a result, all imported functions get the `available_externally` linkage. However, these functions are later removed by the `EliminateAvailableExternallyPass`, effectively undoing the forced import and eventually leading to link errors. The `EliminateAvailableExternallyPass` provides an option to convert `available_externally` functions into local functions, renaming them to avoid conflicts. This behavior is exactly what we need for HIP. This PR enables that option (`avail-extern-to-local`) alongside `force-import-all` when ThinLTO is used. With this change, ThinLTO almost works correctly on AMDGPU. The only remaining issue is an undefined reference to `__assert_fail`, but that falls outside the scope of this PR. >From 14237920a276bdea7d5654bad738a5109b7629fc Mon Sep 17 00:00:00 2001 From: Shilei Tian <i...@tianshilei.me> Date: Fri, 4 Apr 2025 22:58:18 -0400 Subject: [PATCH] [Clang][AMDGPU] Enable `avail-extern-to-local` for ThinLTO in HIP In HIP, the Clang driver already sets `force-import-all` when ThinLTO is enabled. As a result, all imported functions get the `available_externally` linkage. However, these functions are later removed by the `EliminateAvailableExternallyPass`, effectively undoing the forced import and eventually leading to link errors. The `EliminateAvailableExternallyPass` provides an option to convert `available_externally` functions into local functions, renaming them to avoid conflicts. This behavior is exactly what we need for HIP. This PR enables that option (`avail-extern-to-local`) alongside `force-import-all` when ThinLTO is used. With this change, ThinLTO almost works correctly on AMDGPU. The only remaining issue is an undefined reference to `__assert_fail`, but that falls outside the scope of this PR. --- clang/lib/Driver/ToolChains/HIPAMD.cpp | 4 +++- clang/test/Driver/hip-thinlto.hip | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 clang/test/Driver/hip-thinlto.hip diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp index abb83701759ce..0c25a40d94a10 100644 --- a/clang/lib/Driver/ToolChains/HIPAMD.cpp +++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp @@ -103,8 +103,10 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA, // ToDo: Remove this option after AMDGPU backend supports ISA-level linking. // Since AMDGPU backend currently does not support ISA-level linking, all // called functions need to be imported. - if (IsThinLTO) + if (IsThinLTO) { LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all")); + LldArgs.push_back(Args.MakeArgString("-plugin-opt=-avail-extern-to-local")); + } for (const Arg *A : Args.filtered(options::OPT_mllvm)) { LldArgs.push_back( diff --git a/clang/test/Driver/hip-thinlto.hip b/clang/test/Driver/hip-thinlto.hip new file mode 100644 index 0000000000000..0f5ff58ac547f --- /dev/null +++ b/clang/test/Driver/hip-thinlto.hip @@ -0,0 +1,6 @@ +// RUN: %clang -foffload-lto=thin --offload-link %s -### 2>&1 | FileCheck %s + +// CHECK: "-plugin-opt=-force-import-all"{{.*}}"-plugin-opt=-avail-extern-to-local" +int main(int, char *[]) { + return 0; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits