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
NativeAOT retains an unused lambda but eliminates an unused interface implementation #137809
Native AOT retains the target method of a delegate that is never invoked. An instance of a single-method interface implementation that is kept alive in the same way has its method trimmed.
GC.KeepAlive is there only so that the delegate and the Factory instance are both constructed. Without it, the unused Factory allocation is eliminated altogether and the comparison no longer holds. The lambda is retained either way; var unused = () => 42; also reproduces.
Observed behavior
Both programs run successfully. In obj\Release\net11.0\win-x64\native\Repro.map.xml:
Version
Object constructed
Target method retained
Lambda
Yes (Func<int>, cached in <>c)
Yes: Repro_Program___c____Main___b__0_0
Interface
Yes: ConstructedEEType ??_7Repro_Factory@@6B@
No: there is no Factory.Get body
Expected behavior
I would expect a delegate that is never invoked to be handled like an interface implementation whose method is never called, so that its target can be trimmed. Is retaining the target an inherent requirement of how delegates are represented, or could Native AOT recognize this case?
changed the title [-]NativeAOT retains uninvoked lambda bodies but trims equivalent interface implementations[/-][+]NativeAOT retains an unused lambda but eliminates an unused interface implementation[/+]on Nov 18, 2026
Trimming works on larger entities than individual instructions. Codegen removes individual instructions because it knows about things such as side effects (moving to codegen area path). Is this the whole pattern? Just an unused delegate creation that is never stored anywhere and never passed to anything?
Oh, wait this is the thing where the C# compiler will actually store the allocated lambda into a static and read it from there.
.method private hidebysig static void '<Main>$'(string[] args) cil managed
{
.entrypoint
// Code size 37 (0x25)
.maxstack 8
IL_0000: ldsfld class [System.Runtime]System.Func`1<int32> Program/'<>c'::'<>9__0_0'
IL_0005: dup
IL_0006: brtrue.s IL_001f
IL_0008: pop
IL_0009: ldsfld class Program/'<>c' Program/'<>c'::'<>9'
IL_000e: ldftn instance int32 Program/'<>c'::'<<Main>$>b__0_0'()
IL_0014: newobj instance void class [System.Runtime]System.Func`1<int32>::.ctor(object,
native int)
IL_0019: dup
IL_001a: stsfld class [System.Runtime]System.Func`1<int32> Program/'<>c'::'<>9__0_0'
IL_001f: call void [System.Runtime]System.GC::KeepAlive(object)
IL_0024: ret
} // end of method Program::'<Main>$'
Yeah, no, this would require very advanced analysis that I don't expect us to have anytime soon. It requires proving that the field that is being written here is only ever read in this same spot, so this is not just an allocation+delegate creation, but also a dead store from codegen perspective.
If it would help, Roslyn could skip the caching when the lambda is surrounded by a new, matching what it does with method groups. Right now both lambda cases do cache:
usingSystem;// Uses cached delegateGC.KeepAlive(()=>42);GC.KeepAlive(newFunc<int>(()=>42));GC.KeepAlive(F);// Does not use cached delegateGC.KeepAlive(newFunc<int>(F));staticintF()=>42;
Before considering more advanced analysis, it sounds easier to treat delegates as micro-interfaces with an Invoke method, and trim all methods referenced by delegates of type say D, if no instance of D gets invoked (or its Method property called). This won't be very helpful with Func and Action delegates of common types, but for #137566 we would be able to trim the reflection code if no Func<IAttributeProvider> gets invoked anywhere in the app.
Oh, wait this is the thing where the C# compiler will actually store the allocated lambda into a static and read it from there.
.method private hidebysig static void '<Main>$'(string[] args) cil managed
{
.entrypoint
// Code size 37 (0x25)
.maxstack 8
IL_0000: ldsfld class [System.Runtime]System.Func`1<int32> Program/'<>c'::'<>9__0_0'
IL_0005: dup
IL_0006: brtrue.s IL_001f
IL_0008: pop
IL_0009: ldsfld class Program/'<>c' Program/'<>c'::'<>9'
IL_000e: ldftn instance int32 Program/'<>c'::'<<Main>$>b__0_0'()
IL_0014: newobj instance void class [System.Runtime]System.Func`1<int32>::.ctor(object,
native int)
IL_0019: dup
IL_001a: stsfld class [System.Runtime]System.Func`1<int32> Program/'<>c'::'<>9__0_0'
IL_001f: call void [System.Runtime]System.GC::KeepAlive(object)
IL_0024: ret
} // end of method Program::'<Main>$'
Yeah, no, this would require very advanced analysis that I don't expect us to have anytime soon. It requires proving that the field that is being written here is only ever read in this same spot, so this is not just an allocation+delegate creation, but also a dead store from codegen perspective.
It would probably get trivially removed for the use case in the top post (the delegate is not actually used for anything).
It would still not be enough if the delegate is actually stored somewhere, returned from a method, passed as a parameter, etc. That would need yet another piece of analysis to solve global data flow (and it would need to be a pattern that is actually statically solvable, not some halting problem kind of thing). Trimming only looks at control flow, not at data flow.
Before considering more advanced analysis, it sounds easier to treat delegates as micro-interfaces with an Invoke method, and trim all methods referenced by delegates of type say D, if no instance of D gets invoked (or its Method property called). This won't be very helpful with Func and Action delegates of common types, but for #137566 we would be able to trim the reflection code if no Func<IAttributeProvider> gets invoked anywhere in the app.
In theory yes, but all of this breaks down the moment someone calls Delegate.Method, or Delegate.DynamicInvoke or any of the other ways how the target of the delegate could matter. And we still cannot pretend the delegate is simply null, so we still need to allocate something.
Activity
dotnet-policy-service commentedon Nov 18, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
[-]NativeAOT retains uninvoked lambda bodies but trims equivalent interface implementations[/-][+]NativeAOT retains an unused lambda but eliminates an unused interface implementation[/+]MichalStrehovsky commentedon Nov 18, 2026
Trimming works on larger entities than individual instructions. Codegen removes individual instructions because it knows about things such as side effects (moving to codegen area path). Is this the whole pattern? Just an unused delegate creation that is never stored anywhere and never passed to anything?
dotnet-policy-service commentedon Nov 18, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Nov 18, 2026
Oh, wait this is the thing where the C# compiler will actually store the allocated lambda into a static and read it from there.
Yeah, no, this would require very advanced analysis that I don't expect us to have anytime soon. It requires proving that the field that is being written here is only ever read in this same spot, so this is not just an allocation+delegate creation, but also a dead store from codegen perspective.
teo-tsirpanis commentedon Nov 18, 2026
If it would help, Roslyn could skip the caching when the lambda is surrounded by a
new, matching what it does with method groups. Right now both lambda cases do cache:eiriktsarpalis commentedon Nov 18, 2026
If that ends up helping the linker, I could also do it manually in my generated code.
teo-tsirpanis commentedon Nov 18, 2026
Before considering more advanced analysis, it sounds easier to treat delegates as micro-interfaces with an
Invokemethod, and trim all methods referenced by delegates of type sayD, if no instance ofDgets invoked (or itsMethodproperty called). This won't be very helpful withFuncandActiondelegates of common types, but for #137566 we would be able to trim the reflection code if noFunc<IAttributeProvider>gets invoked anywhere in the app.MichalPetryka commentedon Nov 18, 2026
Wouldn't #85014 make this much easier?
MichalStrehovsky commentedon Nov 19, 2026
It would probably get trivially removed for the use case in the top post (the delegate is not actually used for anything).
It would still not be enough if the delegate is actually stored somewhere, returned from a method, passed as a parameter, etc. That would need yet another piece of analysis to solve global data flow (and it would need to be a pattern that is actually statically solvable, not some halting problem kind of thing). Trimming only looks at control flow, not at data flow.
MichalStrehovsky commentedon Nov 19, 2026
In theory yes, but all of this breaks down the moment someone calls Delegate.Method, or Delegate.DynamicInvoke or any of the other ways how the target of the delegate could matter. And we still cannot pretend the delegate is simply null, so we still need to allocate something.
JulieLeeMSFT commentedon Nov 25, 2026
cc @dotnet/jit-contrib.