@@ -1916,14 +1916,6 @@ static SILValue joinElements(ArrayRef<SILValue> elements, SILBuilder &builder,
1916
1916
return builder.createTuple (loc, elements);
1917
1917
}
1918
1918
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
-
1927
1919
// / When a function value is used in an instruction (usually `apply`), there's
1928
1920
// / some conversion instruction in between, e.g. `thin_to_thick_function`. Given
1929
1921
// / a new function value and an old function value, this helper function
@@ -1953,14 +1945,26 @@ reapplyFunctionConversion(SILValue newFunc, SILValue oldFunc,
1953
1945
if (auto *pai = dyn_cast<PartialApplyInst>(oldConvertedFunc)) {
1954
1946
SmallVector<SILValue, 8 > newArgs;
1955
1947
newArgs.reserve (pai->getNumArguments ());
1948
+ SmallVector<AllocStackInst *, 1 > copiedIndirectParams;
1949
+ SWIFT_DEFER {
1950
+ for (auto *alloc : reversed (copiedIndirectParams))
1951
+ builder.createDeallocStack (loc, alloc);
1952
+ };
1956
1953
for (auto arg : pai->getArguments ()) {
1957
1954
// Retain the argument since it's to be owned by the newly created
1958
1955
// closure.
1959
- if (arg->getType ().isObject ())
1956
+ if (arg->getType ().isObject ()) {
1960
1957
builder.createRetainValue (loc, arg, builder.getDefaultAtomicity ());
1961
- else if (arg->getType ().isLoadable (builder.getFunction ()))
1958
+ newArgs.push_back (arg);
1959
+ } else if (arg->getType ().isLoadable (builder.getFunction ())) {
1962
1960
builder.createRetainValueAddr (loc, arg, builder.getDefaultAtomicity ());
1963
- newArgs.push_back (arg);
1961
+ newArgs.push_back (arg);
1962
+ } else {
1963
+ auto *argCopy = builder.createAllocStack (loc, arg->getType ());
1964
+ copiedIndirectParams.push_back (argCopy);
1965
+ builder.createCopyAddr (loc, arg, argCopy, IsNotTake, IsInitialization);
1966
+ newArgs.push_back (argCopy);
1967
+ }
1964
1968
}
1965
1969
auto innerNewFunc = reapplyFunctionConversion (
1966
1970
newFunc, oldFunc, pai->getCallee (), builder, loc, newFuncGenSig);
@@ -3395,8 +3399,7 @@ class VJPEmitter final
3395
3399
3396
3400
// Release the differentiable function.
3397
3401
if (differentiableFunc)
3398
- builder.createReleaseValue (loc, differentiableFunc,
3399
- builder.getDefaultAtomicity ());
3402
+ builder.emitReleaseValueAndFold (loc, differentiableFunc);
3400
3403
3401
3404
// Get the VJP results (original results and pullback).
3402
3405
SmallVector<SILValue, 8 > vjpDirectResults;
@@ -3962,7 +3965,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
3962
3965
LLVM_DEBUG (getADDebugStream () << " Cleaning up temporaries for bb"
3963
3966
<< bb->getDebugID () << ' \n ' );
3964
3967
for (auto temp : blockTemporaries[bb]) {
3965
- emitCleanup ( builder, loc, temp);
3968
+ builder. emitReleaseValueAndFold ( loc, temp);
3966
3969
blockTemporarySet.erase (temp);
3967
3970
}
3968
3971
}
@@ -4534,9 +4537,6 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
4534
4537
auto adjBuf = getAdjointBuffer (origEntry, origParam);
4535
4538
if (errorOccurred)
4536
4539
return ;
4537
- if (adjBuf->getType ().isLoadable (getPullback ()))
4538
- builder.createRetainValueAddr (pbLoc, adjBuf,
4539
- builder.getDefaultAtomicity ());
4540
4540
indParamAdjoints.push_back (adjBuf);
4541
4541
}
4542
4542
};
@@ -4837,7 +4837,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
4837
4837
ApplyInst *ai, CopyAddrInst *cai, AllocStackInst *subscriptBuffer) {
4838
4838
addToAdjointBuffer (cai->getParent (), cai->getSrc (), subscriptBuffer,
4839
4839
cai->getLoc ());
4840
- emitCleanup ( builder, cai->getLoc (), subscriptBuffer);
4840
+ builder. emitDestroyAddrAndFold ( cai->getLoc (), subscriptBuffer);
4841
4841
builder.createDeallocStack (ai->getLoc (), subscriptBuffer);
4842
4842
}
4843
4843
@@ -5033,7 +5033,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
5033
5033
auto tan = *allResultsIt++;
5034
5034
if (tan->getType ().isAddress ()) {
5035
5035
addToAdjointBuffer (bb, origArg, tan, loc);
5036
- emitCleanup ( builder, loc, tan);
5036
+ builder. emitDestroyAddrAndFold ( loc, tan);
5037
5037
} else {
5038
5038
if (origArg->getType ().isAddress ()) {
5039
5039
if (errorOccurred)
@@ -5042,7 +5042,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
5042
5042
builder.createStore (loc, tan, tmpBuf,
5043
5043
getBufferSOQ (tmpBuf->getType ().getASTType (), getPullback ()));
5044
5044
addToAdjointBuffer (bb, origArg, tmpBuf, loc);
5045
- emitCleanup ( builder, loc, tmpBuf);
5045
+ builder. emitDestroyAddrAndFold ( loc, tmpBuf);
5046
5046
builder.createDeallocStack (loc, tmpBuf);
5047
5047
}
5048
5048
else {
@@ -5338,7 +5338,7 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
5338
5338
return ;
5339
5339
auto destType = remapType (adjDest->getType ());
5340
5340
addToAdjointBuffer (bb, cai->getSrc (), adjDest, cai->getLoc ());
5341
- emitCleanup ( builder, cai->getLoc (), adjDest);
5341
+ builder. emitDestroyAddrAndFold ( cai->getLoc (), adjDest);
5342
5342
emitZeroIndirect (destType.getASTType (), adjDest, cai->getLoc ());
5343
5343
}
5344
5344
@@ -6297,13 +6297,15 @@ ADContext::getOrCreateSubsetParametersThunkForLinearMap(
6297
6297
// - Push it to `results` if result is direct.
6298
6298
auto result = allResults[mapOriginalParameterIndex (i)];
6299
6299
if (desiredIndices.isWrtParameter (i)) {
6300
- if (result->getType ().isAddress ())
6301
- continue ;
6302
- results.push_back (result);
6300
+ if (result->getType ().isObject ())
6301
+ results.push_back (result);
6303
6302
}
6304
6303
// Otherwise, cleanup the unused results.
6305
6304
else {
6306
- emitCleanup (builder, loc, result);
6305
+ if (result->getType ().isAddress ())
6306
+ builder.emitDestroyAddrAndFold (loc, result);
6307
+ else
6308
+ builder.emitReleaseValueAndFold (loc, result);
6307
6309
}
6308
6310
}
6309
6311
// Deallocate local allocations and return final direct result.
@@ -6676,7 +6678,7 @@ void ADContext::foldAutoDiffFunctionExtraction(AutoDiffFunctionInst *source) {
6676
6678
if (isInstructionTriviallyDead (source)) {
6677
6679
SILBuilder builder (source);
6678
6680
for (auto &assocFn : source->getAssociatedFunctions ())
6679
- emitCleanup ( builder, source->getLoc (), assocFn.get ());
6681
+ builder. emitDestroyAddrAndFold ( source->getLoc (), assocFn.get ());
6680
6682
source->eraseFromParent ();
6681
6683
}
6682
6684
// Mark `source` as processed so that it won't be reprocessed after deletion.
0 commit comments