Skip to content
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

Convert some more array FCalls to managed #102739

Merged
merged 53 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
566b15e
Implement IsSimpleCopy and CanAssignArrayType in managed code
huoyaoyuan May 27, 2024
8a592b9
Fold IsSimpleCopy back to CanAssignArrayType
huoyaoyuan May 27, 2024
0dce3d4
Delete FCall and QCall definitions for copy
huoyaoyuan May 27, 2024
d29e5d8
Convert InternalSetValue to managed
huoyaoyuan May 28, 2024
5dd0e3c
Setup FCalls
huoyaoyuan May 28, 2024
527021c
Remove FCall for GetCorElementTypeOfElementType
huoyaoyuan May 28, 2024
e7d843a
Complete TryUnBox
huoyaoyuan May 28, 2024
2bb5fe7
Fix FCall definition
huoyaoyuan May 28, 2024
f7da172
Implement InitializeArray in managed
huoyaoyuan May 28, 2024
0924555
Implement GetSpanDataFrom in managed
huoyaoyuan May 28, 2024
dc85381
Remove FCall definition
huoyaoyuan May 28, 2024
f99c553
Fix RVA field address
huoyaoyuan May 28, 2024
c5e6cc4
Fix RVA assert
huoyaoyuan May 28, 2024
470b86f
Do not use hydrated RtFieldInfo
huoyaoyuan May 28, 2024
c7821e6
Use QCall for LoadSize
huoyaoyuan May 29, 2024
5225203
Fix CanAssignArrayType condition
huoyaoyuan May 29, 2024
07293e3
Fix compilation
huoyaoyuan May 29, 2024
5da88c3
Simplify AssignType enum
huoyaoyuan May 29, 2024
5222bca
Fix I and U in CanPrimitiveWiden
huoyaoyuan May 29, 2024
335b2fb
CanCastTo should be QCall
huoyaoyuan May 30, 2024
93ec9d8
Remove ThrowHelper usages that not in hot path
huoyaoyuan May 31, 2024
c3f5097
Merge branch 'main' into array-fcall
huoyaoyuan Jun 6, 2024
481e4d4
Revert the known broken InternalSetValue
huoyaoyuan Jun 6, 2024
a266a30
Revert "Revert the known broken InternalSetValue"
huoyaoyuan Jun 6, 2024
b8e4584
Fix heap overwrite
huoyaoyuan Jun 7, 2024
5465a97
Add disabled test for RVA field reflection
huoyaoyuan Jun 9, 2024
4ce4f51
Push back changes around IsSimpleCopy and CanAssignArrayType
huoyaoyuan Jun 10, 2024
30b7d8f
Move PrimitiveWiden to InvokeUtils
huoyaoyuan Jun 12, 2024
8a9c5bc
Merge coreclr specific InvokeUtils
huoyaoyuan Jun 12, 2024
f95fa34
Revert "Merge coreclr specific InvokeUtils"
huoyaoyuan Jun 13, 2024
ebfd224
Move InvokeUtils to shared
huoyaoyuan Jun 13, 2024
d9205c6
Apply suggestions from code review
huoyaoyuan Jun 18, 2024
7eef724
Merge branch 'main' into array-fcall
huoyaoyuan Jun 18, 2024
8cfb58f
Fix trailing whitespace
huoyaoyuan Jun 18, 2024
fbb9d71
Reduce QCalls
huoyaoyuan Jun 18, 2024
324df76
Reduce CorElementType overhead
huoyaoyuan Jun 18, 2024
089ae12
Separate RVA reflection change out
huoyaoyuan Jun 19, 2024
a5e7441
Revert GetCorElementTypeOfElementType
huoyaoyuan Jun 19, 2024
5552d63
Change the MethodTable FCall to be optimal for primitive and share wi…
huoyaoyuan Jun 19, 2024
133cd8a
Return ref from GetSpanDataFrom
huoyaoyuan Jun 19, 2024
e17bf0e
Handle enum conversion to underlying type
huoyaoyuan Jun 19, 2024
dd1e2dd
Update exception message
huoyaoyuan Jun 19, 2024
32b8789
Add KeepAlive
huoyaoyuan Jun 20, 2024
582eaf1
Remove unnecessary qcall handle
huoyaoyuan Jun 20, 2024
1836ae5
Fix ref src
huoyaoyuan Jun 20, 2024
cb6aa00
Update FCall name and assert
huoyaoyuan Jun 24, 2024
49cf866
Remove unused changes
huoyaoyuan Jun 24, 2024
7a0c398
Use void* for address
huoyaoyuan Jun 24, 2024
db677ad
Apply suggestions from code review
huoyaoyuan Jun 25, 2024
353715e
Remove unused enum FCall
huoyaoyuan Jun 24, 2024
8fb1c96
Use sizeof
huoyaoyuan Jun 25, 2024
26d4c80
Add some comment
huoyaoyuan Jun 25, 2024
5f7263f
Add comment to lifetime
huoyaoyuan Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 102 additions & 148 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public abstract partial class Enum
private static unsafe CorElementType InternalGetCorElementType(RuntimeType rt)
{
Debug.Assert(rt.IsActualEnum);
CorElementType elementType = InternalGetCorElementType((MethodTable*)rt.GetUnderlyingNativeHandle());
CorElementType elementType = rt.GetNativeTypeHandle().AsMethodTable()->GetVerifierCorElementType();
GC.KeepAlive(rt);
return elementType;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe CorElementType InternalGetCorElementType()
{
CorElementType elementType = InternalGetCorElementType(RuntimeHelpers.GetMethodTable(this));
CorElementType elementType = RuntimeHelpers.GetMethodTable(this)->GetVerifierCorElementType();
GC.KeepAlive(this);
return elementType;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ internal static unsafe RuntimeType InternalGetUnderlyingType(RuntimeType enumTyp
// Sanity check the last element in the table
Debug.Assert(s_underlyingTypes[(int)CorElementType.ELEMENT_TYPE_U] == typeof(nuint));

RuntimeType? underlyingType = s_underlyingTypes[(int)InternalGetCorElementType((MethodTable*)enumType.GetUnderlyingNativeHandle())];
RuntimeType? underlyingType = s_underlyingTypes[(int)enumType.GetNativeTypeHandle().AsMethodTable()->GetVerifierCorElementType()];
GC.KeepAlive(enumType);

Debug.Assert(underlyingType != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static unsafe class CastHelpers
// Unlike the IsInstanceOfInterface and IsInstanceOfClass functions,
// this test must deal with all kinds of type tests
[DebuggerHidden]
private static object? IsInstanceOfAny(void* toTypeHnd, object? obj)
internal static object? IsInstanceOfAny(void* toTypeHnd, object? obj)
{
if (obj != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Threading;

namespace System.Runtime.CompilerServices
{
public static partial class RuntimeHelpers
{
[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);
public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHandle)
{
if (array is null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe void* GetSpanDataFrom(
if (fldHandle.IsNullHandle())
throw new ArgumentException(SR.Argument_InvalidHandle);

IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo();

if (!RuntimeFieldHandle.GetRVAFieldInfo(fldInfo.Value, out IntPtr address, out uint size))
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

// Note that we do not check that the field is actually in the PE file that is initializing
// the array. Basically, the data being published can be accessed by anyone with the proper
// permissions (C# marks these as assembly visibility, and thus are protected from outside
// snooping)

MethodTable* pMT = GetMethodTable(array);
TypeHandle elementTH = pMT->GetArrayElementTypeHandle();

if (elementTH.IsTypeDesc || !elementTH.AsMethodTable()->IsPrimitive) // Enum is included
throw new ArgumentException(SR.Argument_BadArrayForInitializeArray);

nuint totalSize = pMT->ComponentSize * array.NativeLength;

// make certain you don't go off the end of the rva static
if (totalSize > size)
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

ref byte src = ref *(byte*)address;
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
GC.KeepAlive(fldInfo);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

ref byte dst = ref MemoryMarshal.GetArrayDataReference(array);

Debug.Assert(!elementTH.AsMethodTable()->ContainsGCPointers);

if (BitConverter.IsLittleEndian)
{
SpanHelpers.Memmove(ref dst, ref src, totalSize);
}
else
{
switch (pMT->ComponentSize)
{
case 1:
Copy link
Member

Choose a reason for hiding this comment

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

Can we use sizeof(byte) and corresponding primitives for this switch statement?

Copy link
Member Author

Choose a reason for hiding this comment

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

We are using such pattern in CoreLib. Sounds reasonable.

SpanHelpers.Memmove(ref dst, ref src, totalSize);
break;
case 2:
BinaryPrimitives.ReverseEndianness(
new ReadOnlySpan<ushort>(ref Unsafe.As<byte, ushort>(ref src), array.Length),
new Span<ushort>(ref Unsafe.As<byte, ushort>(ref dst), array.Length));
break;
case 4:
BinaryPrimitives.ReverseEndianness(
new ReadOnlySpan<uint>(ref Unsafe.As<byte, uint>(ref src), array.Length),
new Span<uint>(ref Unsafe.As<byte, uint>(ref dst), array.Length));
break;
case 8:
BinaryPrimitives.ReverseEndianness(
new ReadOnlySpan<ulong>(ref Unsafe.As<byte, ulong>(ref src), array.Length),
new Span<ulong>(ref Unsafe.As<byte, ulong>(ref dst), array.Length));
break;
default:
Debug.Fail("Incorrect primitive type size!");
break;
}
}
}

private static unsafe ref byte GetSpanDataFrom(
RuntimeFieldHandle fldHandle,
RuntimeTypeHandle targetTypeHandle,
out int count);
out int count)
{
if (fldHandle.IsNullHandle())
throw new ArgumentException(SR.Argument_InvalidHandle);

IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo();

if (!RuntimeFieldHandle.GetRVAFieldInfo(fldInfo.Value, out IntPtr data, out uint totalSize))
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

TypeHandle th = targetTypeHandle.GetNativeTypeHandle();
Debug.Assert(!th.IsTypeDesc); // TypeDesc can't be used as generic parameter
MethodTable* targetMT = th.AsMethodTable();

if (!targetMT->IsPrimitive) // Enum is included
throw new ArgumentException(SR.Argument_BadArrayForInitializeArray);

uint targetTypeSize = targetMT->GetNumInstanceFieldBytes();

if (data % targetTypeSize != 0)
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

if (!BitConverter.IsLittleEndian)
{
throw new PlatformNotSupportedException();
}

count = (int)(totalSize / targetTypeSize);
ref byte dataRef = ref *(byte*)data;
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
GC.KeepAlive(fldInfo);

return ref dataRef;
}

// GetObjectValue is intended to allow value classes to be manipulated as 'Object'
// but have aliasing behavior of a value class. The intent is that you would use
Expand Down Expand Up @@ -655,6 +752,8 @@ public int MultiDimensionalArrayRank
// Warning! UNLIKE the similarly named Reflection api, this method also returns "true" for Enums.
public bool IsPrimitive => (Flags & enum_flag_Category_Mask) is enum_flag_Category_PrimitiveValueType or enum_flag_Category_TruePrimitive;

public bool IsTruePrimitive => (Flags & enum_flag_Category_Mask) is enum_flag_Category_TruePrimitive;

public bool HasInstantiation => (Flags & enum_flag_HasComponentSize) == 0 && (Flags & enum_flag_GenericsMask) != enum_flag_GenericsMask_NonGeneric;

public bool IsGenericTypeDefinition => (Flags & (enum_flag_HasComponentSize | enum_flag_GenericsMask)) == enum_flag_GenericsMask_TypicalInst;
Expand Down Expand Up @@ -684,6 +783,13 @@ public TypeHandle GetArrayElementTypeHandle()

[MethodImpl(MethodImplOptions.InternalCall)]
public extern uint GetNumInstanceFieldBytes();

/// <summary>
/// Get the <see cref="CorElementType"/> representing this type. Enums are represented by underlying type.
/// </summary>
/// <remarks>This method is optimized in favor of primitive types and enums.</remarks>
jkotas marked this conversation as resolved.
Show resolved Hide resolved
[MethodImpl(MethodImplOptions.InternalCall)]
public extern CorElementType GetVerifierCorElementType();
}

// Subset of src\vm\methodtable.h
Expand Down Expand Up @@ -711,7 +817,7 @@ public bool CanCompareBitsOrUseFastGetHashCode
/// <summary>
/// A type handle, which can wrap either a pointer to a <c>TypeDesc</c> or to a <see cref="MethodTable"/>.
/// </summary>
internal unsafe struct TypeHandle
internal readonly unsafe partial struct TypeHandle
{
// Subset of src\vm\typehandle.h

Expand Down Expand Up @@ -758,6 +864,8 @@ public static TypeHandle TypeHandleOf<T>()
{
return new TypeHandle((void*)RuntimeTypeHandle.ToIntPtr(typeof(T).TypeHandle));
}

public static bool AreSameType(TypeHandle left, TypeHandle right) => left.m_asTAddr == right.m_asTAddr;
jkotas marked this conversation as resolved.
Show resolved Hide resolved
}

// Helper structs used for tail calls via helper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ public RuntimeFieldInfoStub(RuntimeFieldHandleInternal fieldHandle, object keepa
}

[NonVersionable]
public unsafe struct RuntimeFieldHandle : IEquatable<RuntimeFieldHandle>, ISerializable
public unsafe partial struct RuntimeFieldHandle : IEquatable<RuntimeFieldHandle>, ISerializable
{
// Returns handle for interop with EE. The handle is guaranteed to be non-null.
internal RuntimeFieldHandle GetNativeHandle()
Expand Down Expand Up @@ -1193,6 +1193,10 @@ internal static RuntimeType GetApproxDeclaringType(IRuntimeFieldInfo field)
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetStaticFieldAddress(RtFieldInfo field);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeFieldHandle_GetRVAFieldInfo")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GetRVAFieldInfo(RuntimeFieldHandleInternal field, out IntPtr address, out uint size);
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int GetToken(RtFieldInfo field);

Expand Down
Loading
Loading