Skip to content

Commit 71eebaa

Browse files
committed
CSE: remove redundant global initializer calls
1 parent 84e2a56 commit 71eebaa

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,11 @@ bool CSE::canHandle(SILInstruction *Inst) {
965965

966966
if (isLazyPropertyGetter(AI))
967967
return true;
968+
969+
if (SILFunction *callee = AI->getReferencedFunctionOrNull()) {
970+
if (callee->isGlobalInit())
971+
return true;
972+
}
968973

969974
return false;
970975
}

test/SILOptimizer/cse.sil

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,31 @@ bb0(%0 : $Proto & Ping):
12671267
return %12 : $Ping
12681268
}
12691269

1270+
sil [global_init] @$s4test10testGlobalSivau : $@convention(thin) () -> Builtin.RawPointer
1271+
1272+
// CHECK-LABEL: sil @cse_global_init
1273+
// CHECK: [[P:%[0-9]+]] = apply
1274+
// CHECK: [[A:%[0-9]+]] = pointer_to_address [[P]]
1275+
// CHECK: begin_access [modify] [dynamic] [no_nested_conflict] [[A]]
1276+
// CHECK: begin_access [read] [dynamic] [no_nested_conflict] [[A]]
1277+
// CHECK: // end sil function 'cse_global_init'
1278+
sil @cse_global_init : $@convention(thin) () -> Int64 {
1279+
bb0:
1280+
%2 = function_ref @$s4test10testGlobalSivau : $@convention(thin) () -> Builtin.RawPointer
1281+
%3 = apply %2() : $@convention(thin) () -> Builtin.RawPointer
1282+
%4 = pointer_to_address %3 : $Builtin.RawPointer to [strict] $*Int64
1283+
%5 = integer_literal $Builtin.Int64, 42
1284+
%6 = struct $Int64 (%5 : $Builtin.Int64)
1285+
%7 = begin_access [modify] [dynamic] [no_nested_conflict] %4 : $*Int64
1286+
store %6 to %7 : $*Int64
1287+
end_access %7 : $*Int64
1288+
%10 = function_ref @$s4test10testGlobalSivau : $@convention(thin) () -> Builtin.RawPointer
1289+
%11 = apply %10() : $@convention(thin) () -> Builtin.RawPointer
1290+
%12 = pointer_to_address %11 : $Builtin.RawPointer to [strict] $*Int64
1291+
%33 = begin_access [read] [dynamic] [no_nested_conflict] %12 : $*Int64
1292+
%35 = load %33 : $*Int64
1293+
end_access %33 : $*Int64
1294+
return %35 : $Int64
1295+
}
1296+
1297+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -parse-as-library -O -module-name=test %s -emit-sil | %FileCheck %s
2+
3+
var gg: Int = {
4+
print("gg init")
5+
return 27
6+
}()
7+
8+
// CHECK-LABEL: sil @$s4test3cseSiyF
9+
// CHECK: builtin "once"
10+
// CHECK-NOT: builtin "once"
11+
// CHECK: [[G:%[0-9]+]] = load
12+
// CHECK-NOT: builtin "once"
13+
// CHECK: builtin "sadd_{{.*}}"([[G]] : $Builtin.Int{{[0-9]+}}, [[G]] : $Builtin.Int{{[0-9]+}}, %{{[0-9]+}} : $Builtin.Int1)
14+
// CHECK-NOT: builtin "once"
15+
// CHECK: } // end sil function '$s4test3cseSiyF'
16+
public func cse() -> Int {
17+
return gg + gg
18+
}

0 commit comments

Comments
 (0)