Skip to content

[AutoDiff] Fix unexpected non-differentiable property access error. #32670

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

Conversation

dan-zheng
Copy link
Contributor

Add base type parameter to TangentStoredPropertyRequest.

Use TypeBase::getTypeOfMember instead of VarDecl::getType to correctly
compute the member type of original stored properties, using the base type.

Resolves SR-13134.


Example:

import _Differentiation

struct Complex<T: FloatingPoint> {
  var real: T
  var imaginary: T
}

extension Complex: Differentiable where T: Differentiable {
  typealias TangentVector = Complex
}
extension Complex: AdditiveArithmetic {}

@differentiable
func addRealComponents(lhs: Complex<Float>, rhs: Complex<Float>) -> Float {
  return lhs.real + rhs.real
}

Before:

$ swiftc complex.swift
complex.swift:13:2: error: function is not differentiable
@differentiable
~^~~~~~~~~~~~~~
complex.swift:14:6: note: when differentiating this function definition
func addRealComponents(lhs: Complex<Float>, rhs: Complex<Float>) -> Float {
     ^
complex.swift:15:14: note: cannot differentiate access to property 'Complex.real' because property type 'T' does not conform to 'Differentiable'
  return lhs.real + rhs.real
             ^

After: no error, as expected.

@dan-zheng dan-zheng requested review from rxwei and marcrasi July 1, 2020 22:41
@@ -2192,7 +2192,7 @@ class DerivativeAttrOriginalDeclRequest
/// property in a `Differentiable`-conforming type.
class TangentStoredPropertyRequest
: public SimpleRequest<TangentStoredPropertyRequest,
TangentPropertyInfo(VarDecl *),
TangentPropertyInfo(VarDecl *, CanType),
Copy link
Contributor Author

@dan-zheng dan-zheng Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: adding a base type parameter to TangentStoredPropertyRequest results in fewer cache hits.
It may be possible to fix SR-13134 without adding a base type parameter, I may take a look later.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

Add base type parameter to `TangentStoredPropertyRequest`.

Use `TypeBase::getTypeOfMember` instead of `VarDecl::getType` to correctly
compute the member type of original stored properties, using the base type.

Resolves SR-13134.
@dan-zheng dan-zheng force-pushed the autodiff-fix-tangent-property branch from dad21de to 007b216 Compare July 3, 2020 04:31
@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

@dan-zheng dan-zheng merged commit d4bbcf9 into swiftlang:master Jul 5, 2020
@dan-zheng dan-zheng deleted the autodiff-fix-tangent-property branch July 5, 2020 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants