Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoDiff] fix SR-12493 #30817

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,63 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
super::visitDestroyValueInst(Destroy);
}

void visitDifferentiableFunctionExtractInst(
DifferentiableFunctionExtractInst *dfei) {
// If the extractee is the original function, do regular cloning.
if (dfei->getExtractee() ==
NormalDifferentiableFunctionTypeComponent::Original) {
super::visitDifferentiableFunctionExtractInst(dfei);
return;
}
// If the extractee is a derivative function, check whether the *remapped
// derivative function type* (BC) is equal to the *derivative remapped
// function type* (AD).
//
// +----------------+ remap +-------------------------+
// | orig. fn type | -------(A)------> | remapped orig. fn type |
// +----------------+ +-------------------------+
// | |
// (B, SILGen) getAutoDiffDerivativeFunctionType (D, here)
// V V
// +----------------+ remap +-------------------------+
// | deriv. fn type | -------(C)------> | remapped deriv. fn type |
// +----------------+ +-------------------------+
//
// (AD) does not always commute with (BC):
// - (AD) is the result of remapping, then computing the derivative type.
// This is the default cloning behavior, but may break invariants in the
// initial SIL generated by SILGen.
// - (BC) is the result of computing the derivative type (SILGen), then
// remapping. This is the expected type, preserving invariants from
// earlier transforms.
//
// If (AD) is not equal to (BC), use (BC) as the explicit type.
SILType remappedOrigType = getOpType(dfei->getOperand()->getType());
auto remappedOrigFnType = remappedOrigType.castTo<SILFunctionType>();
auto derivativeRemappedFnType =
remappedOrigFnType
->getAutoDiffDerivativeFunctionType(
remappedOrigFnType->getDifferentiabilityParameterIndices(),
/*resultIndex*/ 0, dfei->getDerivativeFunctionKind(),
getBuilder().getModule().Types,
LookUpConformanceInModule(SwiftMod))
->getWithoutDifferentiability();
SILType remappedDerivativeFnType = getOpType(dfei->getType());
// If remapped derivative type and derivative remapped type are equal, do
// regular cloning.
if (SILType::getPrimitiveObjectType(derivativeRemappedFnType) ==
remappedDerivativeFnType) {
super::visitDifferentiableFunctionExtractInst(dfei);
return;
}
// Otherwise, explicitly use the remapped derivative type.
recordClonedInstruction(
dfei,
getBuilder().createDifferentiableFunctionExtract(
getOpLocation(dfei->getLoc()), dfei->getExtractee(),
getOpValue(dfei->getOperand()), remappedDerivativeFnType));
}

/// One abstract function in the debug info can only have one set of variables
/// and types. This function determines whether applying the substitutions in
/// \p SubsMap on the generic signature \p Sig will change the generic type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-build-swift -O %s
// RUN: %target-build-swift -O %s

// SR-12493: SIL verification error regarding substituted function types and
// `differentiable_function_extract` instruction. Occurs only with `-O`.
Expand Down
3 changes: 0 additions & 3 deletions test/AutoDiff/stdlib/differential_operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// RUN: %target-run %t/differential_operators
// REQUIRES: executable_test

// FIXME(SR-12493): Disable test for `-O` due to SIL verification error.
// REQUIRES: swift_test_mode_optimize_none

import _Differentiation

import StdlibUnittest
Expand Down