Skip to content

Commit e2de477

Browse files
committed
[embedded] avoid metadata accessors, fix vtable map, add a diagnostic on non-final generic functions
1 parent d2bb064 commit e2de477

File tree

7 files changed

+60
-42
lines changed

7 files changed

+60
-42
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ ERROR(bad_attr_on_non_const_global,none,
358358
"global variable must be a compile-time constant to use %0 attribute", (StringRef))
359359
ERROR(global_must_be_compile_time_const,none,
360360
"global variable must be a compile-time constant", ())
361+
ERROR(non_final_generic_class_function,none,
362+
"classes cannot have non-final generic fuctions in embedded Swift", ())
361363
NOTE(performance_called_from,none,
362364
"called from here", ())
363365

lib/IRGen/GenMeta.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5006,7 +5006,6 @@ void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
50065006
fragileLayout);
50075007
metadataBuilder.layout();
50085008
bool canBeConstant = metadataBuilder.canBeConstant();
5009-
metadataBuilder.createMetadataAccessFunction();
50105009

50115010
CanType declaredType = classDecl->getDeclaredType()->getCanonicalType();
50125011

lib/SIL/IR/SILVTable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ SILVTable *SILVTable::create(SILModule &M, ClassDecl *Class, SILType classType,
4242
auto buf = M.allocate(size, alignof(SILVTable));
4343
SILVTable *vt = ::new (buf) SILVTable(Class, classType, Serialized, Entries);
4444
M.vtables.push_back(vt);
45-
M.VTableMap[Class] = vt;
4645
if (vt->isSpecialized())
4746
M.SpecializedVTableMap[classType] = vt;
47+
else
48+
M.VTableMap[Class] = vt;
4849
// Update the Module's cache with new vtable + vtable entries:
4950
for (const Entry &entry : Entries) {
5051
M.VTableEntryCache.insert({{vt, entry.getMethod()}, entry});

lib/SILOptimizer/Transforms/VTableSpecializer.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define DEBUG_TYPE "sil-vtable-specializer"
1818

1919
#include "llvm/ADT/SmallVector.h"
20+
#include "swift/AST/DiagnosticsSIL.h"
2021
#include "swift/AST/ProtocolConformance.h"
2122
#include "swift/SIL/OptimizationRemark.h"
2223
#include "swift/SIL/SILFunction.h"
@@ -81,26 +82,10 @@ bool VTableSpecializer::specializeVTables(SILModule &module) {
8182
for (SILVTableEntry &entry : vtable->getMutableEntries()) {
8283
SILFunction *method = entry.getImplementation();
8384
if (!method->getLoweredFunctionType()->isPolymorphic()) continue;
84-
85-
if (entry.getKind() != SILVTableEntry::Kind::Inherited) {
86-
vtable->dump();
87-
entry.getMethod().dump();
88-
}
89-
assert(entry.getKind() == SILVTableEntry::Kind::Inherited);
90-
Decl *classOfMethod =
91-
entry.getMethod().getDecl()->getDeclContext()->getAsDecl();
92-
SILType classTy = vtable->getClassType();
93-
while (classTy.getClassOrBoundGenericClass() != classOfMethod) {
94-
classTy = classTy.getSuperclass();
95-
}
96-
auto *classDecl = cast<ClassDecl>(classOfMethod);
97-
SubstitutionMap subs = classTy.getASTType()->getContextSubstitutionMap(
98-
classDecl->getParentModule(), classDecl);
99-
100-
SILFunction *specializedMethod =
101-
specializeVTableMethod(method, subs, module);
102-
entry.setImplementation(specializedMethod);
103-
vtable->updateVTableCache(entry);
85+
86+
ValueDecl *decl = entry.getMethod().getDecl();
87+
module.getASTContext().Diags.diagnose(
88+
decl->getLoc(), diag::non_final_generic_class_function);
10489
}
10590
}
10691

@@ -153,9 +138,6 @@ SILFunction *VTableSpecializer::specializeVTableMethod(SILFunction *origMethod,
153138

154139
if (!origMethod->getLoweredFunctionType()->isPolymorphic()) return origMethod;
155140

156-
LLVM_DEBUG(llvm::dbgs() << "specializeVTableMethod " << origMethod->getName()
157-
<< '\n');
158-
159141
ReabstractionInfo ReInfo(module.getSwiftModule(), module.isWholeModule(),
160142
ApplySite(), origMethod, subs, IsNotSerialized,
161143
/*ConvertIndirectToDirect=*/true,
Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s
1+
// RUN: %target-swift-emit-sil %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s --check-prefix CHECK-SIL
2+
// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s --check-prefix CHECK-IR
23

34
precedencegroup AssignmentPrecedence { assignment: true }
45

@@ -18,17 +19,46 @@ public func bar(t: T2) -> MyClass<T2> {
1819
return MyClass<T2>(t: t)
1920
}
2021

21-
// CHECK: @"$s4main7MyClassCyAA2T2VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfDAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvgAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvsAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvMAA2T1V_Tg5", ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5" }>
22-
// CHECK: @"$s4main7MyClassCyAA2T1VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfDAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvgAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvsAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvMAA2T1V_Tg5", ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5" }>
23-
24-
// CHECK: define {{.*}}void @"$s4main7MyClassC1txvgAA2T1V_Tg5"(ptr swiftself %0)
25-
// CHECK: define {{.*}}void @"$s4main7MyClassC1txvsAA2T1V_Tg5"(ptr swiftself %0)
26-
// CHECK: define {{.*}}ptr @"$s4main2T1VWOd"(ptr %0, ptr %1)
27-
// CHECK: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tgm5"()
28-
// CHECK: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T2V_Tgm5"(i1 %0)
29-
// CHECK: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5"(ptr swiftself %0)
30-
// CHECK: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfcAA2T1V_Tg5"(ptr swiftself %0)
31-
// CHECK: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfcAA2T2V_Tg5"(i1 %0, ptr swiftself %1)
32-
// CHECK: define {{.*}}void @"$s4main7MyClassCfDAA2T1V_Tg5"(ptr swiftself %0)
33-
// CHECK: define {{.*}}ptr @"$s4main3foo1tAA7MyClassCyAA2T1VGAG_tF"()
34-
// CHECK: define {{.*}}ptr @"$s4main3bar1tAA7MyClassCyAA2T2VGAG_tF"(i1 %0)
22+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvgAA2T1V_Tg5 {{.*}}{
23+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvsAA2T1V_Tg5 {{.*}}{
24+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvMAA2T1V_Tg5 {{.*}}{
25+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tgm5 {{.*}}{
26+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassCfDAA2T1V_Tg5 {{.*}}{
27+
28+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvgAA2T2V_Tg5 {{.*}}{
29+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvsAA2T2V_Tg5 {{.*}}{
30+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1txvMAA2T2V_Tg5 {{.*}}{
31+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassC1tACyxGx_tcfCAA2T2V_Tgm5 {{.*}}{
32+
// CHECK-SIL-DAG: sil {{.*}}@$s4main7MyClassCfDAA2T2V_Tg5 {{.*}}{
33+
34+
// CHECK-SIL: sil_vtable MyClass {
35+
// CHECK-SIL: #MyClass.t!getter: <T> (MyClass<T>) -> () -> T : @$s4main7MyClassC1txvgAA2T1V_Tg5 // specialized MyClass.t.getter
36+
// CHECK-SIL: #MyClass.t!setter: <T> (MyClass<T>) -> (T) -> () : @$s4main7MyClassC1txvsAA2T1V_Tg5 // specialized MyClass.t.setter
37+
// CHECK-SIL: #MyClass.t!modify: <T> (MyClass<T>) -> () -> () : @$s4main7MyClassC1txvMAA2T1V_Tg5 // specialized MyClass.t.modify
38+
// CHECK-SIL: #MyClass.init!allocator: <T> (MyClass<T>.Type) -> (T) -> MyClass<T> : @$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5 // specialized MyClass.__allocating_init(t:)
39+
// CHECK-SIL: #MyClass.deinit!deallocator: @$s4main7MyClassCfDAA2T1V_Tg5 // specialized MyClass.__deallocating_deinit
40+
// CHECK-SIL: }
41+
42+
// CHECK-SIL: sil_vtable MyClass {
43+
// CHECK-SIL: #MyClass.t!getter: <T> (MyClass<T>) -> () -> T : @$s4main7MyClassC1txvgAA2T2V_Tg5 // specialized MyClass.t.getter
44+
// CHECK-SIL: #MyClass.t!setter: <T> (MyClass<T>) -> (T) -> () : @$s4main7MyClassC1txvsAA2T2V_Tg5 // specialized MyClass.t.setter
45+
// CHECK-SIL: #MyClass.t!modify: <T> (MyClass<T>) -> () -> () : @$s4main7MyClassC1txvMAA2T2V_Tg5 // specialized MyClass.t.modify
46+
// CHECK-SIL: #MyClass.init!allocator: <T> (MyClass<T>.Type) -> (T) -> MyClass<T> : @$s4main7MyClassC1tACyxGx_tcfCAA2T2V_Tg5 // specialized MyClass.__allocating_init(t:)
47+
// CHECK-SIL: #MyClass.deinit!deallocator: @$s4main7MyClassCfDAA2T2V_Tg5 // specialized MyClass.__deallocating_deinit
48+
// CHECK-SIL: }
49+
50+
51+
// CHECK-IR: @"$s4main7MyClassCyAA2T2VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfDAA2T2V_Tg5", ptr @"$s4main7MyClassC1txvgAA2T2V_Tg5", ptr @"$s4main7MyClassC1txvsAA2T2V_Tg5", ptr @"$s4main7MyClassC1txvMAA2T2V_Tg5", ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T2V_Tg5" }>
52+
// CHECK-IR: @"$s4main7MyClassCyAA2T1VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfDAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvgAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvsAA2T1V_Tg5", ptr @"$s4main7MyClassC1txvMAA2T1V_Tg5", ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5" }>
53+
54+
// CHECK-IR: define {{.*}}void @"$s4main7MyClassC1txvgAA2T1V_Tg5"(ptr swiftself %0)
55+
// CHECK-IR: define {{.*}}void @"$s4main7MyClassC1txvsAA2T1V_Tg5"(ptr swiftself %0)
56+
// CHECK-IR: define {{.*}}ptr @"$s4main2T1VWOd"(ptr %0, ptr %1)
57+
// CHECK-IR: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tgm5"()
58+
// CHECK-IR: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T2V_Tgm5"(i1 %0)
59+
// CHECK-IR: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5"(ptr swiftself %0)
60+
// CHECK-IR: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfcAA2T1V_Tg5"(ptr swiftself %0)
61+
// CHECK-IR: define {{.*}}ptr @"$s4main7MyClassC1tACyxGx_tcfcAA2T2V_Tg5"(i1 %0, ptr swiftself %1)
62+
// CHECK-IR: define {{.*}}void @"$s4main7MyClassCfDAA2T1V_Tg5"(ptr swiftself %0)
63+
// CHECK-IR: define {{.*}}ptr @"$s4main3foo1tAA7MyClassCyAA2T1VGAG_tF"()
64+
// CHECK-IR: define {{.*}}ptr @"$s4main3bar1tAA7MyClassCyAA2T2VGAG_tF"(i1 %0)

test/embedded/classes-no-stdlib.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class MyClass {}
77
// CHECK-DAG: define {{.*}}void @"$s4main7MyClassCfD"
88
// CHECK-DAG: define {{.*}}ptr @"$s4main7MyClassCACycfC"
99
// CHECK-DAG: define {{.*}}ptr @"$s4main7MyClassCACycfc"
10-
// CHECK-DAG: define {{.*}}%swift.metadata_response @"$s4main7MyClassCMa"
1110

1211
public func foo() -> MyClass {
1312
return MyClass()
@@ -21,7 +20,6 @@ public class MySubClass: MyClass {}
2120
// CHECK-DAG: define {{.*}}ptr @"$s4main10MySubClassCACycfc"
2221
// CHECK-DAG: define {{.*}}ptr @"$s4main10MySubClassCfd"
2322
// CHECK-DAG: define {{.*}}void @"$s4main10MySubClassCfD"
24-
// CHECK-DAG: define {{.*}}%swift.metadata_response @"$s4main10MySubClassCMa"
2523

2624
public func bar() -> MyClass {
2725
return MySubClass()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-emit-ir -verify %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none
2+
3+
public class MyClass {
4+
func foo<T>(t: T) { } // expected-error {{classes cannot have non-final generic fuctions in embedded Swift}}
5+
func bar() { }
6+
}

0 commit comments

Comments
 (0)