Skip to content

Commit 9f0f92f

Browse files
author
ematejska
authored
[AutoDiff upstream] Upstream @transpose attribute type-checking. (swiftlang#30664)
The `@transpose` attribute registers a function as the transpose of another function-like declaration: a `func`, `init`, `subscript`, or `var` computed property declaration. The `@transpose` attribute also has an optional `wrt:` clause specifying the linearity parameters, i.e. the parameters that are transposed with respect to. The linearity parameters must conform to the `Differentiable` protocol and satisfy `Self == TangentVector`. If the `wrt:` clause is unspecified, the linearity parameters are inferred to be all parameters that conform to `Differentiable` and that satisfy `Self == TangentVector`. `@transpose` attribute type-checking verifies that the type of the transpose function declaration is consistent with the type of the referenced original declaration and the linearity parameters. Resolves TF-830.
1 parent 0474387 commit 9f0f92f

File tree

3 files changed

+1074
-3
lines changed

3 files changed

+1074
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,6 +3024,23 @@ ERROR(derivative_attr_original_already_has_derivative,none,
30243024
NOTE(derivative_attr_duplicate_note,none,
30253025
"other attribute declared here", ())
30263026

3027+
// @transpose
3028+
ERROR(transpose_attr_invalid_linearity_parameter_or_result,none,
3029+
"cannot transpose with respect to original %select{result|parameter}1 "
3030+
"'%0' that does not conform to 'Differentiable' and satisfy "
3031+
"'%0 == %0.TangentVector'", (StringRef, /*isParameter*/ bool))
3032+
ERROR(transpose_attr_overload_not_found,none,
3033+
"could not find function %0 with expected type %1", (DeclName, Type))
3034+
ERROR(transpose_attr_cannot_use_named_wrt_params,none,
3035+
"cannot use named 'wrt' parameters in '@transpose(of:)' attribute, found "
3036+
"%0", (Identifier))
3037+
ERROR(transpose_attr_wrt_self_must_be_static,none,
3038+
"the transpose of an instance method must be a 'static' method in the "
3039+
"same type when 'self' is a linearity parameter", ())
3040+
NOTE(transpose_attr_wrt_self_self_type_mismatch_note,none,
3041+
"the transpose is declared in %0 but the original function is declared in "
3042+
"%1", (Type, Type))
3043+
30273044
// Automatic differentiation attributes
30283045
ERROR(autodiff_attr_original_decl_invalid_kind,none,
30293046
"%0 is not a 'func', 'init', 'subscript', or 'var' computed property "

0 commit comments

Comments
 (0)