Skip to content

Commit c95b6bd

Browse files
committed
Remove emitCleanup and use folding builder methods.
1 parent d14bb7f commit c95b6bd

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,14 +1916,6 @@ static SILValue joinElements(ArrayRef<SILValue> elements, SILBuilder &builder,
19161916
return builder.createTuple(loc, elements);
19171917
}
19181918

1919-
// Emits a release based on the value's type category (address or object).
1920-
static void emitCleanup(SILBuilder &builder, SILLocation loc, SILValue v) {
1921-
if (v->getType().isAddress())
1922-
builder.createDestroyAddr(loc, v);
1923-
else
1924-
builder.createReleaseValue(loc, v, builder.getDefaultAtomicity());
1925-
}
1926-
19271919
/// When a function value is used in an instruction (usually `apply`), there's
19281920
/// some conversion instruction in between, e.g. `thin_to_thick_function`. Given
19291921
/// a new function value and an old function value, this helper function
@@ -3407,8 +3399,7 @@ class VJPEmitter final
34073399

34083400
// Release the differentiable function.
34093401
if (differentiableFunc)
3410-
builder.createReleaseValue(loc, differentiableFunc,
3411-
builder.getDefaultAtomicity());
3402+
builder.emitReleaseValueAndFold(loc, differentiableFunc);
34123403

34133404
// Get the VJP results (original results and pullback).
34143405
SmallVector<SILValue, 8> vjpDirectResults;
@@ -3974,7 +3965,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
39743965
LLVM_DEBUG(getADDebugStream() << "Cleaning up temporaries for bb"
39753966
<< bb->getDebugID() << '\n');
39763967
for (auto temp : blockTemporaries[bb]) {
3977-
emitCleanup(builder, loc, temp);
3968+
builder.emitReleaseValueAndFold(loc, temp);
39783969
blockTemporarySet.erase(temp);
39793970
}
39803971
}
@@ -4846,7 +4837,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
48464837
ApplyInst *ai, CopyAddrInst *cai, AllocStackInst *subscriptBuffer) {
48474838
addToAdjointBuffer(cai->getParent(), cai->getSrc(), subscriptBuffer,
48484839
cai->getLoc());
4849-
emitCleanup(builder, cai->getLoc(), subscriptBuffer);
4840+
builder.emitDestroyAddrAndFold(cai->getLoc(), subscriptBuffer);
48504841
builder.createDeallocStack(ai->getLoc(), subscriptBuffer);
48514842
}
48524843

@@ -5042,7 +5033,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
50425033
auto tan = *allResultsIt++;
50435034
if (tan->getType().isAddress()) {
50445035
addToAdjointBuffer(bb, origArg, tan, loc);
5045-
emitCleanup(builder, loc, tan);
5036+
builder.emitDestroyAddrAndFold(loc, tan);
50465037
} else {
50475038
if (origArg->getType().isAddress()) {
50485039
if (errorOccurred)
@@ -5051,7 +5042,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
50515042
builder.createStore(loc, tan, tmpBuf,
50525043
getBufferSOQ(tmpBuf->getType().getASTType(), getPullback()));
50535044
addToAdjointBuffer(bb, origArg, tmpBuf, loc);
5054-
emitCleanup(builder, loc, tmpBuf);
5045+
builder.emitDestroyAddrAndFold(loc, tmpBuf);
50555046
builder.createDeallocStack(loc, tmpBuf);
50565047
}
50575048
else {
@@ -5347,7 +5338,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
53475338
return;
53485339
auto destType = remapType(adjDest->getType());
53495340
addToAdjointBuffer(bb, cai->getSrc(), adjDest, cai->getLoc());
5350-
emitCleanup(builder, cai->getLoc(), adjDest);
5341+
builder.emitDestroyAddrAndFold(cai->getLoc(), adjDest);
53515342
emitZeroIndirect(destType.getASTType(), adjDest, cai->getLoc());
53525343
}
53535344

@@ -6306,13 +6297,15 @@ ADContext::getOrCreateSubsetParametersThunkForLinearMap(
63066297
// - Push it to `results` if result is direct.
63076298
auto result = allResults[mapOriginalParameterIndex(i)];
63086299
if (desiredIndices.isWrtParameter(i)) {
6309-
if (result->getType().isAddress())
6310-
continue;
6311-
results.push_back(result);
6300+
if (result->getType().isObject())
6301+
results.push_back(result);
63126302
}
63136303
// Otherwise, cleanup the unused results.
63146304
else {
6315-
emitCleanup(builder, loc, result);
6305+
if (result->getType().isAddress())
6306+
builder.emitDestroyAddrAndFold(loc, result);
6307+
else
6308+
builder.emitReleaseValueAndFold(loc, result);
63166309
}
63176310
}
63186311
// Deallocate local allocations and return final direct result.
@@ -6685,7 +6678,7 @@ void ADContext::foldAutoDiffFunctionExtraction(AutoDiffFunctionInst *source) {
66856678
if (isInstructionTriviallyDead(source)) {
66866679
SILBuilder builder(source);
66876680
for (auto &assocFn : source->getAssociatedFunctions())
6688-
emitCleanup(builder, source->getLoc(), assocFn.get());
6681+
builder.emitDestroyAddrAndFold(source->getLoc(), assocFn.get());
66896682
source->eraseFromParent();
66906683
}
66916684
// Mark `source` as processed so that it won't be reprocessed after deletion.

0 commit comments

Comments
 (0)