Skip to content

Commit a8675de

Browse files
authored
Merge pull request #70683 from eeckstein/fix-simplify-builtin-5.10
[5.10] SimplifyBuiltin: fix simplification of is_same_metatype with dynamic_self types
2 parents 5342ef1 + 168edfc commit a8675de

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

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

187-
let lhsTy = lhsExistential.metatype.type.instanceTypeOfMetatype(in: function)
188-
let rhsTy = rhsExistential.metatype.type.instanceTypeOfMetatype(in: function)
187+
let lhsMetatype = lhsExistential.metatype.type
188+
let rhsMetatype = rhsExistential.metatype.type
189+
if lhsMetatype.isDynamicSelfMetatype != rhsMetatype.isDynamicSelfMetatype {
190+
return nil
191+
}
192+
let lhsTy = lhsMetatype.instanceTypeOfMetatype(in: function)
193+
let rhsTy = rhsMetatype.instanceTypeOfMetatype(in: function)
189194

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

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
9090
bridged.getInstanceTypeOfMetatype(function.bridged.getFunction()).type
9191
}
9292

93+
public var isDynamicSelfMetatype: Bool {
94+
bridged.isDynamicSelfMetatype()
95+
}
96+
9397
public var isCalleeConsumedFunction: Bool { bridged.isCalleeConsumedFunction() }
9498

9599
public var isMarkedAsImmortal: Bool { bridged.isMarkedAsImmortal() }

include/swift/SIL/SILType.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ class SILType {
508508
return isObject() && isClassOrClassMetatype(getASTType());
509509
}
510510

511+
bool isDynamicSelfMetatype() const {
512+
auto metaType = castTo<swift::MetatypeType>();
513+
Type instTy = metaType->getInstanceType();
514+
return instTy->is<DynamicSelfType>();
515+
}
516+
511517
bool isFunctionTypeWithContext() const {
512518
if (auto *fTy = getASTType()->getAs<SILFunctionType>()) {
513519
return fTy->getExtInfo().hasContext();

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)