[NativeAOT] Reflection Invoke refactoring (#73131)
authorJan Kotas <jkotas@microsoft.com>
Tue, 2 Aug 2022 12:54:51 +0000 (05:54 -0700)
committerGitHub <noreply@github.com>
Tue, 2 Aug 2022 12:54:51 +0000 (05:54 -0700)
commitf3483388f770602373cbf031ffbce108784bcf4a
treefe88e15259b074ce3a3bc98ba19758582b740cf4
parentc4bb28f575ccfd09fa4b4a95c97467bd9a9748c8
[NativeAOT] Reflection Invoke refactoring (#73131)

- Refactored reflection Invoke in NativeAOT to be similar to how it is done in CoreCLR.
- All argument validation and coercion is done via static code now. This makes the reflection invoke stubs much smaller and the AOT compiler side a lot simpler.
- The invoke stub is minimal now and just takes the arguments for the methods to invoke as "Span of byrefs". The two notable differences with CoreCLR are:
   - The invoke stub takes the function pointer to call as an argument to allow sharing of the stubs between methods with the same signature. CoreCLR has the thunks non-sharable currently. We have discussed sharing them among methods with the same signature like it is done here.
   - The return value is returned as byref. CoreCLR thunk does boxing of the return value as part of the stub. Again, we have discussed to do it this way in CoreCLR too, we just did not have time to do it yet.

Fixes #72548

Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
43 files changed:
src/coreclr/nativeaot/Runtime/GCHelpers.cpp
src/coreclr/nativeaot/Runtime/thread.cpp
src/coreclr/nativeaot/Runtime/thread.h
src/coreclr/nativeaot/Runtime/thread.inl
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DelegateDynamicInvokeInfo.cs [deleted file]
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DynamicInvokeInfo.cs [new file with mode: 0644]
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeNamedMethodInfo.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/TypeLoaderExports.cs
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Type.Internal.cs
src/coreclr/nativeaot/System.Private.DisabledReflection/src/Internal/Reflection/ReflectionCoreCallbacksImplementation.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/MethodInvokeInfo.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/MethodInvokers/InstanceMethodInvoker.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/MethodInvokers/StaticMethodInvoker.cs
src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/MethodInvokers/VirtualMethodInvoker.cs
src/coreclr/tools/Common/Compiler/DependencyAnalysis/SortableDependencyNode.cs
src/coreclr/tools/Common/Internal/Runtime/MetadataBlob.cs
src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.Mangling.cs [new file with mode: 0644]
src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.Sorting.cs
src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/CompilerTypeSystemContext.DynamicInvoke.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DynamicInvokeTemplateDataNode.cs [deleted file]
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DynamicInvokeTemplateNode.cs [deleted file]
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectionInvokeMapNode.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DynamicInvokeThunkGenerationPolicy.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/GeneratingMetadataManager.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MetadataManager.cs
src/coreclr/tools/aot/ILCompiler.Compiler/ILCompiler.Compiler.csproj
src/libraries/System.Formats.Asn1/tests/Reader/ReadLength.cs
src/libraries/System.Memory/tests/Span/Reflection.cs
src/libraries/System.Runtime/tests/System/Reflection/InvokeWithRefLikeArgs.cs