Files
swift-mirror/test/AutoDiff/SILGen/reabstraction.swift
Richard Wei af8942d940 [AutoDiff] Rename '@differentiable' to '@differentiable(reverse)'.
Compiler:
- Add `Forward` and `Reverse` to `DifferentiabilityKind`.
- Expand `DifferentiabilityMask` in `ExtInfo` to 3 bits so that it now holds all 4 cases of `DifferentiabilityKind`.
- Parse `@differentiable(reverse)` and `@differentiable(_forward)` declaration attributes and type attributes.
- Emit a warning for `@differentiable` without `reverse`.
- Emit an error for `@differentiable(_forward)`.
- Rename `@differentiable(linear)` to `@differentiable(_linear)`.
- Make `@differentiable(reverse)` type lowering go through today's `@differentiable` code path. We will specialize it to reverse-mode in a follow-up patch.

ABI:
- Add `Forward` and `Reverse` to `FunctionMetadataDifferentiabilityKind`.
- Extend `TargetFunctionTypeFlags` by 1 bit to store the highest bit of differentiability kind (linear). Note that there is a 2-bit gap in `DifferentiabilityMask` which is reserved for `AsyncMask` and `ConcurrentMask`; `AsyncMask` is ABI-stable so we cannot change that.

_Differentiation module:
- Replace all occurrences of `@differentiable` with `@differentiable(reverse)`.
- Delete `_transpose(of:)`.

Resolves rdar://69980056.
2021-02-07 14:09:46 -08:00

82 lines
5.5 KiB
Swift

// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s
import _Differentiation
@_silgen_name("triggerReabstraction1")
func triggerReabstraction1<T: Differentiable>(_ f: @escaping @differentiable(reverse) (T) -> T) {}
@_silgen_name("triggerReabstraction2")
func triggerReabstraction2<T>(_ t: T) {}
@_silgen_name("differentiable1")
func differentiable1(_ x: Float) -> Float { x }
@_silgen_name("differentiable2")
func differentiable2<T: Differentiable>(_ t: T) -> T { t }
@_silgen_name("makeSignatureAbstract")
func makeSignatureAbstract() {
triggerReabstraction1(differentiable1)
}
// CHECK-LABEL: sil{{.*}}@makeSignatureAbstract
// CHECK: [[BEFORE:%.*]] = differentiable_function [parameters 0] [results 0]
// CHECK: [[ORIG_0:%.*]] = differentiable_function_extract [original] [[BEFORE]]
// CHECK: [[ORIG_1:%.*]] = copy_value [[ORIG_0]]
// CHECK: [[ORIG_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> Float) -> @out Float
// CHECK: [[ORIG_2:%.*]] = partial_apply [callee_guaranteed] [[ORIG_THUNK]]([[ORIG_1]])
// CHECK: [[ORIG_3:%.*]] = convert_function [[ORIG_2]]
// CHECK: [[JVP_0:%.*]] = differentiable_function_extract [jvp] [[BEFORE]]
// CHECK: [[JVP_1:%.*]] = copy_value [[JVP_0]]
// CHECK: [[JVP_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)) -> (@out Float, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Float, Float>)
// CHECK: [[JVP_2:%.*]] = partial_apply [callee_guaranteed] [[JVP_THUNK]]([[JVP_1]])
// CHECK: [[JVP_3:%.*]] = convert_function [[JVP_2]]
// CHECK: [[VJP_0:%.*]] = differentiable_function_extract [vjp] [[BEFORE]]
// CHECK: [[VJP_1:%.*]] = copy_value [[VJP_0]]
// CHECK: [[VJP_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)) -> (@out Float, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Float, Float>)
// CHECK: [[VJP_2:%.*]] = partial_apply [callee_guaranteed] [[VJP_THUNK]]([[VJP_1]])
// CHECK: [[VJP_3:%.*]] = convert_function [[VJP_2]]
// CHECK: [[AFTER:%.*]] = differentiable_function [parameters 0] [results 0] [[ORIG_3]] {{.*}} with_derivative {[[JVP_3]] {{.*}}, [[VJP_3]] {{.*}}}
// CHECK: [[TRIGGER:%.*]] = function_ref @triggerReabstraction1
// CHECK: apply [[TRIGGER]]<Float>([[AFTER]])
@_silgen_name("makeOpaque")
func makeOpaque() {
let f: @differentiable(reverse) (Float) -> Float = differentiable1
triggerReabstraction2(f)
}
// CHECK-LABEL: sil{{.*}}@makeOpaque
// CHECK: [[STACK_ADDR:%.*]] = alloc_stack $@differentiable(reverse) @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Float, Float>
// CHECK: [[ORIG_0:%.*]] = differentiable_function_extract [original] [[BEFORE:%.*]] : $@differentiable(reverse) @callee_guaranteed (Float) -> Float
// CHECK: [[ORIG_1:%.*]] = copy_value [[ORIG_0]]
// CHECK: [[ORIG_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> Float) -> @out Float
// CHECK: [[ORIG_2:%.*]] = partial_apply [callee_guaranteed] [[ORIG_THUNK]]([[ORIG_1]])
// CHECK: [[ORIG_3:%.*]] = convert_function [[ORIG_2]]
// CHECK: [[JVP_0:%.*]] = differentiable_function_extract [jvp] [[BEFORE]]
// CHECK: [[JVP_1:%.*]] = copy_value [[JVP_0]]
// CHECK: [[JVP_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)) -> (@out Float, @out @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Float, Float>)
// CHECK: [[JVP_2:%.*]] = partial_apply [callee_guaranteed] [[JVP_THUNK]]([[JVP_1]])
// CHECK: [[JVP_3:%.*]] = convert_function [[JVP_2]]
// CHECK: [[VJP_0:%.*]] = differentiable_function_extract [vjp] [[BEFORE]]
// CHECK: [[VJP_1:%.*]] = copy_value [[VJP_0]]
// CHECK: [[VJP_THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)) -> (@out Float, @out @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Float, Float>)
// CHECK: [[VJP_2:%.*]] = partial_apply [callee_guaranteed] [[VJP_THUNK]]([[VJP_1]])
// CHECK: [[VJP_3:%.*]] = convert_function [[VJP_2]]
// CHECK: [[AFTER:%.*]] = differentiable_function [parameters 0] [results 0] [[ORIG_3]] {{.*}} with_derivative {[[JVP_3]] {{.*}}, [[VJP_3]] {{.*}}}
// CHECK: store [[AFTER]] to [init] [[STACK_ADDR]]
// CHECK: [[TRIGGER:%.*]] = function_ref @triggerReabstraction2
// CHECK: apply [[TRIGGER]]<@differentiable(reverse) (Float) -> Float>([[STACK_ADDR]])
@_silgen_name("makeSignatureDirect")
func makeSignatureDirect() {
let _: @differentiable(reverse) (Float) -> Float = differentiable2
}
// CHECK-LABEL: sil{{.*}}@makeSignatureDirect
// CHECK: [[ORIG_0:%.*]] = function_ref @differentiable2
// CHECK: [[ORIG_1:%.*]] = partial_apply [callee_guaranteed] [[ORIG_0]]<Float>()
// CHECK: [[THUNK:%.*]] = function_ref {{.*}} : $@convention(thin) (Float, @guaranteed @callee_guaranteed (@in_guaranteed Float) -> @out Float) -> Float
// CHECK: [[ORIG_2:%.*]] = partial_apply [callee_guaranteed] [[THUNK]]([[ORIG_1]])
// CHECK: differentiable_function [parameters 0] [results 0] [[ORIG_2]]