Skip to content

JIT: forward-sub cheap address temps with multiple uses#129312

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-67187-fwdsub
Open

JIT: forward-sub cheap address temps with multiple uses#129312
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-67187-fwdsub

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Substitute a FIELD_ADDR chain rooted at a local at every use site so the dup-spill temp for field updates no longer blocks address-mode containment in Lowering. Also refactor the FIELD_ADDR walk into gtPeelFieldAddrs and share it with fgAddrCouldBeHeap and impIsAddressInLocal.

Fixes #67187.

Note

This change was authored with the assistance of GitHub Copilot CLI.

Copilot AI review requested due to automatic review settings June 12, 2026 00:41
@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 Jun 12, 2026
@AndyAyersMS

Copy link
Copy Markdown
Member Author

For the canonical repro:

struct FooS { public int a, b; }

class StructContainer
{
    FooS foo;
    public void Update() { foo.a++; foo.b++; }
}

x64 codegen (FullOpts), 12 bytes / 4 insns → 6 bytes / 2 insns:

; Before
lea      rax, [rcx+0x08]
inc      dword ptr [rax]
add      rcx, 12
inc      dword ptr [rcx]

; After
inc      dword ptr [rcx+0x08]
inc      dword ptr [rcx+0x0C]

Note

This comment was authored with the assistance of GitHub Copilot CLI.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@dhartglassMSFT PTAL
fyi @dotnet/jit-contrib

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 extends the JIT’s forward-substitution phase to allow substituting certain cheap “address temp” expressions at multiple use sites (not just a single last-use), primarily to unblock downstream address-mode containment in Lowering. It also factors out a common GT_FIELD_ADDR-peeling loop into Compiler::gtPeelFieldAddrs and reuses it from a couple of other call sites.

Changes:

  • Add Compiler::gtPeelFieldAddrs and use it from impIsAddressInLocal and fgAddrCouldBeHeap.
  • Add a multi-use forward-substitution path for “cheap reorderable address trees”, implemented via fgForwardSubMultiUse.
  • Update compiler.h with new helper/method declarations.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/promotiondecomposition.cpp Adds Compiler::gtPeelFieldAddrs helper implementation.
src/coreclr/jit/importer.cpp Replaces local FIELD_ADDR peeling with gtPeelFieldAddrs.
src/coreclr/jit/forwardsub.cpp Adds cheap-address detection and multi-use forward-substitution logic.
src/coreclr/jit/flowgraph.cpp Reuses gtPeelFieldAddrs in fgAddrCouldBeHeap.
src/coreclr/jit/compiler.h Declares gtPeelFieldAddrs and fgForwardSubMultiUse.

Comment thread src/coreclr/jit/forwardsub.cpp
Comment thread src/coreclr/jit/forwardsub.cpp
Comment thread src/coreclr/jit/forwardsub.cpp Outdated
Comment thread src/coreclr/jit/importer.cpp Outdated
Substitute a FIELD_ADDR chain rooted at a local at every use site so
the dup-spill temp for field updates no longer blocks address-mode
containment in Lowering. Also refactor the FIELD_ADDR walk into
gtPeelFieldAddrs and share it with fgAddrCouldBeHeap and
impIsAddressInLocal.

The multi-use path pre-allocates every clone before mutating the IR
(so a gtCloneExpr failure bails cleanly), caps the use count at four
to keep the targeted patterns in scope, and conservatively clears
GTF_VAR_DEATH bits on every LCL_VAR in the inserted copies. The peel
helper has a const overload so callers like impIsAddressInLocal stay
const-safe.

Fixes dotnet#67187.

> [!NOTE]
> This change was authored with the assistance of GitHub Copilot CLI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 12, 2026 01:58

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 5 out of 5 changed files in this pull request and generated 2 comments.

Comment on lines +258 to +261
// Rebuild metadata since we changed which LCL_VARs appear in nextStmt.
fgSequenceLocals(nextStmt);
gtUpdateStmtSideEffects(nextStmt);
return true;
Comment on lines +183 to +191
fgWalkResult PreOrderVisit(GenTree** use, GenTree* user)
{
GenTree* node = *use;
if (node->OperIs(GT_LCL_VAR) && (node->AsLclVarCommon()->GetLclNum() == m_lclNum))
{
m_useSlots.Push(use);
}
return fgWalkResult::WALK_CONTINUE;
}
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.

peephole optimization for lea command

2 participants