Skip to content

JIT: Model partial defs less conservatively in liveness when possible #115081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Apr 26, 2025

Before this change a partial def v.x = 123 was modelled semantically something like v = v with x = 123.
This matches how SSA models these definitions. However, it has the downside that it causes partial defs to be considered uses.

This PR changes things so that other liveness passes take a more liberal view, where partial defs are no longer considered full defs, but are then also not considered uses. This allows more dead-code elimination around these cases.

The liveness that runs during SSA is not changed; it still takes the old view.

Based on an old change I had lying around in https://github.com/dotnet/runtime/compare/main...jakobbotsch:runtime:partial-defs-allow-dying?expand=1 as an alternative to #115031

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 26, 2025
Copy link
Contributor

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

@jakobbotsch jakobbotsch changed the title JIT: Old liveness change JIT: Model partial defs less conservatively in liveness when possible Apr 26, 2025
@AndyAyersMS
Copy link
Member

Nice. Even a small TP improvement.

@jakobbotsch jakobbotsch marked this pull request as ready for review April 27, 2025 13:50
@Copilot Copilot AI review requested due to automatic review settings April 27, 2025 13:50
Copy link
Contributor

@Copilot 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 the liveness analysis in the JIT by modeling partial definitions more liberally outside SSA, allowing for better dead-code elimination.

  • Introduces a template parameter for fgMarkUseDef to distinguish SSA from non-SSA liveness.
  • Updates multiple call sites to explicitly specify the liveness mode (using either !lowered or false).
  • Documents the new behavior in fgMarkUseDef and related liveness routines.

Reviewed Changes

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

File Description
src/coreclr/jit/liveness.cpp Updates to fgMarkUseDef logic and its various callers to differentiate SSA from non-SSA behavior in partial definitions.
src/coreclr/jit/compiler.h Declaration of the fgMarkUseDef template to enable the new liveness semantics.

const bool isUse = !isDef || ((tree->gtFlags & GTF_VAR_USEASG) != 0);
const bool isDef = ((tree->gtFlags & GTF_VAR_DEF) != 0);
const bool isFullDef = isDef && ((tree->gtFlags & GTF_VAR_USEASG) == 0);
const bool isUse = ssaLiveness ? !isFullDef : !isDef;
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

Verify that the logic for computing the use set aligns with the intended liveness semantics: in SSA mode, only non-full defs are considered uses, whereas in non-SSA mode any def should not be treated as a use.

Copilot uses AI. Check for mistakes.

@@ -216,7 +236,7 @@ void Compiler::fgPerNodeLocalVarLiveness(GenTree* tree)
case GT_LCL_FLD:
case GT_STORE_LCL_VAR:
case GT_STORE_LCL_FLD:
fgMarkUseDef(tree->AsLclVarCommon());
fgMarkUseDef<!lowered>(tree->AsLclVarCommon());
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

Ensure that the expression '!lowered' correctly reflects the intended SSA versus non-SSA evaluation and that 'lowered' is reliably defined and meaningful in this context.

Copilot uses AI. Check for mistakes.

@@ -2087,7 +2110,7 @@ void Compiler::fgInterBlockLocalVarLiveness()
for (GenTree* cur = stmt->GetTreeListEnd(); cur != nullptr;)
{
assert(cur->OperIsAnyLocal());
bool isDef = ((cur->gtFlags & GTF_VAR_DEF) != 0) && ((cur->gtFlags & GTF_VAR_USEASG) == 0);
bool isDef = (cur->gtFlags & GTF_VAR_DEF) != 0;
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

Confirm that removing the additional check for GTF_VAR_USEASG when computing isDef in fgInterBlockLocalVarLiveness is intentional and consistent with the new non-SSA liveness semantics.

Copilot uses AI. Check for mistakes.

@@ -6720,6 +6720,7 @@ class Compiler

PhaseStatus fgEarlyLiveness();

template<bool ssaLiveness>
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

Consider adding inline documentation to clarify the expected values of ssaLiveness and its impact on liveness modeling, ensuring that its usage is clear to future maintainers.

Copilot uses AI. Check for mistakes.

@jakobbotsch
Copy link
Member Author

cc @dotnet/jit-contrib PTAL @AndyAyersMS

Diffs

@jakobbotsch jakobbotsch requested a review from AndyAyersMS April 27, 2025 14:06
Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

LGTM

@jakobbotsch
Copy link
Member Author

/asp run runtime-coreclr jitstress, runtime-coreclr libraries-jitstress, Fuzzlyn

@jakobbotsch
Copy link
Member Author

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

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

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