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

ILC fails to detect and trim some dependency cycles (interface -> attribute -> [DAM] -> type impl -> new()) #112029

Open
Sergio0694 opened this issue Jan 31, 2025 · 1 comment
Labels
area-NativeAOT-coreclr partner-impact This issue impacts a partner who needs to be kept updated trimming-for-aot `EnableAggressiveTrimming=true` used for running tests with AOT untriaged New issue has not been triaged by the area owner

Comments

@Sergio0694
Copy link
Contributor

Sergio0694 commented Jan 31, 2025

Description

Found a possible miss by ILC when working on microsoft/CsWinRT#1907. This causes IReferenceArray<T> to be kept, which then produces a bunch of IL3050 warnings when I try to publish the Microsoft Store with a preview of CsWinRT from that PR. We can rework IReferenceArray<T> to be more AOT friendly, but this feels also feels like something that ILC should be able to handle.

cc. @MichalStrehovsky @sbomer

Reproduction Steps

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace NaotTrimmingDamTest
{
    internal class Program
    {
        static void Main()
        {
            Console.WriteLine(M(new object()));
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        public static bool M(object obj)
        {
            Type type = obj.GetType();

            return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IFoo<>);
        }
    }

    [Keep(typeof(ABI.NaotTrimmingDamTest.IFoo<>))]
    internal interface IFoo<T>
    {
    }

    public sealed class KeepAttribute : Attribute
    {
        public KeepAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
        {
        }
    }
}

namespace ABI.NaotTrimmingDamTest
{
    internal sealed class IFoo<T> : global::NaotTrimmingDamTest.IFoo<T>
    {
        public static object Create()
        {
            return new IFoo<T>();
        }
    }
}

Expected behavior

  • All IFoo<T> types should be completely trimmed.
  • I also expect M to be optimized to just return false, basically.
  • KeepAttribute should also be removed.

There is no type that can possibly be instantiated, that implements IFoo<T>.

Actual behavior

Image

Both IFoo<T> types and [Keep] are kept. Not sure if M is being optimized (don't see the codegen from here).

Additional context

I tried changing the Create method to this:

public static object Create()
{
    if (!RuntimeFeature.IsDynamicCodeCompiled)
    {
        throw new NotSupportedException("");
    }

    return new IFoo<T>();
}

Thinking maybe if this just throws on AOT, ILC will see the type never being constructed and will trim. Nope, no changes.

Regression?

No.

Configuration

  • .NET 9.0.102
@Sergio0694 Sergio0694 added area-NativeAOT-coreclr partner-impact This issue impacts a partner who needs to be kept updated trimming-for-aot `EnableAggressiveTrimming=true` used for running tests with AOT labels Jan 31, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 31, 2025
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr partner-impact This issue impacts a partner who needs to be kept updated trimming-for-aot `EnableAggressiveTrimming=true` used for running tests with AOT untriaged New issue has not been triaged by the area owner
Projects
Status: No status
Development

No branches or pull requests

1 participant