You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ilc: Add the ability to specify whether UnmanagedCallersOnly methods with entry points should be treated as roots or not #127575
Currently, ILC will root [UnmanagedCallersOnly] methods that have an entry point if building a library.
This generally makes sense if you're shipping a library, but for mobile scenarios (ios/android), even if we ask ILC to build a library, we really end up building an executable, so that reasoning doesn't quite apply to us.
Additionally, we generate managed code that will be called from native code, and we need that managed code to be trimmable. As such, we have workarounds for ILC rooting such [UnmanagedCallersOnly] methods by generating UCO methods without an entry point, and then we have a separate managed function pointer lookup method which is trimmable (and thus also the UCO method is trimmable).
This is rather wasteful, we'd like to call the UCO method directly from native code.
Ideas:
Add an option to ILC to never root UCO methods. If we find out that this trims away too much, we can use other means to manually root them if needed (xml files, DynamicDependency attributes, etc.)
Add an option to ILC that specifies a file with a list of native entry points whose UCO methods should not be treated as roots.
This is probably overkill, but combine the two, and possibly make them complimentary: an option to never/always root UCO methods + an option to specify a list of native entry points that should/should not be rooted. That way it's possible to say both "root everything, except X, Y and Z", and the opposite "root nothing, except A, B and C."
Ah, I see. Looks like this is one of the aspects we discussed in the past over at #80912.
The thing that concerns me with the proposed additional UCO controls is that these just support the existing architecture where there is a whole program analysis step before ILC runs that figures out things that should/should not be generated and then dictates what ILC should do.
ILC can typically do a better job at trimming by the nature of its output (output of IL trimming needs to be valid IL, output of ILC is machine code, GC/EH metadata, and reflection metadata - all of which can be pretty disconnected; one can have a method body without knowing its name or signature; one can even have instructions from a method body without having the method body (method that was inlined everywhere)).
I wonder if we could introduce a more declarative mechanism for this - something like what we did with TypeMap. Just throwing a suggestion:
classMyObjectProxy{// TrimTarget tells analysis to only generate force the UCO entrypoint if a MyObjectProxy instance could exist in the program.[UnmanagedCallersOnly(EntryPoint="MyObjectProxy_MyMethod",TrimTarget=typeof(MyObjectProxy)]staticvoidMyMethod(IntPtrhandle){}}
I wonder if we could introduce a more declarative mechanism for this - something like what we did with TypeMap. Just throwing a suggestion:
classMyObjectProxy{// TrimTarget tells analysis to only generate force the UCO entrypoint if a MyObjectProxy instance could exist in the program.[UnmanagedCallersOnly(EntryPoint="MyObjectProxy_MyMethod",TrimTarget=typeof(MyObjectProxy)]staticvoidMyMethod(IntPtrhandle){}}
I like this approach a lot!
I believe this would work for the Apple workloads, but I'm not sure about Android (@simonrozsival ?)
That said, it seems slightly inflexible (what if we'd want TrimTarget to be a method?)
We won't be using UCOs with EntryPoint in Android, we need to set up the Java native callbacks via function pointers anyway.
The TrimTarget attribute property looks like exactly what iOS would need. In the iOS case, I don't think the value will ever be anything other than the declaring type, so a simple boolean would be enough and it might be simpler to implement. OTOH TrimTarget gives more flexibility to other users of this API and I believe neither will have any size impact on the final binary because ILC will throw this metadata out anyway.
The TrimTarget shouldn't add much extra complexity to the implementation so I'm not too worried.
I'll Cc a couple more folks for feedback but if this would work for the necessary scenarios I don't see any blockers. Architecturally it fits the long term goals (be able to write interop layers without deep integration with the compilation pipeline or the runtime).
We'll want to define what the TrimTarget means (same rules as for TypeMap or more restricted to "one can have an actual instance of this at runtime" - feels like we can do the latter first and if really needed, extend). We can decide if this should be a property on UnmanagedCallersOnly or an extra attribute (I prefer the extra property).
In WinRT, for the scenario talked about here, we use UnmanagedCallersOnly without an entry point as we use those functions to setup a virtual function table for the interface and then class which is then able to be called by native code. The scenarios where we do use entry point is where we export the activation factory for the DLL exposing those function tables which we wouldn't want trimmed and probably won't use TrimTarget there as it is called by native code and we don't know whether they will call it or not.
In WinRT, for the scenario talked about here, we use UnmanagedCallersOnly without an entry point as we use those functions to setup a virtual function table for the interface and then class which is then able to be called by native code. The scenarios where we do use entry point is where we export the activation factory for the DLL exposing those function tables which we wouldn't want trimmed and probably won't use TrimTarget there as it is called by native code and we don't know whether they will call it or not.
Yeah, I didn't really expect WinRT would have immediate use for this - mostly cc'd you FYI. This is mostly for interops that need to generate subsequent native glue. WinRT has fully laid out vtables readily available in the image and doesn't require further metadata that would be difficult to emit.
Activity
dotnet-policy-service commentedon Apr 29, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Apr 29, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
MichalStrehovsky commentedon Apr 29, 2026
The list of assemblies the compiler looks at for UCO with entrypoints is controlled with
UnmanagedEntryPointsAssemblyItemGroup.Who computes the list of UCO methods that are needed and based on what?
rolfbjarne commentedon Apr 30, 2026
Right now we add every assembly to the
UnmanagedEntryPointsAssemblyitem group: https://github.com/dotnet/macios/blob/685053919f37d5d07604bdb4a3071ecbffa0244d/dotnet/targets/Xamarin.Shared.Sdk.targets#L1544-L1547We have an issue to see if we can shrink this list: dotnet/macios#18484
We (the iOS workload) compute/generate a number of UCO methods. Application/user code is unlikely to have any, but it's not technically impossible.
Given this C# class:
The managed
MyMethodis exported to Objective-C using themyMethodselector.Meaning that it's possible to do this in Objective-C:
and it should call the managed
MyMethodmethod.We solve this by generating a proxy class with an
[UnmanagedCallersOnly]method (lots of details omitted here, but this is the gist of it):Then we generate an Objective-C version of the
MyObjectclass, where themyMethodselector calls theMyObjectProxy_MyMethodUCO method.Now this solution isn't trimmable, so our workaround is to do something like this:
and then, in Objective-C, we lookup the
MyMethodfunction pointer instead of calling the UCO method directly.MichalStrehovsky commentedon Apr 30, 2026
Ah, I see. Looks like this is one of the aspects we discussed in the past over at #80912.
The thing that concerns me with the proposed additional UCO controls is that these just support the existing architecture where there is a whole program analysis step before ILC runs that figures out things that should/should not be generated and then dictates what ILC should do.
ILC can typically do a better job at trimming by the nature of its output (output of IL trimming needs to be valid IL, output of ILC is machine code, GC/EH metadata, and reflection metadata - all of which can be pretty disconnected; one can have a method body without knowing its name or signature; one can even have instructions from a method body without having the method body (method that was inlined everywhere)).
I wonder if we could introduce a more declarative mechanism for this - something like what we did with
TypeMap. Just throwing a suggestion:rolfbjarne commentedon Apr 30, 2026
I like this approach a lot!
I believe this would work for the Apple workloads, but I'm not sure about Android (@simonrozsival ?)
That said, it seems slightly inflexible (what if we'd want
TrimTargetto be a method?)Maybe something like this:
which is more flexible, but runs into other problems (currently
[DynamicDependency]can't be put on a type)simonrozsival commentedon Apr 30, 2026
We won't be using UCOs with EntryPoint in Android, we need to set up the Java
nativecallbacks via function pointers anyway.The
TrimTargetattribute property looks like exactly what iOS would need. In the iOS case, I don't think the value will ever be anything other than the declaring type, so a simple boolean would be enough and it might be simpler to implement. OTOHTrimTargetgives more flexibility to other users of this API and I believe neither will have any size impact on the final binary because ILC will throw this metadata out anyway.MichalStrehovsky commentedon Apr 30, 2026
The TrimTarget shouldn't add much extra complexity to the implementation so I'm not too worried.
I'll Cc a couple more folks for feedback but if this would work for the necessary scenarios I don't see any blockers. Architecturally it fits the long term goals (be able to write interop layers without deep integration with the compilation pipeline or the runtime).
We'll want to define what the TrimTarget means (same rules as for TypeMap or more restricted to "one can have an actual instance of this at runtime" - feels like we can do the latter first and if really needed, extend). We can decide if this should be a property on UnmanagedCallersOnly or an extra attribute (I prefer the extra property).
Cc @dotnet/interop-contrib @Sergio0694 @manodasanW
MichalStrehovsky commentedon Apr 30, 2026
Would it currently be a blocker if we didn't have this?
rolfbjarne commentedon Apr 30, 2026
No, it wouldn't.
manodasanW commentedon Apr 30, 2026
In WinRT, for the scenario talked about here, we use UnmanagedCallersOnly without an entry point as we use those functions to setup a virtual function table for the interface and then class which is then able to be called by native code. The scenarios where we do use entry point is where we export the activation factory for the DLL exposing those function tables which we wouldn't want trimmed and probably won't use TrimTarget there as it is called by native code and we don't know whether they will call it or not.
MichalStrehovsky commentedon May 1, 2026
Filed #127634 with the public API proposal. Please let me know what you think. If you don't have any comments, a thumbs up is also feedback!
MichalStrehovsky commentedon May 1, 2026
Yeah, I didn't really expect WinRT would have immediate use for this - mostly cc'd you FYI. This is mostly for interops that need to generate subsequent native glue. WinRT has fully laid out vtables readily available in the image and doesn't require further metadata that would be difficult to emit.
MichalStrehovsky commentedon May 1, 2026
Also cc @dotnet/illink since this affects trimming (even though IL trimming is currently unaware of
UnmanagedCallersOnly(EntryPoint)right now).MichalStrehovsky commentedon Jun 11, 2026
#127634 should address the scenario.