Skip to content

[AutoDiff] Ensure adjoint buffer of unchecked_take_enum_data_addr is zeroed after value is accumulated #58457

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
Apr 27, 2022
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
17 changes: 12 additions & 5 deletions lib/SILOptimizer/Differentiation/PullbackCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class PullbackCloner::Implementation final
if (auto adjProj = getAdjointProjection(origBB, originalValue))
return (bufferMap[{origBB, originalValue}] = adjProj);

LLVM_DEBUG(getADDebugStream() << "Creating new adjoint buffer for"
LLVM_DEBUG(getADDebugStream() << "Creating new adjoint buffer for "
<< originalValue
<< "in bb" << origBB->getDebugID() << '\n');

Expand Down Expand Up @@ -589,7 +589,8 @@ class PullbackCloner::Implementation final
auto adjointBuffer = getAdjointBuffer(origBB, originalValue);

LLVM_DEBUG(getADDebugStream() << "Adding"
<< rhsAddress << "to adjoint of "
<< rhsAddress << "to adjoint ("
<< adjointBuffer << ") of "
<< originalValue
<< "in bb" << origBB->getDebugID() << '\n');

Expand Down Expand Up @@ -811,7 +812,8 @@ class PullbackCloner::Implementation final
#endif
SILInstructionVisitor::visit(inst);
LLVM_DEBUG({
auto &s = llvm::dbgs() << "[ADJ] Emitted in pullback:\n";
auto &s = llvm::dbgs() << "[ADJ] Emitted in pullback (pb bb" <<
builder.getInsertionBB()->getDebugID() << "):\n";
auto afterInsertion = builder.getInsertionPoint();
for (auto it = ++beforeInsertion; it != afterInsertion; ++it)
s << *it;
Expand Down Expand Up @@ -1645,7 +1647,7 @@ class PullbackCloner::Implementation final
void
visitUncheckedTakeEnumDataAddrInst(UncheckedTakeEnumDataAddrInst *utedai) {
auto *bb = utedai->getParent();
auto adjBuf = getAdjointBuffer(bb, utedai);
auto adjDest = getAdjointBuffer(bb, utedai);
auto enumTy = utedai->getOperand()->getType();
auto *optionalEnumDecl = getASTContext().getOptionalDecl();
// Only `Optional`-typed operands are supported for now. Diagnose all other
Expand All @@ -1659,7 +1661,8 @@ class PullbackCloner::Implementation final
errorOccurred = true;
return;
}
accumulateAdjointForOptional(bb, utedai->getOperand(), adjBuf);
accumulateAdjointForOptional(bb, utedai->getOperand(), adjDest);
builder.emitZeroIntoBuffer(utedai->getLoc(), adjDest, IsNotInitialization);
}

#define NOT_DIFFERENTIABLE(INST, DIAG) void visit##INST##Inst(INST##Inst *inst);
Expand Down Expand Up @@ -2473,6 +2476,10 @@ void PullbackCloner::Implementation::visitSILBasicBlock(SILBasicBlock *bb) {
for (auto *bbArg : bb->getArguments()) {
if (!getActivityInfo().isActive(bbArg, getConfig()))
continue;
LLVM_DEBUG(getADDebugStream() << "Propagating adjoint value for active bb"
<< bb->getDebugID() << " argument: "
<< *bbArg);

// Get predecessor terminator operands.
SmallVector<std::pair<SILBasicBlock *, SILValue>, 4> incomingValues;
bbArg->getSingleTerminatorOperands(incomingValues);
Expand Down