Skip to content

Commit 2070821

Browse files
committed
Add tests
1 parent 4dcbc42 commit 2070821

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,7 @@ bool SILGlobalOpt::run() {
10881088
optimizeGlobalAccess(Init.first, Init.second);
10891089
}
10901090

1091+
SmallVector<SILGlobalVariable *, 8> addrGlobals;
10911092
for (auto &addrPair : GlobalAddrMap) {
10921093
// Don't optimize functions that are marked with the opt.never attribute.
10931094
bool shouldOptimize = true;
@@ -1100,20 +1101,30 @@ bool SILGlobalOpt::run() {
11001101
if (!shouldOptimize)
11011102
continue;
11021103

1103-
HasChanged |= tryRemoveGlobalAddr(addrPair.first);
1104+
addrGlobals.push_back(addrPair.first);
1105+
}
1106+
1107+
for (auto *global : addrGlobals) {
1108+
HasChanged |= tryRemoveGlobalAddr(global);
11041109
}
11051110

1111+
SmallVector<std::pair<SILGlobalVariable *, AllocGlobalInst *>, 12> globalAllocPairs;
11061112
for (auto &alloc : AllocGlobalStore) {
11071113
if (!alloc.second->getFunction()->shouldOptimize())
11081114
continue;
1109-
HasChanged |= tryRemoveGlobalAlloc(alloc.first, alloc.second);
1115+
globalAllocPairs.push_back(std::make_pair(alloc.first, alloc.second));
1116+
}
1117+
1118+
for (auto &allocPair : globalAllocPairs) {
1119+
HasChanged |= tryRemoveGlobalAlloc(allocPair.first, allocPair.second);
11101120
}
11111121

11121122
// Copy the globals so we don't get issues with modifying while iterating.
11131123
SmallVector<SILGlobalVariable *, 12> globals;
11141124
for (auto &global : Module->getSILGlobals()) {
11151125
globals.push_back(&global);
11161126
}
1127+
11171128
for (auto *global : globals) {
11181129
HasChanged |= tryRemoveUnusedGlobal(global);
11191130
}

test/SILOptimizer/remove_unused_global_vars.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -primary-file %s -O -module-name=test -emit-sil | %FileCheck %s
2+
3+
@_optimize(none) public func make_test(_ x: Int) -> Int {
4+
return x
5+
}
6+
7+
struct Foo {
8+
let x : Int
9+
let y : Int
10+
func both() -> Int { x + y }
11+
}
212

313
// CHECK-NOT: sil_global private [let] {{.*}}unused1{{.*}}
414
private let unused1 = 0
@@ -9,6 +19,14 @@ private let used1 = 0
919
// CHECK: sil_global private @${{.*}}used2{{.*}} : $Int
1020
private var used2 = 0
1121

22+
// non-constant / non-trivial values
23+
// CHECK-NOT: sil_global private {{.*}}unused7{{.*}}
24+
private let unused7 = make_test(42)
25+
// CHECK-NOT: sil_global private {{.*}}unused8{{.*}}
26+
private let unused8 = Foo(x: 1, y: 1)
27+
// CHECK-NOT: sil_global private {{.*}}unused9{{.*}}
28+
private let unused9 = Foo(x: 1, y: 1).both()
29+
1230
// CHECK: sil_global [let] @${{.*}}unused3{{.*}} : $Int
1331
public let unused3 = 0
1432
// CHECK: sil_global @${{.*}}unused4{{.*}} : $Int
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-swift-frontend -O -wmo -emit-sil %s | %FileCheck %s
2+
3+
// CHECK-NOT: sil_global private [let] {{.*}}unused1{{.*}}
4+
private let unused1 = 0
5+
// CHECK-NOT: sil_global private {{.*}}unused2{{.*}}
6+
private var unused2 = 42
7+
// CHECK: sil_global private [let] @${{.*}}used1{{.*}} : $Int
8+
private let used1 = 0
9+
// CHECK: sil_global private @${{.*}}used2{{.*}} : $Int
10+
private var used2 = 0
11+
12+
// CHECK: sil_global [let] @${{.*}}unused3{{.*}} : $Int
13+
public let unused3 = 0
14+
// CHECK: sil_global @${{.*}}unused4{{.*}} : $Int
15+
public var unused4 = 0
16+
17+
// CHECK-NOT: sil_global hidden [let] @${{.*}}unused5{{.*}} : $Int
18+
let unused5 = 0
19+
// CHECK-NOT: sil_global hidden @${{.*}}unused6{{.*}} : $Int
20+
var unused6 = 0
21+
22+
// CHECK-LABEL: sil [Onone] @${{.*}}test{{.*}}
23+
@_optimize(none) public func test(x: Int) -> Int {
24+
// CHECK: %{{[0-9]+}} = global_addr @${{.*}}used2{{.*}}
25+
// CHECK: %{{[0-9]+}} = global_addr @${{.*}}used1{{.*}}
26+
return used1 + used2 + x
27+
}

0 commit comments

Comments
 (0)