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

Leaf level AOT annotation for CoreLib library #66976

Closed
wants to merge 1 commit into from

Conversation

LakshanF
Copy link
Contributor

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:

  1. Suppressed warnings from ComparerHelpers as this internal class seems to be hardened for generic instantiations. The document sample code works in NativeAot mode.
  2. Suppressed warnings from TypeBuilder as Reflection.Emit is not supported in NativeAot mode and will throw a NotSupportedException. I want to explore if this option works instead of adding multiple annotations for this class.
  3. Suppressed warnings from MemberInfoCache<T>.PopulateInterfaces to match the trimmer suppression since this is an internal API.
  4. All other leaf level warnings are propagated including IL3051 ones.

@ghost
Copy link

ghost commented Mar 22, 2022

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details

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:

  1. Suppressed warnings from ComparerHelpers as this internal class seems to be hardened for generic instantiations. The document sample code works in NativeAot mode.
  2. Suppressed warnings from TypeBuilder as Reflection.Emit is not supported in NativeAot mode and will throw a NotSupportedException. I want to explore if this option works instead of adding multiple annotations for this class.
  3. Suppressed warnings from MemberInfoCache<T>.PopulateInterfaces to match the trimmer suppression since this is an internal API.
  4. All other leaf level warnings are propagated including IL3051 ones.
Author: LakshanF
Assignees: LakshanF
Labels:

area-System.Reflection

Milestone: -

@LakshanF LakshanF marked this pull request as draft March 22, 2022 05:23
Copy link
Member

@MichalStrehovsky MichalStrehovsky left a 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.")]
Copy link
Member

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",
Copy link
Member

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")]
Copy link
Member

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)

Suggested change
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.")]
Copy link
Member

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.")]
Copy link
Member

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",
Copy link
Member

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.")]
Copy link
Member

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:

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;
}

@LakshanF
Copy link
Contributor Author

Will close this one without merging since the annotations for CoreClr-CoreLib is not as important for the Native Aot form factor

@LakshanF LakshanF closed this Mar 31, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 30, 2022
@LakshanF LakshanF deleted the NativeAOTCoreLibAnnotation branch May 10, 2022 13:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants