[swift-dev] SILVerifier: Should SuperMethodInst's result type be the same type as the referenced member?

David Farler dfarler at apple.com
Wed Dec 9 20:09:45 CST 2015

[Joe, Roman, sorry for resend, I got the e-mail group wrong]

In SuperMethodInst's verifier, we have:

require(CMI->getType() == TC.getConstantType(CMI->getMember()),
       "result type of super_method must match type of method");

I think this assumption was valid when we only allowed super_method on foreign classes, without needing to worry about reabstractions. Now that we're allowing super_method with operands of native class type which can be generic, does this check make sense anymore?

Consider the following:

class Parent<A> {
 let x: A
 required init(x: A) { self.x = x }

class Child : Parent<String> {
 required init(x: String) {
   super.init(x: x)

class Grandchild : Child {}

Here, the vtable thunks for their initializers have respective types:

$@convention(method) <T> (@in T, @owned Base<T>) -> @owned Base<T>
$@convention(method) (@in String, @owned Child) -> @owned Child
$@convention(method) (@in String, @owned Grandchild) -> @owned Grandchild

However, the real backing implementations have these respective types:

$@convention(method) <T> (@in T, @owned Base<T>) -> @owned Base<T>
$@convention(method) (@owned String, @owned Child) -> @owned Child
$@convention(method) (@owned String, @owned Grandchild) -> @owned Grandchild

So, Child and Grandchild have abstraction differences because their initializers aren't generic. When I make a super_method instruction, the constant appears to always point to the backing implementation, not the thunk, so I needed to get the overridden vtable entry from the constant and I think that's reasonable. That gives me:

super_method %10 : $Child, #Base.init!initializer.1 : <T> Base<T>.Type -> (x: T) -> Base<T> , $@convention(method) <τ_0_0> (@in τ_0_0, @owned Base<τ_0_0>) -> @owned Base<τ_0_0>


super_method %6 : $Grandchild, #Child.init!initializer.1 : Child.Type -> (x: String) -> Child , $@convention(method) (@in String, @owned Child) -> @owned Child

which look good to me.

With my changes today to fix generic substitutions of partial super methods and getting the right type from the vtable, if I disable that verifier check, devirtualization works correctly with super_method instructions.

Is this a problem with SILDeclRef or is this check simply no longer valid in the verifier? If so, I wonder what the suitable replacement check should be. Maybe something like:

if the constant is foreign:
 do the original check
 require super_method's result type == vtable entry's function type (as opposed to the backing implementation)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20151209/4715c3c9/attachment.html>

More information about the swift-dev mailing list