From 657ce1149c9c265704c00db5980bff6b98be308d Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Thu, 23 Jan 2025 16:37:58 +0000 Subject: [PATCH 1/3] gh-129231: group executable JIT code in memory --- Python/jit.c | 10 +++++----- Tools/jit/_stencils.py | 2 +- Tools/jit/_writer.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Python/jit.c b/Python/jit.c index 7dd0da7a45055a..92b26a1e677c03 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -501,8 +501,8 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz // Round up to the nearest page: size_t page_size = get_page_size(); assert((page_size & (page_size - 1)) == 0); - size_t padding = page_size - ((code_size + data_size + state.trampolines.size) & (page_size - 1)); - size_t total_size = code_size + data_size + state.trampolines.size + padding; + size_t padding = page_size - ((code_size + state.trampolines.size + data_size) & (page_size - 1)); + size_t total_size = code_size + state.trampolines.size + data_size + padding; unsigned char *memory = jit_alloc(total_size); if (memory == NULL) { return -1; @@ -516,8 +516,8 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz } // Loop again to emit the code: unsigned char *code = memory; - unsigned char *data = memory + code_size; - state.trampolines.mem = memory + code_size + data_size; + state.trampolines.mem = memory + code_size; + unsigned char *data = memory + code_size + state.trampolines.size; // Compile the shim, which handles converting between the native // calling convention and the calling convention used by jitted code // (which may be different for efficiency reasons). @@ -539,7 +539,7 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz code += group->code_size; data += group->data_size; assert(code == memory + code_size); - assert(data == memory + code_size + data_size); + assert(data == memory + code_size + state.trampolines.size + data_size); #ifdef MAP_JIT pthread_jit_write_protect_np(1); #endif diff --git a/Tools/jit/_stencils.py b/Tools/jit/_stencils.py index ee761a73fa808a..0fef0e7ff59b53 100644 --- a/Tools/jit/_stencils.py +++ b/Tools/jit/_stencils.py @@ -364,7 +364,7 @@ def _get_trampoline_mask(self) -> str: def as_c(self, opname: str) -> str: """Dump this hole as a StencilGroup initializer.""" - return f"{{emit_{opname}, {len(self.code.body)}, {len(self.data.body)}, {self._get_trampoline_mask()}}}" + return f"{{emit_{opname}, {len(self.code.body)}, {self._get_trampoline_mask()}, {len(self.data.body)}}}" def symbol_to_value(symbol: str) -> tuple[HoleValue, str | None]: diff --git a/Tools/jit/_writer.py b/Tools/jit/_writer.py index 5588784544ee00..c1550a0dbde5fb 100644 --- a/Tools/jit/_writer.py +++ b/Tools/jit/_writer.py @@ -18,8 +18,8 @@ def _dump_footer( yield " unsigned char *code, unsigned char *data, _PyExecutorObject *executor," yield " const _PyUOpInstruction *instruction, jit_state *state);" yield " size_t code_size;" - yield " size_t data_size;" yield " symbol_mask trampoline_mask;" + yield " size_t data_size;" yield "} StencilGroup;" yield "" yield f"static const StencilGroup shim = {groups['shim'].as_c('shim')};" From 60168ee79a90020ffc6b7028df1c95a9b7f50938 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:37:24 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-01-24-11-37-22.gh-issue-129231.ZsAP9v.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-01-24-11-37-22.gh-issue-129231.ZsAP9v.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-01-24-11-37-22.gh-issue-129231.ZsAP9v.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-24-11-37-22.gh-issue-129231.ZsAP9v.rst new file mode 100644 index 00000000000000..b30492a1947058 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-24-11-37-22.gh-issue-129231.ZsAP9v.rst @@ -0,0 +1 @@ +Improve memory layout of JIT traces. Patch by Diego Russo From 21212d98f06e443d7d0e9d832fb3e4b70f091175 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Fri, 31 Jan 2025 15:24:20 +0000 Subject: [PATCH 3/3] Address Brandt's feedback --- Tools/jit/_stencils.py | 2 +- Tools/jit/_writer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/jit/_stencils.py b/Tools/jit/_stencils.py index 0fef0e7ff59b53..ee761a73fa808a 100644 --- a/Tools/jit/_stencils.py +++ b/Tools/jit/_stencils.py @@ -364,7 +364,7 @@ def _get_trampoline_mask(self) -> str: def as_c(self, opname: str) -> str: """Dump this hole as a StencilGroup initializer.""" - return f"{{emit_{opname}, {len(self.code.body)}, {self._get_trampoline_mask()}, {len(self.data.body)}}}" + return f"{{emit_{opname}, {len(self.code.body)}, {len(self.data.body)}, {self._get_trampoline_mask()}}}" def symbol_to_value(symbol: str) -> tuple[HoleValue, str | None]: diff --git a/Tools/jit/_writer.py b/Tools/jit/_writer.py index c1550a0dbde5fb..5588784544ee00 100644 --- a/Tools/jit/_writer.py +++ b/Tools/jit/_writer.py @@ -18,8 +18,8 @@ def _dump_footer( yield " unsigned char *code, unsigned char *data, _PyExecutorObject *executor," yield " const _PyUOpInstruction *instruction, jit_state *state);" yield " size_t code_size;" - yield " symbol_mask trampoline_mask;" yield " size_t data_size;" + yield " symbol_mask trampoline_mask;" yield "} StencilGroup;" yield "" yield f"static const StencilGroup shim = {groups['shim'].as_c('shim')};"