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

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 12:13:51 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Amy Kwan (amy-kwan)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/101222.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/GlobalMerge.cpp (+1-1) 
- (added) llvm/test/Transforms/GlobalMerge/private-global.ll (+31) 


``````````diff
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
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/101222


More information about the llvm-commits mailing list