@@ -323,7 +323,9 @@ static void copyParameterArgumentsForApply(
323323 // Copy the argument if it's to be owned by the newly created closure.
324324 // Objects are to be retained.
325325 if (arg->getType ().isObject ()) {
326- auto newArg = copyBuilder.emitCopyValueOperation (loc, arg);
326+ auto newArg = arg;
327+ if (newArg.getOwnershipKind () != ValueOwnershipKind::None)
328+ newArg = copyBuilder.emitCopyValueOperation (loc, arg);
327329 collectNewArg (newArg);
328330 continue ;
329331 }
@@ -498,8 +500,9 @@ emitDerivativeFunctionReference(
498500 builder.emitBeginBorrowOperation (original.getLoc (), original);
499501 SILValue derivativeFn = builder.createDifferentiableFunctionExtract (
500502 borrowedDiffFunc.getLoc (), kind, borrowedDiffFunc);
501- derivativeFn =
502- builder.emitCopyValueOperation (original.getLoc (), derivativeFn);
503+ if (derivativeFn.getOwnershipKind () != ValueOwnershipKind::None)
504+ derivativeFn =
505+ builder.emitCopyValueOperation (original.getLoc (), derivativeFn);
503506 builder.emitEndBorrowOperation (original.getLoc (), borrowedDiffFunc);
504507 return std::make_pair (derivativeFn, desiredIndices);
505508 }
@@ -1208,9 +1211,11 @@ SILValue DifferentiationTransformer::promoteToDifferentiableFunction(
12081211 for (auto *buf : llvm::reverse (newBuffersToDealloc))
12091212 builder.createDeallocStack (loc, buf);
12101213
1211- auto origFnCopy = builder.emitCopyValueOperation (loc, origFnOperand);
1214+ // If our original copy does not have none ownership, copy it.
1215+ if (origFnOperand.getOwnershipKind () != ValueOwnershipKind::None)
1216+ origFnOperand = builder.emitCopyValueOperation (loc, origFnOperand);
12121217 auto *newDiffFn = context.createDifferentiableFunction (
1213- builder, loc, parameterIndices, resultIndices, origFnCopy ,
1218+ builder, loc, parameterIndices, resultIndices, origFnOperand ,
12141219 std::make_pair (derivativeFns[0 ], derivativeFns[1 ]));
12151220 context.getDifferentiableFunctionInstWorklist ().push_back (dfi);
12161221 return newDiffFn;
@@ -1223,7 +1228,8 @@ SILValue DifferentiationTransformer::promoteToLinearFunction(
12231228 // with an undef transpose function operand. Eventually, a legitimate
12241229 // transpose function operand should be created and used.
12251230 auto origFnOperand = lfi->getOriginalFunction ();
1226- auto origFnCopy = builder.emitCopyValueOperation (loc, origFnOperand);
1231+ if (origFnOperand.getOwnershipKind () != ValueOwnershipKind::None)
1232+ origFnOperand = builder.emitCopyValueOperation (loc, origFnOperand);
12271233 auto *parameterIndices = lfi->getParameterIndices ();
12281234 auto originalType = origFnOperand->getType ().castTo <SILFunctionType>();
12291235 auto transposeFnType = originalType->getAutoDiffTransposeFunctionType (
@@ -1232,7 +1238,7 @@ SILValue DifferentiationTransformer::promoteToLinearFunction(
12321238 auto transposeType = SILType::getPrimitiveObjectType (transposeFnType);
12331239 auto transposeFn = SILUndef::get (transposeType, builder.getFunction ());
12341240 auto *newLinearFn = context.createLinearFunction (
1235- builder, loc, parameterIndices, origFnCopy , SILValue (transposeFn));
1241+ builder, loc, parameterIndices, origFnOperand , SILValue (transposeFn));
12361242 context.getLinearFunctionInstWorklist ().push_back (lfi);
12371243 return newLinearFn;
12381244}
0 commit comments