diff --git a/lib/SILOptimizer/Mandatory/Differentiation.cpp b/lib/SILOptimizer/Mandatory/Differentiation.cpp index 2a3989a3cfdf9..501d3e46ebe14 100644 --- a/lib/SILOptimizer/Mandatory/Differentiation.cpp +++ b/lib/SILOptimizer/Mandatory/Differentiation.cpp @@ -4396,8 +4396,13 @@ void DifferentiationTask::createVJP() { // This assumes that the primal direct results correspond exactly to the // adjoint's direct parameters, an assumption that we check in assertions // above. - builder.createRetainValue(loc, primalDirectResults[dirResIdx], - builder.getDefaultAtomicity()); + // + // If this is not the primal value struct (index 0), it must be an + // original result. Retain it because we'll return original results + // alongside the pullback. + if (dirResIdx != 0) + builder.createRetainValue(loc, primalDirectResults[dirResIdx], + builder.getDefaultAtomicity()); partialAdjointArgs.push_back(primalDirectResults[dirResIdx++]); } else { // This assumes that the primal indirect results correspond exactly to the