Skip to content

Commit 51f54b4

Browse files
committed
[embedded] add test for calling methods on classes, fix layout
1 parent ae63e75 commit 51f54b4

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

lib/IRGen/ClassMetadataVisitor.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ template <class Impl> class ClassMetadataVisitor
6868
static_assert(MetadataAdjustmentIndex::Class == 3,
6969
"Adjustment index must be synchronized with this layout");
7070

71+
if (IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
72+
asImpl().noteAddressPoint();
73+
asImpl().addSuperclass();
74+
asImpl().addDestructorFunction();
75+
addEmbeddedClassMembers(Target);
76+
return;
77+
}
78+
7179
// Pointer to layout string
7280
asImpl().addLayoutStringPointer();
7381

@@ -99,13 +107,6 @@ template <class Impl> class ClassMetadataVisitor
99107
addClassMembers(Target, Target);
100108
}
101109

102-
void embeddedLayout() {
103-
asImpl().noteAddressPoint();
104-
asImpl().addSuperclass();
105-
asImpl().addDestructorFunction();
106-
addEmbeddedClassMembers(Target);
107-
}
108-
109110
/// Notes the beginning of the field offset vector for a particular ancestor
110111
/// of a generic-layout class.
111112
void noteStartOfFieldOffsets(ClassDecl *whichClass) {}

lib/IRGen/GenMeta.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,6 +3949,12 @@ namespace {
39493949
}
39503950

39513951
void addDestructorFunction() {
3952+
if (IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
3953+
auto dtorRef = SILDeclRef(Target->getDestructor(), SILDeclRef::Kind::Deallocator);
3954+
addReifiedVTableEntry(dtorRef);
3955+
return;
3956+
}
3957+
39523958
if (asImpl().getFieldLayout().hasObjCImplementation())
39533959
return;
39543960

@@ -4969,8 +4975,8 @@ void irgen::emitClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
49694975

49704976
void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
49714977
const ClassLayout &fragileLayout) {
4972-
assert(!classDecl->isForeign());
49734978
PrettyStackTraceDecl stackTraceRAII("emitting metadata for", classDecl);
4979+
assert(!classDecl->isForeign());
49744980

49754981
// Set up a dummy global to stand in for the metadata object while we produce
49764982
// relative references.
@@ -4984,7 +4990,7 @@ void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
49844990

49854991
FixedClassMetadataBuilder metadataBuilder(IGM, classDecl, init,
49864992
fragileLayout);
4987-
metadataBuilder.embeddedLayout();
4993+
metadataBuilder.layout();
49884994
bool canBeConstant = metadataBuilder.canBeConstant();
49894995
metadataBuilder.createMetadataAccessFunction();
49904996

@@ -4994,6 +5000,7 @@ void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
49945000
bool isPattern = false;
49955001
auto var = IGM.defineTypeMetadata(declaredType, isPattern, canBeConstant,
49965002
init.finishAndCreateFuture(), section);
5003+
(void)var;
49975004
}
49985005

49995006
void irgen::emitSpecializedGenericClassMetadata(IRGenModule &IGM, CanType type,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s
2+
3+
public class MyClass {
4+
func foo() { }
5+
func bar() { }
6+
}
7+
8+
public class MySubClass: MyClass {
9+
override func foo() { }
10+
}
11+
12+
// CHECK: @"$s4main7MyClassCN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfD", ptr @"$s4main7MyClassC3fooyyF", ptr @"$s4main7MyClassC3baryyF", ptr @"$s4main7MyClassCACycfC" }>
13+
// CHECK: @"$s4main10MySubClassCN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr }> <{ ptr @"$s4main7MyClassCN", ptr @"$s4main10MySubClassCfD", ptr @"$s4main10MySubClassC3fooyyF", ptr @"$s4main7MyClassC3baryyF", ptr @"$s4main10MySubClassCACycfC" }>
14+
15+
// CHECK: define {{.*}}void @"$s4main4test1xyAA7MyClassC_tF"(ptr %0)
16+
public func test(x: MyClass) {
17+
18+
x.foo() // goes through the vtable
19+
// CHECK: %1 = load ptr, ptr %0
20+
// CHECK: %2 = getelementptr inbounds ptr, ptr %1, i64 2
21+
// CHECK: %3 = load ptr, ptr %2
22+
// CHECK: call swiftcc void %3(ptr swiftself %0)
23+
24+
x.bar() // does not go through the vtable
25+
// CHECK: call swiftcc void @"$s4main7MyClassC3baryyF"
26+
27+
let y = MySubClass()
28+
// CHECK: call swiftcc %swift.metadata_response @"$s4main10MySubClassCMa"
29+
// CHECK: call swiftcc ptr @"$s4main10MySubClassCACycfC"
30+
31+
y.foo() // does not go through the vtable
32+
// CHECK: call swiftcc void @"$s4main10MySubClassC3fooyyF"
33+
34+
y.bar() // does not go through the vtable
35+
// CHECK: call swiftcc void @"$s4main7MyClassC3baryyF"
36+
37+
}

test/embedded/classes-no-stdlib.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s
22

3-
// TODO: investigate why windows is generating more metadata.
4-
// XFAIL: OS=windows-msvc
5-
63
public class MyClass {}
74

85
// CHECK-DAG: @"$s4main7MyClassCN" = {{.*}}<{ ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfD", ptr @"$s4main7MyClassCACycfC" }>

0 commit comments

Comments
 (0)