@@ -2746,6 +2746,22 @@ bool LoadableByAddress::recreateConvInstr(SILInstruction &I,
2746
2746
auto currSILFunctionType = currSILType.castTo <SILFunctionType>();
2747
2747
GenericEnvironment *genEnv =
2748
2748
convInstr->getFunction ()->getGenericEnvironment ();
2749
+ // SWIFT_ENABLE_TENSORFLOW
2750
+ // Differentiable function conversion instructions can happen while the
2751
+ // function is still generic. In that case, we must calculate the new type
2752
+ // using the converted function's generic environment rather than the
2753
+ // converting function's generic environment.
2754
+ //
2755
+ // This happens in witness thunks for default implementations of derivative
2756
+ // requirements, e.g. `requirement00024` in
2757
+ // "test/AutoDiff/compiler_crashers_fixed/tf961".
2758
+ if (convInstr->getKind () == SILInstructionKind::DifferentiableFunctionInst ||
2759
+ convInstr->getKind () == SILInstructionKind::DifferentiableFunctionExtractInst ||
2760
+ convInstr->getKind () == SILInstructionKind::LinearFunctionInst ||
2761
+ convInstr->getKind () == SILInstructionKind::LinearFunctionExtractInst)
2762
+ if (auto genSig = currSILFunctionType->getSubstGenericSignature ())
2763
+ genEnv = genSig->getGenericEnvironment ();
2764
+ // SWIFT_ENABLE_TENSORFLOW_END
2749
2765
CanSILFunctionType newFnType = MapperCache.getNewSILFunctionType (
2750
2766
genEnv, currSILFunctionType, *currIRMod);
2751
2767
SILType newType = SILType::getPrimitiveObjectType (newFnType);
0 commit comments