Skip to content

JIT: fold char loads from movable static readonly strings#130197

Closed
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:jit-fold-movable-str-char
Closed

JIT: fold char loads from movable static readonly strings#130197
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:jit-fold-movable-str-char

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 4, 2026

Copy link
Copy Markdown
Member

Fold char loads from movable static readonly strings (e.g. Str = 111111.ToString(); ... str[2]).

Removes the getStringChar special path in fgValueNumberConstLoad in favor of the getObjectContent path, and teaches GetObjectHandleAndOffset to resolve movable static readonly string fields (via getStaticFieldContent(ignoreMovableObjects: false)) so their immutable char content can be folded. The movable handle is only used to read content at compile time, never materialized. Arrays are excluded (mutable contents).

Rely on GetImmutableDataFromAddress/getObjectContent instead of the getStringChar special path, and resolve movable 'static readonly' string fields (ignoreMovableObjects=false) so their immutable char content can be folded.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 4, 2026 09: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 Jul 4, 2026
@EgorBo

EgorBo commented Jul 4, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

@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.

Pull request overview

This PR updates RyuJIT value numbering constant-folding so that char loads from immutable string content can be folded even when the string comes from a movable static readonly reference field, by resolving the field’s runtime value at JIT time and reading immutable object content via getObjectContent.

Changes:

  • Extend Compiler::GetObjectHandleAndOffset to recognize addresses based on static readonly ref field loads (via VN patterns) and resolve the field’s object value using getStaticFieldContent(..., ignoreMovableObjects: false) when the target object is immutable (excluding arrays).
  • Remove the getStringChar-based special-casing in fgValueNumberConstLoad so folding relies on the general getObjectContent path.

Comment on lines +12880 to 12885
// Use the liberal VN: the content we are about to read is immutable, so a constant fold is
// correct even when the liberal and conservative VNs differ (e.g. a load from a movable
// 'static readonly' reference field has an opaque conservative VN).
ValueNum treeVN = tree->gtVNPair.GetLiberal();
if (treeVN == ValueNumStore::NoVN)
{
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.

2 participants