Skip to content

Commit da2dfa8

Browse files
committed
Fix generic detection, assert we have addressable metadata, expand tests
1 parent bc95327 commit da2dfa8

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,9 @@ extension Instruction {
466466
// Metatypes (and upcasts of them to existentials) can be used as static initializers for SE-0492, as long as they
467467
// are not generic or resilient
468468
case let mti as MetatypeInst:
469-
if !mti.type.hasTypeParameter,
469+
if !mti.type.isGenericAtAnyLevel,
470470
let nominal = mti.type.canonicalType.instanceTypeOfMetatype.nominal,
471+
!nominal.isGenericAtAnyLevel,
471472
!nominal.isResilient(in: mti.parentFunction) {
472473
return true
473474
}
@@ -476,7 +477,7 @@ extension Instruction {
476477
let isAnyConformanceResilient = iemi.conformances.contains {
477478
!$0.protocol.isInvertible && $0.isResilientConformance(currentModule: context.currentModuleContext)
478479
}
479-
return !iemi.type.hasTypeParameter && !isAnyConformanceResilient
480+
return !iemi.type.isGenericAtAnyLevel && !isAnyConformanceResilient
480481

481482
case is StructInst,
482483
is TupleInst,

lib/IRGen/GenConstant.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "GenIntegerLiteral.h"
2525
#include "GenStruct.h"
2626
#include "GenTuple.h"
27+
#include "MetadataRequest.h"
2728
#include "TypeInfo.h"
2829
#include "StructLayout.h"
2930
#include "Callee.h"
@@ -420,6 +421,7 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
420421
} else if (auto *mti = dyn_cast<MetatypeInst>(operand)) {
421422
auto metaTy = mti->getType().castTo<MetatypeType>();
422423
auto type = metaTy.getInstanceType();
424+
ASSERT(isCanonicalCompleteTypeMetadataStaticallyAddressable(IGM, type));
423425
return IGM.getAddrOfTypeMetadata(type);
424426
} else if (auto *iemi = dyn_cast<InitExistentialMetatypeInst>(operand)) {
425427
auto *mti =
@@ -428,10 +430,11 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
428430

429431
auto metaTy = mti->getType().castTo<MetatypeType>();
430432
auto type = metaTy.getInstanceType();
431-
llvm::Constant *metatype = IGM.getAddrOfTypeMetadata(type);
433+
ASSERT(isCanonicalCompleteTypeMetadataStaticallyAddressable(IGM, type));
434+
llvm::Constant *metadata = IGM.getAddrOfTypeMetadata(type);
432435

433436
Explosion result;
434-
emitExistentialMetatypeContainer(IGM, result, iemi->getType(), metatype,
437+
emitExistentialMetatypeContainer(IGM, result, iemi->getType(), metadata,
435438
iemi->getOperand()->getType(),
436439
iemi->getConformances());
437440
return result;

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
#include "swift/AST/ModuleDependencies.h"
2626
#include "swift/AST/NameLookup.h"
2727
#include "swift/AST/Pattern.h"
28+
#include "swift/AST/PrettyStackTrace.h"
2829
#include "swift/AST/ProtocolConformance.h"
2930
#include "swift/AST/TypeMemberVisitor.h"
3031
#include "swift/AST/Types.h"
3132
#include "swift/Basic/Assertions.h"
3233
#include "swift/Basic/Mangler.h"
34+
#include "swift/Basic/PrettyStackTrace.h"
3335
#include "swift/ClangImporter/ClangModule.h"
3436
#include "swift/Demangling/ManglingMacros.h"
3537
#include "swift/IRGen/Linking.h"
@@ -2931,6 +2933,9 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
29312933
llvm::Constant *IRGenModule::getGlobalInitValue(SILGlobalVariable *var,
29322934
llvm::Type *storageType,
29332935
Alignment alignment) {
2936+
PrettyStackTraceStringAction trace(
2937+
"constant-folding init value for SIL global", var->getName());
2938+
29342939
if (var->isInitializedObject()) {
29352940
StructLayout *layout = StaticObjectLayouts[var].layout.get();
29362941
ObjectInst *oi = cast<ObjectInst>(var->getStaticInitializerValue());

test/SILOptimizer/static_init_metatypes.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
// RUN: %target-swift-frontend %s -parse-as-library -module-name=test -emit-ir | %FileCheck --check-prefix=IR %s
55
// RUN: %target-swift-frontend %s -parse-as-library -O -module-name=test -emit-ir | %FileCheck --check-prefix=IR %s
66

7+
// RUN: %target-run-simple-swift(-parse-as-library)
8+
// RUN: %target-run-simple-swift(-parse-as-library -O)
9+
10+
// REQUIRES: executable_test
11+
712
public protocol MyProtocol {}
813
public protocol MyProtocol2 {}
914

@@ -67,10 +72,8 @@ public let metatype14: any (Hashable & MyProtocol).Type = S.self
6772
// SIL: sil_global [let] @$s4test10metatype14SH_AA10MyProtocolpXpvp : $@thick any (Hashable & MyProtocol).Type
6873
// SIL-EMPTY:
6974
public let metatype15: Any.Type = Array<Int>.self
70-
// SIL: sil_global [let] @$s4test10metatype15ypXpvp : $@thick any Any.Type = {
71-
// SIL-NEXT: %0 = metatype $@thick Array<Int>.Type // user: %1
72-
// SIL-NEXT: %initval = init_existential_metatype %0, $@thick any Any.Type
73-
// SIL-NEXT: }
75+
// SIL: sil_global [let] @$s4test10metatype15ypXpvp : $@thick any Any.Type
76+
// SIL-EMPTY:
7477
public let metatype16: any (Hashable & Sendable).Type = Array<Int>.self
7578
// SIL: sil_global [let] @$s4test10metatype16SH_s8SendablepXpvp : $@thick any (Hashable & Sendable).Type
7679
// SIL-EMPTY:
@@ -80,11 +83,26 @@ public let metatype17: Any.Type = (Bool.random() ? Array<Int>.self : Array<Bool>
8083
public let metatype18: Any.Type = Mirror.self // resilient
8184
// SIL: sil_global [let] @$s4test10metatype18ypXpvp : $@thick any Any.Type
8285
// SIL-EMPTY:
86+
public var metatype19: Any.Type = (any MyProtocol).self
87+
// SIL: sil_global @$s4test10metatype19ypXpvp : $@thick any Any.Type
88+
// SIL-EMPTY:
89+
public var metatype20: [Any.Type] = [(any MyProtocol).self]
90+
// SIL: sil_global @$s4test10metatype20SayypXpGvp : $Array<any Any.Type>
91+
// SIL-EMPTY:
92+
public var metatype21: [Any.Type] = [Int.self]
93+
// SIL: sil_global @$s4test10metatype21SayypXpGvp : $Array<any Any.Type>
94+
// SIL-EMPTY:
95+
public var metatype22: [Any.Type] = [S.self]
96+
// SIL: sil_global @$s4test10metatype22SayypXpGvp : $Array<any Any.Type>
97+
// SIL-EMPTY:
98+
99+
// Only to detect link-time missing symbols
100+
@main
101+
struct Main { static func main() { } }
83102

84103
// IR: @"$s4test9metatype3ypXpvp" = {{.*}}constant ptr @"$sSiN"
85104
// IR: @"$s4test9metatype4ypXpvp" = {{.*}}constant ptr getelementptr inbounds{{.*}} ({{.*}}, ptr @"$s4test1SVMf", {{.*}})
86105
// IR: @"$s4test9metatype5AA10MyProtocol_pXpvp" = {{.*}}constant <{ ptr, ptr }> <{ ptr @"$sSiN", ptr @"$sSi4test10MyProtocolAAWP" }>
87106
// IR: @"$s4test9metatype6AA10MyProtocol_pXpvp" = {{.*}}constant <{ ptr, ptr }> <{ ptr getelementptr inbounds{{.*}} ({{.*}}, ptr @"$s4test1SVMf", {{.*}}), ptr @"$s4test1SVAA10MyProtocolAAWP" }>
88107
// IR: @"$s4test9metatype7AA10MyProtocol_AA0C9Protocol2pXpvp" = {{.*}}constant <{ ptr, ptr, ptr }> <{ ptr @"$sSiN", ptr @"$sSi4test10MyProtocolAAWP", ptr @"$sSi4test11MyProtocol2AAWP" }>
89108
// IR: @"$s4test9metatype8AA10MyProtocol_AA0C9Protocol2pXpvp" = {{.*}}constant <{ ptr, ptr, ptr }> <{ ptr getelementptr inbounds{{.*}} ({{.*}}, ptr @"$s4test1SVMf", {{.*}}), ptr @"$s4test1SVAA10MyProtocolAAWP", ptr @"$s4test1SVAA11MyProtocol2AAWP" }>
90-
// IR: @"$s4test10metatype15ypXpvp" = {{.*}}constant ptr getelementptr inbounds{{.*}} ({{.*}}, ptr @"$sSaySiGMf", {{.*}})

0 commit comments

Comments
 (0)