Skip to content

[AutoDiff] Emit a diagnostic for non-differentiable active values #67697

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

Merged
merged 4 commits into from
Sep 1, 2023

Conversation

asavonic
Copy link
Contributor

@asavonic asavonic commented Aug 3, 2023

For some values we cannot compute types for differentiation (for example, tangent vector type), so it is better to diagnose them earlier. Otherwise we hit assertions when generating code for such invalid values.

The LIT test is a reduced reproducer from the issue #66996. Before the patch the compiler crashed while trying to get a tangent vector type for the following value (partial_apply):

%54 = function_ref @$s4null1o2ffSdAA1FV_tFSdyKXEfu0_ :
  $@convention(thin) @substituted <τ_0_0> (@inout_aliasable Double)
  -> (@out τ_0_0, @error any Error) for <Double>

%55 = partial_apply [callee_guaranteed] %54(%2) :
  $@convention(thin) @substituted <τ_0_0> (@inout_aliasable Double)
  -> (@out τ_0_0, @error any Error) for <Double>

Now we emit a diagnostic instead.

The patch resolves issues #66996 and #63331.

For some values we cannot compute types for differentiation (for example,
tangent vector type), so it is better to diagnose them earlier. Otherwise we hit
assertions when generating code for such invalid values.

The LIT test is a reduced reproducer from the issue swiftlang#66996. Before the patch the
compiler crashed while trying to get a tangent vector type for the following
value (partial_apply):

    %54 = function_ref @$s4null1o2ffSdAA1FV_tFSdyKXEfu0_ :
      $@convention(thin) @Substituted <τ_0_0> (@inout_aliasable Double)
      -> (@out τ_0_0, @error any Error) for <Double>

    %55 = partial_apply [callee_guaranteed] %54(%2) :
      $@convention(thin) @Substituted <τ_0_0> (@inout_aliasable Double)
      -> (@out τ_0_0, @error any Error) for <Double>

Now we emit a diagnostic instead.

The patch resolves issues swiftlang#66996 and swiftlang#63331.
@asl asl requested a review from rxwei August 3, 2023 15:33
@asl
Copy link
Contributor

asl commented Aug 3, 2023

Tagging @BradLarson

@asl
Copy link
Contributor

asl commented Aug 3, 2023

@swift-ci please test

@@ -1804,6 +1804,11 @@ bool PullbackCloner::Implementation::run() {
DominanceOrder domOrder(original.getEntryBlock(), domInfo);
// Keep track of visited values.
SmallPtrSet<SILValue, 8> visited;
// Also check for differentiable for all active values
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I understand this comment. The two lines of code below are just obtaining the protocol and the module and don't seem related to the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, this comment is more suitable next to the actual check below.

@asl
Copy link
Contributor

asl commented Aug 7, 2023

@swift-ci please test

@asavonic
Copy link
Contributor Author

asavonic commented Aug 8, 2023

There is quite a lot of LIT test with tuples being incorrectly (?) diagnosed as non-differentiable. I will check them and update the patch.

@asl
Copy link
Contributor

asl commented Aug 8, 2023

@asavonic Because some components are non-differentiable?

…ocol

Types like `tuple` do not conform to the differentiable protocol, but they are
handled in the compiler as special cases.

getTangentSpace should work for all these types, and discard truly
non-differentiable types.
@asl
Copy link
Contributor

asl commented Aug 15, 2023

@swift-ci please test

@asl asl requested a review from rxwei August 23, 2023 23:02
@asl asl added the AutoDiff label Aug 23, 2023
Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

Sorry for the delayed review

@asl asl merged commit fc640db into swiftlang:main Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants