Skip to content
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

Ternary operator/if-expression not differentiable #79052

Open
loonatick-src opened this issue Jan 30, 2025 · 0 comments
Open

Ternary operator/if-expression not differentiable #79052

loonatick-src opened this issue Jan 30, 2025 · 0 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@loonatick-src
Copy link

Description

Closely related to #54195: the following throws a compilation error on Swift 6.0.3.

import _Differentiation

@differentiable(reverse)
func TF_966(_ x: Float, _ bool: Bool) -> Float {
  let tuple = (x, 1)
  let result = bool ? tuple : tuple
  return result.0
}
<source>:3:2: error: function is not differentiable
1 | import _Differentiation
2 | 
3 | @differentiable(reverse)
  |  `- error: function is not differentiable
4 | func TF_966(_ x: Float, _ bool: Bool) -> Float {
  |      `- note: when differentiating this function definition
5 |   let tuple = (x, 1)
6 |   let result = bool ? tuple : tuple
  |                       `- note: expression is not differentiable
7 |   return result.0
8 | }

But, the following semantically equivalent function compiles successfully.

import _Differentiation

@differentiable(reverse)
func TF_966(_ x: Float, _ bool: Bool) -> Float {
  let tuple = (x, 1)
  return bool ? tuple.0 : tuple.0
}

Compiler explorer link

Reproduction

In a single file

// main.swift
import _Differentiation

@differentiable(reverse)
func TF_966(_ x: Float, _ bool: Bool) -> Float {
  let tuple = (x, 1)
  let result = bool ? tuple : tuple
  return result.0
}

Run swiftc main.swift.

Expected behavior

Should compile successfully.

Environment

Swift version 6.0.3 (swift-6.0.3-RELEASE)
Target: x86_64-unknown-linux-gnu

Additional information

No response

@loonatick-src loonatick-src added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants