Skip to content

Commit d873fb4

Browse files
authored
gh-137838: Move _PyUOpInstruction buffer to PyInterpreterState (gh-138918)
1 parent a9b6b09 commit d873fb4

File tree

8 files changed

+87
-31
lines changed

8 files changed

+87
-31
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
#include "pycore_structs.h" // PyHamtObject
1515
#include "pycore_tstate.h" // _PyThreadStateImpl
1616
#include "pycore_typedefs.h" // _PyRuntimeState
17+
#include "pycore_uop.h" // struct _PyUOpInstruction
1718

1819

1920
#define CODE_MAX_WATCHERS 8
@@ -949,6 +950,8 @@ struct _is {
949950
struct callable_cache callable_cache;
950951
PyObject *common_consts[NUM_COMMON_CONSTANTS];
951952
bool jit;
953+
bool compiling;
954+
struct _PyUOpInstruction *jit_uop_buffer;
952955
struct _PyExecutorObject *executor_list_head;
953956
struct _PyExecutorObject *executor_deletion_list_head;
954957
struct _PyExecutorObject *cold_executor;

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_typedefs.h" // _PyInterpreterFrame
12+
#include "pycore_uop.h" // _PyUOpInstruction
1213
#include "pycore_uop_ids.h"
1314
#include "pycore_stackref.h" // _PyStackRef
1415
#include <stdbool.h>
@@ -41,32 +42,6 @@ typedef struct {
4142
PyCodeObject *code; // Weak (NULL if no corresponding ENTER_EXECUTOR).
4243
} _PyVMData;
4344

44-
/* Depending on the format,
45-
* the 32 bits between the oparg and operand are:
46-
* UOP_FORMAT_TARGET:
47-
* uint32_t target;
48-
* UOP_FORMAT_JUMP
49-
* uint16_t jump_target;
50-
* uint16_t error_target;
51-
*/
52-
typedef struct {
53-
uint16_t opcode:15;
54-
uint16_t format:1;
55-
uint16_t oparg;
56-
union {
57-
uint32_t target;
58-
struct {
59-
uint16_t jump_target;
60-
uint16_t error_target;
61-
};
62-
};
63-
uint64_t operand0; // A cache entry
64-
uint64_t operand1;
65-
#ifdef Py_STATS
66-
uint64_t execution_count;
67-
#endif
68-
} _PyUOpInstruction;
69-
7045
typedef struct _PyExitData {
7146
uint32_t target;
7247
uint16_t index;
@@ -118,9 +93,6 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
11893
// trace_run_counter is greater than this value.
11994
#define JIT_CLEANUP_THRESHOLD 100000
12095

121-
// This is the length of the trace we project initially.
122-
#define UOP_MAX_TRACE_LENGTH 1200
123-
12496
#define TRACE_STACK_SIZE 5
12597

12698
int _Py_uop_analyze_and_optimize(_PyInterpreterFrame *frame,

Include/internal/pycore_uop.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef Py_CORE_UOP_H
2+
#define Py_CORE_UOP_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
#include <stdint.h>
12+
/* Depending on the format,
13+
* the 32 bits between the oparg and operand are:
14+
* UOP_FORMAT_TARGET:
15+
* uint32_t target;
16+
* UOP_FORMAT_JUMP
17+
* uint16_t jump_target;
18+
* uint16_t error_target;
19+
*/
20+
typedef struct _PyUOpInstruction{
21+
uint16_t opcode:15;
22+
uint16_t format:1;
23+
uint16_t oparg;
24+
union {
25+
uint32_t target;
26+
struct {
27+
uint16_t jump_target;
28+
uint16_t error_target;
29+
};
30+
};
31+
uint64_t operand0; // A cache entry
32+
uint64_t operand1;
33+
#ifdef Py_STATS
34+
uint64_t execution_count;
35+
#endif
36+
} _PyUOpInstruction;
37+
38+
// This is the length of the trace we project initially.
39+
#define UOP_MAX_TRACE_LENGTH 1200
40+
#define UOP_BUFFER_SIZE (UOP_MAX_TRACE_LENGTH * sizeof(_PyUOpInstruction))
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
#endif /* !Py_INTERNAL_UOP_H */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ PYTHON_HEADERS= \
14351435
$(srcdir)/Include/internal/pycore_unicodeobject_generated.h \
14361436
$(srcdir)/Include/internal/pycore_unionobject.h \
14371437
$(srcdir)/Include/internal/pycore_uniqueid.h \
1438+
$(srcdir)/Include/internal/pycore_uop.h \
14381439
$(srcdir)/Include/internal/pycore_uop_ids.h \
14391440
$(srcdir)/Include/internal/pycore_uop_metadata.h \
14401441
$(srcdir)/Include/internal/pycore_warnings.h \

PCbuild/pythoncore.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,15 @@
882882
<ClInclude Include="..\Include\internal\pycore_uniqueid.h">
883883
<Filter>Include\internal</Filter>
884884
</ClInclude>
885+
<ClInclude Include="..\Include\internal\pycore_uop.h">
886+
<Filter>Include\internal</Filter>
887+
</ClInclude>
888+
<ClInclude Include="..\Include\internal\pycore_uop_ids.h">
889+
<Filter>Include\internal</Filter>
890+
</ClInclude>
891+
<ClInclude Include="..\Include\internal\pycore_uop_metadata.h">
892+
<Filter>Include\internal</Filter>
893+
</ClInclude>
885894
<ClInclude Include="..\Include\internal\mimalloc\mimalloc.h">
886895
<Filter>Include\internal\mimalloc</Filter>
887896
</ClInclude>

Python/optimizer.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ _PyOptimizer_Optimize(
116116
_PyExecutorObject **executor_ptr, int chain_depth)
117117
{
118118
_PyStackRef *stack_pointer = frame->stackpointer;
119-
assert(_PyInterpreterState_GET()->jit);
119+
PyInterpreterState *interp = _PyInterpreterState_GET();
120+
assert(interp->jit);
121+
assert(!interp->compiling);
122+
interp->compiling = true;
120123
// The first executor in a chain and the MAX_CHAIN_DEPTH'th executor *must*
121124
// make progress in order to avoid infinite loops or excessively-long
122125
// side-exit chains. We can only insert the executor into the bytecode if
@@ -126,10 +129,12 @@ _PyOptimizer_Optimize(
126129
PyCodeObject *code = _PyFrame_GetCode(frame);
127130
assert(PyCode_Check(code));
128131
if (progress_needed && !has_space_for_executor(code, start)) {
132+
interp->compiling = false;
129133
return 0;
130134
}
131135
int err = uop_optimize(frame, start, executor_ptr, (int)(stack_pointer - _PyFrame_Stackbase(frame)), progress_needed);
132136
if (err <= 0) {
137+
interp->compiling = false;
133138
return err;
134139
}
135140
assert(*executor_ptr != NULL);
@@ -143,6 +148,7 @@ _PyOptimizer_Optimize(
143148
* it might get confused by the executor disappearing,
144149
* but there is not much we can do about that here. */
145150
Py_DECREF(*executor_ptr);
151+
interp->compiling = false;
146152
return 0;
147153
}
148154
insert_executor(code, start, index, *executor_ptr);
@@ -152,6 +158,7 @@ _PyOptimizer_Optimize(
152158
}
153159
(*executor_ptr)->vm_data.chain_depth = chain_depth;
154160
assert((*executor_ptr)->vm_data.valid);
161+
interp->compiling = false;
155162
return 1;
156163
}
157164

@@ -1280,7 +1287,14 @@ uop_optimize(
12801287
{
12811288
_PyBloomFilter dependencies;
12821289
_Py_BloomFilter_Init(&dependencies);
1283-
_PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH];
1290+
PyInterpreterState *interp = _PyInterpreterState_GET();
1291+
if (interp->jit_uop_buffer == NULL) {
1292+
interp->jit_uop_buffer = (_PyUOpInstruction *)_PyObject_VirtualAlloc(UOP_BUFFER_SIZE);
1293+
if (interp->jit_uop_buffer == NULL) {
1294+
return 0;
1295+
}
1296+
}
1297+
_PyUOpInstruction *buffer = interp->jit_uop_buffer;
12841298
OPT_STAT_INC(attempts);
12851299
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
12861300
bool is_noopt = true;

Python/pylifecycle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ finalize_modules(PyThreadState *tstate)
17021702

17031703
// Invalidate all executors and turn off JIT:
17041704
interp->jit = false;
1705+
interp->compiling = false;
17051706
#ifdef _Py_TIER2
17061707
_Py_Executors_InvalidateAll(interp, 0);
17071708
#endif

Python/pystate.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
2323
#include "pycore_stackref.h" // Py_STACKREF_DEBUG
2424
#include "pycore_time.h" // _PyTime_Init()
25+
#include "pycore_uop.h" // UOP_BUFFER_SIZE
2526
#include "pycore_uniqueid.h" // _PyObject_FinalizePerThreadRefcounts()
2627

2728

@@ -550,6 +551,11 @@ init_interpreter(PyInterpreterState *interp,
550551
#ifdef Py_GIL_DISABLED
551552
_Py_brc_init_state(interp);
552553
#endif
554+
555+
#ifdef _Py_TIER2
556+
// Ensure the buffer is to be set as NULL.
557+
interp->jit_uop_buffer = NULL;
558+
#endif
553559
llist_init(&interp->mem_free_queue.head);
554560
llist_init(&interp->asyncio_tasks_head);
555561
interp->asyncio_tasks_lock = (PyMutex){0};
@@ -565,6 +571,7 @@ init_interpreter(PyInterpreterState *interp,
565571
}
566572
interp->_code_object_generation = 0;
567573
interp->jit = false;
574+
interp->compiling = false;
568575
interp->executor_list_head = NULL;
569576
interp->executor_deletion_list_head = NULL;
570577
interp->executor_deletion_list_remaining_capacity = 0;
@@ -797,6 +804,10 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
797804

798805
#ifdef _Py_TIER2
799806
_Py_ClearExecutorDeletionList(interp);
807+
if (interp->jit_uop_buffer != NULL) {
808+
_PyObject_VirtualFree(interp->jit_uop_buffer, UOP_BUFFER_SIZE);
809+
interp->jit_uop_buffer = NULL;
810+
}
800811
#endif
801812
_PyAST_Fini(interp);
802813
_PyAtExit_Fini(interp);

0 commit comments

Comments
 (0)