Skip to content

Commit 38d1bc9

Browse files
committed
DeadFunctionElimination: consider globals which are referenced by global_value instructions in static initializers
1 parent cc78c8f commit 38d1bc9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class DeadFunctionAndGlobalElimination {
222222
ensureAlive(fRef->getReferencedFunction());
223223
if (auto *gRef = dyn_cast<GlobalAddrInst>(&initInst))
224224
ensureAlive(gRef->getReferencedGlobal());
225+
if (auto *gVal = dyn_cast<GlobalValueInst>(&initInst))
226+
ensureAlive(gVal->getReferencedGlobal());
225227
}
226228
}
227229

test/SILOptimizer/remove_unused_func.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sil_stage canonical
55

66
import Builtin
77
import Swift
8+
import SwiftShims
89

910
// CHECK-LABEL: sil_global private @privateGlobal
1011
sil_global private @privateGlobal : $Int64
@@ -43,6 +44,27 @@ sil_global @referencing_private_globals : $Builtin.RawPointer = {
4344
%initval = address_to_pointer %0 : $*Builtin.RawPointer to $Builtin.RawPointer
4445
}
4546

47+
// CHECK-LABEL: sil_global private @array_buffer
48+
sil_global private @array_buffer : $_ContiguousArrayStorage<Int> = {
49+
%0 = integer_literal $Builtin.Int64, 3
50+
%1 = struct $Int (%0 : $Builtin.Int64)
51+
%2 = integer_literal $Builtin.Int64, 6
52+
%3 = struct $UInt (%2 : $Builtin.Int64)
53+
%4 = struct $_SwiftArrayBodyStorage (%1 : $Int, %3 : $UInt)
54+
%5 = struct $_ArrayBody (%4 : $_SwiftArrayBodyStorage)
55+
%6 = integer_literal $Builtin.Int64, 1
56+
%7 = struct $Int (%6 : $Builtin.Int64)
57+
%8 = integer_literal $Builtin.Int64, 2
58+
%9 = struct $Int (%8 : $Builtin.Int64)
59+
%initval = object $_ContiguousArrayStorage<Int> (%5 : $_ArrayBody, [tail_elems] %7 : $Int, %9 : $Int, %1 : $Int)
60+
}
61+
62+
// CHECK-LABEL: sil_global @referencing_object
63+
sil_global @referencing_object : $Builtin.BridgeObject = {
64+
%0 = global_value @array_buffer : $_ContiguousArrayStorage<Int>
65+
%initval = unchecked_ref_cast %0 : $_ContiguousArrayStorage<Int> to $Builtin.BridgeObject
66+
}
67+
4668
// KEEP-NOT: @dead_self_referencing_private_global
4769
sil_global private @dead_self_referencing_private_global : $Builtin.RawPointer = {
4870
%0 = global_addr @dead_self_referencing_private_global : $*Builtin.RawPointer

0 commit comments

Comments
 (0)