Skip to content

Conversation

@asl
Copy link
Contributor

@asl asl commented Jun 30, 2025

This adds initial support for differentiation of functions that may produce Error result. Essentially we wrap the pullback into Optional and emit a diamond-shape control flow pattern depending on whether the pullback value is available or not. VJP emission was modified to accommodate for this. In addition to this, some additional tricks are required as try_apply result is not available in the instruction parent block, it is available in normal successor basic block.

As a result we can now:

  • differentiate an active try_apply result (that would be produced from do ... try .. catch constructions)
  • try_apply when error result is unreachable (usually try! and similar source code constructs)
  • Support (some) throwing functions with builtin differentiation operators. stdlib change will follow. Though we cannot support typed throws here (yet)
  • Correctly propagate error types during currying around differentiable functions as well as type-checking for @derivative(of:) attribute, so we can register custom derivatives for functions producing error result
  • Added custom derivative for Optional.?? operator (note that support here is not yet complete as we cannot differentiate through autoclosures, so x ?? y works only if y is not active, e.g. a constant value).

Some fixes here and there

@asl asl requested a review from rxwei June 30, 2025 22:53
@asl asl requested review from a team, eeckstein, hborla, jckarter, slavapestov and xedin as code owners June 30, 2025 22:53
@asl
Copy link
Contributor Author

asl commented Jun 30, 2025

@asl
Copy link
Contributor Author

asl commented Jun 30, 2025

@swift-ci please test

@asl
Copy link
Contributor Author

asl commented Jul 1, 2025

@swift-ci please test

@asl asl closed this Jul 1, 2025
@asl asl reopened this Jul 1, 2025
@asl
Copy link
Contributor Author

asl commented Jul 1, 2025

Windows fails in unrelated IRGen/async/run-call-void-throws-to-int-throwing_call-sync-nothrow_call-async-throw.sil

@swift-ci please test windows platform

@asl
Copy link
Contributor Author

asl commented Jul 11, 2025

@swift-ci please test

@asl asl removed the request for review from a team July 11, 2025 05:49
@asl asl requested a review from kavon as a code owner October 24, 2025 05:52
@asl
Copy link
Contributor Author

asl commented Oct 24, 2025

@swift-ci please test

@asl asl requested a review from kovdan01 October 31, 2025 18:45
Copy link
Contributor

@kovdan01 kovdan01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Functionally this is totally OK, but please consider addressing minor suggestions I've left in comments before merging this. Feel free to ignore though.

@asl
Copy link
Contributor Author

asl commented Nov 6, 2025

@swift-ci please test

@kovdan01
Copy link
Contributor

kovdan01 commented Nov 6, 2025

@swift-ci please test Windows platform

@asl
Copy link
Contributor Author

asl commented Nov 6, 2025

@swift-ci please test Windows platform

@asl asl merged commit 89a7663 into main Nov 6, 2025
5 checks passed
@asl asl deleted the throw-diff branch November 6, 2025 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants