diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs index 8197802b44b76..1340d8669ca08 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs @@ -527,6 +527,19 @@ public static bool HandleCall( } break; + // + // System.Array + // + // CreateInstance (Type, Int32) + // + case IntrinsicId.Array_CreateInstance: + { + // We could try to analyze if the type is known, but for now making sure this works for canonical arrays is enough. + TypeDesc canonArrayType = reflectionMarker.Factory.TypeSystemContext.CanonType.MakeArrayType(); + reflectionMarker.Dependencies.Add(reflectionMarker.Factory.NativeLayout.TemplateTypeLayout(canonArrayType), "Array.CreateInstance was called"); + goto case IntrinsicId.None; + } + // // System.Enum // diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/IntrinsicId.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/IntrinsicId.cs index a9d3d4e83dd46..9a8e2e343d79c 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/IntrinsicId.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/IntrinsicId.cs @@ -54,6 +54,10 @@ internal enum IntrinsicId // the reflection body scanner. RequiresReflectionBodyScanner_Sentinel = 1000, /// + /// + /// + Array_CreateInstance, + /// /// /// Type_MakeGenericType, diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/Intrinsics.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/Intrinsics.cs index 72378937ec80b..bdc94c11195c9 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/Intrinsics.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/Intrinsics.cs @@ -293,6 +293,12 @@ public static IntrinsicId GetIntrinsicIdForMethod (MethodProxy calledMethod) "Empty" when calledMethod.IsDeclaredOnType ("System.Array") => IntrinsicId.Array_Empty, + // static System.Array.CreateInstance (System.Type type, int length) + "CreateInstance" when calledMethod.IsDeclaredOnType ("System.Array") + && calledMethod.HasMetadataParametersCount (2) + && calledMethod.HasParameterOfType ((ParameterIndex) 1, "System.Int32") + => IntrinsicId.Array_CreateInstance, + // static System.Activator.CreateInstance (System.Type type) // static System.Activator.CreateInstance (System.Type type, bool nonPublic) // static System.Activator.CreateInstance (System.Type type, params object?[]? args) diff --git a/src/tools/illink/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs b/src/tools/illink/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs index 2e3fbc4b0cef5..72e9339bc3bc3 100644 --- a/src/tools/illink/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs +++ b/src/tools/illink/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs @@ -263,6 +263,7 @@ public static bool HandleCall ( } break; + case IntrinsicId.Array_CreateInstance: case IntrinsicId.Enum_GetValues: case IntrinsicId.Marshal_SizeOf: case IntrinsicId.Marshal_OffsetOf: