8000 gh-116968: Reimplement Tier 2 counters by gvanrossum · Pull Request #117144 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116968: Reimplement Tier 2 counters #117144

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 46 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e7e819b
Baby steps: reimplement thresholds using adaptive counter abstractions
gvanrossum Mar 22, 2024
8c74dfa
Make temperature an adaptive counter like the rest
gvanrossum Mar 22, 2024
79036ff
Fix tests
gvanrossum Mar 22, 2024
54c1f8e
Remove dead adaptive_counter_jump_init()
gvanrossum Mar 22, 2024
015bb00
Fix no-GIL build failure in _COLD_EXIT
gvanrossum Mar 22, 2024
1730295
Use the right named constant in initial temperature
gvanrossum Mar 23, 2024
95f93b7
Add pycore_backoff.h, and include it, but don't use it yet
gvanrossum Mar 26, 2024
7df0f10
Reimplement adaptive counters in terms of backoff_counter
gvanrossum Mar 26, 2024
925cae7
Redefine T2 temperature as a backoff counter
gvanrossum Mar 27, 2024
f0c7fb0
Don't increment branch cache (bitmask) in INSTRUMENTED_INSTRUCTION
gvanrossum Mar 27, 2024
8f79a60
Don't increment branch cache (bitmask) in INSTRUMENTED_LINE
gvanrossum Mar 27, 2024
149e9c4
Don't update unreachable counters
gvanrossum Mar 27, 2024
1d76112
Simplify dynamic counter initialization for JUMP_BACKWARD
gvanrossum Mar 27, 2024
a5ffe02
Revert "Don't increment branch cache (bitmask) in INSTRUMENTED_LINE"
gvanrossum Mar 27, 2024
ce7726c
Different approach to avoid incrementing bitmask in INSTRUMENTED_LINE
gvanrossum Mar 27, 2024
e2c39f2
Different approach to avoid incrementing bitmask in INSTRUMENTED_INST…
gvanrossum Mar 27, 2024
8d22790
Fix dynamic counter initialization for JUMP_BACKWARD
gvanrossum Mar 27, 2024
8000
cd8264e
Get rid of (unused) resume_threshold
gvanrossum Mar 27, 2024
d72c2ef
Hopeful fix for non-clang builds
gvanrossum Mar 27, 2024
f6bf194
In no-GIL mode, make INCREMENT_ADAPTIVE_COUNTER() a no-op
gvanrossum Mar 27, 2024
b991843
Revert "In no-GIL mode, make INCREMENT_ADAPTIVE_COUNTER() a no-op"
gvanrossum Mar 27, 2024
9b9f26a
Fix comment and fix size of optimizer_backedge_threshold
gvanrossum Mar 27, 2024
354cd81
_COLD_EXIT is not conditional on ENABLE_SPECIALIZATION
gvanrossum Mar 27, 2024
c1de44f
Rewrite _COLD_EXIT using backoff_counter_t directly
gvanrossum Mar 27, 2024
1fe27c9
Rename increment,decrement to advance,pause
gvanrossum Mar 27, 2024
225ea17
Give up on restricting backoff to <= 12
gvanrossum Mar 27, 2024
63d8bc7
Rip out backoff thresholds
gvanrossum Mar 29, 2024
8aa2c75
Fix tests
gvanrossum Mar 29, 2024
7bd4daa
Fix initial temperature, clean up
gvanrossum Mar 29, 2024
3f1c58a
Merge branch 'main' into exp-backoff
gvanrossum Mar 29, 2024
8ce8068
Remove unused variable
gvanrossum Mar 29, 2024
7bb5618
Admit defeat: advance_backoff_counter() may be entered when value == 0
gvanrossum Mar 29, 2024
63e286c
Merge branch 'main' into exp-backoff
gvanrossum Apr 1, 2024
7f64392
Merge remote-tracking branch 'origin/main' into exp-backoff
gvanrossum Apr 2, 2024
8eee1b4
Put backoff field before value
gvanrossum Apr 2, 2024
42c1f26
Small cleanup in .h files
gvanrossum Apr 3, 2024
a80cd0a
Rename DECREMENT_ADAPTIVE_COUNTER to ADVANCE_...
gvanrossum Apr 3, 2024
545c60e
Rename ADAPTIVE_COUNTER_IS_ZERO to ..._TRIGGERS
gvanrossum Apr 3, 2024
6c0bb30
Rename backoff_counter_is_zero to ..._triggers
gvanrossum Apr 3, 2024
a7c9b6d
Rename reset_background_counter to restart_...
gvanrossum Apr 3, 2024
3fee35f
Make _Py_BackoffCounter a member of _Py_CODEUNIT
gvanrossum Apr 3, 2024
df6f34c
Refactor initial counter values.
gvanrossum Apr 3, 2024
72f6b0d
Export tier 2 threshold from _testinternalcapi
gvanrossum Apr 3, 2024
dcee362
Merge remote-tracking branch 'origin/main' into exp-backoff
gvanrossum Apr 3, 2024
f38d922
Add news
gvanrossum Apr 3, 2024
ef6366b
Fix blurb formatting (I hope)
gvanrossum Apr 4, 2024
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
Reimplement adaptive counters in terms of backoff_counter
  • Loading branch information
gvanrossum committed Mar 26, 2024
commit 7df0f10d9018f12cbb0f07b0efcc87193ccc9c77
24 changes: 5 additions & 19 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,14 @@ write_location_entry_start(uint8_t *ptr, int code, int length)

/** Counters
* The first 16-bit value in each inline cache is a counter.
* When counting misses, the counter is treated as a simple unsigned value.
*
* When counting executions until the next specialization attempt,
* exponential backoff is used to reduce the number of specialization failures.
* The high 12 bits store the counter, the low 4 bits store the backoff exponent.
* On a specialization failure, the backoff exponent is incremented and the
* counter set to (2**backoff - 1).
* Backoff == 6 -> starting counter == 63, backoff == 10 -> starting counter == 1023.
* See pycore_backoff.h for more details.
* On a specialization failure, the backoff counter is reset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* On a specialization failure, the backoff counter is reset.
* On a specialization failure, the backoff counter is restarted.

*/

/* With a 16-bit counter, we have 12 bits for the counter value, and 4 bits for the backoff */
#define ADAPTIVE_BACKOFF_BITS 4
#include "pycore_backoff.h"

// A value of 1 means that we attempt to specialize the *second* time each
// instruction is executed. Executing twice is a much better indicator of
Expand All @@ -477,13 +473,9 @@ write_location_entry_start(uint8_t *ptr, int code, int length)
#define ADAPTIVE_COOLDOWN_VALUE 52
#define ADAPTIVE_COOLDOWN_BACKOFF 0

#define MAX_BACKOFF_VALUE (16 - ADAPTIVE_BACKOFF_BITS)


static inline uint16_t
adaptive_counter_bits(uint16_t value, uint16_t backoff) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe drop the adaptive_counter functions and replace them with the backoff_counter equivalents?

To do that cleanly we will probably need to change _Py_CODEUNIT to:

typedef union {
    uint16_t cache;
    struct {
        uint8_t code;
        uint8_t arg;
    } op;
    backoff_counter_t counter;
} _Py_CODEUNIT;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to keep them and not expose the backoff counter structure to other places.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could dispense with adaptive_counter_bits(), using make_backoff_counter() directly below, but I would oppose getting rid of the cooldown() and warmup() helpers, because they are used in several/many places.

return ((value << ADAPTIVE_BACKOFF_BITS)
| (backoff & ((1 << ADAPTIVE_BACKOFF_BITS) - 1)));
return make_backoff_counter(value, backoff).counter;
}

static inline uint16_t
Expand All @@ -500,13 +492,7 @@ adaptive_counter_cooldown(void) {

static inline uint16_t
adaptive_counter_backoff(uint16_t counter) {
uint16_t backoff = counter & ((1 << ADAPTIVE_BACKOFF_BITS) - 1);
backoff++;
if (backoff > MAX_BACKOFF_VALUE) {
backoff = MAX_BACKOFF_VALUE;
}
uint16_t value = (uint16_t)(1 << backoff) - 1;
return adaptive_counter_bits(value, backoff);
return reset_backoff_counter(forge_backoff_counter(counter)).counter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this confusing. Is there a way to do this with a single call, rather than two calls and a field read?

}


Expand Down
10 changes: 3 additions & 7 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
}

#define ADAPTIVE_COUNTER_IS_ZERO(COUNTER) \
(((COUNTER) >> ADAPTIVE_BACKOFF_BITS) == 0)

#define ADAPTIVE_COUNTER_IS_MAX(COUNTER) \
(((COUNTER) >> ADAPTIVE_BACKOFF_BITS) == ((1 << MAX_BACKOFF_VALUE) - 1))
backoff_counter_is_zero(forge_backoff_counter((COUNTER)))

#ifdef Py_GIL_DISABLED
#define DECREMENT_ADAPTIVE_COUNTER(COUNTER) \
Expand All @@ -305,14 +302,13 @@ GETITEM(PyObject *v, Py_ssize_t i) {
#else
#define DECREMENT_ADAPTIVE_COUNTER(COUNTER) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this with the backoff counter function?

Copy link
Member Author
@gvanrossum gvanrossum Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hoping to avoid 30+ diff chunks where I just replace this macro call with another. Also it is (for now) redefined when the GIL is disabled.

6D47

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about a few extra lines in the diff (there are 14 uses of DECREMENT_ADAPTIVE_COUNTER in bytecodes.c) if it makes the code simpler. I only need to review the diff once or twice, we need to work with the merged code for years.

do { \
assert(!ADAPTIVE_COUNTER_IS_ZERO((COUNTER))); \
(COUNTER) -= (1 << ADAPTIVE_BACKOFF_BITS); \
(COUNTER) = decrement_backoff_counter(forge_backoff_counter((COUNTER))).counter; \
} while (0);
#endif

#define INCREMENT_ADAPTIVE_COUNTER(COUNTER) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I changed, it's much more esoteric. It's now called PAUSE.

do { \
(COUNTER) += (1 << ADAPTIVE_BACKOFF_BITS); \
(COUNTER) = increment_backoff_counter(forge_backoff_counter((COUNTER))).counter; \
} while (0);

#define UNBOUNDLOCAL_ERROR_MSG \
Expand Down
0