-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SILGen] Fix the type of closure thunks that are passed const reference structs #75491
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea92765
[SILGen] Fix the type of closure thunks that are passed const reference
ahatanaka d780051
Add a precondition to SILDeclRef's constructor
ahatanaka 921bcc4
Add test cases for testing closure-to-block conversion
ahatanaka d5e3b4b
Add a comment
ahatanaka b0e0408
Fix size of bridged classes
ahatanaka 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
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
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
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
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
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 |
---|---|---|
|
@@ -1723,7 +1723,19 @@ static ManagedValue convertCFunctionSignature(SILGenFunction &SGF, | |
FunctionConversionExpr *e, | ||
SILType loweredResultTy, | ||
llvm::function_ref<ManagedValue ()> fnEmitter) { | ||
SILType loweredDestTy = SGF.getLoweredType(e->getType()); | ||
SILType loweredDestTy; | ||
auto destTy = e->getType(); | ||
auto clangInfo = | ||
destTy->castTo<AnyFunctionType>()->getExtInfo().getClangTypeInfo(); | ||
if (clangInfo.empty()) | ||
loweredDestTy = SGF.getLoweredType(destTy); | ||
else | ||
// This won't be necessary after we stop dropping clang types when | ||
// canonicalizing function types. | ||
loweredDestTy = SGF.getLoweredType( | ||
AbstractionPattern(destTy->getCanonicalType(), clangInfo.getType()), | ||
destTy); | ||
|
||
ManagedValue result; | ||
|
||
// We're converting between C function pointer types. They better be | ||
|
@@ -1794,7 +1806,9 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF, | |
#endif | ||
semanticExpr = conv->getSubExpr()->getSemanticsProvidingExpr(); | ||
} | ||
|
||
|
||
const clang::Type *destFnType = nullptr; | ||
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. This ought to be named something that makes it clear that this is a Clang type. |
||
|
||
if (auto declRef = dyn_cast<DeclRefExpr>(semanticExpr)) { | ||
setLocFromConcreteDeclRef(declRef->getDeclRef()); | ||
} else if (auto memberRef = dyn_cast<MemberRefExpr>(semanticExpr)) { | ||
|
@@ -1808,12 +1822,18 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF, | |
loc = closure; | ||
return ManagedValue(); | ||
}); | ||
auto clangInfo = conversionExpr->getType() | ||
->castTo<FunctionType>() | ||
->getExtInfo() | ||
.getClangTypeInfo(); | ||
if (!clangInfo.empty()) | ||
destFnType = clangInfo.getType(); | ||
} else { | ||
llvm_unreachable("c function pointer converted from a non-concrete decl ref"); | ||
} | ||
|
||
// Produce a reference to the C-compatible entry point for the function. | ||
SILDeclRef constant(loc, /*foreign*/ true); | ||
SILDeclRef constant(loc, /*foreign*/ true, false, false, destFnType); | ||
SILConstantInfo constantInfo = | ||
SGF.getConstantInfo(SGF.getTypeExpansionContext(), constant); | ||
|
||
|
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
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
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.
This should not be necessary. If we're lowering a foreign function type, we should be honoring the Clang type stored in it automatically without requiring that we pass down separately as an abstraction pattern.
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'm passing the type in the abstraction pattern because the stored clang type gets dropped in
TypeConverter::getTypeLowering
if I just pass the type without the abstraction pattern togetLoweredType
.The call to
t->getCanonicalType()
drops the clang type.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.
Actually,
t
still has the clang type, but it's dropped somewhere else 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.
It'd be good to understand that. I don't think there's any good reason for us to drop clang types, especially during type lowering.
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.
Setting
LangOptions::UseClangFunctionTypes
or passing-use-clang-function-types
prevents clang types from getting dropped, but it also causes other regression tests to crash/fail.It looks like it hasn't been turned on by default because of unresolved issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
Do you mean the
SILDeclRef
that's declared later in the function?loc
passed to the constructor is anAbstractClosureExpr
, whose type doesn't have a clang type. The type of parameterFunctionConversionExpr *e
passed to this function has the clang type.Uh oh!
There was an error while loading. Please reload this page.
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 still think this shouldn't be necessary — lowering a
@convention(c)
function type that has a Clang type attached should definitely be producing a type that uses the conventions of that Clang type. Can you identify what exactly drops this? It's one thing if it's just not being propagated into theSILFunctionType
, but it seems bizarre that we'd not use this information when it's available in type lowering.Maybe the query about whether we have a Clang type on an
AbstractionPattern
just needs to consider the possibility that we have a@convention(c)
function type that's carrying such a type.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.
The clang type is dropped in
TypeConverter::getTypeLowering
.The call to
t->getCanonicalType()
drops the clang type becauseUseClangFunctionTypes
isn't set.I experimented with creating an
AbstractionPattern
with a clang type in that function if it exists and it seemed to work although I didn't test it extensively.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.
And the type
t
passed to the overload ofgetTypeLowering
loses its clang type whengetCanonicalType
is called again.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.
Ah, okay. You'd said up-thread that it wasn't dropped by canonicalization, but if it is, I agree that we can't do much here. Please at least add a comment explaining that this won't be necessary when we switch to Clang types, because the Clang function type should survive canonicalization.