Skip to content

Commit 31e114b

Browse files
committed
Further restrict MetatypeInst constant folding to not constant fold classes
1 parent b241932 commit 31e114b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,10 @@ extension Instruction {
464464
return context.canMakeStaticObjectReadOnly(objectType: gvi.type)
465465

466466
// Metatypes (and upcasts of them to existentials) can be used as static initializers for SE-0492, as long as they
467-
// are not generic or resilient
467+
// do not require an accessor to reference the metadata (i.e. are not generic, not resilient and not a class).
468468
case let mti as MetatypeInst:
469469
if !mti.type.isGenericAtAnyLevel,
470+
!mti.type.canonicalType.instanceTypeOfMetatype.isClass,
470471
let nominal = mti.type.canonicalType.instanceTypeOfMetatype.nominal,
471472
!nominal.isGenericAtAnyLevel,
472473
!nominal.isResilient(in: mti.parentFunction) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -parse-as-library -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -O
5+
// XXX: %target-swift-frontend -parse-as-library -I %t %t/Main.swift -O -emit-sil | %FileCheck %s --check-prefix SIL
6+
// RUN: %target-swift-frontend -parse-as-library -I %t %t/Main.swift -O -emit-ir | %FileCheck %s --check-prefix IR
7+
8+
// BEGIN MyModule.swift
9+
10+
public class MyClass {}
11+
public struct MyStruct {}
12+
13+
// BEGIN Main.swift
14+
15+
import MyModule
16+
17+
public let classMetatype: Any.Type = MyClass.self
18+
public let classMetatypeArray: [Any.Type] = [classMetatype]
19+
20+
public let structMetatype: Any.Type = MyStruct.self
21+
public let structMetatypeArray: [Any.Type] = [structMetatype]
22+
23+
// SIL: sil_global [let] @$s4Main13classMetatypeypXpvp : $@thick any Any.Type
24+
// SIL-EMPTY:
25+
// SIL: sil_global [let] @$s4Main18classMetatypeArraySayypXpGvp : $Array<any Any.Type>
26+
// SIL-EMPTY:
27+
// SIL: sil_global [let] @$s4Main14structMetatypeypXpvp : $@thick any Any.Type = {
28+
// SIL-NEXT: %0 = metatype $@thick MyStruct.Type
29+
// SIL-NEXT: %initval = init_existential_metatype %0, $@thick any Any.Type
30+
// SIL-NEXT: }
31+
// SIL: sil_global [let] @$s4Main19structMetatypeArraySayypXpGvp : $Array<any Any.Type>
32+
// SIL-EMPTY:
33+
34+
// IR: @"$s4Main13classMetatypeypXpvp" = {{.*}}global { ptr } zeroinitializer
35+
// IR: @"$s4Main18classMetatypeArraySayypXpGvp" = {{.*}}global %TSa zeroinitializer
36+
// IR: @"$s8MyModule0A6StructVN" = {{.*}}global %swift.type
37+
// IR: @"$s4Main14structMetatypeypXpvp" = {{.*}}constant ptr @"$s8MyModule0A6StructVN"
38+
// IR: @"$s4Main19structMetatypeArraySayypXpGvp" = {{.*}}global %TSa zeroinitializer

0 commit comments

Comments
 (0)