[llvm] [GlobalMerge] Update the GlobalMerge pass to merge private global variables. (PR #101222)

Amy Kwan via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 10:20:51 PDT 2024


https://github.com/amy-kwan updated https://github.com/llvm/llvm-project/pull/101222

>From b9ed23b97d8c01934230c3f6778d5a8512744f6e Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Tue, 30 Jul 2024 12:36:37 -0500
Subject: [PATCH 1/2] [GlobalMerge] Update the GlobalMerge pass to merge
 private global variables.

This patch updates the GlobalMerge pass to be able to handle private global
variables, which is required for a follow-up PowerPC specific GlobalMerge patch
to merge internal and private globals.

A new LIT test is also added to exhibit the ability to merge private globals.
---
 llvm/lib/CodeGen/GlobalMerge.cpp              |  2 +-
 .../Transforms/GlobalMerge/private-global.ll  | 31 +++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/GlobalMerge/private-global.ll

diff --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp
index 65bf7161441ba..e420c2bb7a25e 100644
--- a/llvm/lib/CodeGen/GlobalMerge.cpp
+++ b/llvm/lib/CodeGen/GlobalMerge.cpp
@@ -664,7 +664,7 @@ bool GlobalMergeImpl::run(Module &M) {
       continue;
 
     if (!(Opt.MergeExternal && GV.hasExternalLinkage()) &&
-        !GV.hasInternalLinkage())
+        !GV.hasInternalLinkage() && !GV.hasPrivateLinkage())
       continue;
 
     PointerType *PT = dyn_cast<PointerType>(GV.getType());
diff --git a/llvm/test/Transforms/GlobalMerge/private-global.ll b/llvm/test/Transforms/GlobalMerge/private-global.ll
new file mode 100644
index 0000000000000..f0ff89061f07c
--- /dev/null
+++ b/llvm/test/Transforms/GlobalMerge/private-global.ll
@@ -0,0 +1,31 @@
+; RUN: opt -global-merge -global-merge-max-offset=100 -S -o - %s | FileCheck %s
+; RUN: opt -passes='global-merge<max-offset=100>' -S -o - %s | FileCheck %s
+
+; NOTE: This is a copy of the llvm/test/Transforms/GlobalMerge/used.ll test,
+; using `private` global variables instead of `internal`. This is to show that
+; that private globals can be merged in the GlobalMerge pass.
+
+target datalayout = "e-p:64:64"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: @_MergedGlobals = private global <{ i32, i32 }> <{ i32 3, i32 3 }>, align 4
+
+ at a = private global i32 1
+ at b = private global i32 2
+ at c = private global i32 3
+ at d = private global i32 3
+
+ at llvm.used = appending global [1 x ptr] [ptr @a], section "llvm.metadata"
+ at llvm.compiler.used = appending global [1 x ptr] [ptr @b], section "llvm.metadata"
+
+define void @use_private() {
+  ; CHECK: load i32, ptr @a
+  %x = load i32, ptr @a
+  ; CHECK: load i32, ptr @b
+  %y = load i32, ptr @b
+  ; CHECK: load i32, ptr @_MergedGlobals
+  %z1 = load i32, ptr @c
+  ; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
+  %z2 = load i32, ptr @d
+  ret void
+}

>From a5adad15904c2c666ae5830c5d61b4aac433cecb Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Thu, 1 Aug 2024 12:20:36 -0500
Subject: [PATCH 2/2] Use hasLocalLinkage() instead of separately checking for
 internal and private linkage

---
 llvm/lib/CodeGen/GlobalMerge.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp
index e420c2bb7a25e..8aa4345cfd6df 100644
--- a/llvm/lib/CodeGen/GlobalMerge.cpp
+++ b/llvm/lib/CodeGen/GlobalMerge.cpp
@@ -664,7 +664,7 @@ bool GlobalMergeImpl::run(Module &M) {
       continue;
 
     if (!(Opt.MergeExternal && GV.hasExternalLinkage()) &&
-        !GV.hasInternalLinkage() && !GV.hasPrivateLinkage())
+        !GV.hasLocalLinkage())
       continue;
 
     PointerType *PT = dyn_cast<PointerType>(GV.getType());



More information about the llvm-commits mailing list