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

Add AOT annotations to System.Private.Xml and System.Data.Common #109528

Merged
merged 11 commits into from
Nov 13, 2024

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Nov 5, 2024

Contributes to #75480

Copy link
Contributor

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

DAM on TypeDesc would require bubbling up RDC to parts of
TypeScope (AddPrimitive of byte[]) that was unnecessarily viral.
@sbomer sbomer changed the title Add AOT annotations to System.Private.Xml Add AOT annotations to System.Private.Xml and System.Data.Common Nov 6, 2024
@sbomer sbomer requested a review from eerhardt November 6, 2024 23:20
@sbomer sbomer marked this pull request as ready for review November 6, 2024 23:20
@sbomer sbomer requested a review from a team November 6, 2024 23:20
@@ -429,6 +429,7 @@ internal void NewArray(Type elementType, object len)
_ilGen!.Emit(OpCodes.Newarr, elementType);
}

[RequiresDynamicCode(XmlSerializer.AotSerializationWarning)]
Copy link
Member

Choose a reason for hiding this comment

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

I would think this whole class needs to be annotated with [RequiresDynamicCode].

Copy link
Member Author

Choose a reason for hiding this comment

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

The only places that popped up were CreateAssemblyBuilder (because it calls DefineDynamicAssembly, and StackallocSpan (because it calls MakeGenericType).

The rest of the code in this class seems OK because it's built on abstractions like TypeBuilder that don't inherently have problems with AOT (but it won't be possible to create a type builder for a dynamic assembly).

That said, we could add annotations to the class to signal that it's not intended to be used without dynamic code support. @MichalStrehovsky curious if you have any input on this.

Copy link
Member

@eerhardt eerhardt Nov 8, 2024

Choose a reason for hiding this comment

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

Is it correct that ILGenerator isn't marked as RequiresDynamicCode?

namespace System.Reflection.Emit
{
public abstract class ILGenerator

Is the thinking that you can generate IL all you want in a native AOT application? You just can't execute it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's my thinking.

Copy link
Member

Choose a reason for hiding this comment

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

Is it correct that ILGenerator isn't marked as RequiresDynamicCode?

AFAIK (didn't check) one could use ILGenerator with the new "Reflection.Emit SaveToDisk" APIs - those don't actually produce IL that is executable within the current process (unless one then loads the assembly from disk). ILGenerator by itself doesn't seem harmful. It's mostly about those that generate executable code, like the one obtained from a DynamicMethod.

@@ -87,7 +87,6 @@ internal sealed class TypeDesc
private TypeDesc? _nullableTypeDesc;
private readonly TypeKind _kind;
private readonly XmlSchemaType? _dataType;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
Copy link
Member

Choose a reason for hiding this comment

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

Why remove these annotations? Typically we try to keep them if they "work" so if new code starts calling the methods it is annotated correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

The DAMT.All on AddSoapEncodedPrimitive was producing lots of warnings for the call that passes in a byte[], like:

AOT analysis warning IL3050: System.Xml.Serialization.TypeScope.AddSoapEncodedTypes(String): Using member 'System.Array.InternalCreate(RuntimeType,Int32,Int32*,Int32*)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available.

I don't think the mentioned Array methods would be used on this code path, but I didn't feel totally comfortable suppressing them, because the type gets passed into TypeDesc that might be returned from (for example) TypeScope.GetTypeDesc - and from there it was hard to verify that the type didn't leak out.

I tried annotating TypeScope with RequiresDynamicCode instead, but this becomes more viral, so I thought it better to remove the DAMT annotations (I'm not sure what value they were adding in the first place).

Copy link
Member

Choose a reason for hiding this comment

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

Would the newly added DAMT annotations be better for this (once I get that merged)? (I don't have an opinion about keeping the annotations or removing or updating it since I don't know what any of this is for - just bringing up one more option).

Copy link
Member Author

Choose a reason for hiding this comment

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

It's possible that All wasn't necessary - but the new options still don't help with the warnings from AddSoapEncodedPrimitive. It looks like all of the code paths where the the annotations I removed were relevant were only reachable in the first place from scopes already guarded by RUC, so my preference is to remove them.

- Fix whitespace
- Suppress warning for ToArray of known reference type
- RequiresUnreferencedCode -> RequiresDynamicCode
@sbomer sbomer requested a review from eerhardt November 12, 2024 23:35
Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for cleaning up the code where we could.

@sbomer sbomer merged commit 756f7eb into dotnet:main Nov 13, 2024
81 of 83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants