-
Notifications
You must be signed in to change notification settings - Fork 5.1k
NativeAOT: Allow Delegate and MulticastDelegate marshalling #63219
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
MichalStrehovsky
merged 8 commits into
dotnet:main
from
kant2002:kant/delegage-marshalling
Jan 17, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
27eca73
NativeAOT: Allow Delegate and MulticastDelegate marshalling
kant2002 09a661b
Produce warning during analysis
kant2002 54afafb
Update src/coreclr/tools/aot/ILLink.Shared/SharedStrings.resx
kant2002 ea43a6b
Use resources for compiler message
kant2002 ebc5282
Apply PR feedback
kant2002 76955d3
Add smoke tests for abstract delegates marshalling
kant2002 86589b4
Apply PR feedback
kant2002 51d00cb
Remove not used code.
kant2002 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ | |
using Internal.TypeSystem; | ||
using Internal.TypeSystem.Interop; | ||
|
||
using ILCompiler.Dataflow; | ||
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 think the new usings are not needed now. 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. Both usings are needed for |
||
using ILCompiler.DependencyAnalysis; | ||
using ILLink.Shared; | ||
|
||
using Debug = System.Diagnostics.Debug; | ||
using DependencyList = ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>.DependencyList; | ||
|
@@ -18,9 +20,12 @@ namespace ILCompiler | |
/// </summary> | ||
public class UsageBasedInteropStubManager : CompilerGeneratedInteropStubManager | ||
{ | ||
public UsageBasedInteropStubManager(InteropStateManager interopStateManager, PInvokeILEmitterConfiguration pInvokeILEmitterConfiguration) | ||
private Logger _logger; | ||
|
||
public UsageBasedInteropStubManager(InteropStateManager interopStateManager, PInvokeILEmitterConfiguration pInvokeILEmitterConfiguration, Logger logger) | ||
: base(interopStateManager, pInvokeILEmitterConfiguration) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public override void AddDependeciesDueToPInvoke(ref DependencyList dependencies, NodeFactory factory, MethodDesc method) | ||
|
@@ -30,11 +35,11 @@ public override void AddDependeciesDueToPInvoke(ref DependencyList dependencies, | |
dependencies = dependencies ?? new DependencyList(); | ||
|
||
MethodSignature methodSig = method.Signature; | ||
AddParameterMarshallingDependencies(ref dependencies, factory, methodSig.ReturnType); | ||
AddParameterMarshallingDependencies(ref dependencies, factory, method, methodSig.ReturnType); | ||
|
||
for (int i = 0; i < methodSig.Length; i++) | ||
{ | ||
AddParameterMarshallingDependencies(ref dependencies, factory, methodSig[i]); | ||
AddParameterMarshallingDependencies(ref dependencies, factory, method, methodSig[i]); | ||
} | ||
} | ||
|
||
|
@@ -45,13 +50,25 @@ public override void AddDependeciesDueToPInvoke(ref DependencyList dependencies, | |
} | ||
} | ||
|
||
private static void AddParameterMarshallingDependencies(ref DependencyList dependencies, NodeFactory factory, TypeDesc type) | ||
private void AddParameterMarshallingDependencies(ref DependencyList dependencies, NodeFactory factory, MethodDesc method, TypeDesc type) | ||
{ | ||
if (type.IsDelegate) | ||
{ | ||
dependencies.Add(factory.DelegateMarshallingData((DefType)type), "Delegate marshaling"); | ||
} | ||
|
||
TypeSystemContext context = type.Context; | ||
if ((type.IsWellKnownType(WellKnownType.MulticastDelegate) | ||
|| type == context.GetWellKnownType(WellKnownType.MulticastDelegate).BaseType)) | ||
{ | ||
var message = new DiagnosticString(DiagnosticId.CorrectnessOfAbstractDelegatesCannotBeGuaranteed).GetMessage(DiagnosticUtilities.GetMethodSignatureDisplayName(method)); | ||
_logger.LogWarning( | ||
message, | ||
(int)DiagnosticId.CorrectnessOfAbstractDelegatesCannotBeGuaranteed, | ||
method, | ||
MessageSubCategory.AotAnalysis); | ||
} | ||
|
||
// struct may contain delegate fields, hence we need to add dependencies for it | ||
if (type.IsByRef) | ||
type = ((ParameterizedType)type).ParameterType; | ||
|
@@ -63,7 +80,7 @@ private static void AddParameterMarshallingDependencies(ref DependencyList depen | |
if (field.IsStatic) | ||
continue; | ||
|
||
AddParameterMarshallingDependencies(ref dependencies, factory, field.FieldType); | ||
AddParameterMarshallingDependencies(ref dependencies, factory, method, field.FieldType); | ||
} | ||
} | ||
} | ||
|
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
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.
System.Delegate
andSystem.MulticastDelegate
marshaling is not AOT friendly. We cannot tell the delegate type to use during AOT compilation for these cases. The marshaling logic is suppressing the AOT warning for the delegates, but this suppression is not correct for the abstract delegate types.This change also needs a matching change in the marshalling logic to avoid suppressing the AOT compatibility warning for these cases.
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.
Short version: I miss some part of puzzle about who produce AOT warnings. Please advice.
Longer version
I look at places where mentioned
MulticastDelegate
insrc/coreclr/tools/
runtime/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs
Line 2371 in b2dc37b
and others
Then for
IsDelegate
in same folder. Since this checks for something derived from MulticastDelegate I clearly do not seeruntime/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedInteropStubManager.cs
Line 109 in b2dc37b
and others
DelegateMarshaller
relies onPInvokeMarshal
which does not annotated for AOT warnings.runtime/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs
Lines 2023 to 2027 in 6e286dd
PInvokeMarshal
class itself appears to not care at all about marshalling. I assume this is low-level logic.runtime/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs
Line 46 in b2dc37b
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.
AOT warnings are produced by calling methods annotated with
RequiresUnreferencedCode
. I think you may want to create a new methods onPInvokeMarshal
that are annotated withRequiresUnreferencedCode
and just forward to the unannotated methods, and call these methods for abstract delegates.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 will produce a warning, but it won't be a particularly useful warning (it will produce a warning saying that a compiler-generated method is calling a method annotated
RequiresUnreferencedCode
, which is not particularly actionable).The proper fix would be to add handling of this somewhere around here (and a new warning code):
runtime/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs
Lines 2178 to 2203 in d0631be
There's a pull request in flight that moves the warning codes to someplace else, so I would wait for that (#63230) to land first.
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 assume that plan was to revert last commit which generate annotated code and made changes in analysis. Force push changes.