Skip to content

[AutoDiff] Destroy unused pullback direct results. #28207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6923,8 +6923,8 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
? *applyInfo.originalPullbackType
: pullbackType;
for (auto indRes : actualPullbackType->getIndirectFormalResults()) {
auto *alloc =
builder.createAllocStack(loc, remapType(indRes.getSILStorageInterfaceType()));
auto *alloc = builder.createAllocStack(
loc, remapType(indRes.getSILStorageInterfaceType()));
pullbackIndirectResults.push_back(alloc);
args.push_back(alloc);
}
Expand Down Expand Up @@ -6975,13 +6975,22 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
addToAdjointBuffer(bb, origArg, tmpBuf, loc);
builder.emitDestroyAddrAndFold(loc, tmpBuf);
builder.createDeallocStack(loc, tmpBuf);
}
else {
} else {
recordTemporary(tan);
addAdjointValue(bb, origArg, makeConcreteAdjointValue(tan), loc);
}
}
}
// Destroy unused pullback direct results. Needed for pullback results from
// VJPs extracted from `@differentiable` function callees, where the
// `@differentiable` function's differentiation parameter indices are a
// superset of the active `apply` parameter indices.
while (allResultsIt != allResults.end()) {
auto unusedPullbackDirectResult = *allResultsIt++;
if (unusedPullbackDirectResult->getType().isAddress())
continue;
builder.emitDestroyValueOperation(loc, unusedPullbackDirectResult);
}
// Destroy and deallocate pullback indirect results.
for (auto *alloc : llvm::reverse(pullbackIndirectResults)) {
builder.emitDestroyAddrAndFold(loc, alloc);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: not --crash %target-swift-emit-sil %s
// REQUIRES: asserts
// RUN: %target-swift-emit-sil %s

// TF-928: Ownership verification error in pullback function generated by the
// differentiation transform.
Expand Down Expand Up @@ -27,6 +26,7 @@ func TF_928(
_ x: Tracked<Float>
) {
_ = pullback(at: x) { x in lossFunction(x, Tracked<Float>()) }
_ = pullback(at: x) { x in lossFunction(Tracked<Float>(), x) }
}

// Function: 'AD__$s4main6TF_928yyAA7TrackedVySfGAE_AEtXF_AEtFA2EcfU___pullback_src_0_wrt_0'
Expand Down