Skip to content

[AutoDiff] Prevent crash when using @differentiable without importing _Differentiation #67162

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 1 commit into from
Jul 7, 2023

Conversation

jkshtj
Copy link
Contributor

@jkshtj jkshtj commented Jul 6, 2023

The above referenced issue was causing a compiler crash when writing differentiable protocols and corresponding implementers, without importing the _Differentiation module.

Changes in this CR fix the issue by marking any @differentiable attributes as invalid, if the _Differentiation module has not been imported. This ignores the @differentiable attributes when the protocol witnesses are being verified. Witness verification was previously leading to an error (due to missing @differentiable attribute on the protocol requirement implementer), and the corresponding diagnostic emission code was then leading to a crash, because it was expecting the _Differentiation module to be present.

Fixes #59100

@jkshtj
Copy link
Contributor Author

jkshtj commented Jul 6, 2023

@BradLarson could you invoke the CI bot for me?

@BradLarson
Copy link
Contributor

@swift-ci please test

@BradLarson BradLarson changed the title Fixes https://github.com/apple/swift/issues/59100 [AutoDiff] Prevent crash when using @differentiable without importing _Differentiation Jul 6, 2023
@BradLarson BradLarson requested review from asl and rxwei July 6, 2023 17:20
// **NOTE** - This test can likely be removed when/if the differentiation module is
// available by default in the Swift stdlib.
public func req() {}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we end with a newline here like most other tests?

Suggested change
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

Comment on lines 16 to 17
// **NOTE** - This test can likely be removed when/if the differentiation module is
// available by default in the Swift stdlib.
Copy link
Contributor

Choose a reason for hiding this comment

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

The likelihood of this may be 0 though, as domain-specific stdlibs tend to be standalone modules, like Distributed. Not sure it's worth mentioning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know. I'll remove the comment.

@@ -5789,7 +5789,8 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
void AttributeChecker::visitDifferentiableAttr(DifferentiableAttr *attr) {
// Call `getParameterIndices` to trigger
// `DifferentiableAttributeTypeCheckRequest`.
(void)attr->getParameterIndices();
if (attr->getParameterIndices() == nullptr)
attr->setInvalid();
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead, should DifferentiableAttributeTypeCheckRequest be responsible for setting the attr as invalid? The request is already doing so in some places when it calls typecheckDifferentiableAttrforDecl.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I'll make the change.

The above referenced issue was causing a compiler crash when writing
differentiable protocols and corresponding implementers, without importing
the `_Differentiation` module.

Changes in this CR fix the issue by marking any `@differentiable`
attributes as invalid, if the `_Differentiation` module has not been
imported. This ignores the `@differentiable` attributes when the
protocol witnesses are being verified. Witness verification was previously
leading to an error (due to missing `@differentiable` attribute on the protocol
requirement implementer), and the corresponding diagnostic emission code was
then leading to a crash, because it was expecting the `_Differentiation`
module to be present.
@asl
Copy link
Contributor

asl commented Jul 6, 2023

@swift-ci please test

@BradLarson BradLarson merged commit 8b73c98 into swiftlang:main Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AutoDiff] Compiler crash when forgetting to import _Differentiation
4 participants