Skip to content

Commit fe3c1af

Browse files
committed
gh-137838: Move _PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH] to _PyThreadStateImpl
1 parent 097fc12 commit fe3c1af

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
#include "pycore_typedefs.h" // _PyInterpreterFrame
1212
#include "pycore_uop_ids.h"
1313
#include "pycore_stackref.h" // _PyStackRef
14+
#include "pycore_tstate.h" // _PyUOpInstruction
1415
#include <stdbool.h>
1516

1617

@@ -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_tstate.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,43 @@ extern "C" {
1313
#include "pycore_mimalloc.h" // struct _mimalloc_thread_state
1414
#include "pycore_qsbr.h" // struct qsbr
1515

16-
1716
#ifdef Py_GIL_DISABLED
1817
struct _gc_thread_state {
1918
/* Thread-local allocation count. */
2019
Py_ssize_t alloc_count;
2120
};
2221
#endif
2322

23+
/* Depending on the format,
24+
* the 32 bits between the oparg and operand are:
25+
* UOP_FORMAT_TARGET:
26+
* uint32_t target;
27+
* UOP_FORMAT_JUMP
28+
* uint16_t jump_target;
29+
* uint16_t error_target;
30+
*/
31+
typedef struct _PyUOpInstruction{
32+
uint16_t opcode:15;
33+
uint16_t format:1;
34+
uint16_t oparg;
35+
union {
36+
uint32_t target;
37+
struct {
38+
uint16_t jump_target;
39+
uint16_t error_target;
40+
};
41+
};
42+
uint64_t operand0; // A cache entry
43+
uint64_t operand1;
44+
#ifdef Py_STATS
45+
uint64_t execution_count;
46+
#endif
47+
} _PyUOpInstruction;
48+
49+
// This is the length of the trace we project initially.
50+
#define UOP_MAX_TRACE_LENGTH 1200
51+
52+
2453
// Every PyThreadState is actually allocated as a _PyThreadStateImpl. The
2554
// PyThreadState fields are exposed as part of the C API, although most fields
2655
// are intended to be private. The _PyThreadStateImpl fields not exposed.
@@ -75,6 +104,7 @@ typedef struct _PyThreadStateImpl {
75104
#if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED)
76105
Py_ssize_t reftotal; // this thread's total refcount operations
77106
#endif
107+
struct _PyUOpInstruction buffer[1200];
78108

79109
} _PyThreadStateImpl;
80110

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Move _PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH] to _PyThreadStateImpl.
2+
Patch By Donghee Na.

Python/optimizer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,8 @@ uop_optimize(
12801280
{
12811281
_PyBloomFilter dependencies;
12821282
_Py_BloomFilter_Init(&dependencies);
1283-
_PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH];
1283+
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
1284+
_PyUOpInstruction *buffer = tstate->buffer;
12841285
OPT_STAT_INC(attempts);
12851286
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
12861287
bool is_noopt = true;

0 commit comments

Comments
 (0)