Skip to content

Comments

Forward sub cleanup#124784

Closed
AndyAyersMS wants to merge 5 commits intodotnet:mainfrom
AndyAyersMS:ForwardSubCleanup
Closed

Forward sub cleanup#124784
AndyAyersMS wants to merge 5 commits intodotnet:mainfrom
AndyAyersMS:ForwardSubCleanup

Conversation

@AndyAyersMS
Copy link
Member

No description provided.

AndyAyersMS and others added 2 commits February 23, 2026 16:32
- Fix typos (bactrack->backtrack, unalised->unaliased, incomplete sentence)
- Add const to EffectsVisitor::GetFlags()
- Fix misleading JITDUMP message for address-exposed locals
- Move profitability checks earlier for throughput improvement
- Add JitForwardSubLookahead config to search N statements ahead for use
- Support intermediate statement effect accumulation and store interference

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings February 24, 2026 04:44
@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 Feb 24, 2026
Copy link
Contributor

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 refactors and enhances the forward substitution optimization in the JIT compiler by introducing multi-statement lookahead capability. Forward substitution attempts to merge a local variable definition with its single use, eliminating the intermediate local. Previously, the optimization only checked the immediately following statement; now it can search ahead multiple statements (configurable via JitForwardSubLookahead, default 10).

Changes:

  • Introduces configurable multi-statement lookahead for forward substitution to find uses that are not in the immediate next statement
  • Moves profitability checks earlier in the flow to avoid wasted work
  • Fixes spelling errors and improves code comments/documentation throughout
  • Renames variables from nextStmt to useStmt for better clarity in the context of multi-statement lookahead

Reviewed changes

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

File Description
src/coreclr/jit/jitconfigvalues.h Adds JitForwardSubLookahead configuration with default value of 10 to control how many statements ahead to search
src/coreclr/jit/forwardsub.cpp Implements multi-statement lookahead logic with intermediate effect accumulation, moves profitability checks earlier, fixes typos ("backtrack", "unaliased", "exceptions can be thrown"), adds SetIntermediateEffects method, makes GetFlags() const, renames nextStmt to useStmt throughout, and updates documentation

Copilot AI review requested due to automatic review settings February 24, 2026 14:30
Copy link
Contributor

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

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

Comment on lines +573 to 574
unsigned lookahead = max(1u, (unsigned)JitConfig.JitForwardSubLookahead());

Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

JitForwardSubLookahead() returns an int. Casting to unsigned before clamping means negative config values wrap to a huge lookahead, potentially scanning the rest of the block and doing extra gtUpdateStmtSideEffects/walks. Clamp in the signed domain first (e.g., treat values <= 0 as 1) before converting to unsigned.

Suggested change
unsigned lookahead = max(1u, (unsigned)JitConfig.JitForwardSubLookahead());
int configLookahead = JitConfig.JitForwardSubLookahead();
if (configLookahead <= 0)
{
configLookahead = 1;
}
unsigned lookahead = static_cast<unsigned>(configLookahead);

Copilot uses AI. Check for mistakes.
if (!found)
if (useStmt == nullptr)
{
JITDUMP(" no next stmt use\n");
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

The diagnostic message "no next stmt use" is now misleading because the logic can scan multiple statements (up to JitForwardSubLookahead) rather than just the next statement. Consider updating the message to reflect that no eligible use was found within the lookahead range.

Suggested change
JITDUMP(" no next stmt use\n");
JITDUMP(" no eligible use within lookahead range\n");

Copilot uses AI. Check for mistakes.
@AndyAyersMS
Copy link
Member Author

Seems like this approach has some flaws.. going to try something a bit more general

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.

1 participant