@@ -1376,22 +1376,25 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
13761376 bool shouldUseRestoreTracker =
13771377 utils::shouldUseRestoreTracker (request.Function );
13781378 bool hasCustomPullback = LookupCustomDerivativeDecl (request);
1379+ // Share one request between early classification and final scheduling.
1380+ DiffRequest forwPassRequest;
1381+ if (request.Mode == DiffMode::pullback) {
1382+ forwPassRequest.Function = request.Function ;
1383+ forwPassRequest.BaseFunctionName = request.BaseFunctionName ;
1384+ forwPassRequest.Mode = DiffMode::reverse_mode_forward_pass;
1385+ forwPassRequest.CallContext = request.CallContext ;
1386+ forwPassRequest.UseRestoreTracker = shouldUseRestoreTracker;
1387+ }
1388+
13791389 if (hasNoMemoryInputForPointerOrRefReturn) {
13801390 const auto * calledMethod = dyn_cast<CXXMethodDecl>(FD );
13811391 bool isRefReturningInstanceCall =
13821392 request.Mode == DiffMode::pullback && calledMethod &&
13831393 calledMethod->isInstance () &&
13841394 utils::isNonConstReferenceType (request->getReturnType ());
13851395 bool hasCustomReverseForw = false ;
1386- if (isRefReturningInstanceCall) {
1387- DiffRequest forwPassRequest;
1388- forwPassRequest.Function = request.Function ;
1389- forwPassRequest.BaseFunctionName = request.BaseFunctionName ;
1390- forwPassRequest.Mode = DiffMode::reverse_mode_forward_pass;
1391- forwPassRequest.CallContext = request.CallContext ;
1392- forwPassRequest.UseRestoreTracker = shouldUseRestoreTracker;
1396+ if (isRefReturningInstanceCall)
13931397 hasCustomReverseForw = LookupCustomDerivativeDecl (forwPassRequest);
1394- }
13951398
13961399 // A reverse_forw still needs a matching pullback. Preserve the existing
13971400 // forward-only contract only when a custom reverse_forw has no custom
@@ -1475,11 +1478,8 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
14751478 }
14761479
14771480 if (request.Mode == DiffMode::pullback) {
1478- DiffRequest forwPassRequest;
1479- forwPassRequest.Function = request.Function ;
1480- forwPassRequest.BaseFunctionName = request.BaseFunctionName ;
1481- forwPassRequest.Mode = DiffMode::reverse_mode_forward_pass;
1482- forwPassRequest.CallContext = request.CallContext ;
1481+ // TBR can prove that no state needs restoring, so refresh this before
1482+ // scheduling the request.
14831483 forwPassRequest.UseRestoreTracker = shouldUseRestoreTracker;
14841484 QualType returnType = request->getReturnType ();
14851485 bool hasCustomReverseForw = LookupCustomDerivativeDecl (forwPassRequest);
0 commit comments