Skip to content

[AutoDiff] Unify adjoint entry block generation with the general logic. #25255

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 2 commits into from
Jun 5, 2019
Merged
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
27 changes: 19 additions & 8 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4130,6 +4130,10 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
: nullptr;
Lowering::GenericContextScope genericContextScope(
getContext().getTypeConverter(), adjGenSig);
auto origExitIt = original.findReturnBB();
assert(origExitIt != original.end() &&
"Functions without returns must have been diagnosed");
auto *origExit = &*origExitIt;

// Get dominated active values in original blocks.
// Adjoint values of dominated active values are passed as adjoint block
Expand Down Expand Up @@ -4174,12 +4178,22 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
for (auto *origBB : postOrderInfo->getPostOrder()) {
auto *adjointBB = adjoint.createBasicBlock();
adjointBBMap.insert({origBB, adjointBB});
// If adjoint block is the adjoint entry, continue.
if (adjointBB->isEntry())
continue;
// Otherwise, add a pullback struct argument to the adjoint block.
auto pbStructLoweredType =
remapType(getPullbackInfo().getPullbackStructLoweredType(origBB));
// If the BB is the original exit, then the adjoint block that we just
// created must be the adjoint function's entry. For the adjoint entry,
// create entry arguments and continue to the next block.
if (origBB == origExit) {
assert(adjointBB->isEntry());
createEntryArguments(&getAdjoint());
auto *lastArg = adjointBB->getArguments().back();
assert(lastArg->getType() == pbStructLoweredType);
adjointPullbackStructArguments[origBB] = lastArg;
continue;
}

// Otherwise, we create a phi argument for the corresponding pullback
// struct, and handle dominated active values/buffers.
auto *pbStructArg = adjointBB->createPhiArgument(
pbStructLoweredType, ValueOwnershipKind::Guaranteed);
adjointPullbackStructArguments[origBB] = pbStructArg;
Expand Down Expand Up @@ -4221,15 +4235,11 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
}
}

auto *origEntry = original.getEntryBlock();
auto *origExit = &*original.findReturnBB();
auto *adjointEntry = adjoint.getEntryBlock();
createEntryArguments(&adjoint);
// The adjoint function has type (seed, exit_pbs) -> ([arg0], ..., [argn]).
auto adjParamArgs = adjoint.getArgumentsWithoutIndirectResults();
assert(adjParamArgs.size() == 2);
seed = adjParamArgs[0];
adjointPullbackStructArguments[origExit] = adjParamArgs[1];

// Assign adjoint for original result.
SmallVector<SILValue, 8> origFormalResults;
Expand Down Expand Up @@ -4433,6 +4443,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
// Place the builder at the adjoint exit, i.e. the adjoint block
// corresponding to the original entry. Return the adjoints wrt parameters
// in the adjoint exit.
auto *origEntry = getOriginal().getEntryBlock();
builder.setInsertionPoint(getAdjointBlock(origEntry));

// This vector will contain all the materialized return elements.
Expand Down