Skip to content

Commit b537a91

Browse files
committed
[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.
1 parent 449e2f5 commit b537a91

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
103103
// ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
104104
// Since AMDGPU backend currently does not support ISA-level linking, all
105105
// called functions need to be imported.
106-
if (IsThinLTO)
106+
if (IsThinLTO) {
107107
LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
108+
LldArgs.push_back(Args.MakeArgString("-plugin-opt=-avail-extern-to-local"));
109+
}
108110

109111
for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
110112
LldArgs.push_back(

clang/test/Driver/hip-thinlto.hip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang -foffload-lto=thin %s -### 2>&1 | FileCheck %s
2+
3+
// CHECK: -plugin-opt=thinlto
4+
// CHECK-SAME: -plugin-opt=-force-import-all
5+
// CHECK-SAME: -plugin-opt=-avail-extern-to-local
6+
int main(int, char *[]) {
7+
return 0;
8+
}

0 commit comments

Comments
 (0)