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

[API Proposal]: Create array from array type #88620

Merged
merged 26 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
14 changes: 11 additions & 3 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand All @@ -15,9 +14,18 @@ namespace System
// IList<U> and IReadOnlyList<U>, where T : U dynamically. See the SZArrayHelper class for details.
public abstract partial class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable
{
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe Array InternalCreate(RuntimeType elementType, int rank, int* pLengths, int* pLowerBounds);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Array_CreateInstance")]
private static unsafe partial void InternalCreate(QCallTypeHandle type, int rank, int* pLengths, int* pLowerBounds, ObjectHandleOnStack retArray);

private static unsafe Array InternalCreate(RuntimeType type, int rank, int* pLengths, int* pLowerBounds)
{
Array? retArray = null;
InternalCreate(new QCallTypeHandle(ref type), rank, pLengths, pLowerBounds, ObjectHandleOnStack.Create(ref retArray));
return retArray!;
}

private static unsafe Array InternalCreateFromArrayType(RuntimeType type, int rank, int* pLengths, int* pLowerBounds)
=> InternalCreate(type, -rank, pLengths, pLowerBounds);
jkotas marked this conversation as resolved.
Show resolved Hide resolved

private static unsafe void CopyImpl(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable)
{
Expand Down
97 changes: 49 additions & 48 deletions src/coreclr/classlibnative/bcltype/arraynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,8 @@ FCIMPLEND


// Check we're allowed to create an array with the given element type.
void ArrayNative::CheckElementType(TypeHandle elementType)
static void CheckElementType(TypeHandle elementType)
{
// Checks apply recursively for arrays of arrays etc.
while (elementType.IsArray())
{
elementType = elementType.GetArrayElementTypeHandle();
}

// Check for simple types first.
if (!elementType.IsTypeDesc())
{
Expand All @@ -753,62 +747,69 @@ void ArrayNative::CheckElementType(TypeHandle elementType)
}
}

FCIMPL4(Object*, ArrayNative::CreateInstance, ReflectClassBaseObject* pElementTypeUNSAFE, INT32 rank, INT32* pLengths, INT32* pLowerBounds)
void QCALLTYPE Array_CreateInstance(QCall::TypeHandle pTypeHnd, INT32 rank, INT32* pLengths, INT32* pLowerBounds, QCall::ObjectHandleOnStack retArray)
{
CONTRACTL {
FCALL_CHECK;
PRECONDITION(rank > 0);
QCALL_CHECK;
PRECONDITION(rank != 0);
PRECONDITION(CheckPointer(pLengths));
PRECONDITION(CheckPointer(pLowerBounds, NULL_OK));
} CONTRACTL_END;

OBJECTREF pRet = NULL;

REFLECTCLASSBASEREF pElementType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pElementTypeUNSAFE);

// pLengths and pLowerBounds are pinned buffers. No need to protect them.
HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(pElementType);
BEGIN_QCALL;

TypeHandle elementType(pElementType->GetType());
TypeHandle typeHnd = pTypeHnd.AsTypeHandle();

CheckElementType(elementType);
// Negative rank means that the type handle is the array type
if (rank < 0)
{
rank = -rank;

CorElementType CorType = elementType.GetSignatureCorElementType();
_ASSERTE((INT32)typeHnd.GetRank() == rank);
_ASSERTE(typeHnd.IsArray());

CorElementType kind = ELEMENT_TYPE_ARRAY;
CheckElementType(typeHnd.GetArrayElementTypeHandle());

// Is it ELEMENT_TYPE_SZARRAY array?
if (rank == 1 && (pLowerBounds == NULL || pLowerBounds[0] == 0)
#ifdef FEATURE_64BIT_ALIGNMENT
// On platforms where 64-bit types require 64-bit alignment and don't obtain it naturally force us
// through the slow path where this will be handled.
&& (CorType != ELEMENT_TYPE_I8)
&& (CorType != ELEMENT_TYPE_U8)
&& (CorType != ELEMENT_TYPE_R8)
#endif
)
{
// Shortcut for common cases
if (CorTypeInfo::IsPrimitiveType(CorType))
if (!typeHnd.AsMethodTable()->IsMultiDimArray())
{
pRet = AllocatePrimitiveArray(CorType,pLengths[0]);
_ASSERTE(pLowerBounds == NULL || pLowerBounds[0] == 0);

GCX_COOP();
retArray.Set(AllocateSzArray(typeHnd, pLengths[0]));
goto Done;
}
else
if (CorTypeInfo::IsObjRef(CorType))
}
else
{
CheckElementType(typeHnd);

// Is it ELEMENT_TYPE_SZARRAY array?
if (rank == 1 && (pLowerBounds == NULL || pLowerBounds[0] == 0))
{
pRet = AllocateObjectArray(pLengths[0],elementType);
goto Done;
CorElementType corType = typeHnd.GetSignatureCorElementType();

// Shortcut for common cases
if (CorTypeInfo::IsPrimitiveType(corType))
{
GCX_COOP();
retArray.Set(AllocatePrimitiveArray(corType, pLengths[0]));
goto Done;
}

typeHnd = ClassLoader::LoadArrayTypeThrowing(typeHnd);

{
GCX_COOP();
retArray.Set(AllocateSzArray(typeHnd, pLengths[0]));
goto Done;
}
}

kind = ELEMENT_TYPE_SZARRAY;
pLowerBounds = NULL;
// Find the Array class...
typeHnd = ClassLoader::LoadArrayTypeThrowing(typeHnd, ELEMENT_TYPE_ARRAY, rank);
}

{
// Find the Array class...
TypeHandle typeHnd = ClassLoader::LoadArrayTypeThrowing(elementType, kind, rank);

_ASSERTE(rank <= MAX_RANK); // Ensures that the stack buffer size allocations below won't overflow

DWORD boundsSize = 0;
Expand All @@ -834,15 +835,15 @@ FCIMPL4(Object*, ArrayNative::CreateInstance, ReflectClassBaseObject* pElementTy
bounds[i] = pLengths[i];
}

pRet = AllocateArrayEx(typeHnd, bounds, boundsSize);
{
GCX_COOP();
retArray.Set(AllocateArrayEx(typeHnd, bounds, boundsSize));
}
}

Done: ;
HELPER_METHOD_FRAME_END();

return OBJECTREFToObject(pRet);
END_QCALL;
}
FCIMPLEND

FCIMPL3(void, ArrayNative::SetValue, ArrayBase* refThisUNSAFE, Object* objUNSAFE, INT_PTR flattenedIndex)
{
Expand Down
6 changes: 1 addition & 5 deletions src/coreclr/classlibnative/bcltype/arraynative.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class ArrayNative
static FCDECL2(FC_BOOL_RET, IsSimpleCopy, ArrayBase* pSrc, ArrayBase* pDst);
static FCDECL5(void, CopySlow, ArrayBase* pSrc, INT32 iSrcIndex, ArrayBase* pDst, INT32 iDstIndex, INT32 iLength);

static FCDECL4(Object*, CreateInstance, ReflectClassBaseObject* pElementTypeUNSAFE, INT32 rank, INT32* pLengths, INT32* pBounds);

// This set of methods will set a value in an array
static FCDECL3(void, SetValue, ArrayBase* refThisUNSAFE, Object* objUNSAFE, INT_PTR flattenedIndex);

Expand All @@ -44,9 +42,6 @@ class ArrayNative
static FCDECL3_VVI(void*, GetSpanDataFrom, FCALLRuntimeFieldHandle structField, FCALLRuntimeTypeHandle targetTypeUnsafe, INT32* count);

private:
// Helper for CreateInstance
static void CheckElementType(TypeHandle elementType);

// Return values for CanAssignArrayType
enum AssignArrayEnum
{
Expand All @@ -66,6 +61,7 @@ class ArrayNative

};

extern "C" void QCALLTYPE Array_CreateInstance(QCall::TypeHandle pTypeHnd, INT32 rank, INT32* pLengths, INT32* pBounds, QCall::ObjectHandleOnStack retArray);
extern "C" PCODE QCALLTYPE Array_GetElementConstructorEntrypoint(QCall::TypeHandle pArrayTypeHnd);

#endif // _ARRAYNATIVE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ public static Attribute Instantiate(this CustomAttributeData cad)
//
// Convert the argument value reported by Reflection into an actual object.
//
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "The AOT compiler ensures array types required by custom attribute blobs are generated.")]
private static object? Convert(this CustomAttributeTypedArgument typedArgument)
{
Type argumentType = typedArgument.ArgumentType;
Expand All @@ -144,8 +142,7 @@ public static Attribute Instantiate(this CustomAttributeData cad)
IList<CustomAttributeTypedArgument>? typedElements = (IList<CustomAttributeTypedArgument>?)(typedArgument.Value);
if (typedElements == null)
return null;
Type? elementType = argumentType.GetElementType();
Array array = Array.CreateInstance(elementType, typedElements.Count);
Array array = Array.CreateInstanceFromArrayType(argumentType, typedElements.Count);
AlexRadch marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < typedElements.Count; i++)
{
object? elementValue = typedElements[i].Convert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,68 @@ private static unsafe Array InternalCreate(RuntimeType elementType, int rank, in
if (rank == 1)
{
return RuntimeImports.RhNewArray(elementType.MakeArrayType().TypeHandle.ToEETypePtr(), pLengths[0]);

}
else
{
// Create a local copy of the lengths that cannot be motified by the caller
Type arrayType = elementType.MakeArrayType(rank);

// Create a local copy of the lengths that cannot be modified by the caller
int* pImmutableLengths = stackalloc int[rank];
for (int i = 0; i < rank; i++)
pImmutableLengths[i] = pLengths[i];

return NewMultiDimArray(elementType.MakeArrayType(rank).TypeHandle.ToEETypePtr(), pImmutableLengths, rank);
return NewMultiDimArray(arrayType.TypeHandle.ToEETypePtr(), pImmutableLengths, rank);
}
}

#pragma warning disable CA1859 // https://github.com/dotnet/roslyn-analyzers/issues/6451
private static void ValidateElementType(Type elementType)
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "The compiler ensures that if we have a TypeHandle of a Rank-1 MdArray, we also generated the SzArray.")]
jkotas marked this conversation as resolved.
Show resolved Hide resolved
private static unsafe Array InternalCreateFromArrayType(RuntimeType arrayType, int rank, int* pLengths, int* pLowerBounds)
{
while (elementType.IsArray)
Debug.Assert(arrayType.IsArray);
Debug.Assert(arrayType.GetArrayRank() == rank);

ValidateElementType(arrayType.GetElementType());

if (pLowerBounds != null)
{
elementType = elementType.GetElementType()!;
for (int i = 0; i < rank; i++)
{
if (pLowerBounds[i] != 0)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NonZeroLowerBound);
}
}

EETypePtr eeType = arrayType.TypeHandle.ToEETypePtr();
if (rank == 1)
{
// Multidimensional array of rank 1 with 0 lower bounds gets actually allocated
// as an SzArray. SzArray is castable to MdArray rank 1.
if (!eeType.IsSzArray)
eeType = arrayType.GetElementType().MakeArrayType().TypeHandle.ToEETypePtr();

return RuntimeImports.RhNewArray(eeType, pLengths[0]);
}
else
{
// Create a local copy of the lengths that cannot be modified by the caller
int* pImmutableLengths = stackalloc int[rank];
for (int i = 0; i < rank; i++)
pImmutableLengths[i] = pLengths[i];

return NewMultiDimArray(eeType, pImmutableLengths, rank);
}
}

private static void ValidateElementType(Type elementType)
{
if (elementType.IsByRef || elementType.IsByRefLike)
throw new NotSupportedException(SR.NotSupported_ByRefLikeArray);
if (elementType == typeof(void))
throw new NotSupportedException(SR.NotSupported_VoidArray);
if (elementType.ContainsGenericParameters)
throw new NotSupportedException(SR.NotSupported_OpenType);
}
#pragma warning restore CA1859

public unsafe void Initialize()
{
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ FCFuncStart(gArrayFuncs)
FCFuncElement("GetCorElementTypeOfElementType", ArrayNative::GetCorElementTypeOfElementType)
FCFuncElement("IsSimpleCopy", ArrayNative::IsSimpleCopy)
FCFuncElement("CopySlow", ArrayNative::CopySlow)
FCFuncElement("InternalCreate", ArrayNative::CreateInstance)
FCFuncElement("InternalSetValue", ArrayNative::SetValue)
FCFuncEnd()

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ static const Entry s_QCall[] =
DllImportEntry(TypeBuilder_SetConstantValue)
DllImportEntry(TypeBuilder_DefineCustomAttribute)
DllImportEntry(MdUtf8String_EqualsCaseInsensitive)
DllImportEntry(Array_CreateInstance)
DllImportEntry(Array_GetElementConstructorEntrypoint)
DllImportEntry(AssemblyName_InitializeAssemblySpec)
DllImportEntry(AssemblyNative_GetFullName)
Expand Down
Loading
Loading