Skip to content

Commit c7f35f9

Browse files
committed
SILCombine: remove dead string.init_empty_with_capacity semantic calls
This avoids leftover runtime calls for constant folded large interpolated strings. rdar://127308237
1 parent c2a5028 commit c7f35f9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,16 @@ static bool shouldReplaceCallByMetadataConstructor(CanType storageMetaTy) {
14481448
return false;
14491449
}
14501450

1451+
static bool canBeRemovedIfResultIsNotUsed(SILFunction *f) {
1452+
if (f->getEffectsKind() < EffectsKind::ReleaseNone)
1453+
return true;
1454+
1455+
if (f->hasSemanticsAttr("string.init_empty_with_capacity"))
1456+
return true;
1457+
1458+
return false;
1459+
}
1460+
14511461
SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
14521462
Builder.setCurrentDebugScope(AI->getDebugScope());
14531463
// apply{partial_apply(x,y)}(z) -> apply(z,x,y) is triggered
@@ -1469,7 +1479,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
14691479

14701480
// Optimize readonly functions with no meaningful users.
14711481
SILFunction *SF = AI->getReferencedFunctionOrNull();
1472-
if (SF && SF->getEffectsKind() < EffectsKind::ReleaseNone) {
1482+
if (SF && canBeRemovedIfResultIsNotUsed(SF)) {
14731483
UserListTy Users;
14741484
if (recursivelyCollectARCUsers(Users, AI)) {
14751485
if (eraseApply(AI, Users))

test/SILOptimizer/string_optimization.swift

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public func testFoldConcat() -> String {
6969
return "a" + "b" + "c"
7070
}
7171

72+
// CHECK-LABEL: sil hidden [noinline] @$s4test0A25InterpolationInLongStringSSyF :
73+
// CHECK-NOT: apply
74+
// CHECK-NOT: bb1
75+
// CHECK: } // end sil function '$s4test0A25InterpolationInLongStringSSyF'
76+
@inline(never)
77+
func testInterpolationInLongString() -> String {
78+
return "\(#function) used in a veeeeeeeeeeeeeeeeeeeery long string"
79+
}
80+
7281
// CHECK-LABEL: sil [noinline] @$s4test0A19UnqualifiedTypeNameSSyF
7382
// CHECK-NOT: apply
7483
// CHECK-NOT: bb1
@@ -141,6 +150,9 @@ printEmbedded(testFoldStaticLet())
141150
// CHECK-OUTPUT: <abc>
142151
printEmbedded(testFoldConcat())
143152

153+
// CHECK-OUTPUT: <testInterpolationInLongString() used in a veeeeeeeeeeeeeeeeeeeery long string>
154+
printEmbedded(testInterpolationInLongString())
155+
144156
// CHECK-OUTPUT: <Inner>
145157
printEmbedded(testUnqualifiedTypeName())
146158

0 commit comments

Comments
 (0)