Skip to content

Commit f3131c9

Browse files
authored
[GlobalMerge] Aggressively merge constants to reduce TOC entries (#111756)
Symbols that get mapped into the read-only section are loaded as part of the text segment and will always need a TOC entry to be addressable. Add an option to aggressively merge these read only globals to reduce TOC usage.
1 parent 2e686d6 commit f3131c9

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

llvm/lib/CodeGen/GlobalMerge.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ static cl::opt<bool> GlobalMergeGroupByUse(
119119
"global-merge-group-by-use", cl::Hidden,
120120
cl::desc("Improve global merge pass to look at uses"), cl::init(true));
121121

122+
static cl::opt<bool> GlobalMergeAllConst(
123+
"global-merge-all-const", cl::Hidden,
124+
cl::desc("Merge all const globals without looking at uses"),
125+
cl::init(false));
126+
122127
static cl::opt<bool> GlobalMergeIgnoreSingleUse(
123128
"global-merge-ignore-single-use", cl::Hidden,
124129
cl::desc("Improve global merge pass to ignore globals only used alone"),
@@ -263,7 +268,7 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
263268
});
264269

265270
// If we want to just blindly group all globals together, do so.
266-
if (!GlobalMergeGroupByUse) {
271+
if (!GlobalMergeGroupByUse || (GlobalMergeAllConst && isConst)) {
267272
BitVector AllGlobals(Globals.size());
268273
AllGlobals.set();
269274
return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
; RUN: llc --verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff \
2+
; RUN: -global-merge-all-const=true < %s | FileCheck %s
3+
4+
; RUN: llc --verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff \
5+
; RUN: -global-merge-all-const=false < %s | FileCheck --check-prefix=NOMERGE %s
6+
7+
%struct.pc_t = type { i8 }
8+
%struct.S = type { i32, i32, i32, i32, [9 x i32] }
9+
10+
@constinit = private unnamed_addr constant <{ i32, i32, i32, i32, [9 x i32] }> <{ i32 0, i32 0, i32 0, i32 2, [9 x i32] zeroinitializer }>, align 4
11+
@.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
12+
@.str.1 = private unnamed_addr constant [6 x i8] c"world\00", align 1
13+
@.str.2 = private unnamed_addr constant [6 x i8] c"abcde\00", align 1
14+
@.str.3 = private unnamed_addr constant [6 x i8] c"fghij\00", align 1
15+
@pc = internal constant %struct.pc_t zeroinitializer, align 1
16+
@s = internal constant %struct.S { i32 1, i32 2, i32 3, i32 4, [9 x i32] [i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13] }, align 4
17+
18+
; Function Attrs: mustprogress
19+
define noundef i32 @f5() {
20+
entry:
21+
%call = tail call noundef i32 @f4(ptr noundef nonnull @pc)
22+
ret i32 %call
23+
}
24+
25+
declare noundef i32 @f4(ptr noundef)
26+
declare noundef i32 @printf(ptr nocapture noundef readonly, ...)
27+
28+
define noundef i32 @f1() {
29+
entry:
30+
%call = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
31+
ret i32 %call
32+
}
33+
34+
35+
; Function Attrs: mustprogress nofree nounwind
36+
define noundef i32 @f2() {
37+
entry:
38+
%call = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
39+
ret i32 %call
40+
}
41+
42+
define noundef i32 @f3() {
43+
entry:
44+
%call = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
45+
ret i32 %call
46+
}
47+
48+
define noundef i32 @f7() {
49+
entry:
50+
%call = tail call noundef i32 @f6(ptr noundef nonnull @s)
51+
ret i32 %call
52+
}
53+
54+
declare noundef i32 @f6(ptr noundef)
55+
56+
; CHECK: .csect L.._MergedGlobals[RO],2
57+
; CHECK-NEXT: .lglobl pc # @_MergedGlobals
58+
; CHECK-NEXT: .lglobl s
59+
; CHECK-NEXT: .align 2
60+
; CHECK-NEXT:pc:
61+
; CHECK-NEXT: .space 1
62+
; CHECK-NEXT:L...str:
63+
; CHECK-NEXT: .string "hello"
64+
; CHECK-NEXT:L...str.1:
65+
; CHECK-NEXT: .string "world"
66+
; CHECK-NEXT:L...str.2:
67+
; CHECK-NEXT: .string "abcde"
68+
; CHECK-NEXT:L...str.3:
69+
; CHECK-NEXT: .string "fghij"
70+
; CHECK-NEXT: .space 3
71+
; CHECK-NEXT:L..constinit:
72+
; CHECK-NEXT: .vbyte 4, 0 # 0x0
73+
; CHECK-NEXT: .vbyte 4, 0 # 0x0
74+
; CHECK-NEXT: .vbyte 4, 0 # 0x0
75+
; CHECK-NEXT: .vbyte 4, 2 # 0x2
76+
; CHECK-NEXT: .space 36
77+
; CHECK-NEXT:s:
78+
; CHECK-NEXT: .vbyte 4, 1 # 0x1
79+
; CHECK-NEXT: .vbyte 4, 2 # 0x2
80+
; CHECK-NEXT: .vbyte 4, 3 # 0x3
81+
; CHECK-NEXT: .vbyte 4, 4 # 0x4
82+
; CHECK-NEXT: .vbyte 4, 5 # 0x5
83+
; CHECK-NEXT: .vbyte 4, 6 # 0x6
84+
; CHECK-NEXT: .vbyte 4, 7 # 0x7
85+
; CHECK-NEXT: .vbyte 4, 8 # 0x8
86+
; CHECK-NEXT: .vbyte 4, 9 # 0x9
87+
; CHECK-NEXT: .vbyte 4, 10 # 0xa
88+
; CHECK-NEXT: .vbyte 4, 11 # 0xb
89+
; CHECK-NEXT: .vbyte 4, 12 # 0xc
90+
; CHECK-NEXT: .vbyte 4, 13 # 0xd
91+
92+
93+
; NOMERGE-NOT: L.._MergedGGlobals[RO]

0 commit comments

Comments
 (0)