Skip to content

[SR-13096] Fix autodiff typing rules for class-typed function parameters #55542

@dan-zheng

Description

@dan-zheng
Previous ID SR-13096
Radar rdar://problem/72819053
Original Reporter @dan-zheng
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: bece69c35790a09fc5e98c37400842e1

relates to:

  • TF-1175 Self-mutating class method call is not marked active

Issue Description:

Fix @differentiable and @derivative type-checking for class-typed function parameters.

Class-typed values have reference semantics and can be freely mutated. Thus, they should be treated like inout parameters for the purposes of @differentiable and @derivative type-checking.


Example:

import _Differentiation

class Class: Differentiable {
  var x: Float = 0

  // Semantically acts like `Class.x.set`.
  // Type: `(Class) -> (Float) -> Void`.
  func setterForX(_ newValue: Float) {
    self.x = newValue
  }

  // This VJP is expected to pass type-checking but does not.
  @derivative(of: setterForX, wrt: (self, newValue))
  func vjpSetterForX(_ newValue: Float) -> (
    value: (), pullback: (inout TangentVector) -> Float
  ) {
    fatalError()
  }
}

Unexpected error, because the class-typed self parameter is not treated as a "semantic result" by autodiff::getFunctionSemanticResultTypes.

class.swift:13:4: error: cannot differentiate void function 'setterForX'
  @derivative(of: setterForX, wrt: (self, newValue))
   ^              ~~~~~~~~~~

Metadata

Metadata

Assignees

Labels

AutoDiffbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions