-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Leaf level AOT annotation for CoreLib library #66976
Conversation
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsFirst round of CoreLib (
|
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.
Most of these changes are to files that are not built with NativeAOT's CoreLib (we really only care about NativeAOT's CoreLib).
I wonder if it would be a better strategy to disable the analyzer on CoreCLR's corelib and only run it on NativeAOT's.
@@ -434,6 +434,7 @@ private static AttributeUsageAttribute InternalGetAttributeUsage(Type type) | |||
SR.Format(SR.Format_AttributeUsage, type)); | |||
} | |||
|
|||
[RequiresDynamicCode("The code for an array of the specified type might not be available.")] |
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 can be a suppression - we're creating an instance of a reference type array (System.Attribute
is a reference type and Type
is expected to be deriving from Attribute
or the cast would fail).
Array.CreateInstance
with a reference type element is safe.
@@ -25,6 +26,8 @@ internal static class ComparerHelpers | |||
/// The logic in this method is replicated in vm/compile.cpp to ensure that NGen saves the right instantiations, | |||
/// and in vm/jitinterface.cpp so the jit can model the behavior of this method. | |||
/// </remarks> | |||
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern", |
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 file is not used with NativeAOT - src/coreclr/System.Private.Corelib are files specific to the JIT-based CoreCLR.
It would not be safe - NativeAOT approaches comparers differently and does some work in the compiler to support them.
I don't know how best to approach this - suppressing this runs a risk that if we ever move this code under src/libraries/System.Private.CoreLib and for whatever reason it gets activated under NativeAOT, we'll have a bug.
Maybe the suppression is the best we can do, but it gives me a bad feeling.
As an option to consider, we could also disable the analyzer for CoreCLR's CoreLib.
@@ -55,6 +55,8 @@ public void Bake(ModuleBuilder module, int token) | |||
#region Public Static Methods | |||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", | |||
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")] | |||
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern", | |||
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")] |
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.
(Similar comments apply below too - the mention of trimming doesn't describe the reason well enough)
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")] | |
Justification = "First parameter has to be a TypeBuilder, but it's not possible to obtain a TypeBuilder without getting an AOT warning.")] |
@@ -557,6 +557,7 @@ private static RuntimeType ResolveType(RuntimeModule scope, string typeName) | |||
} | |||
#endregion | |||
|
|||
[RequiresDynamicCode("The code for an array of the specified type might not be available.")] |
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.
Same comment as ComparerHelpers applies - this is not used for NativeAOT and the compiler otherwise ensures custom attribute blobs are constructable.
@@ -1433,6 +1434,7 @@ internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedA | |||
return attributeUsageAttribute ?? AttributeUsageAttribute.Default; | |||
} | |||
|
|||
[RequiresDynamicCode("The code for an array of the specified type might not be available.")] |
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 can be suppressed because the type used in CreateInstance is a reference type (we handle IsValueType with an object array).
@@ -1015,6 +1015,10 @@ private void PopulateLiteralFields(Filter filter, RuntimeType declaringType, ref | |||
Justification = "Calls to GetInterfaces technically require all interfaces on ReflectedType" + | |||
"But this is not a public API to enumerate reflection items, all the public APIs which do that" + | |||
"should be annotated accordingly.")] | |||
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern", |
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 would not be safe (and the justification is off) - this code is not active for NativeAOT. Similar comments to the other place applies.
I really wonder if it would be better to disable the analyzer for CoreCLR's corelib.
@@ -302,6 +302,7 @@ internal static ulong ToUInt64(object value) | |||
enumType.GetEnumUnderlyingType(); | |||
|
|||
#if !CORERT | |||
[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload instead.")] |
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.
The implementation for NativeAOT of this is AOT friendly - we don't want to bubble this out like this because the only form factor that currently cares is safe:
runtime/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.CoreRT.cs
Lines 116 to 122 in b2dc37b
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum | |
{ | |
Array values = GetEnumInfo(typeof(TEnum)).ValuesAsUnderlyingType; | |
TEnum[] result = new TEnum[values.Length]; | |
Array.Copy(values, result, values.Length); | |
return result; | |
} |
Will close this one without merging since the annotations for CoreClr-CoreLib is not as important for the Native Aot form factor |
First round of CoreLib (
coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj
) annotation to get feedback on warning suppression strategy for this level before propagating to all impacted public APIs:ComparerHelpers
as this internal class seems to be hardened for generic instantiations. The document sample code works inNativeAot
mode.TypeBuilder
asReflection.Emit
is not supported inNativeAot
mode and will throw aNotSupportedException
. I want to explore if this option works instead of adding multiple annotations for this class.MemberInfoCache<T>.PopulateInterfaces
to match the trimmer suppression since this is an internal API.IL3051
ones.