Skip to content

[AutoDiff] Check @noDerivative for function type comparison #67121

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 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,13 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
return getTypeMatchFailure(argumentLocator);
}

// If functions are differentiable, ensure that @noDerivative is not
// discarded.
if (func1->isDifferentiable() && func2->isDifferentiable() &&
func1Param.isNoDerivative() && !func2Param.isNoDerivative()) {
return getTypeMatchFailure(argumentLocator);
}

// FIXME: We should check value ownership too, but it's not completely
// trivial because of inout-to-pointer conversions.

Expand Down
3 changes: 2 additions & 1 deletion test/AutoDiff/SILOptimizer/differentiation_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ struct TF_675 : Differentiable {
let _: @differentiable(reverse) (Float) -> Float = TF_675().method

// TF-918: Test parameter subset thunk + partially-applied original function.
Copy link
Contributor

Choose a reason for hiding this comment

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

I can no longer find TF-918... Is there a GitHub issue for it?

Copy link
Contributor

@asl asl Jul 5, 2023

Choose a reason for hiding this comment

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

I'm afraid all TF issues gone long time ago. And if they were not converted to normal Swift JIRA issues, then they cannot be found anywhere...

@shahmishal will you please confirm?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably go over all TF- bugs in comments and remove them.

Copy link
Contributor

Choose a reason for hiding this comment

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

And rewrite to present issues when the issue was migrated (there are few of them IIRC). Some of the TF's refer to some design decisions made in the past. It would be good to document them somehow while there are some memories on them :) I'll check what are the most important / interesting ones

let _: @differentiable(reverse) (Float, Float) -> Float = (+) as @differentiable(reverse) (Float, @noDerivative Float) -> Float
let _: @differentiable(reverse) (Float, @noDerivative Float) -> Float = (+) as @differentiable(reverse) (Float, Float) -> Float
let _: @differentiable(reverse) (Float, @noDerivative Float) -> Float = (+) as @differentiable(reverse) (Float, @noDerivative Float) -> Float
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised this test was there! Maybe it was meant to test function conversions to a function with more @noDerivative. That should be possible with "subset parameter thunks".

let _: @differentiable(reverse) (Float, @noDerivative Float) -> Float = (+) as @differentiable(reverse) (Float, Float) -> Float

Is the above possible today?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the above example is also possible. I think it is the same as test1 in the new LIT test, but we can add it to differentiation_diagnostics.swift as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it actually work in a runtime test?


//===----------------------------------------------------------------------===//
// Differentiation in fragile functions
Expand Down
25 changes: 25 additions & 0 deletions test/Constraints/noderivative.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-typecheck-verify-swift

import _Differentiation
Copy link
Contributor

Choose a reason for hiding this comment

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

Because the test is not in the test/AutoDiff folder, it needs REQUIRES: differentiable_programming or people that build without that feature will fail this test.

Copy link
Contributor

Choose a reason for hiding this comment

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

@asavonic Please submit a follow-up PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry for causing this issue. I've submitted a PR to fix it: #67190


// Allow Type -> @noDerivative Type
//
func test1(_ foo: @escaping @differentiable(reverse) (Float, Float) -> Float) {
let fn: @differentiable(reverse) (Float, @noDerivative Float) -> Float = foo
_ = fn(0, 0)
}

// Allow @noDerivative Type -> Type when LHS function is not differentiable
//
func test2(_ foo: @escaping @differentiable(reverse) (Float, @noDerivative Float) -> Float) {
let fn: (Float, Float) -> Float = foo
_ = fn(0, 0)
}

// Disallow @noDerivative Type -> Type when LHS function is also differentiable
//
func test3(_ foo: @escaping @differentiable(reverse) (Float, @noDerivative Float) -> Float) {
// expected-error @+1 {{cannot convert value of type '@differentiable(reverse) (Float, @noDerivative Float) -> Float' to specified type '@differentiable(reverse) (Float, Float) -> Float'}}
let fn: @differentiable(reverse) (Float, Float) -> Float = foo
_ = fn(0, 0)
}