Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 6, 2025

Ignore pinning on top of NonGC objects (string literals, static readonly arrays, etc.).

void Test() => Foo("Test");

unsafe void Foo(string s)
{
    fixed (char* p = s)
        Console.WriteLine(p[2]);
}

codegen diff:

; Method Program:Test():this (FullOpts)
       sub      rsp, 40
-      mov      rcx, 0x274803D568C
-      mov      bword ptr [rsp+0x20], rcx
-      mov      rcx, bword ptr [rsp+0x20]
-      movzx    rcx, word  ptr [rcx+0x04]
+      mov      ecx, 115 ;; 's'
       call     [System.Console:WriteLine(char)]
+      nop
-      xor      eax, eax
-      mov      bword ptr [rsp+0x20], rax
       add      rsp, 40
       ret      
-; Total bytes of code: 46
+; Total bytes of code: 21

spmi diffs

@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 Jul 6, 2025
@dotnet-policy-service
Copy link
Contributor

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refines JIT handling of pinned and non-GC objects by:

  • Peeling through certain VNF_Cast nodes when computing constant offsets.
  • Ignoring ldarga on constant string nodes during inline argument substitution.
  • Extending GenTree::IsNotGcDef() to treat NonGC objects (and simple offset patterns) as non-GC definitions.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/valuenum.cpp Allow VNF_Cast (GC→I) to be peeled out when computing offsets
src/coreclr/jit/importer.cpp Refine argCanBeModified to skip ldarga on GT_CNS_STR
src/coreclr/jit/gentree.h Enhance IsNotGcDef() to recognize NonGC object handles and GT_ADD offsets
Comments suppressed due to low confidence (2)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@EgorBo
Copy link
Member Author

EgorBo commented Jul 7, 2025

PTAL @jakobbotsch @dotnet/jit-contrib

@EgorBo EgorBo requested a review from jakobbotsch July 7, 2025 09:10
@EgorBo EgorBo merged commit 8429820 into dotnet:main Jul 7, 2025
108 of 112 checks passed
@EgorBo EgorBo deleted the remove-fixed-for-nongc branch July 7, 2025 17:07
@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2025
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.

2 participants