Skip to content

NativeAOT delegate creation helper call blocks Escape Analysis #110847

@MichalPetryka

Description

@MichalPetryka

Description

While working on #109679 I've noticed that NativeAOT is unable to stack allocate delegate instances since it emits a call to a helper for the constructor which marks the variable as escaped.

cc @AndyAyersMS what would be the preferred solution for you here?

Configuration

n/a

Regression?

No.

Data

		public static void Alloc()
		{
			int a = 1;
			Unused(() => a);

			void Unused(Func<int> f) { }
		}

NativeAOT:

G_M6383_IG01:  ;; offset=0x0000
       push     rbx
       sub      rsp, 32
						;; size=5 bbWeight=1 PerfScore 1.25
G_M6383_IG02:  ;; offset=0x0005
       lea      rcx, [(reloc 0x40000000004203d0)]      ; DisasmoPlayground.Disasms.DelegateDisasm+<>c__DisplayClass3_0
       call     CORINFO_HELP_NEWSFAST
       mov      rbx, rax
       mov      dword ptr [rbx+0x08], 1
       lea      rcx, [(reloc 0x40000000004203f8)]      ; System.Func`1[int]
       call     CORINFO_HELP_NEWSFAST
       mov      rcx, rax
       mov      rdx, rbx
       call     CORINFO_HELP_READYTORUN_DELEGATE_CTOR
       nop      
						;; size=46 bbWeight=1 PerfScore 6.00
G_M6383_IG03:  ;; offset=0x0033
       add      rsp, 32
       pop      rbx
       ret      
						;; size=6 bbWeight=1 PerfScore 1.75

CoreCLR:

G_M6383_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25
G_M6383_IG02:  ;; offset=0x0004
       mov      rcx, 0x7FF8358C0820      ; DisasmoPlayground.Disasms.DelegateDisasm+<>c__DisplayClass3_0
       call     CORINFO_HELP_NEWSFAST
       mov      dword ptr [rax+0x08], 1
						;; size=22 bbWeight=1 PerfScore 2.25
G_M6383_IG03:  ;; offset=0x001A
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25

Analysis

The helper could either be special cased for escape analysis, NativeAOT could be migrated to work similar to CoreCLR or we could get a JIT intrinsic directly setting the delegate fields for known cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions