-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff upstream] @derivative
attribute type-checking fixes.
#28853
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
swift-ci
merged 1 commit into
swiftlang:master
from
dan-zheng:derivative-attr-type-checking
Dec 18, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3263,25 +3263,34 @@ static bool checkFunctionSignature( | |
return false; | ||
} | ||
|
||
// Map type into the required function type's generic signature, if it exists. | ||
// This is significant when the required generic signature has same-type | ||
// requirements while the candidate generic signature does not. | ||
auto mapType = [&](Type type) { | ||
if (!requiredGenSig) | ||
return type->getCanonicalType(); | ||
return requiredGenSig->getCanonicalTypeInContext(type); | ||
}; | ||
|
||
// Check that parameter types match, disregarding labels. | ||
if (required->getNumParams() != candidateFnTy->getNumParams()) | ||
return false; | ||
if (!std::equal(required->getParams().begin(), required->getParams().end(), | ||
candidateFnTy->getParams().begin(), | ||
[](AnyFunctionType::Param x, AnyFunctionType::Param y) { | ||
return x.getPlainType()->isEqual(y.getPlainType()); | ||
[&](AnyFunctionType::Param x, AnyFunctionType::Param y) { | ||
return x.getPlainType()->isEqual(mapType(y.getPlainType())); | ||
})) | ||
return false; | ||
|
||
// If required result type is not a function type, check that result types | ||
// match exactly. | ||
auto requiredResultFnTy = dyn_cast<AnyFunctionType>(required.getResult()); | ||
auto candidateResultTy = mapType(candidateFnTy.getResult()); | ||
if (!requiredResultFnTy) { | ||
auto requiredResultTupleTy = dyn_cast<TupleType>(required.getResult()); | ||
auto candidateResultTupleTy = | ||
dyn_cast<TupleType>(candidateFnTy.getResult()); | ||
auto candidateResultTupleTy = dyn_cast<TupleType>(candidateResultTy); | ||
if (!requiredResultTupleTy || !candidateResultTupleTy) | ||
return required.getResult()->isEqual(candidateFnTy.getResult()); | ||
return required.getResult()->isEqual(candidateResultTy); | ||
// If result types are tuple types, check that element types match, | ||
// ignoring labels. | ||
if (requiredResultTupleTy->getNumElements() != | ||
|
@@ -3294,7 +3303,7 @@ static bool checkFunctionSignature( | |
} | ||
|
||
// Required result type is a function. Recurse. | ||
return checkFunctionSignature(requiredResultFnTy, candidateFnTy.getResult()); | ||
return checkFunctionSignature(requiredResultFnTy, candidateResultTy); | ||
}; | ||
|
||
// Returns an `AnyFunctionType` with the same `ExtInfo` as `fnType`, but with | ||
|
@@ -3578,8 +3587,20 @@ void AttributeChecker::visitDerivativeAttr(DerivativeAttr *attr) { | |
auto resultTanType = valueResultConf.getTypeWitnessByName( | ||
valueResultType, Ctx.Id_TangentVector); | ||
|
||
// Compute the actual differential/pullback type that we use for comparison | ||
// with the expected type. We must canonicalize the derivative interface type | ||
// before extracting the differential/pullback type from it, so that the | ||
// derivative interface type generic signature is available for simplifying | ||
// types. | ||
CanType canActualResultType = derivativeInterfaceType->getCanonicalType(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it's possible to canonicalize - auto *derivativeInterfaceType = derivative->getInterfaceType()
- ->castTo<AnyFunctionType>();
+ auto derivativeInterfaceType = dyn_cast<AnyFunctionType>(
+ derivative->getInterfaceType()->getCanonicalType()); I tried this on
I believe this error occurs when diagnostic messages contain archetypes ( |
||
while (isa<AnyFunctionType>(canActualResultType)) { | ||
canActualResultType = | ||
cast<AnyFunctionType>(canActualResultType).getResult(); | ||
} | ||
CanType actualFuncEltType = | ||
cast<TupleType>(canActualResultType).getElementType(1); | ||
|
||
// Compute expected differential/pullback type. | ||
auto funcEltType = funcResultElt.getType(); | ||
Type expectedFuncEltType; | ||
if (kind == AutoDiffDerivativeFunctionKind::JVP) { | ||
auto diffParams = map<SmallVector<AnyFunctionType::Param, 4>>( | ||
|
@@ -3595,7 +3616,7 @@ void AttributeChecker::visitDerivativeAttr(DerivativeAttr *attr) { | |
expectedFuncEltType = expectedFuncEltType->mapTypeOutOfContext(); | ||
|
||
// Check if differential/pullback type matches expected type. | ||
if (!funcEltType->isEqual(expectedFuncEltType)) { | ||
if (!actualFuncEltType->isEqual(expectedFuncEltType)) { | ||
// Emit differential/pullback type mismatch error on attribute. | ||
diagnoseAndRemoveAttr(attr, diag::derivative_attr_result_func_type_mismatch, | ||
funcResultElt.getName(), originalAFD->getFullName()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's possible to remap
candidateFnTy
once, instead of remapping its individual components.Filed TF-1073 to track this improvement.