diff options
author | Guido van Rossum <guido@python.org> | 2023-09-11 11:20:24 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-11 18:20:24 +0000 |
commit | bcce5e271815c0bdbe894964e853210d2c75949b (patch) | |
tree | 52bb552678ee685e56900b3f85e20027fa8f3212 /Python/specialize.c | |
parent | ecd21a629a2a30bcae89902f7cad5670e9441e2c (diff) | |
download | cpython-bcce5e271815c0bdbe894964e853210d2c75949b.tar.gz cpython-bcce5e271815c0bdbe894964e853210d2c75949b.zip |
gh-109039: Branch prediction for Tier 2 interpreter (#109038)
This adds a 16-bit inline cache entry to the conditional branch instructions POP_JUMP_IF_{FALSE,TRUE,NONE,NOT_NONE} and their instrumented variants, which is used to keep track of the branch direction.
Each time we encounter these instructions we shift the cache entry left by one and set the bottom bit to whether we jumped.
Then when it's time to translate such a branch to Tier 2 uops, we use the bit count from the cache entry to decided whether to continue translating the "didn't jump" branch or the "jumped" branch.
The counter is initialized to a pattern of alternating ones and zeros to avoid bias.
The .pyc file magic number is updated. There's a new test, some fixes for existing tests, and a few miscellaneous cleanups.
Diffstat (limited to 'Python/specialize.c')
-rw-r--r-- | Python/specialize.c | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/Python/specialize.c b/Python/specialize.c index 8b4aac2f890..91243419ec6 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -338,9 +338,23 @@ _PyCode_Quicken(PyCodeObject *code) assert(opcode < MIN_INSTRUMENTED_OPCODE); int caches = _PyOpcode_Caches[opcode]; if (caches) { - // JUMP_BACKWARD counter counts up from 0 until it is > backedge_threshold - instructions[i + 1].cache = - opcode == JUMP_BACKWARD ? 0 : adaptive_counter_warmup(); + // The initial value depends on the opcode + int initial_value; + switch (opcode) { + case JUMP_BACKWARD: + initial_value = 0; + break; + case POP_JUMP_IF_FALSE: + case POP_JUMP_IF_TRUE: + case POP_JUMP_IF_NONE: + case POP_JUMP_IF_NOT_NONE: + initial_value = 0x5555; // Alternating 0, 1 bits + break; + default: + initial_value = adaptive_counter_warmup(); + break; + } + instructions[i + 1].cache = initial_value; i += caches; } } |