Skip to content

Commit 8935350

Browse files
committed
SimplifyBuiltin: support metatype and function types when optimizing Builtin.sizeof/alignof/strideof/destroyArray
Get the maximally abstracted lowered type instead of bailing if the ast type isn't equal to the lowered SIL type.
1 parent 4fdf16d commit 8935350

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ extension BuiltinInst : OnoneSimplifyable {
3333
.Alignof:
3434
optimizeTargetTypeConst(context)
3535
case .DestroyArray:
36-
if let elementType = substitutionMap.replacementTypes[0].canonical.silType,
37-
elementType.isTrivial(in: parentFunction)
38-
{
36+
let elementType = substitutionMap.replacementType.loweredType(in: parentFunction, maximallyAbstracted: true)
37+
if elementType.isTrivial(in: parentFunction) {
3938
context.erase(instruction: self)
4039
return
4140
}
@@ -161,10 +160,7 @@ private extension BuiltinInst {
161160
}
162161

163162
func optimizeTargetTypeConst(_ context: SimplifyContext) {
164-
guard let ty = substitutionMap.replacementTypes[0].canonical.silType else {
165-
return
166-
}
167-
163+
let ty = substitutionMap.replacementType.loweredType(in: parentFunction, maximallyAbstracted: true)
168164
let value: Int?
169165
switch id {
170166
case .Sizeof:

test/SILOptimizer/simplify_builtin.sil

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,54 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
615615
%3 = builtin "destroyArray"<Int>(%2 : $@thin Int.Type, %0 : $Builtin.RawPointer, %1 : $Builtin.Word) : $()
616616
return %0 : $Builtin.RawPointer
617617
}
618+
619+
// CHECK-LABEL: sil @remove_trivial_destroy_metatype_array :
620+
// CHECK-NOT: builtin
621+
// CHECK: } // end sil function 'remove_trivial_destroy_metatype_array'
622+
sil @remove_trivial_destroy_metatype_array : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Builtin.RawPointer {
623+
bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
624+
%2 = metatype $@thin Int.Type.Type
625+
%3 = builtin "destroyArray"<Int.Type>(%2 : $@thin Int.Type.Type, %0 : $Builtin.RawPointer, %1 : $Builtin.Word) : $()
626+
return %0 : $Builtin.RawPointer
627+
}
628+
629+
// CHECK-LABEL: sil @sizeof_concrete_type :
630+
// CHECK: %0 = integer_literal $Builtin.Word, 4
631+
// CHECK: return %0
632+
// CHECK: } // end sil function 'sizeof_concrete_type'
633+
sil @sizeof_concrete_type : $@convention(thin) () -> Builtin.Word {
634+
bb0:
635+
%0 = builtin "sizeof"<Int32>() : $Builtin.Word
636+
return %0
637+
}
638+
639+
// CHECK-LABEL: sil @sizeof_generic_type :
640+
// CHECK: %1 = builtin
641+
// CHECK: return %1
642+
// CHECK: } // end sil function 'sizeof_generic_type'
643+
sil @sizeof_generic_type : $@convention(thin) <T> (@thick T.Type) -> Builtin.Word {
644+
bb0(%0 : $@thick T.Type):
645+
%1 = builtin "sizeof"<T>() : $Builtin.Word
646+
return %1
647+
}
648+
649+
// CHECK-LABEL: sil @sizeof_metatype :
650+
// CHECK: %0 = integer_literal $Builtin.Word, {{(4|8)}}
651+
// CHECK: return %0
652+
// CHECK: } // end sil function 'sizeof_metatype'
653+
sil @sizeof_metatype : $@convention(thin) () -> Builtin.Word {
654+
bb0:
655+
%0 = builtin "sizeof"<Bool.Type>() : $Builtin.Word
656+
return %0
657+
}
658+
659+
// CHECK-LABEL: sil @sizeof_function_type :
660+
// CHECK: %0 = integer_literal $Builtin.Word, {{(8|16)}}
661+
// CHECK: return %0
662+
// CHECK: } // end sil function 'sizeof_function_type'
663+
sil @sizeof_function_type : $@convention(thin) () -> Builtin.Word {
664+
bb0:
665+
%0 = builtin "sizeof"<()->()>() : $Builtin.Word
666+
return %0
667+
}
668+

0 commit comments

Comments
 (0)