Skip to content

Branch confidence decay in Tier 2 translator  #112320

Closed

Description

@gvanrossum

This is a follow-up to gh-109039 (and to a lesser extent gh-111848). (I'm sure I read about it somewhere on https://github.com/faster-cpython/ideas/issues too, but I can't find the relevant issue, so I'll describe the idea from scratch here.)

When we translate a branch instruction (e.g. POP_JUMP_IF_TRUE), Tier 1 has a 16-bit shift register tracking how often we branched in the past 16 times this instruction was reached. During Tier 2 translation, if the bit count indicates that we've taken the branch more often than not, we continue the trace at the branch destination; otherwise, we continue following the branch instruction.

What we should also do in the translator have a variable (for the entire trace) indicating how likely we are still "on trace". This "confidence factor" starts off at 100%. If we translate a branch that is taken X% of the time, for X >= 50%, we should multiply the confidence by X%. If the confidence ends too low (say, below 33%) we should end the trace at this point, generating an _EXIT_TRACE uop.

UPDATE: Ideally we should also adjust the confidence each time we generate a guard. But what factor should we use there? Most guards fail rarely. I propose to punt on this now; later we can add code to adjust the same variable on deoptimization exits.

Linked PRs

Activity

  1. changed the title [-]BranchBrahch confidence decay in Tier 2 translator [/-] [+]Branch confidence decay in Tier 2 translator [/+] on Nov 22, 2023
  2. added a commit that references this issue on Dec 12, 2023
  3. added a commit that references this issue on Feb 11, 2024
  4. hugovk commented on Mar 15, 2024

    @hugovk
    Member

    Closing because the PR has been merged. Please re-open if there's more needed here.

  5. gvanrossum commented on Mar 15, 2024

    @gvanrossum
    MemberAuthor

    Thanks, and sorry. I usually forget to use the "fixes gh-blahblah" magic phrase. :-(

  6. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions