@@ -757,7 +757,8 @@ class BridgedArgument {
757757 : BridgeFun(nullptr ), BridgeCall(nullptr ), OptionalResult(nullptr ),
758758 ReleaseAfterBridge(nullptr ), ReleaseArgAfterCall(nullptr ), Idx(0 ) {}
759759
760- static BridgedArgument match (unsigned ArgIdx, SILValue Arg, ApplyInst *AI);
760+ static BridgedArgument match (unsigned ArgIdx, SILValue Arg, ApplyInst *AI,
761+ DeadEndBlocks *deBlocks);
761762
762763 operator bool () const { return BridgeFun != nullptr ; }
763764 SILValue bridgedValue () { return BridgedValue; }
@@ -826,7 +827,7 @@ static SILInstruction *findReleaseOf(SILValue releasedValue,
826827}
827828
828829BridgedArgument BridgedArgument::match (unsigned ArgIdx, SILValue Arg,
829- ApplyInst *AI) {
830+ ApplyInst *AI, DeadEndBlocks *deBlocks ) {
830831 // Match
831832 // %15 = function_ref @$SSS10FoundationE19_bridgeToObjectiveCSo8NSStringCyF
832833 // %16 = apply %15(%14) : $@convention(method) (@guaranteed String) -> @owned NSString
@@ -879,6 +880,7 @@ BridgedArgument BridgedArgument::match(unsigned ArgIdx, SILValue Arg,
879880
880881 if (SILBasicBlock::iterator (BridgeCall) == BridgeCall->getParent ()->begin ())
881882 return BridgedArgument ();
883+
882884 auto *FunRef =
883885 dyn_cast<FunctionRefInst>(std::prev (SILBasicBlock::iterator (BridgeCall)));
884886 if (!FunRef || !FunRef->hasOneUse () || BridgeCall->getCallee () != FunRef)
@@ -912,6 +914,12 @@ BridgedArgument BridgedArgument::match(unsigned ArgIdx, SILValue Arg,
912914 BridgeFun->getName () != bridgeWitness.mangle ())
913915 return BridgedArgument ();
914916
917+ if (hasOwnership && !BridgedValueRelease) {
918+ SmallVector<Operand *> newUses{&AI->getOperandRef (ArgIdx)};
919+ if (!areUsesWithinValueLifetime (BridgedValue, newUses, deBlocks)) {
920+ return BridgedArgument ();
921+ }
922+ }
915923 return BridgedArgument (ArgIdx, FunRef, BridgeCall, Enum, BridgedValueRelease,
916924 ReleaseAfter);
917925}
@@ -1215,7 +1223,8 @@ bool ObjCMethodCall::matchInstSequence(SILBasicBlock::iterator I) {
12151223 if (Ty.isAnyObject ())
12161224 continue ;
12171225
1218- auto BridgedArg = BridgedArgument::match (CurIdx, Param.get (), BridgedCall);
1226+ auto BridgedArg =
1227+ BridgedArgument::match (CurIdx, Param.get (), BridgedCall, deBlocks);
12191228 if (!BridgedArg)
12201229 continue ;
12211230
0 commit comments