Skip to content

Commit 75f6a28

Browse files
committed
Don't constant fold metatypes that indirectly contain resilient types
1 parent 9cd56b1 commit 75f6a28

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,15 @@ extension Instruction {
465465

466466
// Metatypes (and upcasts of them to existentials) can be used as static initializers for SE-0492, as long as they
467467
// do not require an accessor to reference the metadata (i.e. are not generic, not resilient and not a class).
468+
// See also irgen::isCanonicalCompleteTypeMetadataStaticallyAddressable.
468469
case let mti as MetatypeInst:
470+
let silType = mti.type.loweredInstanceTypeOfMetatype(in: parentFunction)
471+
let instanceType = mti.type.canonicalType.instanceTypeOfMetatype
469472
if !mti.type.isGenericAtAnyLevel,
470-
!mti.type.canonicalType.instanceTypeOfMetatype.isClass,
471-
let nominal = mti.type.canonicalType.instanceTypeOfMetatype.nominal,
473+
!instanceType.isClass,
474+
let nominal = instanceType.nominal,
472475
!nominal.isGenericAtAnyLevel,
473-
!nominal.isResilient(in: mti.parentFunction) {
476+
silType.isFixedABI(in: mti.parentFunction) {
474477
return true
475478
}
476479
return false

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
103103
bridged.isAddressableForDeps(function.bridged)
104104
}
105105

106+
public func isFixedABI(in function: Function) -> Bool {
107+
bridged.isFixedABI(function.bridged)
108+
}
109+
106110
/// If this is a raw layout type, returns the substituted like-type.
107111
public var rawLayoutSubstitutedLikeType: AST.`Type`? {
108112
.init(bridgedOrNil: bridged.getRawLayoutSubstitutedLikeType())

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ struct BridgedType {
290290
BRIDGED_INLINE bool isExactSuperclassOf(BridgedType t) const;
291291
BRIDGED_INLINE bool isMarkedAsImmortal() const;
292292
BRIDGED_INLINE bool isAddressableForDeps(BridgedFunction f) const;
293+
BRIDGED_INLINE bool isFixedABI(BridgedFunction f) const;
293294
BRIDGED_INLINE SWIFT_IMPORT_UNSAFE BridgedASTType getRawLayoutSubstitutedLikeType() const;
294295
BRIDGED_INLINE SWIFT_IMPORT_UNSAFE BridgedASTType getRawLayoutSubstitutedCountType() const;
295296
BRIDGED_INLINE bool mayHaveCustomDeinit(BridgedFunction f) const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ bool BridgedType::isAddressableForDeps(BridgedFunction f) const {
398398
return unbridged().isAddressableForDeps(*f.getFunction());
399399
}
400400

401+
bool BridgedType::isFixedABI(BridgedFunction f) const {
402+
return unbridged().isFixedABI(*f.getFunction());
403+
}
404+
401405
BridgedASTType BridgedType::getRawLayoutSubstitutedLikeType() const {
402406
return {unbridged().getRawLayoutSubstitutedLikeType().getPointer()};
403407
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// RUN: %target-swift-frontend -parse-as-library -I %t %t/Main.swift -O -emit-ir | %FileCheck %s
6+
7+
// BEGIN MyModule.swift
8+
9+
public struct NonResilientTypeContainingAResilientType {
10+
var m: Mirror? = nil
11+
}
12+
13+
// BEGIN Main.swift
14+
15+
import MyModule
16+
17+
public protocol MyProtocol {}
18+
19+
public struct MyStruct: MyProtocol {
20+
public var x: NonResilientTypeContainingAResilientType? = nil
21+
}
22+
23+
public let glob: [any MyProtocol.Type] = [MyStruct.self]
24+
25+
// CHECK: @"$s4Main4globSayAA10MyProtocol_pXpGvp" = {{.*}}global %TSa zeroinitializer

0 commit comments

Comments
 (0)