Skip to content

apply of differentiable_function is not optimized #65489

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

Closed
asl opened this issue Apr 28, 2023 · 1 comment · Fixed by #65605
Closed

apply of differentiable_function is not optimized #65489

asl opened this issue Apr 28, 2023 · 1 comment · Fixed by #65605
Assignees
Labels
AutoDiff compiler The Swift compiler itself feature A feature request or implementation SILOptimizer Area → compiler: SIL optimization passes

Comments

@asl
Copy link
Contributor

asl commented Apr 28, 2023

Consider the testcase from #65073

Now we compile it down to:

..
  // function_ref closure #1 in f(array:)
  %7 = function_ref @$s6arrupd1f5arraySdSaySdG_tFySdzcfU_ : $@convention(thin) (@inout Double) -> () // user: %10
  // function_ref forward-mode derivative of closure #1 in f(array:)
  %8 = function_ref @$s6arrupd1f5arraySdSaySdG_tFySdzcfU_TJfSpSr : $@convention(thin) (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () // user: %11
  // function_ref reverse-mode derivative of closure #1 in f(array:)
  %9 = function_ref @$s6arrupd1f5arraySdSaySdG_tFySdzcfU_TJrSpSr : $@convention(thin) (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () // user: %12
  %10 = thin_to_thick_function %7 : $@convention(thin) (@inout Double) -> () to $@noescape @callee_guaranteed (@inout Double) -> () // user: %13
  %11 = thin_to_thick_function %8 : $@convention(thin) (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () to $@noescape @callee_guaranteed (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () // user: %14
  %12 = thin_to_thick_function %9 : $@convention(thin) (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () to $@noescape @callee_guaranteed (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () // user: %15
  %13 = convert_function %10 : $@noescape @callee_guaranteed (@inout Double) -> () to $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <Double> // user: %16
  %14 = convert_function %11 : $@noescape @callee_guaranteed (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () to $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> (@owned @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <τ_0_0.TangentVector>) for <Double> // user: %16
  %15 = convert_function %12 : $@noescape @callee_guaranteed (@inout Double) -> @owned @callee_guaranteed (@inout Double) -> () to $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> (@owned @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <τ_0_0.TangentVector>) for <Double> // user: %16
  %16 = differentiable_function [parameters 0] [results 0] %13 : $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <Double> with_derivative {%14 : $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> (@owned @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <τ_0_0.TangentVector>) for <Double>, %15 : $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> (@owned @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <τ_0_0.TangentVector>) for <Double>} // users: %95, %18
...
  %95 = apply %16(%94) : $@differentiable(reverse) @noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0) -> () for <Double>

Note that we can fold differentiable_function here enabling further simplifications and inlining of $s6arrupd1f5arraySdSaySdG_tFySdzcfU_

@asl asl added feature A feature request or implementation triage needed This issue needs more specific labels labels Apr 28, 2023
@asl asl self-assigned this Apr 28, 2023
@asl asl added SILOptimizer Area → compiler: SIL optimization passes and removed triage needed This issue needs more specific labels labels Apr 28, 2023
@asl
Copy link
Contributor Author

asl commented Apr 28, 2023

Tagging @BradLarson @rxwei @dan-zheng

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself AutoDiff labels Apr 28, 2023
asl added a commit that referenced this issue May 3, 2023
…xes one small potential issue while there.

Fixes #65489
asl added a commit that referenced this issue Sep 1, 2023
…xes one small potential issue while there.

Fixes #65489 #67992
@asl asl closed this as completed in #65605 Sep 5, 2023
asl added a commit that referenced this issue Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff compiler The Swift compiler itself feature A feature request or implementation SILOptimizer Area → compiler: SIL optimization passes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants