Skip to content

JIT: Use importer stack type to elide ldelema covariance check - #135074

Closed
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:ldelema-stack-type
Closed

EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:ldelema-stack-type

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 1, 2026

Copy link
Copy Markdown
Member

When ldelema is imported inside a shared generic inlinee (e.g. ref _items[i] in MyList<T>), the array tree only knows __Canon[], so the existing "T[] with exact T" check fails and we emit CORINFO_HELP_LDELEMA_REF. The importer's stack type for that array was resolved in the exact context (Instruction[]), so use it as a fallback.

sealed class Instruction { }

class MyList<T>
{
    private T[] _items;
    public ref T this[int i] => ref _items[i];
}

static ref Instruction Test(MyList<Instruction> list, int i) => ref list[i];
 G_M40277_IG01:
        sub      rsp, 40
 G_M40277_IG02:
-       mov      rcx, gword ptr [rcx+0x08]
-       movsxd   rdx, edx
-       mov      r8, 0x7FFB49FFA3E0      ; Instruction
-       call     [CORINFO_HELP_LDELEMA_REF]
-       nop
+       mov      rax, gword ptr [rcx+0x08]
+       cmp      edx, dword ptr [rax+0x08]
+       jae      SHORT G_M40277_IG04
+       mov      ecx, edx
+       lea      rax, bword ptr [rax+8*rcx+0x10]
 G_M40277_IG03:
        add      rsp, 40
        ret
+G_M40277_IG04:
+       call     CORINFO_HELP_RNGCHKFAIL
+       int3

When ldelema is imported in a shared generic inlinee, the array tree only
knows the __Canon[] type, while the stack type was resolved in the exact
context. Fall back to it so CORINFO_HELP_LDELEMA_REF can be skipped.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 569c0a24-cd54-4744-af08-35c084720ae3
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 1, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@EgorBo

EgorBo commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

@MihuBot

@EgorBo

EgorBo commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

@EgorBo

EgorBo commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

Doesn't look to be worth it

@EgorBo EgorBo closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant