From 8b40e192e3153240a3ed16fc0fcc7fb6b941d057 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 6 Sep 2024 15:15:35 -0700 Subject: [PATCH 1/5] Use Laplace's rule of succession for trace confidence --- Python/optimizer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 978649faa04d45..2113d559b9a3f7 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -643,14 +643,15 @@ translate_bytecode_to_trace( int bitcount = _Py_popcount32(counter); int jump_likely = bitcount > 8; /* 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). + If it's 16 or 0 (all or none were taken), adjust by ~6% + (since the future is still somewhat uncertain - see Laplace's + rule of succession, used here with 16 observations). For values in between, adjust proportionally. */ if (jump_likely) { - confidence = confidence * (bitcount + 2) / 20; + confidence = confidence * (bitcount + 1) / 18; } else { - confidence = confidence * (18 - bitcount) / 20; + confidence = confidence * (17 - bitcount) / 18; } 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", From 130eede300efae17404ae44609842acd3f2bac46 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 6 Sep 2024 15:15:58 -0700 Subject: [PATCH 2/5] Clarify exact value --- Python/optimizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 2113d559b9a3f7..0ff09b3a284292 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -643,7 +643,7 @@ translate_bytecode_to_trace( int bitcount = _Py_popcount32(counter); int jump_likely = bitcount > 8; /* 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 ~6% + If it's 16 or 0 (all or none were taken), adjust by 1/18th. (since the future is still somewhat uncertain - see Laplace's rule of succession, used here with 16 observations). For values in between, adjust proportionally. */ From 18cf54743b31366fc6069a6529dd06acf7e77cbc Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Sat, 7 Sep 2024 13:38:27 -0700 Subject: [PATCH 3/5] Don't assume the future is uncertain --- Python/optimizer.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 0ff09b3a284292..4d12a7f45ca8f3 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -643,15 +643,13 @@ translate_bytecode_to_trace( int bitcount = _Py_popcount32(counter); int jump_likely = bitcount > 8; /* 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 1/18th. - (since the future is still somewhat uncertain - see Laplace's - rule of succession, used here with 16 observations). + If it's 16 or 0 (all or none were taken), adjust by 0%. For values in between, adjust proportionally. */ if (jump_likely) { - confidence = confidence * (bitcount + 1) / 18; + confidence = confidence * bitcount / 16; } else { - confidence = confidence * (17 - bitcount) / 18; + confidence = confidence * (16 - bitcount) / 16; } 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", From 6c156af38df6247ccbd36c7ba0e54700021bc519 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Mon, 30 Sep 2024 16:39:45 -0700 Subject: [PATCH 4/5] blurb add --- .../2024-09-30-16-39-37.gh-issue-118093.J2A3gz.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-09-30-16-39-37.gh-issue-118093.J2A3gz.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-09-30-16-39-37.gh-issue-118093.J2A3gz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-30-16-39-37.gh-issue-118093.J2A3gz.rst new file mode 100644 index 00000000000000..2e5c64581b6aef --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-30-16-39-37.gh-issue-118093.J2A3gz.rst @@ -0,0 +1,2 @@ +Improve the experimental JIT compiler's ability to stay "on trace" when +encountering highly-biased branches. From 4e14b5cee43035249609f44c6fe5421fad1d116d Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 2 Oct 2024 11:59:36 -0700 Subject: [PATCH 5/5] Remove weird comment --- Python/optimizer.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 4d12a7f45ca8f3..b876b6c2bd72fd 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -643,7 +643,6 @@ translate_bytecode_to_trace( int bitcount = _Py_popcount32(counter); int jump_likely = bitcount > 8; /* 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 0%. For values in between, adjust proportionally. */ if (jump_likely) { confidence = confidence * bitcount / 16;