Skip to content

SILCombine: remove dead string.init_empty_with_capacity semantic calls #73388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,16 @@ static bool shouldReplaceCallByMetadataConstructor(CanType storageMetaTy) {
return false;
}

static bool canBeRemovedIfResultIsNotUsed(SILFunction *f) {
if (f->getEffectsKind() < EffectsKind::ReleaseNone)
return true;

if (f->hasSemanticsAttr("string.init_empty_with_capacity"))
return true;

return false;
}

SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
Builder.setCurrentDebugScope(AI->getDebugScope());
// apply{partial_apply(x,y)}(z) -> apply(z,x,y) is triggered
Expand All @@ -1469,7 +1479,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {

// Optimize readonly functions with no meaningful users.
SILFunction *SF = AI->getReferencedFunctionOrNull();
if (SF && SF->getEffectsKind() < EffectsKind::ReleaseNone) {
if (SF && canBeRemovedIfResultIsNotUsed(SF)) {
UserListTy Users;
if (recursivelyCollectARCUsers(Users, AI)) {
if (eraseApply(AI, Users))
Expand Down
12 changes: 12 additions & 0 deletions test/SILOptimizer/string_optimization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public func testFoldConcat() -> String {
return "a" + "b" + "c"
}

// CHECK-LABEL: sil hidden [noinline] @$s4test0A25InterpolationInLongStringSSyF :
// CHECK-NOT: apply
// CHECK-NOT: bb1
// CHECK: } // end sil function '$s4test0A25InterpolationInLongStringSSyF'
@inline(never)
func testInterpolationInLongString() -> String {
return "\(#function) used in a veeeeeeeeeeeeeeeeeeeery long string"
}

// CHECK-LABEL: sil [noinline] @$s4test0A19UnqualifiedTypeNameSSyF
// CHECK-NOT: apply
// CHECK-NOT: bb1
Expand Down Expand Up @@ -141,6 +150,9 @@ printEmbedded(testFoldStaticLet())
// CHECK-OUTPUT: <abc>
printEmbedded(testFoldConcat())

// CHECK-OUTPUT: <testInterpolationInLongString() used in a veeeeeeeeeeeeeeeeeeeery long string>
printEmbedded(testInterpolationInLongString())

// CHECK-OUTPUT: <Inner>
printEmbedded(testUnqualifiedTypeName())

Expand Down