Skip to content

Commit 1e17238

Browse files
authored
Merge pull request #70585 from eeckstein/fix-simplify-same-metatype
SimplifyBuiltin: fix simplification of is_same_metatype with dynamic_self types
2 parents 0d35c42 + 85be963 commit 1e17238

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,13 @@ private func typesOfValuesAreEqual(_ lhs: Value, _ rhs: Value, in function: Func
271271
return nil
272272
}
273273

274-
let lhsTy = lhsExistential.metatype.type.instanceTypeOfMetatype(in: function)
275-
let rhsTy = rhsExistential.metatype.type.instanceTypeOfMetatype(in: function)
274+
let lhsMetatype = lhsExistential.metatype.type
275+
let rhsMetatype = rhsExistential.metatype.type
276+
if lhsMetatype.isDynamicSelfMetatype != rhsMetatype.isDynamicSelfMetatype {
277+
return nil
278+
}
279+
let lhsTy = lhsMetatype.instanceTypeOfMetatype(in: function)
280+
let rhsTy = rhsMetatype.instanceTypeOfMetatype(in: function)
276281

277282
// Do we know the exact types? This is not the case e.g. if a type is passed as metatype
278283
// to the function.

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
112112
bridged.getInstanceTypeOfMetatype(function.bridged).type
113113
}
114114

115+
public var isDynamicSelfMetatype: Bool {
116+
bridged.isDynamicSelfMetatype()
117+
}
118+
115119
public func representationOfMetatype(in function: Function) -> MetatypeRepresentation {
116120
bridged.getRepresentationOfMetatype(function.bridged)
117121
}

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ struct BridgedType {
214214
BRIDGED_INLINE bool isBuiltinFixedWidthInteger(SwiftInt width) const;
215215
BRIDGED_INLINE bool isExactSuperclassOf(BridgedType t) const;
216216
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getInstanceTypeOfMetatype(BridgedFunction f) const;
217+
BRIDGED_INLINE bool isDynamicSelfMetatype() const;
217218
BRIDGED_INLINE MetatypeRepresentation getRepresentationOfMetatype(BridgedFunction f) const;
218219
BRIDGED_INLINE bool isCalleeConsumedFunction() const;
219220
BRIDGED_INLINE bool isMarkedAsImmortal() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ BridgedType BridgedType::getInstanceTypeOfMetatype(BridgedFunction f) const {
236236
return unbridged().getInstanceTypeOfMetatype(f.getFunction());
237237
}
238238

239+
bool BridgedType::isDynamicSelfMetatype() const {
240+
auto metaType = unbridged().castTo<swift::MetatypeType>();
241+
swift::Type instTy = metaType->getInstanceType();
242+
return instTy->is<swift::DynamicSelfType>();
243+
}
244+
239245
BridgedType::MetatypeRepresentation BridgedType::getRepresentationOfMetatype(BridgedFunction f) const {
240246
return BridgedType::MetatypeRepresentation(
241247
unbridged().getRepresentationOfMetatype(f.getFunction()));

test/SILOptimizer/simplify_builtin.sil

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class C1<T> {
2626
class C2<T> : C1<T> {
2727
}
2828

29+
class A {
30+
}
31+
2932
// CHECK-LABEL: sil @constantFoldAdd
3033
// CHECK: [[A:%.*]] = integer_literal $Builtin.Int64, 12
3134
// CHECK: [[C:%.*]] = integer_literal $Builtin.Int1, 0
@@ -267,6 +270,34 @@ bb0:
267270
return %4 : $Builtin.Int1
268271
}
269272

273+
// CHECK-LABEL: sil @same_metatype_dynamic_self
274+
// CHECK: [[R:%.*]] = integer_literal $Builtin.Int1, -1
275+
// CHECK-NEXT: return [[R]]
276+
// CHECK: } // end sil function 'same_metatype_dynamic_self'
277+
sil @same_metatype_dynamic_self : $@convention(method) (@guaranteed A) -> Builtin.Int1 {
278+
bb0(%0 : $A):
279+
%2 = metatype $@thick @dynamic_self A.Type
280+
%3 = init_existential_metatype %2 : $@thick @dynamic_self A.Type, $@thick any Any.Type
281+
%5 = metatype $@thick @dynamic_self A.Type
282+
%6 = init_existential_metatype %5 : $@thick @dynamic_self A.Type, $@thick any Any.Type
283+
%13 = builtin "is_same_metatype"(%3 : $@thick any Any.Type, %6 : $@thick any Any.Type) : $Builtin.Int1
284+
return %13 : $Builtin.Int1
285+
}
286+
287+
// CHECK-LABEL: sil @unknown_same_metatype_dynamic_self
288+
// CHECK: [[R:%.*]] = builtin "is_same_metatype"
289+
// CHECK: return [[R]]
290+
// CHECK: } // end sil function 'unknown_same_metatype_dynamic_self'
291+
sil @unknown_same_metatype_dynamic_self : $@convention(method) (@guaranteed A) -> Builtin.Int1 {
292+
bb0(%0 : $A):
293+
%2 = metatype $@thick @dynamic_self A.Type
294+
%3 = init_existential_metatype %2 : $@thick @dynamic_self A.Type, $@thick any Any.Type
295+
%5 = metatype $@thick A.Type
296+
%6 = init_existential_metatype %5 : $@thick A.Type, $@thick any Any.Type
297+
%13 = builtin "is_same_metatype"(%3 : $@thick any Any.Type, %6 : $@thick any Any.Type) : $Builtin.Int1
298+
return %13 : $Builtin.Int1
299+
}
300+
270301
sil_global hidden [let] @g : $Int32
271302

272303
sil [global_init_once_fn] @side_effect_free_init : $@convention(c) () -> () {

0 commit comments

Comments
 (0)