Skip to content

Commit d896b1f

Browse files
[PowerPC] Do not string pool globals that are part of llvm used. (llvm#66848)
The string pooling pass was incorrectly pooling global varables that were part of llvm.used or llvm.compiler.used. This patch fixes the pass to prevent that by checking each candidate to make sure that it is not in either of those lists.
1 parent 0ff5281 commit d896b1f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ static bool hasReplaceableUsers(GlobalVariable &GV) {
140140
// valid candidates to be merged into the string pool. Valid candidates will
141141
// be added to MergeableStrings.
142142
void PPCMergeStringPool::collectCandidateConstants(Module &M) {
143+
SmallVector<GlobalValue *, 4> UsedV;
144+
collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/false);
145+
SmallVector<GlobalValue *, 4> UsedVCompiler;
146+
collectUsedGlobalVariables(M, UsedVCompiler, /*CompilerUsed=*/true);
147+
// Combine all of the Global Variables marked as used into a SmallPtrSet for
148+
// faster lookup inside the loop.
149+
SmallPtrSet<GlobalValue *, 8> AllUsedGlobals;
150+
AllUsedGlobals.insert(UsedV.begin(), UsedV.end());
151+
AllUsedGlobals.insert(UsedVCompiler.begin(), UsedVCompiler.end());
152+
143153
for (GlobalVariable &Global : M.globals()) {
144154
LLVM_DEBUG(dbgs() << "Looking at global:");
145155
LLVM_DEBUG(Global.dump());
@@ -171,6 +181,10 @@ void PPCMergeStringPool::collectCandidateConstants(Module &M) {
171181
if (!ConstData)
172182
continue;
173183

184+
// Do not pool globals that are part of llvm.used or llvm.compiler.end.
185+
if (AllUsedGlobals.contains(&Global))
186+
continue;
187+
174188
if (!hasReplaceableUsers(Global))
175189
continue;
176190

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;; Test that the string pooling pass does not pool globals that are
2+
;; in llvm.used or in llvm.compiler.used.
3+
4+
; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple powerpc-ibm-aix-xcoff -data-sections=false < %s | \
5+
; RUN: FileCheck %s
6+
7+
; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple powerpc64-ibm-aix-xcoff -data-sections=false < %s | \
8+
; RUN: FileCheck %s
9+
10+
@keep_this = internal constant [5 x i8] c"keep1", align 1
11+
@keep_this2 = internal constant [5 x i8] c"keep2", align 1
12+
@.str.1 = private unnamed_addr constant [12 x i8] c"str1_STRING\00", align 1
13+
@.str.2 = private unnamed_addr constant [12 x i8] c"str2_STRING\00", align 1
14+
@.str.3 = private unnamed_addr constant [12 x i8] c"str3_STRING\00", align 1
15+
@llvm.used = appending global [1 x ptr] [ptr @keep_this], section "llvm.metadata"
16+
@llvm.compiler.used = appending global [1 x ptr] [ptr @keep_this2], section "llvm.metadata"
17+
18+
declare signext i32 @callee(ptr noundef)
19+
20+
define dso_local signext i32 @keep1() {
21+
entry:
22+
%call = tail call signext i32 @callee(ptr noundef nonnull @keep_this)
23+
ret i32 %call
24+
}
25+
26+
define dso_local signext i32 @keep2() {
27+
entry:
28+
%call = tail call signext i32 @callee(ptr noundef nonnull @keep_this2)
29+
ret i32 %call
30+
}
31+
32+
define dso_local signext i32 @str1() {
33+
entry:
34+
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.1)
35+
ret i32 %call
36+
}
37+
38+
define dso_local signext i32 @str2() {
39+
entry:
40+
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.2)
41+
ret i32 %call
42+
}
43+
44+
define dso_local signext i32 @str3() {
45+
entry:
46+
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.3)
47+
ret i32 %call
48+
}
49+
50+
; CHECK: .lglobl keep_this
51+
; CHECK: keep_this:
52+
; CHECK: .lglobl keep_this2
53+
; CHECK: keep_this2:
54+
; CHECK: L..__ModuleStringPool:
55+
; CHECK: .string "str1_STRING"
56+
; CHECK: .string "str2_STRING"
57+
; CHECK: .string "str3_STRING"

0 commit comments

Comments
 (0)