GH-118093: Don't lose confidence when tracing through 100% biased branches (GH-124813)

This commit is contained in:
Brandt Bucher 2024-10-02 12:24:37 -07:00 committed by GitHub
parent b85923a0fe
commit 6810928927
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,2 @@
Improve the experimental JIT compiler's ability to stay "on trace" when
encountering highly-biased branches.

View File

@ -643,14 +643,12 @@ translate_bytecode_to_trace(
int bitcount = _Py_popcount32(counter); int bitcount = _Py_popcount32(counter);
int jump_likely = bitcount > 8; int jump_likely = bitcount > 8;
/* If bitcount is 8 (half the jumps were taken), adjust confidence by 50%. /* If bitcount is 8 (half the jumps were taken), adjust confidence by 50%.
If it's 16 or 0 (all or none were taken), adjust by 10%
(since the future is still somewhat uncertain).
For values in between, adjust proportionally. */ For values in between, adjust proportionally. */
if (jump_likely) { if (jump_likely) {
confidence = confidence * (bitcount + 2) / 20; confidence = confidence * bitcount / 16;
} }
else { else {
confidence = confidence * (18 - bitcount) / 20; confidence = confidence * (16 - bitcount) / 16;
} }
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely]; uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely];
DPRINTF(2, "%d: %s(%d): counter=%04x, bitcount=%d, likely=%d, confidence=%d, uopcode=%s\n", DPRINTF(2, "%d: %s(%d): counter=%04x, bitcount=%d, likely=%d, confidence=%d, uopcode=%s\n",