From 683830838a505eceaa54d991f1077be251026143 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Wed, 17 Apr 2024 15:52:29 +1000 Subject: [PATCH 1/8] Expose JIT code via access method in byte string for experimental UOp Executor Type --- Python/optimizer.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 5c69d9d5de92eb..18703255e0067d 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -394,6 +394,29 @@ executor_traverse(PyObject *o, visitproc visit, void *arg) return 0; } +static PyObject * +get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + if (!Py_IS_TYPE(self, &_PyUOpExecutor_Type)) { + PyErr_SetString(PyExc_TypeError, "get_jit_code() requires a uop_executor object."); + return NULL; + } + _PyExecutorObject *executor = (_PyExecutorObject *)self; + if (executor->jit_code == NULL || executor->jit_size == 0) { + PyErr_SetString(PyExc_ValueError, "No JIT code available."); + return NULL; + } + return PyBytes_FromStringAndSize(executor->jit_code, executor->jit_size); +} + +static PyMethodDef uop_executor_methods[] = { + { "is_valid", is_valid, METH_NOARGS, NULL }, + { "get_jit_code", get_jit_code, METH_NOARGS, NULL}, + { "get_opcode", get_opcode, METH_NOARGS, NULL }, + { "get_oparg", get_oparg, METH_NOARGS, NULL }, + { NULL, NULL }, +}; + PyTypeObject _PyUOpExecutor_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) .tp_name = "uop_executor", @@ -402,7 +425,7 @@ PyTypeObject _PyUOpExecutor_Type = { .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC, .tp_dealloc = (destructor)uop_dealloc, .tp_as_sequence = &uop_as_sequence, - .tp_methods = executor_methods, + .tp_methods = uop_executor_methods, .tp_traverse = executor_traverse, .tp_clear = executor_clear, }; From 7be0d85cc0f6d3fcc87f854bbd979d02b55a443d Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Thu, 18 Apr 2024 13:45:33 +1000 Subject: [PATCH 2/8] Catch undefined behaviour of JIT fields --- Python/optimizer.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Python/optimizer.c b/Python/optimizer.c index 18703255e0067d..b9e4eb574629e2 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -397,6 +397,10 @@ executor_traverse(PyObject *o, visitproc visit, void *arg) static PyObject * get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) { +#ifndef _Py_JIT + PyErr_SetString(PyExc_RuntimeError, "JIT support not enabled."); + return NULL; +#endif if (!Py_IS_TYPE(self, &_PyUOpExecutor_Type)) { PyErr_SetString(PyExc_TypeError, "get_jit_code() requires a uop_executor object."); return NULL; From 21b547e212874d962dc1b1111d4e44b74d643640 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 18 Apr 2024 03:49:45 +0000 Subject: [PATCH 3/8] =?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 --- .../2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst new file mode 100644 index 00000000000000..5a32390421f356 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst @@ -0,0 +1,2 @@ +Added a method to access JIT compiled machine code from the UOp Executor when the experimental JIT is enabled. Patch +by Anthony Shaw From 87f1d74c21682e295e3fc05e755105c08dad5b38 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Thu, 18 Apr 2024 18:22:09 +1000 Subject: [PATCH 4/8] Update Python/optimizer.c Co-authored-by: Jelle Zijlstra --- Python/optimizer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index b9e4eb574629e2..a6fda232bf8e82 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -400,7 +400,7 @@ get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) #ifndef _Py_JIT PyErr_SetString(PyExc_RuntimeError, "JIT support not enabled."); return NULL; -#endif +#else if (!Py_IS_TYPE(self, &_PyUOpExecutor_Type)) { PyErr_SetString(PyExc_TypeError, "get_jit_code() requires a uop_executor object."); return NULL; @@ -411,6 +411,7 @@ get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) return NULL; } return PyBytes_FromStringAndSize(executor->jit_code, executor->jit_size); +#endif } static PyMethodDef uop_executor_methods[] = { From 4b1341984f8f443120670d8f95776cc2ea703e7f Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Fri, 19 Apr 2024 10:37:44 +1000 Subject: [PATCH 5/8] Update Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst Co-authored-by: Guido van Rossum --- .../2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst index 5a32390421f356..3c8927b9206e98 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst @@ -1,2 +1,2 @@ Added a method to access JIT compiled machine code from the UOp Executor when the experimental JIT is enabled. Patch -by Anthony Shaw +by Anthony Shaw. From bc9284a5a0f156ae4750a193d91b4acbca41b2fe Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Fri, 19 Apr 2024 10:38:15 +1000 Subject: [PATCH 6/8] Update 2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst --- .../2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst index 3c8927b9206e98..c127786bc129b1 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-18-03-49-41.gh-issue-117958.-EsfUs.rst @@ -1,2 +1,2 @@ -Added a method to access JIT compiled machine code from the UOp Executor when the experimental JIT is enabled. Patch +Added a ``get_jit_code()`` method to access JIT compiled machine code from the UOp Executor when the experimental JIT is enabled. Patch by Anthony Shaw. From bf5bd37c27471982e91b685ff0a3199d56b10be0 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 23 Apr 2024 11:39:47 +1000 Subject: [PATCH 7/8] Update Python/optimizer.c Co-authored-by: Brandt Bucher --- Python/optimizer.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index a6fda232bf8e82..43b9823e24e04b 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -401,10 +401,6 @@ get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) PyErr_SetString(PyExc_RuntimeError, "JIT support not enabled."); return NULL; #else - if (!Py_IS_TYPE(self, &_PyUOpExecutor_Type)) { - PyErr_SetString(PyExc_TypeError, "get_jit_code() requires a uop_executor object."); - return NULL; - } _PyExecutorObject *executor = (_PyExecutorObject *)self; if (executor->jit_code == NULL || executor->jit_size == 0) { PyErr_SetString(PyExc_ValueError, "No JIT code available."); From 23bbe83119a096185e0528276318888fa347803c Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 23 Apr 2024 11:45:39 +1000 Subject: [PATCH 8/8] Return none if there is no JIT --- Python/optimizer.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index a6fda232bf8e82..891c0079a94674 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -407,8 +407,7 @@ get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored)) } _PyExecutorObject *executor = (_PyExecutorObject *)self; if (executor->jit_code == NULL || executor->jit_size == 0) { - PyErr_SetString(PyExc_ValueError, "No JIT code available."); - return NULL; + Py_RETURN_NONE; } return PyBytes_FromStringAndSize(executor->jit_code, executor->jit_size); #endif