Skip to content

JIT: Decouple "DoNotEnregister" from liveness#127932

Merged
jakobbotsch merged 9 commits into
dotnet:mainfrom
jakobbotsch:decouple-liveness-dner
May 18, 2026
Merged

JIT: Decouple "DoNotEnregister" from liveness#127932
jakobbotsch merged 9 commits into
dotnet:mainfrom
jakobbotsch:decouple-liveness-dner

Conversation

@jakobbotsch

Copy link
Copy Markdown
Member

Liveness was marking locals as DNER in various places, including when it found locals to be live into EH handlers.
DNER is sticky and cannot be unset, and we call liveness multiple times, so that made things hard to reason about and led to unnecessary DNER.

This showed up for runtime async functions. Even when we were able to optimize out the try-fault clauses inserted around the body we would often still end up with lvaAsyncContinuationArg unnecessarily DNER'd because we had already marked it as DNER during a previous liveness pass, when it was live into the fault handler.

This PR:

  • Resets lvLiveInOutOfHandler on all locals every time we run liveness
  • Moves all the DNER setting from liveness to LSRA and to lowering
  • Encapsulates lvLiveInOutOfHandler into LclVarDsc::IsLiveInOutOfHandler() and adds an assert(lvTracked) to this function, so it cannot be misused
  • Introduces a few updates to heuristics to use IsLiveInOutOfHandler() in addition to DNER

Diffs aren't all that great, but I think this is a good clean up on its own.

Copilot AI review requested due to automatic review settings May 7, 2026 19:15
@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 May 7, 2026
Comment thread src/coreclr/jit/compiler.cpp
Comment thread src/coreclr/jit/lclvars.cpp
Comment thread src/coreclr/jit/regalloc.cpp

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 refactors CoreCLR JIT local liveness/EH handling so that EH “live-in/out-of-handler” state is recomputed each liveness run and lvDoNotEnregister (DNER) is no longer set by liveness, instead being set by regalloc / lowering.

Changes:

  • Recompute and encapsulate EH live-in/out state via LclVarDsc::IsLiveInOutOfHandler() and reset EH/must-init flags each liveness run.
  • Move DNER-setting decisions out of liveness into regalloc (checkForDNER) and lowering (lvSetEHVarsDoNotEnreg), and update heuristics to consult EH-liveness in addition to DNER.
  • Add Compiler::IsEHVarARegCandidate to centralize “EH write-thru candidate” logic and use it in multiple optimizations.

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/coreclr/jit/sideeffects.h Use IsLiveInOutOfHandler() for EH-related aliasing decisions.
src/coreclr/jit/regallocwasm.h Declare checkForDNER for Wasm regalloc.
src/coreclr/jit/regallocwasm.cpp Call checkForDNER and use encapsulated EH-live query.
src/coreclr/jit/regalloc.cpp Add checkForDNER and remove DNER-setting from isRegCandidate.
src/coreclr/jit/optimizer.cpp Switch EH-live checks to IsLiveInOutOfHandler().
src/coreclr/jit/morph.cpp Stop copying EH-live flag during implicit-byref arg retyping.
src/coreclr/jit/lsra.h Declare checkForDNER for LSRA.
src/coreclr/jit/lsra.cpp Call checkForDNER and use IsLiveInOutOfHandler() in LSRA logic.
src/coreclr/jit/lower.cpp Add lvSetEHVarsDoNotEnreg() call to support containment heuristics in lowering.
src/coreclr/jit/liveness.cpp Reset lvMustInit and EH-live state each liveness run; set EH-live without setting DNER.
src/coreclr/jit/lclvars.cpp Add lvSetEHVarsDoNotEnreg; remove lvaSetVarLiveInOutOfHandler; adjust DNER behavior.
src/coreclr/jit/jiteh.cpp Track when EH clauses are removed post-liveness.
src/coreclr/jit/inductionvariableopts.cpp Update IV widening heuristics to consider EH-liveness independently of DNER.
src/coreclr/jit/gschecks.cpp Remove direct clearing of EH-live flag on shadowing locals.
src/coreclr/jit/gentree.cpp Add Compiler::IsEHVarARegCandidate helper; update EH-live checks.
src/coreclr/jit/earlyprop.cpp Update EH-live checks to IsLiveInOutOfHandler().
src/coreclr/jit/copyprop.cpp Adjust “profitability” gating to compare expected enregistration status incl. EH-liveness.
src/coreclr/jit/compiler.h Encapsulate EH-live flag, add helper APIs, and add optRemovedEHClausesAfterLiveness.
src/coreclr/jit/compiler.cpp Update enregister stats accounting to include PinningRef unconditionally.
src/coreclr/jit/codegenwasm.cpp Update EH-live checks to IsLiveInOutOfHandler().
src/coreclr/jit/codegencommon.cpp Update EH-live checks to IsLiveInOutOfHandler().
Comments suppressed due to low confidence (1)

src/coreclr/jit/compiler.h:5734

  • optRemovedEHClausesAfterLiveness is currently only set/reset (in fgRemoveEHTableEntry and liveness) but not consumed anywhere else. If it’s meant to guard heuristics that rely on IsLiveInOutOfHandler() (e.g., avoid setting DNER based on stale EH liveness after EH removal), consider wiring it into the relevant decision points; otherwise it should be removed to avoid accumulating dead state.
    bool fgBBVarSetsInited = false;

    // Track how many artificial ref counts we've added to fgEntryBB (for OSR)

Comment thread src/coreclr/jit/lclvars.cpp
Comment thread src/coreclr/jit/liveness.cpp Outdated
Copilot AI review requested due to automatic review settings May 7, 2026 19:42

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

Copilot reviewed 20 out of 20 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/jit/lower.cpp
Comment thread src/coreclr/jit/compiler.cpp
Comment thread src/coreclr/jit/liveness.cpp Outdated
Copilot AI review requested due to automatic review settings May 8, 2026 08:19

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

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

Comment thread src/coreclr/jit/gschecks.cpp

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.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label May 12, 2026
@jakobbotsch

Copy link
Copy Markdown
Member Author

Sorry for the requests, accidentally rebased across a merge here...

@jakobbotsch jakobbotsch force-pushed the decouple-liveness-dner branch from d3d7400 to 15f6d4e Compare May 12, 2026 13:38
@jakobbotsch jakobbotsch removed the linkable-framework Issues associated with delivering a linker friendly framework label May 12, 2026
@jakobbotsch

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr jitstress, runtime-coreclr libraries-jitstress

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@jakobbotsch jakobbotsch marked this pull request as ready for review May 13, 2026 12:28
Copilot AI review requested due to automatic review settings May 13, 2026 12:28
@jakobbotsch

Copy link
Copy Markdown
Member Author

cc @dotnet/jit-contrib PTAL @AndyAyersMS

Diffs.

This will be more important for future work once we start optimizing out IR that allows us to remove the EH we inserted for async.

@jakobbotsch jakobbotsch requested a review from AndyAyersMS May 13, 2026 12:29

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

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/jit/lclvars.cpp
Comment thread src/coreclr/jit/lower.cpp
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.

3 participants