Skip to content

Incorrect gradients in some optional-related cases #74978

@asl

Description

@asl

Description

The code below should produce identical gradients, however, it does not

Reproduction

import _Differentiation

@differentiable(reverse)
func square(y: Double) -> Double? {
  return y * y
}

@differentiable(reverse)
func square2(y: Double?) -> Double {
  return y! * y!
}

print(valueWithGradient(at: 42.0, of: {y in square(y:y)!}))
print(valueWithGradient(at: 42.0, of: {y in square2(y:y)}))

Expected behavior

(value: 1764.0, gradient: 84.0)
(value: 1764.0, gradient: 84.0)

Environment

Swift version 6.0-dev (LLVM 76ba61e88550d3b, Swift 9470ad7)
Target: arm64-apple-macosx13.0

Additional information

Currently we are printing:
(value: 1764.0, gradient: 84.0)
(value: 1764.0, gradient: 42.0)

Metadata

Metadata

Assignees

Labels

AutoDiffSILbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfswift 6.0unexpected behaviorBug: Unexpected behavior or incorrect output

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions