Skip to content

SILCombine: handle properties in superclasses in the keypath optimization #27804

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
Oct 21, 2019
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
16 changes: 15 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,23 @@ static SILValue createKeypathProjections(SILValue keyPath, SILValue root,
if (addr->getType().getStructOrBoundGenericStruct()) {
addr = builder.createStructElementAddr(loc, addr, storedProperty);
} else if (addr->getType().getClassOrBoundGenericClass()) {
LoadInst *Ref = builder.createLoad(loc, addr,
SingleValueInstruction *Ref = builder.createLoad(loc, addr,
LoadOwnershipQualifier::Unqualified);
insertEndAccess(beginAccess, /*isModify*/ false, builder);

// Handle the case where the storedProperty is in a super class.
while (Ref->getType().getClassOrBoundGenericClass() !=
storedProperty->getDeclContext()) {
SILType superCl = Ref->getType().getSuperclass();
if (!superCl) {
// This should never happen, because the property should be in the
// decl or in a superclass of it. Just handle this to be on the safe
// side.
return SILValue();
}
Ref = builder.createUpcast(loc, Ref, superCl);
}

addr = builder.createRefElementAddr(loc, Ref, storedProperty);

// Class members need access enforcement.
Expand Down
43 changes: 43 additions & 0 deletions test/SILOptimizer/optimize_keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ final class GenClass<T : P> : P {
}
}

class Base<T> {
final var i: Int = 12
}

class DerivedClass<T> : Base<T> {
}

final class DerivedClass2 : DerivedClass<Int> {
}

final class SimpleClass : P {
var i: Int
static var numObjs = 0
Expand Down Expand Up @@ -97,6 +107,33 @@ func testGenClassRead<T>(_ c: GenClass<T>) -> T {
return c[keyPath: kp]
}

// CHECK-LABEL: sil {{.*}}testDerivedClassRead
// CHECK: [[C:%[0-9]+]] = upcast %0
// CHECK: [[E:%[0-9]+]] = ref_element_addr [[C]]
// CHECK: [[A:%[0-9]+]] = begin_access [read] [dynamic] [no_nested_conflict] [[E]]
// CHECK: [[V:%[0-9]+]] = load [[A]]
// CHECK: end_access [[A]]
// CHECK: return [[V]]
@inline(never)
@_semantics("optimize.sil.specialize.generic.never")
func testDerivedClassRead<T>(_ c: DerivedClass<T>) -> Int {
let kp = \DerivedClass<T>.i
return c[keyPath: kp]
}

// CHECK-LABEL: sil {{.*}}testDerivedClass2Read
// CHECK: [[C:%[0-9]+]] = upcast %0
// CHECK: [[E:%[0-9]+]] = ref_element_addr [[C]]
// CHECK: [[A:%[0-9]+]] = begin_access [read] [dynamic] [no_nested_conflict] [[E]]
// CHECK: [[V:%[0-9]+]] = load [[A]]
// CHECK: end_access [[A]]
// CHECK: return [[V]]
@inline(never)
func testDerivedClass2Read(_ c: DerivedClass2) -> Int {
let kp = \DerivedClass2.i
return c[keyPath: kp]
}

// CHECK-LABEL: sil {{.*}}testGenClassWrite
// CHECK: [[S:%[0-9]+]] = alloc_stack $T
// CHECK: [[E:%[0-9]+]] = ref_element_addr %0
Expand Down Expand Up @@ -233,6 +270,12 @@ func testit() {
// CHECK-OUTPUT: GenClassRead: 29
print("GenClassRead: \(testGenClassRead(GenClass(SimpleClass(29))).i)")

// CHECK-OUTPUT: DerivedClassRead: 12
print("DerivedClassRead: \(testDerivedClassRead(DerivedClass<Int>())))")

// CHECK-OUTPUT: DerivedClass2Read: 12
print("DerivedClass2Read: \(testDerivedClass2Read(DerivedClass2())))")

// CHECK-OUTPUT: GenClassWrite: 30
let c = GenClass(SimpleClass(0))
testGenClassWrite(c, SimpleClass(30))
Expand Down