Skip to content

Commit 10d131d

Browse files
authored
Merge pull request #62569 from meg-gupta/checkedcastjumpthreadingbug
Fix CheckedCastBrJumpThreading when we have dominating checked_cast_br on failure path only
2 parents e2c53dd + 1c05e76 commit 10d131d

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

lib/SILOptimizer/Utils/CheckedCastBrJumpThreading.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ SILValue CheckedCastBrJumpThreading::isArgValueEquivalentToCondition(
253253
// Return false if an ownership RAUW is necessary but cannot be performed.
254254
bool CheckedCastBrJumpThreading::Edit::
255255
canRAUW(OwnershipFixupContext &rauwContext) {
256-
if (InvertSuccess || SuccessPreds.empty())
256+
if (InvertSuccess || (SuccessPreds.empty() && !hasUnknownPreds))
257257
return true;
258258

259259
auto *ccbi = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
@@ -334,7 +334,7 @@ void CheckedCastBrJumpThreading::Edit::modifyCFGForSuccessPreds(
334334

335335
auto *checkedCastBr = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
336336
auto *oldSuccessArg = checkedCastBr->getSuccessBB()->getArgument(0);
337-
if (InvertSuccess) {
337+
if (InvertSuccess || (SuccessPreds.empty() && !hasUnknownPreds)) {
338338
assert(!hasUnknownPreds && "is not handled, should have been checked");
339339
// This success path is unused, so undef its uses and delete the cast.
340340
oldSuccessArg->replaceAllUsesWithUndef();

lib/SILOptimizer/Utils/LoopUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static SILBasicBlock *createInitialPreheader(SILBasicBlock *Header) {
3333
llvm::SmallVector<SILValue, 8> Args;
3434
for (auto *HeaderArg : Header->getArguments()) {
3535
Args.push_back(Preheader->createPhiArgument(HeaderArg->getType(),
36-
OwnershipKind::Owned));
36+
HeaderArg->getOwnershipKind()));
3737
}
3838

3939
// Create the branch to the header.

test/SILOptimizer/simplify_cfg_checkcast.sil

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Klass {
2121
protocol OtherKlass : AnyObject {}
2222

2323
sil [ossa] @consume_klass : $@convention(thin) (@owned Klass) -> ()
24-
sil [ossa] @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
2524
sil [ossa] @get_klass : $@convention(thin) () -> @owned Klass
2625

2726
sil [ossa] @unknown : $@convention(thin) () -> ()
@@ -676,6 +675,69 @@ bb8:
676675
return %25 : $()
677676
}
678677

678+
// CHECK-LABEL: sil [ossa] @redundant_checked_cast_failure_path :
679+
// CHECK: checked_cast_br
680+
// CHECK-NOT: checked_cast_br
681+
// CHECK-LABEL: } // end sil function 'redundant_checked_cast_failure_path'
682+
sil [ossa] @redundant_checked_cast_failure_path : $@convention(method) (@owned Base) -> () {
683+
bb0(%0 : @owned $Base):
684+
%borrow = begin_borrow %0 : $Base
685+
checked_cast_br %borrow : $Base to Derived, bb1, bb2
686+
687+
bb1(%succ1 : @guaranteed $Derived):
688+
br bbexit
689+
690+
bb2(%5 : @guaranteed $Base):
691+
checked_cast_br %5 : $Base to Derived, bb3, bb4
692+
693+
bb3(%9 : @guaranteed $Derived):
694+
br bbexit
695+
696+
bb4(%10 : @guaranteed $Base):
697+
%m = class_method %10 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
698+
apply %m(%10) : $@convention(method) (@guaranteed Base) -> ()
699+
br bbexit
700+
701+
bbexit:
702+
end_borrow %borrow : $Base
703+
destroy_value %0 : $Base
704+
%t = tuple ()
705+
return %t : $()
706+
}
707+
708+
// CHECK-LABEL: sil [ossa] @redundant_checked_cast_failure_path_not_idom :
709+
// CHECK: checked_cast_br
710+
// CHECK-NOT: checked_cast_br
711+
// CHECK-LABEL: } // end sil function 'redundant_checked_cast_failure_path_not_idom'
712+
sil [ossa] @redundant_checked_cast_failure_path_not_idom : $@convention(method) (@owned Base) -> () {
713+
bb0(%0 : @owned $Base):
714+
%borrow = begin_borrow %0 : $Base
715+
checked_cast_br %borrow : $Base to Derived, bb1, bb2
716+
717+
bb1(%succ1 : @guaranteed $Derived):
718+
br bbexit
719+
720+
bb2(%5 : @guaranteed $Base):
721+
br bb3(%5 : $Base)
722+
723+
bb3(%6 : @guaranteed $Base):
724+
checked_cast_br %6 : $Base to Derived, bb4, bb5
725+
726+
bb4(%9 : @guaranteed $Derived):
727+
br bbexit
728+
729+
bb5(%10 : @guaranteed $Base):
730+
%m = class_method %10 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
731+
apply %m(%10) : $@convention(method) (@guaranteed Base) -> ()
732+
br bbexit
733+
734+
bbexit:
735+
end_borrow %borrow : $Base
736+
destroy_value %0 : $Base
737+
%t = tuple ()
738+
return %t : $()
739+
}
740+
679741
//!!!TODO: test replacing a guaranteed value with an ownedvalue
680742
// test borrowOverValue->borrowCopyOverScope path
681743

0 commit comments

Comments
 (0)