Skip to content

Commit

Permalink
Revert "[AutoDiff] Fix custom derivative thunk for Optional (#71721)"
Browse files Browse the repository at this point in the history
This reverts commit aa5dddb.
  • Loading branch information
MaxDesiatov committed Jun 10, 2024
1 parent abc9490 commit ad89497
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 49 deletions.
9 changes: 3 additions & 6 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6347,13 +6347,10 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(
arguments.push_back(indErrorRes.getLValueAddress());
forwardFunctionArguments(thunkSGF, loc, fnRefType, params, arguments);

SubstitutionMap subs = thunk->getForwardingSubstitutionMap();
SILType substFnType = fnRef->getType().substGenericArgs(
M, subs, thunk->getTypeExpansionContext());

// Apply function argument.
auto apply =
thunkSGF.emitApplyWithRethrow(loc, fnRef, substFnType, subs, arguments);
auto apply = thunkSGF.emitApplyWithRethrow(
loc, fnRef, /*substFnType*/ fnRef->getType(),
thunk->getForwardingSubstitutionMap(), arguments);

// Self reordering thunk is necessary if wrt at least two parameters,
// including self.
Expand Down
29 changes: 11 additions & 18 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,15 +906,6 @@ bool DifferentiationTransformer::canonicalizeDifferentiabilityWitness(
traceMessage.c_str(), witness->getOriginalFunction());

assert(witness->isDefinition());
SILFunction *orig = witness->getOriginalFunction();

// We can generate empty JVP / VJP for functions available externally. These
// functions have the same linkage as the original ones sans `external`
// flag. Important exception here hidden_external functions as they are
// serializable but corresponding hidden ones would be not and the SIL
// verifier will fail. Patch `serializeFunctions` for this case.
if (orig->getLinkage() == SILLinkage::HiddenExternal)
serializeFunctions = IsNotSerialized;

// If the JVP doesn't exist, need to synthesize it.
if (!witness->getJVP()) {
Expand All @@ -923,8 +914,9 @@ bool DifferentiationTransformer::canonicalizeDifferentiabilityWitness(
// - Functions with unsupported control flow.
if (context.getASTContext()
.LangOpts.hasFeature(Feature::ForwardModeDifferentiation) &&
(diagnoseNoReturn(context, orig, invoker) ||
diagnoseUnsupportedControlFlow(context, orig, invoker)))
(diagnoseNoReturn(context, witness->getOriginalFunction(), invoker) ||
diagnoseUnsupportedControlFlow(
context, witness->getOriginalFunction(), invoker)))
return true;

// Create empty JVP.
Expand All @@ -941,10 +933,10 @@ bool DifferentiationTransformer::canonicalizeDifferentiabilityWitness(
!witness->getVJP()) {
// JVP and differential generation do not currently support functions with
// multiple basic blocks.
if (orig->size() > 1) {
context.emitNondifferentiabilityError(orig->getLocation().getSourceLoc(),
invoker,
diag::autodiff_jvp_control_flow_not_supported);
if (witness->getOriginalFunction()->size() > 1) {
context.emitNondifferentiabilityError(
witness->getOriginalFunction()->getLocation().getSourceLoc(),
invoker, diag::autodiff_jvp_control_flow_not_supported);
return true;
}
// Emit JVP function.
Expand All @@ -958,7 +950,7 @@ bool DifferentiationTransformer::canonicalizeDifferentiabilityWitness(
"_fatalErrorForwardModeDifferentiationDisabled");
LLVM_DEBUG(getADDebugStream()
<< "Generated empty JVP for "
<< orig->getName() << ":\n"
<< witness->getOriginalFunction()->getName() << ":\n"
<< *jvp);
}
}
Expand All @@ -968,8 +960,9 @@ bool DifferentiationTransformer::canonicalizeDifferentiabilityWitness(
// Diagnose:
// - Functions with no return.
// - Functions with unsupported control flow.
if (diagnoseNoReturn(context, orig, invoker) ||
diagnoseUnsupportedControlFlow(context, orig, invoker))
if (diagnoseNoReturn(context, witness->getOriginalFunction(), invoker) ||
diagnoseUnsupportedControlFlow(
context, witness->getOriginalFunction(), invoker))
return true;

// Create empty VJP.
Expand Down
25 changes: 0 additions & 25 deletions test/AutoDiff/SILGen/nil_coalescing.swift

This file was deleted.

0 comments on commit ad89497

Please sign in to comment.