Skip to content

Commit ba75b3b

Browse files
committed
Add Builtin.is_same_metatype to SILCombine.
To optimize String decoding.
1 parent b5570a1 commit ba75b3b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerBuiltinVisitors.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ static SILInstruction *optimizeBuiltinWithSameOperands(SILBuilder &Builder,
252252
};
253253
return B.createTuple(I->getLoc(), Ty, Elements);
254254
}
255-
255+
256+
// Replace the type check with 'true'.
257+
case BuiltinValueKind::IsSameMetatype:
258+
return Builder.createIntegerLiteral(I->getLoc(), I->getType(), true);
259+
256260
default:
257261
break;
258262
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-frontend %s -O -emit-sil -parse-as-library | %FileCheck %s
2+
protocol SomeP {}
3+
4+
public enum SpecialEnum : SomeP {}
5+
6+
// CHECK-LABEL: sil shared [noinline] @$s20existential_metatype17checkProtocolType0aE0Sbxm_tAA5SomePRzlFAA11SpecialEnumO_Tg5Tf4d_n : $@convention(thin) () -> Bool {
7+
// CHECK-NEXT: bb0:
8+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
9+
// CHECK-NEXT: %1 = struct $Bool (%0 : $Builtin.Int1)
10+
// CHECK-NEXT: return %1 : $Bool
11+
// CHECK-LABEL: } // end sil function '$s20existential_metatype17checkProtocolType0aE0Sbxm_tAA5SomePRzlFAA11SpecialEnumO_Tg5Tf4d_n'
12+
@inline(never)
13+
func checkProtocolType<P : SomeP>(existentialType: P.Type) -> Bool {
14+
return existentialType == SpecialEnum.self
15+
}
16+
17+
public func testProtocolType() -> Bool {
18+
return checkProtocolType(existentialType: SpecialEnum.self)
19+
}

test/SILOptimizer/sil_combine.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,3 +3687,22 @@ bb0(%0 : $@callee_guaranteed (@in Int, @in Int) -> @out (), %1 : $*Int):
36873687
return %5 : $@callee_guaranteed () -> @out ()
36883688

36893689
}
3690+
3691+
// Test builtin "is_same_metatype" folding.
3692+
protocol SomeP {}
3693+
3694+
public enum SpecialEnum : SomeP {}
3695+
3696+
// CHECK-LABEL: sil @testFoldBuiltinIsSameMetatype : $@convention(thin) (@thick SpecialEnum.Type) -> Bool {
3697+
// CHECK-NEXT: bb0(%0 : $@thick SpecialEnum.Type):
3698+
// CHECK-NEXT: [[TRUE:%.*]] = integer_literal $Builtin.Int1, -1
3699+
// CHECK-NEXT: struct $Bool ([[TRUE]] : $Builtin.Int1)
3700+
// CHECK-NEXT: return
3701+
// CHECK-LABEL: } // end sil function 'testFoldBuiltinIsSameMetatype'
3702+
sil @testFoldBuiltinIsSameMetatype : $@convention(thin) (@thick SpecialEnum.Type) -> Bool {
3703+
bb0(%0 : $@thick SpecialEnum.Type):
3704+
%1 = init_existential_metatype %0 : $@thick SpecialEnum.Type, $@thick Any.Type
3705+
%3 = builtin "is_same_metatype"(%1 : $@thick Any.Type, %1 : $@thick Any.Type) : $Builtin.Int1
3706+
%4 = struct $Bool (%3 : $Builtin.Int1)
3707+
return %4 : $Bool
3708+
}

0 commit comments

Comments
 (0)