Skip to content

Commit ec3a34c

Browse files
eecksteintkremenek
authored andcommitted
Optimize the mangling of specialized generic functions. (#2845)
Don't include all the dependent types, which are not really needed to get unique symbol names. This reduces the symbol length (and therefore the binary size) significantly. Thanks, rjmccall, for your help!
1 parent ef6c0d7 commit ec3a34c

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

lib/SIL/Mangle.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,21 @@ static void mangleSubstitution(Mangler &M, Substitution Sub) {
5656
void GenericSpecializationMangler::mangleSpecialization() {
5757
Mangler &M = getMangler();
5858

59-
for (auto &Sub : Subs) {
60-
mangleSubstitution(M, Sub);
61-
M.append('_');
59+
SILFunctionType *FTy = Function->getLoweredFunctionType();
60+
CanGenericSignature Sig = FTy->getGenericSignature();
61+
62+
unsigned idx = 0;
63+
for (Type DepType : Sig->getAllDependentTypes()) {
64+
// It is sufficient to only mangle the substitutions of the "primary"
65+
// dependent types. As all other dependent types are just derived from the
66+
// primary types, this will give us unique symbol names.
67+
if (DepType->is<GenericTypeParamType>()) {
68+
mangleSubstitution(M, Subs[idx]);
69+
M.append('_');
70+
}
71+
++idx;
6272
}
73+
assert(idx == Subs.size() && "subs not parallel to dependent types");
6374
}
6475

6576
//===----------------------------------------------------------------------===//

test/SILOptimizer/eager_specialize.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bb0(%0 : $*T, %1 : $G<T>, %2 : $*T.Elt):
9696

9797
// Specialization getGenericContainer<S, X>
9898
//
99-
// CHECK-LABEL: sil shared @_TTSg5V4main1SS0_S_6HasEltS__VS_1XS2_S_5AnEltS____TF16eager_specialize19getGenericContaineruRxS_6HasEltwx3EltS_5AnEltrFTGVS_1Gx_1ewxS1__x : $@convention(thin) (G<S>, X) -> S {
99+
// CHECK-LABEL: sil shared @_TTSg5V4main1SS0_S_6HasEltS____TF16eager_specialize19getGenericContaineruRxS_6HasEltwx3EltS_5AnEltrFTGVS_1Gx_1ewxS1__x : $@convention(thin) (G<S>, X) -> S {
100100
// CHECK: bb0(%0 : $G<S>, %1 : $X):
101101
// CHECK: return %{{.*}} : $S
102102

@@ -126,7 +126,7 @@ bb0(%0 : $*T, %1 : $G<T>, %2 : $*T.Elt):
126126
// CHECK: %16 = unchecked_addr_cast %2 : $*T.Elt to $*X
127127
// CHECK: %17 = load %16 : $*X
128128
// function_ref specialized getGenericContainer<A where ...> (G<A>, e : A.Elt) -> A
129-
// CHECK: %18 = function_ref @_TTSg5V4main1SS0_S_6HasEltS__VS_1XS2_S_5AnEltS____TF16eager_specialize19getGenericContaineruRxS_6HasEltwx3EltS_5AnEltrFTGVS_1Gx_1ewxS1__x : $@convention(thin) (G<S>, X) -> S
129+
// CHECK: %18 = function_ref @_TTSg5V4main1SS0_S_6HasEltS____TF16eager_specialize19getGenericContaineruRxS_6HasEltwx3EltS_5AnEltrFTGVS_1Gx_1ewxS1__x : $@convention(thin) (G<S>, X) -> S
130130
// CHECK: %19 = apply %18(%15, %17) : $@convention(thin) (G<S>, X) -> S
131131
// CHECK: store %19 to %14 : $*S
132132
// CHECK: %21 = tuple ()
@@ -196,7 +196,7 @@ bb2: // Preds: bb0
196196
}
197197

198198
// specialized divideNum<A where ...> (A, den : A) throws -> A
199-
// CHECK-LABEL: sil shared @_TTSg5SiSis13SignedIntegers_SiSis33_BuiltinIntegerLiteralConvertibles_SiSis12SignedNumbers_SiSiS0_s_Si___TF16eager_specialize9divideNumuRxs13SignedIntegerrFzTx3denx_x : $@convention(thin) (Int, Int) -> (Int, @error ErrorProtocol) {
199+
// CHECK-LABEL: sil shared @_TTSg5SiSis13SignedIntegers___TF16eager_specialize9divideNumuRxs13SignedIntegerrFzTx3denx_x : $@convention(thin) (Int, Int) -> (Int, @error ErrorProtocol) {
200200
// CHECK: bb0(%0 : $Int, %1 : $Int):
201201
// CHECK: return %{{.*}}
202202
// CHECK: throw %{{.*}}
@@ -238,7 +238,7 @@ bb2: // Preds: bb0
238238
// CHECK: %{{.*}} = unchecked_addr_cast %2 : $*T to $*Int
239239
// CHECK: %{{.*}} = load %{{.*}} : $*Int
240240
// CHECK: // function_ref specialized divideNum<A where ...> (A, den : A) throws -> A
241-
// CHECK: %{{.*}} = function_ref @_TTSg5SiSis13SignedIntegers_SiSis33_BuiltinIntegerLiteralConvertibles_SiSis12SignedNumbers_SiSiS0_s_Si___TF16eager_specialize9divideNumuRxs13SignedIntegerrFzTx3denx_x : $@convention(thin) (Int, Int) -> (Int, @error ErrorProtocol)
241+
// CHECK: %{{.*}} = function_ref @_TTSg5SiSis13SignedIntegers___TF16eager_specialize9divideNumuRxs13SignedIntegerrFzTx3denx_x : $@convention(thin) (Int, Int) -> (Int, @error ErrorProtocol)
242242
// CHECK: try_apply %{{.*}}(%{{.*}}, %{{.*}}) : $@convention(thin) (Int, Int) -> (Int, @error ErrorProtocol), normal bb8, error bb7
243243

244244
// CHECK: bb7(%{{.*}} : $ErrorProtocol): // Preds: bb6

test/SILOptimizer/prespecialize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// CHECK-LABEL: sil [noinline] @_TF13prespecialize4testFTRGSaSi_4sizeSi_T_
99
//
1010
// function_ref specialized Collection<A where ...>.makeIterator() -> IndexingIterator<A>
11-
// CHECK: function_ref @_TTSgq5GVs14CountableRangeSi_GS_Si_s10Collections_SiSis10Comparables_SiSis13SignedIntegers_SiSis33_BuiltinIntegerLiteralConvertibles_SiSis12SignedNumbers_SiSiS3_s_Si_GS_Si_GS_Si_s13IndexableBasesGS_Si_s8Sequences_SiSiS1_s_GVs16IndexingIteratorGS_Si__GS7_GS_Si__s16IteratorProtocols_Si_GS_Si__Si_GS_Si_GS_Si_S5_sGS_Si_S6_s_SiSiS1_s_GS7_GS_Si__GS7_GS_Si__S8_s_Si_GS_Si__Si_Si___TFesRxs10Collectionwx8IteratorzGVs16IndexingIteratorx_wx8_ElementzWxS0_7Element_rS_12makeIteratorfT_GS1_x_
11+
// CHECK: function_ref @_TTSgq5GVs14CountableRangeSi_GS_Si_s10Collections___TFesRxs10Collectionwx8IteratorzGVs16IndexingIteratorx_wx8_ElementzWxS0_7Element_rS_12makeIteratorfT_GS1_x_
1212
//
1313
// function_ref specialized IndexingIterator.next() -> A._Element?
14-
// CHECK: function_ref @_TTSgq5GVs14CountableRangeSi_GS_Si_s13IndexableBases_SiSis10Comparables_GS_Si__Si___TFVs16IndexingIterator4nextfT_GSqwx8_Element_
14+
// CHECK: function_ref @_TTSgq5GVs14CountableRangeSi_GS_Si_s13IndexableBases___TFVs16IndexingIterator4nextfT_GSqwx8_Element_
1515
//
1616
// Look for generic specialization <Swift.Int> of Swift.Array.subscript.getter : (Swift.Int) -> A
1717
// CHECK: function_ref {{@_TTSgq5Si___TFSag9subscriptFSix|@_TTSg5Si___TFSaap9subscriptFSix}}

test/SILOptimizer/specialize_reabstraction.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ sil @test : $@convention(thin) (Val<Bool>, Val<Int>) -> Val<(Bool, Int)> {
6666
bb0(%0 : $Val<Bool>, %1 : $Val<Int>):
6767
// CHECK: [[COERCE:%.*]] = function_ref @coerce
6868
%2 = function_ref @coerce : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2> (@owned @callee_owned (@owned Ref<τ_0_0>) -> @owned @callee_owned (@owned Ref<τ_0_1>) -> @owned Ref<τ_0_2>) -> @owned @callee_owned (Val<τ_0_1>) -> Val<τ_0_2>
69-
// CHECK: [[MERGE:%.*]] = function_ref @_TTSg5GC4main3RefSb_GS0_Sb_S_8RefProtoS__Sb_Si__merge_curried
69+
// CHECK: [[MERGE:%.*]] = function_ref @_TTSg5GC4main3RefSb_GS0_Sb_S_8RefProtoS__Si__merge_curried
7070
%3 = function_ref @merge_curried : $@convention(thin) <τ_0_0 where τ_0_0 : RefProto><τ_1_0> (@in τ_0_0) -> @owned @callee_owned (@owned Ref<τ_1_0>) -> @owned Ref<(τ_0_0.T, τ_1_0)>
7171
// CHECK: [[PARTIAL:%.*]] = partial_apply [[MERGE]]()
7272
%4 = partial_apply %3<Ref<Bool>, Bool, Int>() : $@convention(thin) <τ_0_0 where τ_0_0 : RefProto><τ_1_0> (@in τ_0_0) -> @owned @callee_owned (@owned Ref<τ_1_0>) -> @owned Ref<(τ_0_0.T, τ_1_0)>

0 commit comments

Comments
 (0)