Skip to content

Fix edge likelihoods out of optimize bools - #131356

Merged
dhartglassMSFT merged 5 commits into
dotnet:mainfrom
dhartglassMSFT:repair_edges_opt_bools
Aug 14, 2026
Merged

Fix edge likelihoods out of optimize bools#131356
dhartglassMSFT merged 5 commits into
dotnet:mainfrom
dhartglassMSFT:repair_edges_opt_bools

Conversation

@dhartglassMSFT

@dhartglassMSFT dhartglassMSFT commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Optimize bools could produce incorrect edge likelihoods.

From the bug, we'd combine (note BB03->BB06 likelihood of 0.96):
------------ BB03 [0002] [016..022) -> BB06(0.9697322),BB04(0.03026775) (cond), preds={BB02} succs={BB04,BB06} ------------ BB04 [0003] [022..02B) -> BB06(0),BB05(1) (cond), preds={BB03} succs={BB05,BB06}
into this after fusing BB003,04 (Note BB03->BB06 likelihood of 0)
------------ BB03 [0002] [016..02B) -> BB06(0),BB05(1) (cond), preds={BB02} succs={BB05,BB06}

Fix optOptimizeCompareChainCondBlock to correct this likelihood.

I also replaced the call to fgRepairProfileCondToUncond which would incorrectly decrease the profile weight of BB06 in the above example.

Spent a bit looking through diffs. Regressions and improvements in perfscore are due to block weight inside loops now being calculated correctly. Some codesize changes caused by block reordering changes, as blockweight has gone 0.00->0.01 in some examples.

fixes #130150

Copilot AI lite review requested due to automatic review settings July 24, 2026 23:48
@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 Jul 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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

Copy link
Copy Markdown
Contributor

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

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 fixes edge-likelihood propagation when optOptimizeCompareChainCondBlock merges two consecutive conditional blocks into a single compare-chain condition, ensuring the resulting block’s outgoing edge likelihoods preserve the original control-flow probabilities (including the “shared target” case).

Changes:

  • Capture the pre-merge likelihoods for b1’s removed and fallthrough edges and use them to recompute b2’s outgoing edge likelihoods as unconditional probabilities after compaction.
  • Adjust b2’s profile weight prior to fgCompactBlock so the merged block inherits the correct weight without incorrectly decrementing the shared-target’s weight.
  • Remove the prior call to fgRepairProfileCondToUncond (which is tailored for true removal of alternate flow) in favor of this targeted repair for the compare-chain merge shape.

@dhartglassMSFT
dhartglassMSFT marked this pull request as ready for review July 27, 2026 00:00
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

@dhartglassMSFT

Copy link
Copy Markdown
Contributor Author

/azp list

@azure-pipelines

Copy link
Copy Markdown
CI/CD Pipelines for this repository:

@dhartglassMSFT

Copy link
Copy Markdown
Contributor Author

/azp run runtime-coreclr superpmi-diffs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings August 13, 2026 17:59

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

Suppressed comments (2)

src/coreclr/jit/optimizebools.cpp:1103

  • The loop over b2Edges assumes GetTrueEdge() and GetFalseEdge() are distinct. If m_b2 is a degenerate BBJ_COND where both edges are the same (possible elsewhere in the JIT), this code will apply the likelihood adjustment twice to the same FlowEdge, producing an incorrect final likelihood. Handle the trueEdge == falseEdge case explicitly and adjust each unique edge at most once.
    FlowEdge* const b2Edges[] = {m_b2->GetTrueEdge(), m_b2->GetFalseEdge()};
    for (FlowEdge* const b2Edge : b2Edges)
    {
        weight_t combined = fallthroughLikelihood * b2Edge->getLikelihood();
        if (b2Edge->getDestinationBlock() == b1RemovedTarget)

src/coreclr/jit/optimizebools.cpp:1090

  • Profile-weight repair is gated only on m_b2->hasProfileWeight(), but the added weight uses removedEdge->getLikelyWeight() which depends on m_b1's weight. If m_b2 has profile data but m_b1 does not, this can incorrectly treat a non-profile weight as profile and corrupt m_b2's profile weight. Gate this update on m_b1 having profile weight as well (matching fgRepairProfileCondToUncond’s precondition).

This issue also appears on line 1099 of the same file.

    if (m_b2->hasProfileWeight())
    {
        m_b2->increaseBBProfileWeight(removedEdge->getLikelyWeight());
    }

Copilot AI review requested due to automatic review settings August 13, 2026 18:48

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

Suppressed comments (1)

src/coreclr/jit/optimizebools.cpp:1090

  • The profile-weight repair is currently gated on m_b2->hasProfileWeight(), but the value you add (removedEdge->getLikelyWeight()) is derived from m_b1->bbWeight. This diverges from the previous fgRepairProfileCondToUncond behavior (which only adjusted weights when the source block had profile weight) and can incorrectly perturb PGO weights if m_b2 happens to have profile weight but m_b1 does not. It also silently does nothing if m_b1 has profile weight but m_b2 does not, leaving fgCompactBlock to inherit an underweight m_b2 and potentially making PGO inconsistent.

Consider gating the adjustment on m_b1->hasProfileWeight() and explicitly marking the profile inconsistent when required profile data is missing on m_b2.

    if (m_b2->hasProfileWeight())
    {
        m_b2->increaseBBProfileWeight(removedEdge->getLikelyWeight());
    }

@dhartglassMSFT
dhartglassMSFT enabled auto-merge (squash) August 14, 2026 17:55
@dhartglassMSFT

Copy link
Copy Markdown
Contributor Author

wasm failure is this #131925
x64 NAOT failure is #132030

@dhartglassMSFT

Copy link
Copy Markdown
Contributor Author

/ba-g known failing tests

@dhartglassMSFT
dhartglassMSFT merged commit f11b245 into dotnet:main Aug 14, 2026
126 of 129 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 17, 2026
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.

Optimize bools miscalculates edge likelihoods

3 participants