Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9e4a81f

Browse files
authoredJun 26, 2024
pythongh-120642: Move private PyCode APIs to the internal C API (python#120643)
* Move _Py_CODEUNIT and related functions to pycore_code.h. * Move _Py_BackoffCounter to pycore_backoff.h. * Move Include/cpython/optimizer.h content to pycore_optimizer.h. * Remove Include/cpython/optimizer.h. * Remove PyUnstable_Replace_Executor(). Rename functions: * PyUnstable_GetExecutor() => _Py_GetExecutor() * PyUnstable_GetOptimizer() => _Py_GetOptimizer() * PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer() * PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter() * PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()
1 parent 9e45fd9 commit 9e4a81f

18 files changed

+212
-227
lines changed
 

‎Include/Python.h

-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,5 @@
132132
#include "fileutils.h"
133133
#include "cpython/pyfpe.h"
134134
#include "cpython/tracemalloc.h"
135-
#include "cpython/optimizer.h"
136135

137136
#endif /* !Py_PYTHON_H */

‎Include/cpython/code.h

-52
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,6 @@ typedef struct _Py_GlobalMonitors {
2424
uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
2525
} _Py_GlobalMonitors;
2626

27-
typedef struct {
28-
union {
29-
struct {
30-
uint16_t backoff : 4;
31-
uint16_t value : 12;
32-
};
33-
uint16_t as_counter; // For printf("%#x", ...)
34-
};
35-
} _Py_BackoffCounter;
36-
37-
/* Each instruction in a code object is a fixed-width value,
38-
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
39-
* opcode allows for larger values but the current limit is 3 uses
40-
* of EXTENDED_ARG (see Python/compile.c), for a maximum
41-
* 32-bit value. This aligns with the note in Python/compile.c
42-
* (compiler_addop_i_line) indicating that the max oparg value is
43-
* 2**32 - 1, rather than INT_MAX.
44-
*/
45-
46-
typedef union {
47-
uint16_t cache;
48-
struct {
49-
uint8_t code;
50-
uint8_t arg;
51-
} op;
52-
_Py_BackoffCounter counter; // First cache entry of specializable op
53-
} _Py_CODEUNIT;
54-
55-
56-
/* These macros only remain defined for compatibility. */
57-
#define _Py_OPCODE(word) ((word).op.code)
58-
#define _Py_OPARG(word) ((word).op.arg)
59-
60-
static inline _Py_CODEUNIT
61-
_py_make_codeunit(uint8_t opcode, uint8_t oparg)
62-
{
63-
// No designated initialisers because of C++ compat
64-
_Py_CODEUNIT word;
65-
word.op.code = opcode;
66-
word.op.arg = oparg;
67-
return word;
68-
}
69-
70-
static inline void
71-
_py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
72-
{
73-
word->op.code = opcode;
74-
}
75-
76-
#define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg))
77-
#define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode))
78-
7927

8028
typedef struct {
8129
PyObject *_co_code;

‎Include/cpython/optimizer.h

-135
This file was deleted.

‎Include/internal/pycore_backoff.h

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ extern "C" {
1313
#include <stdbool.h>
1414
#include <stdint.h>
1515

16+
17+
typedef struct {
18+
union {
19+
struct {
20+
uint16_t backoff : 4;
21+
uint16_t value : 12;
22+
};
23+
uint16_t as_counter; // For printf("%#x", ...)
24+
};
25+
} _Py_BackoffCounter;
26+
27+
1628
/* 16-bit countdown counters using exponential backoff.
1729
1830
These are used by the adaptive specializer to count down until

‎Include/internal/pycore_code.h

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,50 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_lock.h" // PyMutex
12+
#include "pycore_backoff.h" // _Py_BackoffCounter
13+
14+
15+
/* Each instruction in a code object is a fixed-width value,
16+
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
17+
* opcode allows for larger values but the current limit is 3 uses
18+
* of EXTENDED_ARG (see Python/compile.c), for a maximum
19+
* 32-bit value. This aligns with the note in Python/compile.c
20+
* (compiler_addop_i_line) indicating that the max oparg value is
21+
* 2**32 - 1, rather than INT_MAX.
22+
*/
23+
24+
typedef union {
25+
uint16_t cache;
26+
struct {
27+
uint8_t code;
28+
uint8_t arg;
29+
} op;
30+
_Py_BackoffCounter counter; // First cache entry of specializable op
31+
} _Py_CODEUNIT;
32+
33+
34+
/* These macros only remain defined for compatibility. */
35+
#define _Py_OPCODE(word) ((word).op.code)
36+
#define _Py_OPARG(word) ((word).op.arg)
37+
38+
static inline _Py_CODEUNIT
39+
_py_make_codeunit(uint8_t opcode, uint8_t oparg)
40+
{
41+
// No designated initialisers because of C++ compat
42+
_Py_CODEUNIT word;
43+
word.op.code = opcode;
44+
word.op.arg = oparg;
45+
return word;
46+
}
47+
48+
static inline void
49+
_py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
50+
{
51+
word->op.code = opcode;
52+
}
53+
54+
#define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg))
55+
#define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode))
1256

1357

1458
// We hide some of the newer PyCodeObject fields behind macros.

‎Include/internal/pycore_interp.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#include "pycore_list.h" // struct _Py_list_state
3131
#include "pycore_mimalloc.h" // struct _mimalloc_interp_state
3232
#include "pycore_object_state.h" // struct _py_object_state
33+
#include "pycore_optimizer.h" // _PyOptimizerObject
3334
#include "pycore_obmalloc.h" // struct _obmalloc_state
3435
#include "pycore_qsbr.h" // struct _qsbr_state
3536
#include "pycore_tstate.h" // _PyThreadStateImpl

0 commit comments

Comments
 (0)