8000 gh-112320: Implement on-trace confidence tracking for branches by gvanrossum · Pull Request #112321 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112320: Implement on-trace confidence tracking for branches #112321

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

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Track confidence as a scaled int
  • Loading branch information
gvanrossum committed Dec 12, 2023
commit caa6ab3ae9133b0ad3a199cf23887bac473c8006
15 changes: 9 additions & 6 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ BRANCH_TO_GUARD[4][2] = {

#define TRACE_STACK_SIZE 5

#define CONFIDENCE_RANGE 1000
#define CONFIDENCE_CUTOFF 333

/* Returns 1 on success,
* 0 if it failed to produce a worthwhile trace,
* and -1 on an error.
Expand All @@ -431,7 +434,7 @@ translate_bytecode_to_trace(
_Py_CODEUNIT *instr;
} trace_stack[TRACE_STACK_SIZE];
int trace_stack_depth = 0;
float confidence = 1.0; // Adjusted by branch instructions
int confidence = CONFIDENCE_RANGE; // Adjusted by branch instructions

#ifdef Py_DEBUG
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
Expand Down Expand Up @@ -544,20 +547,20 @@ translate_bytecode_to_trace(
int bitcount = _Py_popcount32(counter);
int jump_likely = bitcount > 8;
if (jump_likely) {
confidence *= bitcount / 16.0;
confidence = confidence * bitcount / 16;
}
else {
confidence *= (16 - bitcount) / 16.0;
confidence = confidence * (16 - bitcount) / 16;
}
// TODO: Tweak the confidence cutoff
if (confidence < 0.33) {
DPRINTF(2, "Confidence too low (%.3f)\n", confidence);
if (confidence < CONFIDENCE_CUTOFF) {
DPRINTF(2, "Confidence too low (%d)\n", confidence);
OPT_STAT_INC(low_confidence);
goto done;
}
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely];
_Py_CODEUNIT *next_instr = instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
DPRINTF(2, "%s(%d): counter=%x, bitcount=%d, likely=%d, confidence=%.3f, uopcode=%s\n",
DPRINTF(2, "%s(%d): counter=%x, bitcount=%d, likely=%d, confidence=%d, uopcode=%s\n",
_PyUOpName(opcode), oparg,
counter, bitcount, jump_likely, confidence, _PyUOpName(uopcode));
ADD_TO_TRACE(uopcode, max_length, 0, target);
Expand Down
0