Add AssociatedSourceType for conditional unmanaged exports#128180
Add AssociatedSourceType for conditional unmanaged exports#128180Copilot wants to merge 9 commits into
Conversation
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1ab9107a-9310-4fa2-8342-70d882ff3d8e Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1ab9107a-9310-4fa2-8342-70d882ff3d8e Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1ab9107a-9310-4fa2-8342-70d882ff3d8e Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d8e7b00e-4ed9-4e1b-a82b-817269ef4eaf Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d8e7b00e-4ed9-4e1b-a82b-817269ef4eaf Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
| /// * Must only have <see href="https://learn.microsoft.com/dotnet/framework/interop/blittable-and-non-blittable-types">blittable</see> arguments. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Method, Inherited = false)] | ||
| public sealed class UnmanagedCallersOnlyAttribute : Attribute |
There was a problem hiding this comment.
Why does this definition of UnmanagedCallersOnlyAttribute exist? Shouldn't this just use the one in SPCL via a source include or something?
There was a problem hiding this comment.
We could, but this rarely changes and Runtime.Base is supposed to be low overhead and ceremony (i.e. people shouldn't have to deal with Runtime.Base build breaks if they modify a file in CoreLib). E.g. we also define the CallConvs types in this file, the ones that we needed.
This addition is unnecessary. @copilot revert changes in this file
There was a problem hiding this comment.
Reverted the unnecessary Runtime.Base attribute copy change in 3494d17.
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/64e55336-9d89-4a8e-b32c-0b3b445781a1 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/03d7e02a-55b5-4666-b1e8-ce6d2bb62b46 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
| { | ||
| foreach (EcmaMethod method in methods) | ||
| { | ||
| if (compilationResults.IsMethodBodyCompiled(method)) |
There was a problem hiding this comment.
| if (compilationResults.IsMethodBodyCompiled(method)) | |
| // Some of the exported methods could be conditioned with UnmanagedCallersOnly.AssociatedSourceType | |
| // so need to filter down to compiled methods. | |
| if (compilationResults.IsMethodBodyCompiled(method)) |
| [UnmanagedCallersOnly(EntryPoint = UnusedSourceExport, AssociatedSourceType = typeof(UnusedSource))] | ||
| private static int UnusedSourceEntryPoint() => 3; |
There was a problem hiding this comment.
Add one more test that uses AssociatedSourceType = typeof(UnusedSource[]) (array of the type). Since the element type is never referenced, that one should not be generated either.
Then add one more test that uses AssociatedSourceType = typeof(UsedAsElementOnly[]) and make sure we use that with a public static object CreateArray<T>() => new T[0]; that is reached through MakeGenericMethod. The associated export should be generated because we constructed the array through a template at runtime.
| [UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", | ||
| Justification = "The test verifies runtime creation of the generic type through a type loader template.")] |
There was a problem hiding this comment.
You can eliminate this warning by constraining the T in methods used with MakeGeneric to be a class. Example: public static object CreateDynamicSource<T>() where T : class => new DynamicSource<T>();. No warnings are generated for these because we can construct the method at runtime.
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/fea7fc44-9eef-4a72-b701-3e70c6fd0262 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
UnmanagedCallersOnlyAttributeexports withEntryPointwere always rooted and emitted. This adds an associated source type so NativeAOT can keep/export generated native entrypoints only when the source type survives trimming.UnmanagedCallersOnlyAttribute.AssociatedSourceTypeto implementation and ref assemblies.NativeAOT rooting
UnmanagedEntryPointsNode.AssociatedSourceType.Shared trim-target dependency handling
RuntimeConstructableTypeDependencies.Export generation
Coverage
TrimmingBehaviorscoverage for unconditional, kept associated, trimmed associated, unused array associated-source, dynamically loadable generic associated-source, dynamically loadable array associated-source, and array associated-source constructed through a runtime generic array template.