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

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 13 07:13:41 PDT 2024


Author: Amy Kwan
Date: 2024-08-13T10:13:38-04:00
New Revision: 40897638837fdc5d64d8932fd892f3b9a687ee84

URL: https://github.com/llvm/llvm-project/commit/40897638837fdc5d64d8932fd892f3b9a687ee84
DIFF: https://github.com/llvm/llvm-project/commit/40897638837fdc5d64d8932fd892f3b9a687ee84.diff

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

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.

Added: 
    llvm/test/Transforms/GlobalMerge/private-global.ll

Modified: 
    llvm/lib/CodeGen/GlobalMerge.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp
index 65bf7161441bac..8aa4345cfd6df6 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.hasLocalLinkage())
       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 00000000000000..c4152a242d59fb
--- /dev/null
+++ b/llvm/test/Transforms/GlobalMerge/private-global.ll
@@ -0,0 +1,36 @@
+; 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/basic.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 1, i32 2 }>, align 4
+; CHECK: @_MergedGlobals.1 = private global <{ i32, i32 }> <{ i32 3, i32 4 }>, section "foo", align 4
+
+; CHECK-DAG: @a = private alias i32, ptr @_MergedGlobals{{$}}
+ at a = private global i32 1
+
+; CHECK-DAG: @b = private alias i32, getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
+ at b = private global i32 2
+
+; CHECK-DAG: @c = private alias i32, ptr @_MergedGlobals.1{{$}}
+ at c = private global i32 3, section "foo"
+
+; CHECK-DAG: @d = private alias i32, getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals.1, i32 0, i32 1)
+ at d = private global i32 4, section "foo"
+
+define void @use_private() {
+  ; CHECK: load i32, ptr @_MergedGlobals,
+  %x = load i32, ptr @a
+  ; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
+  %y = load i32, ptr @b
+  ; CHECK: load i32, ptr @_MergedGlobals.1
+  %z1 = load i32, ptr @c
+  ; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals.1, i32 0, i32 1)
+  %z2 = load i32, ptr @d
+  ret void
+}


        


More information about the llvm-commits mailing list