[llvm] [ElimAvailExtern] Add an option to allow to convert global variables in a specified address space to local (PR #144287)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 15 21:07:57 PDT 2025
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/144287
>From 1cfc90347fbeeefaaae1a0c2480108d1cbd83714 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 16 Jun 2025 00:07:37 -0400
Subject: [PATCH] [ElimAvailExtern] Add an option to allow to convert global
variables in a specified address space to local
Currently, the `EliminateAvailableExternallyPass` only converts certain
available externally functions to local. For global variables, it only drops
their initializers.
This PR adds an option to allow the pass to convert global variables in a
specified address space to local. The motivation for this change is to correctly
support lowering of LDS variables (`__shared__` variables, in more generic
terminology) when ThinLTO is enabled for AMDGPU.
A `__shared__` variable is lowered to a hidden global variable in a particular
address space by the frontend, which is roughly same as a `static` local
variable. To properly lower it in the backend, the compiler needs to check all
its uses. Enabling ThinLTO currently breaks this when a function containing a
`__shared__` variable is imported from another module. Even though the global
variable is imported along with its associated function, and the function is
privatized by the `EliminateAvailableExternallyPass`, the global variable itself
is not.
It's safe to privatize such global variables, because they're _local_ to their
associated functions. If the function itself is privatized, its associated
global variables should also be privatized accordingly.
---
llvm/lib/Transforms/IPO/ElimAvailExtern.cpp | 31 ++++++++++++++++++-
.../convert-global-variables-to-local.ll | 21 +++++++++++++
.../transform-to-local.ll | 4 +--
3 files changed, 53 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/Transforms/EliminateAvailableExternally/convert-global-variables-to-local.ll
diff --git a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
index 718452fc02764..f4b8e145c1a29 100644
--- a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
+++ b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
@@ -35,8 +35,14 @@ static cl::opt<bool> ConvertToLocal(
cl::desc("Convert available_externally into locals, renaming them "
"to avoid link-time clashes."));
+static cl::opt<unsigned> ConvertGlobalVariableInAddrSpace(
+ "avail-extern-gv-in-addrspace-to-local", cl::Hidden,
+ cl::desc(
+ "Convert available_externally global variables into locals if they are "
+ "in specificed addrspace, renaming them to avoid link-time clashes."));
+
STATISTIC(NumRemovals, "Number of functions removed");
-STATISTIC(NumConversions, "Number of functions converted");
+STATISTIC(NumConversions, "Number of functions and globalbs converted");
STATISTIC(NumVariables, "Number of global variables removed");
void deleteFunction(Function &F) {
@@ -88,9 +94,32 @@ static void convertToLocalCopy(Module &M, Function &F) {
++NumConversions;
}
+static void convertToLocalCopy(Module &M, GlobalValue &GV) {
+ assert(GV.hasAvailableExternallyLinkage());
+ std::string OrigName = GV.getName().str();
+ std::string NewName = OrigName + ".__uniq" + getUniqueModuleId(&M);
+ GV.setName(NewName);
+ GV.setLinkage(GlobalValue::InternalLinkage);
+ ++NumConversions;
+}
+
static bool eliminateAvailableExternally(Module &M, bool Convert) {
bool Changed = false;
+ // Convert global variables in specified address space before changing it to
+ // external linkage below.
+ if (ConvertGlobalVariableInAddrSpace.getNumOccurrences()) {
+ for (GlobalVariable &GV : M.globals()) {
+ if (!GV.hasAvailableExternallyLinkage() || GV.use_empty())
+ continue;
+
+ if (GV.getAddressSpace() == ConvertGlobalVariableInAddrSpace) {
+ convertToLocalCopy(M, GV);
+ Changed = true;
+ }
+ }
+ }
+
// Drop initializers of available externally global variables.
for (GlobalVariable &GV : M.globals()) {
if (!GV.hasAvailableExternallyLinkage())
diff --git a/llvm/test/Transforms/EliminateAvailableExternally/convert-global-variables-to-local.ll b/llvm/test/Transforms/EliminateAvailableExternally/convert-global-variables-to-local.ll
new file mode 100644
index 0000000000000..6995b97e79887
--- /dev/null
+++ b/llvm/test/Transforms/EliminateAvailableExternally/convert-global-variables-to-local.ll
@@ -0,0 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
+; RUN: opt -S -passes=elim-avail-extern -avail-extern-gv-in-addrspace-to-local=3 %s -o - | FileCheck %s
+
+ at shared = internal addrspace(3) global i32 undef, align 4
+ at shared.imported = available_externally hidden unnamed_addr addrspace(3) global i32 undef, align 4
+
+;.
+; CHECK: @shared = internal addrspace(3) global i32 undef, align 4
+; CHECK: @shared.imported.__uniq.[[UUID:.*]] = internal unnamed_addr addrspace(3) global i32 undef, align 4
+;.
+define void @foo(i32 %v) {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: i32 [[V:%.*]]) {
+; CHECK-NEXT: store i32 [[V]], ptr addrspace(3) @shared, align 4
+; CHECK-NEXT: store i32 [[V]], ptr addrspace(3) @shared.imported.__uniq.[[UUID]], align 4
+; CHECK-NEXT: ret void
+;
+ store i32 %v, ptr addrspace(3) @shared, align 4
+ store i32 %v, ptr addrspace(3) @shared.imported, align 4
+ ret void
+}
diff --git a/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll b/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
index c24e4925fe78f..55b700447f2ab 100644
--- a/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
+++ b/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
@@ -47,7 +47,7 @@ define void @hello(ptr %g) !guid !0 {
; CHECK-NEXT: load ptr, ptr @f
; CHECK-NEXT: call void @call_out(ptr %f)
; CHECK: Statistics Collected
-; CHECK: 1 elim-avail-extern - Number of functions converted
+; CHECK: 1 elim-avail-extern - Number of functions and globalbs converted
; CHECK: 1 elim-avail-extern - Number of functions removed
-; NOOP: 2 elim-avail-extern - Number of functions removed
\ No newline at end of file
+; NOOP: 2 elim-avail-extern - Number of functions removed
More information about the llvm-commits
mailing list