Skip to content

JIT: Improve cast helper fallbacks - #133324

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-cast-helper-fallbacks
Sep 9, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-cast-helper-fallbacks

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Optimize fallbacks for cast expansions, e.g. Fallback for (string[])obj always throws.

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static string[] CastToArrayOfStrings(object obj) => (string[])obj;
 G_M14817_IG01:
        sub      rsp, 40
 G_M14817_IG02:
        mov      rax, rcx
        test     rax, rax
-       je       SHORT G_M14817_IG05
+       je       SHORT G_M14817_IG04
 G_M14817_IG03:
        mov      r8, 0xD1FFAB1E      ; System.String[]
        cmp      qword ptr [rax], r8
-       je       SHORT G_M14817_IG05
+       jne      SHORT G_M14817_IG05
 G_M14817_IG04:
+       add      rsp, 40
+       ret
+G_M14817_IG05:
        mov      rdx, rcx
        mov      rcx, r8
        call     [CORINFO_HELP_CHKCASTARRAY]
-G_M14817_IG05:
-       nop
-G_M14817_IG06:
-       add      rsp, 40
-       ret
+       int3
// Shared generic code, e.g. T = string
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static List<T> CastToList<T>(object obj) => (List<T>)obj;
 G_M25441_IG05:
        cmp      qword ptr [rax], rcx
        je       SHORT G_M25441_IG07
 G_M25441_IG06:
        mov      rdx, rbx
-       call     [CORINFO_HELP_CHKCASTCLASS]
+       call     [CORINFO_HELP_CHKCASTCLASS_SPECIAL]
 G_M25441_IG07:
        nop
class Base { }
class Derived : Base { }

// Tier1; warmed with 80% Base instances and 20% Derived instances
[MethodImpl(MethodImplOptions.NoInlining)]
static Base CastToBase(object obj) => (Base)obj;
 G_M5904_IG05:
        mov      rdx, rcx
        mov      rcx, r8
-       call     CORINFO_HELP_CHKCASTCLASS
+       call     CORINFO_HELP_CHKCASTCLASS_SPECIAL
        jmp      SHORT G_M5904_IG04

Copilot AI lite review requested due to automatic review settings September 6, 2026 17:33
@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 Sep 6, 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It changes JIT cast expansion behavior/codegen in core paths and should be validated by a human reviewer with appropriate correctness/perf testing coverage.

Pull request overview

This PR refines CoreCLR JIT late cast-helper expansion so the generated fast/slow paths better match helper semantics (notably when the cast target is provably exact), enabling tighter fallbacks and use of the specialized cast helper when its preconditions are already guarded.

Changes:

  • Treat CHKCASTCLASS/CHKCASTARRAY to an exact target type as having an always-throwing fallback on type-check failure (after the null check), allowing the fallback block to be marked no-return.
  • Prefer TypeCheckFailedAction::CallHelper_Specialized for CHKCASTCLASS in cases where the expansion already guards null and exact-type equality.
  • Minor comment cleanup/clarification around exact-type handling and fallback behavior.
File summaries
File Description
src/coreclr/jit/helperexpansion.cpp Adjusts candidate selection and fallback strategy for late cast-helper expansion, including specialized-helper selection and always-throw classification for exact targets.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

@EgorBo

EgorBo commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

PTAL @jakobbotsch @dotnet/jit-contrib small change to improve fallbacks for cast expansion

@EgorBo
EgorBo requested a review from jakobbotsch September 7, 2026 11:07
@EgorBo
EgorBo merged commit 90dbb51 into dotnet:main Sep 9, 2026
139 of 140 checks passed
@EgorBo
EgorBo deleted the jit-cast-helper-fallbacks branch September 9, 2026 12:10
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 10, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants