Skip to content

Commit 075f6db

Browse files
authored
Merge pull request swiftlang#22705 from gottesmm/pr-43e258d511846b74dfb5b6e4f6a0634d0aa5da67
2 parents 26a6df1 + f6313d1 commit 075f6db

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,12 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
831831
return NewI;
832832
}
833833

834-
// This exact cast will fail.
835-
auto *NewI = Builder.createBranch(Loc, FailureBB);
834+
// This exact cast will fail. With ownership enabled, we pass a copy of the
835+
// original casts value to the failure block.
836+
TinyPtrVector<SILValue> Args;
837+
if (Builder.hasOwnership())
838+
Args.push_back(Inst->getOperand());
839+
auto *NewI = Builder.createBranch(Loc, FailureBB, Args);
836840
EraseInstAction(Inst);
837841
WillFailAction();
838842
return NewI;
@@ -859,9 +863,11 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
859863
TargetType, isSourceTypeExact);
860864

861865
SILBuilderWithScope Builder(Inst, BuilderContext);
862-
863866
if (Feasibility == DynamicCastFeasibility::WillFail) {
864-
auto *NewI = Builder.createBranch(Loc, FailureBB);
867+
TinyPtrVector<SILValue> Args;
868+
if (Builder.hasOwnership())
869+
Args.push_back(Inst->getOperand());
870+
auto *NewI = Builder.createBranch(Loc, FailureBB, Args);
865871
EraseInstAction(Inst);
866872
WillFailAction();
867873
return NewI;

test/SILOptimizer/constant_propagation_ownership.sil

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ bb0:
852852
struct Value {}
853853

854854
class AnObject {}
855+
class AnSubObject : AnObject {}
855856

856857
// CHECK-LABEL: sil [ossa] @replace_unconditional_check_cast_failure
857858
// CHECK: bb2:
@@ -1084,3 +1085,50 @@ bb0(%0 : $*Error, %1 : $*E1):
10841085
%2 = tuple ()
10851086
return %2 : $()
10861087
}
1088+
1089+
// CHECK-LABEL: sil [ossa] @always_fail_protocolmeta_to_concretemeta_checkedcastbr : $@convention(thin) (@thin P.Protocol) -> Builtin.Int1 {
1090+
// CHECK: bb0(
1091+
// CHECK-NEXT: metatype
1092+
// CHECK-NEXT: br bb2(
1093+
// CHECK: } // end sil function 'always_fail_protocolmeta_to_concretemeta_checkedcastbr'
1094+
sil [ossa] @always_fail_protocolmeta_to_concretemeta_checkedcastbr : $@convention(thin) (@thin P.Protocol) -> Builtin.Int1 {
1095+
bb0(%0 : $@thin P.Protocol):
1096+
%2 = metatype $@thick P.Protocol
1097+
checked_cast_br %2 : $@thick P.Protocol to $@thick X.Type, bb1, bb2
1098+
1099+
bb1(%4 : $@thick X.Type):
1100+
%5 = metatype $@thin X.Type
1101+
%6 = integer_literal $Builtin.Int1, -1
1102+
br bb3(%6 : $Builtin.Int1)
1103+
1104+
bb2(%8 : $@thick P.Protocol):
1105+
%9 = integer_literal $Builtin.Int1, 0
1106+
br bb3(%9 : $Builtin.Int1)
1107+
1108+
bb3(%11 : $Builtin.Int1):
1109+
return %11 : $Builtin.Int1
1110+
}
1111+
1112+
// CHECK-LABEL: sil [ossa] @always_succeed_subtoparent : $@convention(thin) (@owned AnSubObject) -> Builtin.Int1 {
1113+
// CHECK: bb0(
1114+
// CHECK-NEXT: upcast
1115+
// CHECK-NEXT: br bb1(
1116+
// CHECK: } // end sil function 'always_succeed_subtoparent'
1117+
sil [ossa] @always_succeed_subtoparent : $@convention(thin) (@owned AnSubObject) -> Builtin.Int1 {
1118+
bb0(%0 : @owned $AnSubObject):
1119+
checked_cast_br %0 : $AnSubObject to $AnObject, bb1, bb2
1120+
1121+
bb1(%4 : @owned $AnObject):
1122+
%5 = metatype $@thin X.Type
1123+
%6 = integer_literal $Builtin.Int1, -1
1124+
destroy_value %4 : $AnObject
1125+
br bb3(%6 : $Builtin.Int1)
1126+
1127+
bb2(%8 : @owned $AnSubObject):
1128+
%9 = integer_literal $Builtin.Int1, 0
1129+
destroy_value %8 : $AnSubObject
1130+
br bb3(%9 : $Builtin.Int1)
1131+
1132+
bb3(%11 : $Builtin.Int1):
1133+
return %11 : $Builtin.Int1
1134+
}

0 commit comments

Comments
 (0)