Skip to content

Add Builtin.is_same_metatype to SILCombine. #30892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerBuiltinVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ static SILInstruction *optimizeBuiltinWithSameOperands(SILBuilder &Builder,
};
return B.createTuple(I->getLoc(), Ty, Elements);
}


// Replace the type check with 'true'.
case BuiltinValueKind::IsSameMetatype:
return Builder.createIntegerLiteral(I->getLoc(), I->getType(), true);

default:
break;
}
Expand Down
19 changes: 19 additions & 0 deletions test/SILOptimizer/existential_metatype.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend %s -O -emit-sil -parse-as-library | %FileCheck %s
protocol SomeP {}

public enum SpecialEnum : SomeP {}

// CHECK-LABEL: sil shared [noinline] @$s20existential_metatype17checkProtocolType0aE0Sbxm_tAA5SomePRzlFAA11SpecialEnumO_Tg5Tf4d_n : $@convention(thin) () -> Bool {
// CHECK-NEXT: bb0:
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool (%0 : $Builtin.Int1)
// CHECK-NEXT: return %1 : $Bool
// CHECK-LABEL: } // end sil function '$s20existential_metatype17checkProtocolType0aE0Sbxm_tAA5SomePRzlFAA11SpecialEnumO_Tg5Tf4d_n'
@inline(never)
func checkProtocolType<P : SomeP>(existentialType: P.Type) -> Bool {
return existentialType == SpecialEnum.self
}

public func testProtocolType() -> Bool {
return checkProtocolType(existentialType: SpecialEnum.self)
}
19 changes: 19 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3687,3 +3687,22 @@ bb0(%0 : $@callee_guaranteed (@in Int, @in Int) -> @out (), %1 : $*Int):
return %5 : $@callee_guaranteed () -> @out ()

}

// Test builtin "is_same_metatype" folding.
protocol SomeP {}

public enum SpecialEnum : SomeP {}

// CHECK-LABEL: sil @testFoldBuiltinIsSameMetatype : $@convention(thin) (@thick SpecialEnum.Type) -> Bool {
// CHECK-NEXT: bb0(%0 : $@thick SpecialEnum.Type):
// CHECK-NEXT: [[TRUE:%.*]] = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: struct $Bool ([[TRUE]] : $Builtin.Int1)
// CHECK-NEXT: return
// CHECK-LABEL: } // end sil function 'testFoldBuiltinIsSameMetatype'
sil @testFoldBuiltinIsSameMetatype : $@convention(thin) (@thick SpecialEnum.Type) -> Bool {
bb0(%0 : $@thick SpecialEnum.Type):
%1 = init_existential_metatype %0 : $@thick SpecialEnum.Type, $@thick Any.Type
%3 = builtin "is_same_metatype"(%1 : $@thick Any.Type, %1 : $@thick Any.Type) : $Builtin.Int1
%4 = struct $Bool (%3 : $Builtin.Int1)
return %4 : $Bool
}