Skip to content

Optimize keypaths in language 6 mode #81139

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
Apr 29, 2025
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
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Utils/KeyPathProjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,8 @@ class CompleteKeyPathProjector : public KeyPathProjector {

KeyPathInst *
KeyPathProjector::getLiteralKeyPath(SILValue keyPath) {
while (auto *upCast = dyn_cast<UpcastInst>(keyPath)) {
keyPath = lookThroughOwnershipInsts(upCast->getOperand());
while (isa<UpcastInst>(keyPath) || isa<OpenExistentialRefInst>(keyPath)) {
keyPath = lookThroughOwnershipInsts(cast<SingleValueInstruction>(keyPath)->getOperand(0));
}

return dyn_cast<KeyPathInst>(keyPath);
Expand Down
6 changes: 6 additions & 0 deletions test/SILOptimizer/capture_propagate_keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func testGenStr(_ mymap: ((GenStr<Int>)->Int) -> ()) {

// CHECK-LABEL: sil {{.*}} @$s4test0A22GenericEscapingClosureAA5GetIDVySayAA3StrVGSiGyF :
// CHECK-NOT: keypath
// CHECK: = function_ref @[[SPECIALIZED_CLOSURE:.*]] : $@convention(thin) (@in_guaranteed Str) -> @out Int
// CHECK-NOT: keypath
// CHECK-LABEL: } // end sil function '$s4test0A22GenericEscapingClosureAA5GetIDVySayAA3StrVGSiGyF'
@inline(never)
func testGenericEscapingClosure() -> GetID<[Str], Int> {
Expand All @@ -85,6 +87,10 @@ public func _opaqueIdentity<T>(_ x: T) -> T {
return x
}

// CHECK: sil shared {{.*}}@[[SPECIALIZED_CLOSURE]] :
// CHECK-NOT: keypath
// CHECK: } // end sil function '[[SPECIALIZED_CLOSURE]]'

func calltests() {
// CHECK-OUTPUT-LABEL: testSimple:
print("testSimple:")
Expand Down
36 changes: 29 additions & 7 deletions test/SILOptimizer/optimize_keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
// RUN: %FileCheck %s < %t/output.sil
// RUN: %FileCheck -check-prefix=CHECK-ALL %s < %t/output.sil

// RUN: %target-swift-frontend -primary-file %s -O -sil-verify-all -swift-version 6 -Xllvm -sil-print-types -emit-sil >%t/output6.sil
// RUN: %FileCheck %s < %t/output6.sil
// RUN: %FileCheck -check-prefix=CHECK-ALL %s < %t/output6.sil

// RUN: %target-build-swift -O %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT -check-prefix=CHECK5-OUTPUT

// RUN: %target-build-swift -swift-version 6 -O %s -o %t/a6.out
// RUN: %target-run %t/a6.out | %FileCheck %s -check-prefix=CHECK-OUTPUT

// REQUIRES: executable_test,optimized_stdlib
// REQUIRES: CPU=arm64 || CPU=x86_64

Expand All @@ -28,7 +36,9 @@ struct GenStruct<T : P> : P {
}
}

#if !swift(>=6)
var numGenClassObjs = 0
#endif

final class GenClass<T : P> : P {
var ct: T
Expand All @@ -39,11 +49,15 @@ final class GenClass<T : P> : P {
init(_ ct: T) {
self.ct = ct
self.gs = .init(ct)
#if !swift(>=6)
numGenClassObjs += 1
#endif
}

deinit {
#if !swift(>=6)
numGenClassObjs -= 1
#endif
}

func modifyIt() {
Expand All @@ -63,7 +77,9 @@ final class DerivedClass2 : DerivedClass<Int> {

final class SimpleClass : P {
var i: Int
#if !swift(>=6)
static var numObjs = 0
#endif

var tuple = (0, 1)

Expand All @@ -78,11 +94,15 @@ final class SimpleClass : P {
init(_ i: Int, nested: Int? = nil) {
self.i = i
self.opt = nested.map { Nested(i: $0) }
#if !swift(>=6)
Self.numObjs += 1
#endif
}

deinit {
#if !swift(>=6)
Self.numObjs -= 1
#endif
}

func modifyIt() {
Expand Down Expand Up @@ -422,7 +442,7 @@ func testModifyOptionalForceClass(_ s: inout SimpleClass) {
//
// Check if value is null
// CHECK: switch_enum [[O:%[0-9]+]]
// CHECK: {{bb.}}:
// CHECK: bb{{.*}}:
// Unwrap value
// CHECK: [[A1:%[0-9]+]] = alloc_stack
// CHECK: store [[O]] to [[A1]]
Expand Down Expand Up @@ -453,7 +473,7 @@ func testOptionalChain(_ s: SimpleStruct) -> Int? {
// CHECK: [[E2:%[0-9]+]] = begin_access [read] [dynamic] [no_nested_conflict] [[E1]]
// Check if value is null
// CHECK: switch_enum [[O:%[0-9]+]]
// CHECK: {{bb.}}:
// CHECK: bb{{.*}}:
// Unwrap value
// CHECK: [[A1:%[0-9]+]] = alloc_stack
// CHECK: store [[O]] to [[A1]]
Expand Down Expand Up @@ -483,15 +503,15 @@ func testOptionalChainClass(_ s: SimpleClass) -> Int? {
//
// Check if value is null
// CHECK: switch_enum [[O:%[0-9]+]]
// CHECK: {{bb.}}:
// CHECK: bb{{.*}}:
// Unwrap value
// CHECK: [[A1:%[0-9]+]] = alloc_stack
// CHECK: store [[O]] to [[A1]]
// CHECK: [[U:%[0-9]+]] = unchecked_take_enum_data_addr [[A1]]
//
// Unwrap nested optional
// CHECK: switch_enum [[O2:%[0-9]+]]
// CHECK: {{bb.}}:
// CHECK: bb{{.*}}:
// CHECK: [[A2:%[0-9]+]] = alloc_stack
// CHECK: store [[O2]] to [[A2]]
// CHECK: [[U2:%[0-9]+]] = unchecked_take_enum_data_addr [[A2]]
Expand Down Expand Up @@ -705,9 +725,11 @@ func testit() {

testit()

// CHECK-OUTPUT: SimpleClass obj count: 0
#if !swift(>=6)
// CHECK5-OUTPUT: SimpleClass obj count: 0
print("SimpleClass obj count: \(SimpleClass.numObjs)")
// CHECK-OUTPUT: GenClass obj count: 0
// CHECK5-OUTPUT: GenClass obj count: 0
print("GenClass obj count: \(numGenClassObjs)")
#endif