Repository navigation
ILC: only keep delegate Invoke methods reflectable when they can be looked up on unknown delegate types - #138052
Conversation
…ooked up on unknown delegate types Every delegate type with metadata had its Invoke method made reflectable, because the reflection stack and some libraries look up Invoke on delegate types that are not statically known (Delegate.DynamicInvoke, Delegate/MethodInfo.CreateDelegate, Delegate.Method on marshalled function pointer and LINQ expression delegates, System.Linq.Expressions, EventHandlerType). All of these look up "Invoke" by name on a type that is not statically known. Reflection dataflow now marks a node when it sees such a lookup, or GetType on System.Delegate (which is modeled as typeof(Delegate)), and the Invoke methods are only made reflectable when that node is present. When dataflow does not see the whole program or metadata is not trimmed, the previous unconditional behavior is kept. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
|
This may break people who had a suppression for this. For example if the way they structured this doesn't let us see This will potentially be a support call. If as a result of this support call we'll say "okay, rewrite your pattern to this pattern and suppress the warning again, we pinky promise we'll not break this again", people will not be happy. I think we'll need to pair this with a trim safe API to get delegate Invoke method so that we can say "use this new API that is trim safe". I might have more opinion on all this when rt-sz result for this are in. |
|
I'm starting to feel perhaps those rt-sz scenarios might not be representative of a real world complex app. I find it a bit suspicious that the largest saving across those test projects is ~5 KB, yet this saves over 850 KB in the Store 🤔 Makes me wonder what we might've disregarded before because rt-sz didn't show enough savings in those samples. |
|
Does CsWinRT somehow end up with lots of delegates? Or some other pattern that is heavy on delegates in the Store app? |
This shows no savings for any app where the savings could be meaningful (ASP.NET or Avalonia) because they probably trigger the "I don't know what |
|
Ok so I looked into both the Store and those samples, it makes a lot more sense now why this saves so much for us. StoreThis change removes a total of 1263 delegate
With this change, 374 keys are dropped from the type map, which drops 253 KB just from rt-sz samplesLike you said, the fix isn't kicking in at all there. There's two reasons why:
If you just comment those calls out to get an approximate number, TodosApi saves 148,480 B (0.58%) with this fix. Opus suggested these two possible fixes for them:
|
NativeAOT makes the
Invokemethod of every delegate type with metadata reflectable ("Delegate invoke method is always reflectable", #72077), which keeps its metadata, reflection invoke stub and body for every delegate in the app. The rule exists because the reflection stack and some libraries look upInvokeon delegate types that are not statically known:Delegate.DynamicInvoke,Delegate.CreateDelegate,MethodInfo.CreateDelegate,Delegate.Methodon marshalled function pointer and LINQ expression delegates, System.Linq.Expressions, andEventInfo.EventHandlerType.GetMethod("Invoke").All of these look up
"Invoke"by name on a type that dataflow doesn't know statically. This adds aDelegateInvokeReflectedOnUnknownTypeNode, marked by reflection dataflow when:Type.GetMethod,Type.GetMemberorRuntimeReflectionExtensions.GetRuntimeMethodis called with"Invoke"on a type that is not statically known, orGetType()is called on a value of typeSystem.Delegate, which dataflow models astypeof(Delegate).Delegate
Invokemethods are now made reflectable only when that node is present. When dataflow doesn't see the whole program (no reflection scanning, multi-file builds), or withIlcTrimMetadata=false, the previous unconditional behavior is kept. Lookups on statically known delegate types are already handled precisely by the existing analysis, and other reflection on unknown types (e.g.GetMethods()and filtering by name) already produces trim warnings.Size impact
Microsoft Store (CsWinRT 3.0), same inputs, ILC with #137541, with and without this change,
OptimizationPreference=Size:WinStore.App.dllThe MSIX column is the image compressed at zlib level 6, as a proxy for its size in the package. It is not a measured MSIX build. With
OptimizationPreference=Speedthe saving is 887,296 bytes. All 141 delegateInvokemethod metadata records are removed.Testing
SmokeTests/DelegateInvoke, with one variant usingDynamicInvokeand one usingGetType().GetMethod("Invoke")on aDelegate. Each fails if its trigger is removed from the compiler.AttributeTrimmingthat a delegate that is only invoked doesn't have a reflectableInvoke.src/testsNativeAOT run was not done locally.Part of #137567.
Note
This PR description and the change were prepared with GitHub Copilot.