Registry Mirror
Skip to content

Conversation

@NinoFloris

@NinoFloris NinoFloris commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Preferred order (regression mitigation): #137513 -> #137826 -> #137835.

Today the JIT uses the first occurrence of a CSE candidate to estimate what it saves at every use. Two expressions can have the same value number and still cost different amounts to compute. This PR prices the expressions each use actually replaces and accounts for cases where the definition or a use can reuse a register.

Commits:

  • Replace the first-occurrence estimate with the sum of the actual uses’ block-weighted execution costs and individual code-size savings, and treat rounding-sized differences as break-even.
  • Charge one unit for a temp read when its sole use assigns directly to an enregisterable local.
  • Extend that discount to cases where keeping the result lets an input die at the defining occurrence.
  • Reduce the definition cost by one unit when a store already materializes the integer result in a register.
  • On Wasm, charge one unit to read an eligible non-GC local across a call and omit the caller-save cost.
  • On ARM64, discount an aggressive, single-definition integer load that already produces a register value. Cap the credit at the lesser of the definition and use weights.
    On ARM64, an integer load puts its result in a register before a compare or arithmetic instruction can use it. On x64 the instruction can read memory directly, so the same discount can underestimate the cost of keeping the value. ARM64 is where I’ve measured the benefit.
  • Look through GT_COMMA when identifying the value a store consumes, so a preceding bounds check still allows the definition discount.
  • Record which blocks contain a call during the existing CSE candidate walk. This keeps the live-across-call estimate accurate when splitting or merging blocks moves a call between the definition and a use.
  • On ARM64, discount shared constants that already take multiple instructions to materialize in a register. Exclude hoisted definitions and cap the credit at the weighted use count minus the weighted definition count.
    On ARM64, materializing a counter address can take a movz and two movks. Keeping the base lets later counter accesses use a single add. On x64 the full address fits in one mov, and applying the discount to eligible handles grew code on both Linux and Windows.

@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 Nov 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 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 dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 18, 2026
@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.

Sum the weighted costs and size savings of the expressions replaced at use sites. Allow a rounding-sized difference when comparing break-even costs.
When the sole use stores the result into an enregisterable local, the local and the CSE temp can share a register. Charge one unit for the read in speed-oriented code without a call crossing.
Use SSA to find a single-register input consumed by every occurrence. Re-entering the defining occurrence must produce a fresh input. Apply the existing use discount once when its register can hold the result.
@NinoFloris NinoFloris changed the title JIT: Price CSE savings at each use JIT: Account for per-use savings and register reuse in CSE pricing Nov 29, 2026
@NinoFloris
NinoFloris marked this pull request as ready for review November 29, 2026 17:46
Discount the cost of capturing a multi-instruction constant that already needs a register. Exclude hoisted definitions and cap the credit at the weighted uses remaining after the first use per definition.

This branch has not been deployed

No deployments
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant