Skip to content

Commit 384ee99

Browse files
authored
Merge pull request swiftlang#87425 from eeckstein/vtable-specialization-6.3
[6.3] embedded: specialize vtables of classes which are a target of cast operations
2 parents 7c644ab + 6e8aee3 commit 384ee99

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
9595
worklist.pushIfNotVisited(f)
9696
}
9797
}
98-
98+
99+
func specializeVTable(for type: Type, instruction: Instruction) {
100+
if context.options.enableEmbeddedSwift,
101+
type.isClass
102+
{
103+
Optimizer.specializeVTable(forClassType: type, errorLocation: instruction.location, moduleContext) {
104+
worklist.pushIfNotVisited($0)
105+
}
106+
}
107+
}
108+
99109
var changed = true
100110
while changed {
101111
changed = runSimplification(on: function, context, preserveDebugInfo: true) { instruction, simplifyCtxt in
@@ -111,21 +121,15 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
111121

112122
// Embedded Swift specific transformations
113123
case let alloc as AllocRefInst:
114-
if context.options.enableEmbeddedSwift {
115-
specializeVTable(forClassType: alloc.type, errorLocation: alloc.location, moduleContext) {
116-
worklist.pushIfNotVisited($0)
117-
}
118-
}
124+
specializeVTable(for: alloc.type, instruction: alloc)
119125
case let metatype as MetatypeInst:
120-
if context.options.enableEmbeddedSwift,
121-
metatype.type.representationOfMetatype == .thick {
122-
let instanceType = metatype.type.loweredInstanceTypeOfMetatype(in: function)
123-
if instanceType.isClass {
124-
specializeVTable(forClassType: instanceType, errorLocation: metatype.location, moduleContext) {
125-
worklist.pushIfNotVisited($0)
126-
}
127-
}
126+
if metatype.type.representationOfMetatype == .thick {
127+
specializeVTable(for: metatype.type.loweredInstanceTypeOfMetatype(in: function), instruction: metatype)
128128
}
129+
case let cast as UnconditionalCheckedCastInst:
130+
specializeVTable(for: cast.type, instruction: cast)
131+
case let cast as UncheckedRefCastInst:
132+
specializeVTable(for: cast.type, instruction: cast)
129133
case let classMethod as ClassMethodInst:
130134
if context.options.enableEmbeddedSwift {
131135
_ = context.specializeClassMethodInst(classMethod)

test/embedded/generic-classes.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ func testBaseDerived3() -> Derived3<Int, Bool> {
9292
return Derived3()
9393
}
9494

95+
// Check that IRGen doesn't crash
96+
public func castToClassWhichIsNeverCreated(_ o: AnyObject) -> Outer<Bool> {
97+
return o as! Outer<Bool>
98+
}
99+
100+
95101
@main
96102
struct Main {
97103
static func main() {

0 commit comments

Comments
 (0)