Skip to content

Commit 336b964

Browse files
author
Marc Rasi
committed
[AutoDiff] fix ownership instructions (SR-13973)
1 parent 6220644 commit 336b964

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ static void copyParameterArgumentsForApply(
324324
// Objects are to be retained.
325325
if (arg->getType().isObject()) {
326326
auto newArg = arg;
327-
if (newArg.getOwnershipKind() != OwnershipKind::None)
327+
if (!copyBuilder.hasOwnership() ||
328+
newArg.getOwnershipKind() != OwnershipKind::None)
328329
newArg = copyBuilder.emitCopyValueOperation(loc, arg);
329330
collectNewArg(newArg);
330331
continue;
@@ -500,7 +501,8 @@ emitDerivativeFunctionReference(
500501
builder.emitBeginBorrowOperation(original.getLoc(), original);
501502
SILValue derivativeFn = builder.createDifferentiableFunctionExtract(
502503
borrowedDiffFunc.getLoc(), kind, borrowedDiffFunc);
503-
if (derivativeFn.getOwnershipKind() != OwnershipKind::None)
504+
if (!builder.hasOwnership() ||
505+
derivativeFn.getOwnershipKind() != OwnershipKind::None)
504506
derivativeFn =
505507
builder.emitCopyValueOperation(original.getLoc(), derivativeFn);
506508
builder.emitEndBorrowOperation(original.getLoc(), borrowedDiffFunc);
@@ -867,7 +869,8 @@ static void emitFatalError(ADContext &context, SILFunction *f,
867869
auto loc = f->getLocation();
868870
// Destroy all owned arguments to pass ownership verification.
869871
for (auto *arg : entry->getArguments())
870-
if (arg->getOwnershipKind() == OwnershipKind::Owned)
872+
if (!builder.hasOwnership() ||
873+
arg->getOwnershipKind() == OwnershipKind::Owned)
871874
builder.emitDestroyOperation(loc, arg);
872875
// Fatal error with a nice message.
873876
auto neverResultInfo =
@@ -1213,7 +1216,8 @@ SILValue DifferentiationTransformer::promoteToDifferentiableFunction(
12131216
builder.createDeallocStack(loc, buf);
12141217

12151218
// If our original copy does not have none ownership, copy it.
1216-
if (origFnOperand.getOwnershipKind() != OwnershipKind::None)
1219+
if (!builder.hasOwnership() ||
1220+
origFnOperand.getOwnershipKind() != OwnershipKind::None)
12171221
origFnOperand = builder.emitCopyValueOperation(loc, origFnOperand);
12181222
auto *newDiffFn = context.createDifferentiableFunction(
12191223
builder, loc, parameterIndices, resultIndices, origFnOperand,
@@ -1229,7 +1233,8 @@ SILValue DifferentiationTransformer::promoteToLinearFunction(
12291233
// with an undef transpose function operand. Eventually, a legitimate
12301234
// transpose function operand should be created and used.
12311235
auto origFnOperand = lfi->getOriginalFunction();
1232-
if (origFnOperand.getOwnershipKind() != OwnershipKind::None)
1236+
if (!builder.hasOwnership() ||
1237+
origFnOperand.getOwnershipKind() != OwnershipKind::None)
12331238
origFnOperand = builder.emitCopyValueOperation(loc, origFnOperand);
12341239
auto *parameterIndices = lfi->getParameterIndices();
12351240
auto originalType = origFnOperand->getType().castTo<SILFunctionType>();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -sanitize=address -o %t/sr13973
3+
// RUN: %target-run %t/sr13973
4+
5+
// REQUIRES: executable_test
6+
// REQUIRES: asan_runtime
7+
8+
import _Differentiation
9+
10+
struct SR13973 {
11+
let x: Float = 0
12+
13+
@differentiable
14+
func errorVector(_ t: Float) -> Float {
15+
return t
16+
}
17+
}
18+
19+
func sr13973() {
20+
let s = SR13973()
21+
_ = valueWithPullback(at: 0, in: s.errorVector)
22+
}
23+
24+
sr13973()

0 commit comments

Comments
 (0)