Skip to content

Commit

Permalink
IRGen: Use the static superclass for virtual dynamic dispatch
Browse files Browse the repository at this point in the history
Following the self instance pointer to its class metadata isn't sound
for chained super calls since it means the same metadata is pulled,
resulting infinite recursion.

NFC for actual IRGen. SILGen isn't using super_method for native
dispatch yet.

rdar://problem/22749732
  • Loading branch information
bitjammer committed Dec 3, 2015
1 parent 7dd3046 commit 58e2b0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
26 changes: 15 additions & 11 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4309,18 +4309,22 @@ llvm::Value *irgen::emitVirtualMethodValue(IRGenFunction &IGF,

// Find the metadata.
llvm::Value *metadata;
if ((isa<FuncDecl>(methodDecl) && cast<FuncDecl>(methodDecl)->isStatic()) ||
(isa<ConstructorDecl>(methodDecl) &&
method.kind == SILDeclRef::Kind::Allocator)) {
metadata = base;
} else {
metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType,
/*suppress cast*/ true);
}

if (useSuperVTable) {
auto superField = emitAddressOfSuperclassRefInClassMetadata(IGF, metadata);
metadata = IGF.Builder.CreateLoad(superField);
if (auto metaTy = dyn_cast<MetatypeType>(baseType.getSwiftRValueType()))
baseType = SILType::getPrimitiveObjectType(metaTy.getInstanceType());

auto superType = baseType.getSuperclass(/*resolver=*/nullptr);
metadata = emitClassHeapMetadataRef(IGF, superType.getSwiftRValueType(),
MetadataValueType::TypeMetadata);
} else {
if ((isa<FuncDecl>(methodDecl) && cast<FuncDecl>(methodDecl)->isStatic()) ||
(isa<ConstructorDecl>(methodDecl) &&
method.kind == SILDeclRef::Kind::Allocator)) {
metadata = base;
} else {
metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType,
/*suppress cast*/ true);
}
}

// Use the type of the method we were type-checked against, not the
Expand Down
7 changes: 3 additions & 4 deletions test/IRGen/super_class_method.sil
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ sil_vtable Grandchild {
}

// CHECK-LABEL: define void @Grandchild_foo(%swift.type*) #0
// CHECK: [[OPAQUE_GRANDCHILD:%[0-9]+]] = bitcast %swift.type* %0 to %swift.type**
// CHECK: [[SUPER_METADATA_PTR:%[0-9]+]] = getelementptr inbounds %swift.type*, %swift.type** [[OPAQUE_GRANDCHILD]], i32 1
// CHECK: [[SUPER_METADATA:%[0-9]+]] = load %swift.type*, %swift.type** [[SUPER_METADATA_PTR]]
// CHECK: [[FOO_VTABLE_SLOT:%[0-9]+]] = getelementptr inbounds void (%swift.type*)*, void (%swift.type*)**
// CHECK: [[CHILD_METADATA_PTR:%[0-9]+]] = call %swift.type* @_TMaC18super_class_method5Child()
// CHECK: [[CASTED_CHILD_METADATA_PTR:%[0-9]+]] = bitcast %swift.type* [[CHILD_METADATA_PTR]] to void (%swift.type*)**
// CHECK: [[FOO_VTABLE_SLOT:%[0-9]+]] = getelementptr inbounds void (%swift.type*)*, void (%swift.type*)** [[CASTED_CHILD_METADATA_PTR]]
// CHECK: [[FOO_FNPTR:%[0-9]+]] = load void (%swift.type*)*, void (%swift.type*)** [[FOO_VTABLE_SLOT]]
// CHECK: call void

13 changes: 5 additions & 8 deletions test/IRGen/super_instance_method.sil
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ sil_vtable Grandchild {
#Parent.foo!1: Grandchild_foo
}

// CHECK: define void @Grandchild_foo(%C21super_instance_method10Grandchild*)
// CHECK: [[OPAQUE_GRANDCHILD:%[0-9]+]] = bitcast %C21super_instance_method10Grandchild* %0 to %swift.type**
// CHECK: [[GRANDCHILD_METADATA:%\..*]] = load %swift.type*, %swift.type** [[OPAQUE_GRANDCHILD]]
// CHECK: [[OPAQUE_METADATA:%[0-9]+]] = bitcast %swift.type* [[GRANDCHILD_METADATA]] to %swift.type**
// CHECK: [[SUPER_METADATA_PTR:%[0-9]+]] = getelementptr inbounds %swift.type*, %swift.type** [[OPAQUE_METADATA]], i32 1
// CHECK: [[SUPER_METADATA:%[0-9]+]] = load %swift.type*, %swift.type** [[SUPER_METADATA_PTR]]
// CHECK: [[FOO_VTABLE_SLOT:%[0-9]+]] = getelementptr inbounds void (%C21super_instance_method6Parent*)*, void (%C21super_instance_method6Parent*)**
// CHECK: [[FOO_FNPTR:%[0-9]+]] = load void (%C21super_instance_method6Parent*)*, void (%C21super_instance_method6Parent*)** [[FOO_VTABLE_SLOT]]
// CHECK-LABEL: define void @Grandchild_foo(%C21super_instance_method10Grandchild*)
// CHECK: [[CHILD_METADATA_PTR:%[0-9]+]] = call %swift.type* @_TMaC21super_instance_method5Child()
// CHECK: [[CASTED_CHILD_METADATA_PTR:%[0-9]+]] = bitcast %swift.type* [[CHILD_METADATA_PTR]] to void (%C21super_instance_method6Parent*)**
// CHECK: [[FOO_VTABLE_SLOT:%[0-9]+]] = getelementptr inbounds void (%C21super_instance_method6Parent*)*, void (%C21super_instance_method6Parent*)** [[CASTED_CHILD_METADATA_PTR]]
// CHECK: [[FOO_FN_PTR:%[0-9]+]] = load void (%C21super_instance_method6Parent*)*, void (%C21super_instance_method6Parent*)** [[FOO_VTABLE_SLOT]]
// CHECK: call void

0 comments on commit 58e2b0d

Please sign in to comment.