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

[AutoDiff] Unoptimized code generation for simple derivatives when called from conditional control-flow contexts #71510

Open
jkshtj opened this issue Feb 9, 2024 · 0 comments · May be fixed by #73185
Assignees
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@jkshtj
Copy link
Collaborator

jkshtj commented Feb 9, 2024

Description

The compiler is not optimizing the code as well as it could when simple derivatives are called from code that contains control-flow.

Reproduction

import _Differentiation

@differentiable(reverse)
func oneOperation(a: Float) -> Float {
    return a * 2
}

func f() {
  var obj: Float = 0

  for i in 1...1000 {
       obj += gradient(at: Float(i), of: oneOperation)
  }
}

// As compared to `f()` derivative of `oneOperation`
// is fully inlined into `g()`.
func g() {
  // Simple function composition.
  var obj: Float = 0
  obj += gradient(at: Float(i), of: oneOperation)
}
f()

Expected behavior

More optimized code should be generated. Specifically, the derivative of the simple function, called from a conditional control-flow context, should ideally have the same performance (minus the overhead of control-flow itself) as when called from a non-conditional control-flow context.

Environment

Present in 5.9 and likely all higher and lower versioned toolchains.

Additional information

No response

@jkshtj jkshtj added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 9, 2024
@jkshtj jkshtj self-assigned this Mar 27, 2024
jkshtj added a commit to jkshtj/swift that referenced this issue Apr 22, 2024
…ns in closure-lifetime-fixup pass

This commit modifies the closure-lifetime-fixup pass to skip fixing up
closure-lifetimes for differentiable functions derived from `thin_to_thick_function`
instructions. Such closures should be trivial and should not necessitate
lifetime fixups as they do not capture any values.

Fixes apple#71510
@hborla hborla removed the triage needed This issue needs more specific labels label Apr 27, 2024
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.
Projects
None yet
3 participants