Skip to content
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
3 changes: 2 additions & 1 deletion lib/SIL/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ CONSTANT_OWNERSHIP_INST(Unowned, ValueToBridgeObject)
}
CONSTANT_OR_TRIVIAL_OWNERSHIP_INST(Guaranteed, StructExtract)
CONSTANT_OR_TRIVIAL_OWNERSHIP_INST(Guaranteed, TupleExtract)
// SWIFT_ENABLE_TENSORFLOW
CONSTANT_OR_TRIVIAL_OWNERSHIP_INST(Guaranteed, AutoDiffFunctionExtract)
// OpenExistentialValue opens the boxed value inside an existential
// CoW box. The semantics of an existential CoW box implies that we
// can only consume the projected value inside the box if the box is
Expand Down Expand Up @@ -254,7 +256,6 @@ FORWARDING_OWNERSHIP_INST(SelectEnum)
FORWARDING_OWNERSHIP_INST(Enum)
// SWIFT_ENABLE_TENSORFLOW
FORWARDING_OWNERSHIP_INST(AutoDiffFunction)
FORWARDING_OWNERSHIP_INST(AutoDiffFunctionExtract)
#undef FORWARDING_OWNERSHIP_INST

ValueOwnershipKind
Expand Down
20 changes: 14 additions & 6 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3749,9 +3749,14 @@ class VJPEmitter final
context.getResultIndices()[autoDiffFuncInst] =
activeResultIndices.front();

vjpValue = getBuilder().createAutoDiffFunctionExtract(
auto borrowedADFunc =
builder.emitBeginBorrowOperation(loc, autoDiffFuncInst);
auto extractedVJP = getBuilder().createAutoDiffFunctionExtract(
loc, AutoDiffFunctionExtractInst::Extractee::VJP,
/*differentiationOrder*/ 1, autoDiffFuncInst);
/*differentiationOrder*/ 1, borrowedADFunc);
vjpValue = builder.emitCopyValueOperation(loc, extractedVJP);
builder.emitEndBorrowOperation(loc, borrowedADFunc);
builder.emitDestroyValueOperation(loc, autoDiffFuncInst);
}

// Record desired/actual VJP indices.
Expand Down Expand Up @@ -4850,7 +4855,6 @@ class JVPEmitter final
// on the remapped original function operand and `autodiff_function_extract`
// the JVP. The actual JVP functions will be populated in the
// `autodiff_function` during the transform main loop.
SILValue differentiableFunc;
if (!jvpValue) {
// FIXME: Handle indirect differentiation invokers. This may require some
// redesign: currently, each original function + attribute pair is mapped
Expand Down Expand Up @@ -4911,16 +4915,20 @@ class JVPEmitter final
auto *autoDiffFuncInst =
context.createAutoDiffFunction(builder, loc, indices.parameters,
/*differentiationOrder*/ 1, original);
differentiableFunc = autoDiffFuncInst;

// Record the `autodiff_function` instruction.
context.getAutoDiffFunctionInsts().push_back(autoDiffFuncInst);
context.getResultIndices()[autoDiffFuncInst] =
activeResultIndices.front();

jvpValue = builder.createAutoDiffFunctionExtract(
auto borrowedADFunc =
builder.emitBeginBorrowOperation(loc, autoDiffFuncInst);
auto extractedJVP = builder.createAutoDiffFunctionExtract(
loc, AutoDiffFunctionExtractInst::Extractee::JVP,
/*differentiationOrder*/ 1, autoDiffFuncInst);
/*differentiationOrder*/ 1, borrowedADFunc);
jvpValue = builder.emitCopyValueOperation(loc, extractedJVP);
builder.emitEndBorrowOperation(loc, borrowedADFunc);
builder.emitDestroyValueOperation(loc, autoDiffFuncInst);
}

// Call the JVP using the original parameters.
Expand Down