Skip to content

[AutoDiff] Fix crasher when a closure with context gets differentiated #20186

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

Merged
merged 1 commit into from
Nov 1, 2018
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
6 changes: 4 additions & 2 deletions lib/SILOptimizer/Mandatory/TFDifferentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ static FunctionRefInst *findReferenceToVisibleFunction(SILValue value) {
return findReferenceToVisibleFunction(thinToThick->getOperand());
if (auto *convertFn = dyn_cast<ConvertFunctionInst>(inst))
return findReferenceToVisibleFunction(convertFn->getOperand());
if (auto *partialApply = dyn_cast<PartialApplyInst>(inst))
return findReferenceToVisibleFunction(partialApply->getCallee());
return nullptr;
}

Expand Down Expand Up @@ -1568,7 +1570,7 @@ reapplyFunctionConversion(SILValue newFunc, SILValue oldFunc,
newFunc, oldFunc, pai->getCallee(), builder, loc, substituteOperand);
return builder.createPartialApply(
loc, innerNewFunc, pai->getSubstitutionMap(), newArgs,
pai->getOrigCalleeType()->getCalleeConvention());
ParameterConvention::Direct_Guaranteed);
}
llvm_unreachable("Unhandled function convertion instruction");
}
Expand Down Expand Up @@ -4004,7 +4006,7 @@ void AdjointEmitter::accumulateMaterializedAdjointsIndirect(
// Ensure the witness method is linked.
getModule().lookUpFunctionInWitnessTable(confRef, declRef);
auto subMap =
SubstitutionMap::getProtocolSubstitutions(proto, adjointASTTy, confRef);
SubstitutionMap::getProtocolSubstitutions(proto, adjointASTTy, confRef);
// %1 = metatype $T.Type
auto metatypeType =
CanMetatypeType::get(adjointASTTy, MetatypeRepresentation::Thick);
Expand Down
11 changes: 5 additions & 6 deletions test/AutoDiff/autodiff_e2e_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func adjointId(_ x: Float, originalValue: Float, seed: Float) -> Float {

_ = #gradient(id)(2)

// CHECK: @{{.*}}id{{.*}}__grad_src_0_wrt_0
// CHECK-LABEL: @{{.*}}id{{.*}}__grad_src_0_wrt_0
// CHECK-LABEL: @{{.*}}id{{.*}}__grad_src_0_wrt_0_s_p

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
Expand Down Expand Up @@ -41,7 +41,7 @@ let x = #gradient(sigmoid)(3)
let (value: y, gradient: z) = #valueAndGradient(sigmoid)(4)
print(x * z)

// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0
// CHECK-LABEL: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0
// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0_s_p
// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0_p

Expand All @@ -51,7 +51,6 @@ public func publicFunc(_ x: Float) -> Float {
}
_ = #gradient(publicFunc)

// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__grad_src_0_wrt_0
// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__primal_src_0_wrt_0
// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__adjoint_src_0_wrt_0

// CHECK-LABEL: @{{.*}}publicFunc{{.*}}__grad_src_0_wrt_0
// CHECK: @{{.*}}publicFunc{{.*}}__primal_src_0_wrt_0
// CHECK: @{{.*}}publicFunc{{.*}}__adjoint_src_0_wrt_0
15 changes: 15 additions & 0 deletions test/AutoDiff/closures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s

public func closureCapture() {
let val: Float = 10
let clo: (Float) -> Float = { x in
val * x
}
_ = #gradient(clo)
}

// CHECK-LABEL: @{{.*}}closureCapture{{.*}}
// CHECK: [[ORIG_FN:%.*]] = function_ref @{{.*}}closureCapture{{.*}}
// CHECK: [[PARTIAL_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[ORIG_FN]]
// CHECK: [[GRAD_FN:%.*]] = function_ref @{{.*}}closureCapture{{.*}}___grad
// CHECK: [[PARTIAL_APPLIED_GRAD:%.*]] = partial_apply [callee_guaranteed] [[GRAD_FN]]
9 changes: 9 additions & 0 deletions test/AutoDiff/simple_math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ SimpleMathTests.test("ResultSelection") {
expectEqual((0, 1), #gradient(foo, result: .1)(3, 3))
}


SimpleMathTests.test("CaptureGlobal") {
let z: Float = 10
func foo(_ x: Float) -> Float {
return z * x
}
expectEqual(10, #gradient(foo)(0))
}

runAllTests()