[AutoDiff] Support '@differentiable(linear)' function conversion. (#27687)
- Support the following implicit conversions:
- Non-differentiable to `@differentiable(linear)`
- Sema: Emit `LinearFunctionExpr`.
- SILGen: Lower `LinearFunctionExpr` to `linear_function`.
- `@differentiable(linear)` to non-differentiable
- Sema: Emit `LinearFunctionExtractOriginalExpr`.
- SILGen: Lower `LinearFunctionExtractOriginalExpr` to `linear_function_extract [original]`.
- Reject the following implicit conversions:
- `@differentiable` to `@differentiable(linear)`
- This conversion is not rejected because a `@differentiable` function can never come directly from a closure expression or from a declaration/member reference.
- `@differentiable(linear)` to `@differentiable` ([TF-908](https://bugs.swift.org/browse/TF-908))
- This is supported by design, but is not yet implemented due to its complexity. This requires thunking `@differentiable(linear)` to a derivative (JVP) function where the derivative function returns the original result and the same linear map with `@nondiff` parameters partial-applied away.
- Properly handle `linear_function`, `differentiable_function_extract`, and `linear_function_extract` in `swift::isGuaranteedForwardingValueKind` and `swift::isOwnershipForwardingValueKind`. This fixed a previously uncaught assertion, which was because `differentiable_function_extract` was in `swift::isOwnershipForwardingValueKind` while it should really be in `swift::isGuaranteedForwardingValueKind`.
For complete conversion rules among `@differentiable` functions, see [Differentiable Programming Mega-Proposal - Type conversion](https://github.com/dan-zheng/swift/blob/differentiable-programming/docs/DifferentiableProgramming.md#type-conversion).
Resolves [TF-900](https://bugs.swift.org/browse/TF-900).