|
1 | 1 | #include <torch/csrc/dynamo/cpython_defs.h>
|
2 |
| - |
3 |
| -#ifdef _WIN32 |
4 |
| -#define unlikely(x) (x) |
5 |
| -#else |
6 |
| -#define unlikely(x) __builtin_expect((x), 0) |
7 |
| -#endif |
8 |
| - |
9 |
| -#define CHECK(cond) \ |
10 |
| - if (unlikely(!(cond))) { \ |
11 |
| - fprintf(stderr, "DEBUG CHECK FAILED: %s:%d\n", __FILE__, __LINE__); \ |
12 |
| - abort(); \ |
13 |
| - } else { \ |
14 |
| - } |
| 2 | +#include <torch/csrc/dynamo/cpython_includes.h> |
| 3 | +#include <torch/csrc/dynamo/debug_macros.h> |
15 | 4 |
|
16 | 5 | #if IS_PYTHON_3_11_PLUS
|
17 | 6 |
|
18 |
| -// Problem in CPython includes when mixing core and non-core build |
19 |
| -// The fix was not backported to 3.12 so this is needed here |
20 |
| -// https://github.com/python/cpython/issues/105268 |
21 |
| -#if IS_PYTHON_3_12_PLUS |
22 |
| -#undef _PyGC_FINALIZED |
23 |
| -#endif |
24 |
| - |
25 | 7 | #define Py_BUILD_CORE
|
26 |
| -#include <internal/pycore_pystate.h> |
27 |
| - |
28 | 8 | #define NEED_OPCODE_TABLES // To get _PyOpcode_Deopt, _PyOpcode_Caches
|
| 9 | + |
29 | 10 | #if IS_PYTHON_3_13_PLUS
|
30 | 11 | #include <cpython/code.h> // To get PyUnstable_Code_GetFirstFree
|
31 | 12 | #define NEED_OPCODE_METADATA
|
|
34 | 15 | #else
|
35 | 16 | #include <internal/pycore_opcode.h>
|
36 | 17 | #endif
|
37 |
| -#undef NEED_OPCODE_TABLES |
38 |
| - |
39 |
| -#include <internal/pycore_frame.h> |
40 | 18 |
|
| 19 | +#undef NEED_OPCODE_TABLES |
41 | 20 | #undef Py_BUILD_CORE
|
42 | 21 |
|
43 | 22 | // As a simple way to reduce the impact of ABI changes on the CPython side, this check forces
|
@@ -74,189 +53,10 @@ THP_PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
|
74 | 53 |
|
75 | 54 | #if IS_PYTHON_3_12_PLUS
|
76 | 55 |
|
77 |
| -// https://github.com/python/cpython/blob/0325a8a8cdba6c091bcbbb3c995f3bf1d1217012/Objects/frameobject.c#L1136 |
78 |
| -// Initialize frame free variables if needed |
79 |
| -// free_vars_copied argument added in order to let caller know that the COPY_FREE_VARS |
80 |
| -// codepath occurred. |
81 |
| -static void |
82 |
| -frame_init_get_vars(_PyInterpreterFrame *frame, int *free_vars_copied) |
83 |
| -{ |
84 |
| - // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt |
85 |
| - // here: |
86 |
| - PyCodeObject *co = F_CODE(frame); |
87 |
| - int lasti = _PyInterpreterFrame_LASTI(frame); |
88 |
| - if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS |
89 |
| - && PyFunction_Check(frame->f_funcobj))) |
90 |
| - { |
91 |
| - /* Free vars are initialized */ |
92 |
| - return; |
93 |
| - } |
94 |
| - |
95 |
| - /* Free vars have not been initialized -- Do that */ |
96 |
| - PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure; |
97 |
| - #if IS_PYTHON_3_13_PLUS |
98 |
| - int offset = PyUnstable_Code_GetFirstFree(co); |
99 |
| - #else |
100 |
| - int offset = PyCode_GetFirstFree(co); |
101 |
| - #endif |
102 |
| - for (int i = 0; i < co->co_nfreevars; ++i) { |
103 |
| - PyObject *o = PyTuple_GET_ITEM(closure, i); |
104 |
| - frame->localsplus[offset + i] = Py_NewRef(o); |
105 |
| - } |
106 |
| - // COPY_FREE_VARS doesn't have inline CACHEs, either: |
107 |
| - PREV_INSTR(frame) = _PyCode_CODE(F_CODE(frame)); |
108 |
| - |
109 |
| - *free_vars_copied = 1; |
110 |
| -} |
111 |
| - |
112 |
| -// https://github.com/python/cpython/blob/0325a8a8cdba6c091bcbbb3c995f3bf1d1217012/Objects/frameobject.c#L1162 |
113 |
| -static int |
114 |
| -frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i, |
115 |
| - PyObject **pvalue) |
116 |
| -{ |
117 |
| - _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); |
118 |
| - |
119 |
| - /* If the namespace is unoptimized, then one of the |
120 |
| - following cases applies: |
121 |
| - 1. It does not contain free variables, because it |
122 |
| - uses import * or is a top-level namespace. |
123 |
| - 2. It is a class namespace. |
124 |
| - We don't want to accidentally copy free variables |
125 |
| - into the locals dict used by the class. |
126 |
| - */ |
127 |
| - if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) { |
128 |
| - return 0; |
129 |
| - } |
130 |
| - |
131 |
| - PyObject *value = frame->localsplus[i]; |
132 |
| - if (frame->stacktop) { |
133 |
| - if (kind & CO_FAST_FREE) { |
134 |
| - // The cell was set by COPY_FREE_VARS. |
135 |
| - CHECK(value != NULL && PyCell_Check(value)); |
136 |
| - value = PyCell_GET(value); |
137 |
| - } |
138 |
| - else if (kind & CO_FAST_CELL) { |
139 |
| - // Note that no *_DEREF ops can happen before MAKE_CELL |
140 |
| - // executes. So there's no need to duplicate the work |
141 |
| - // that MAKE_CELL would otherwise do later, if it hasn't |
142 |
| - // run yet. |
143 |
| - if (value != NULL) { |
144 |
| - if (PyCell_Check(value) && |
145 |
| - THP_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) { |
146 |
| - // (likely) MAKE_CELL must have executed already. |
147 |
| - value = PyCell_GET(value); |
148 |
| - } |
149 |
| - // (likely) Otherwise it it is an arg (kind & CO_FAST_LOCAL), |
150 |
| - // with the initial value set when the frame was created... |
151 |
| - // (unlikely) ...or it was set to some initial value by |
152 |
| - // an earlier call to PyFrame_LocalsToFast(). |
153 |
| - } |
154 |
| - } |
155 |
| - } |
156 |
| - else { |
157 |
| - CHECK(value == NULL); |
158 |
| - } |
159 |
| - *pvalue = value; |
160 |
| - return 1; |
161 |
| -} |
162 |
| - |
163 |
| -// https://github.com/python/cpython/blob/0325a8a8cdba6c091bcbbb3c995f3bf1d1217012/Objects/frameobject.c#L1213 |
164 |
| -static PyObject * |
165 |
| -THP_PyFrame_GetLocals(_PyInterpreterFrame *frame, int include_hidden, int *free_vars_copied) |
166 |
| -{ |
167 |
| - /* Merge fast locals into f->f_locals */ |
168 |
| - PyObject *locals = frame->f_locals; |
169 |
| - if (locals == NULL) { |
170 |
| - locals = frame->f_locals = PyDict_New(); |
171 |
| - if (locals == NULL) { |
172 |
| - return NULL; |
173 |
| - } |
174 |
| - } |
175 |
| - PyObject *hidden = NULL; |
176 |
| - |
177 |
| - /* If include_hidden, "hidden" fast locals (from inlined comprehensions in |
178 |
| - module/class scopes) will be included in the returned dict, but not in |
179 |
| - frame->f_locals; the returned dict will be a modified copy. Non-hidden |
180 |
| - locals will still be updated in frame->f_locals. */ |
181 |
| - if (include_hidden) { |
182 |
| - hidden = PyDict_New(); |
183 |
| - if (hidden == NULL) { |
184 |
| - return NULL; |
185 |
| - } |
186 |
| - } |
187 |
| - |
188 |
| - frame_init_get_vars(frame, free_vars_copied); |
189 |
| - |
190 |
| - PyCodeObject *co = F_CODE(frame); |
191 |
| - for (int i = 0; i < co->co_nlocalsplus; i++) { |
192 |
| - PyObject *value; // borrowed reference |
193 |
| - if (!frame_get_var(frame, co, i, &value)) { |
194 |
| - continue; |
195 |
| - } |
196 |
| - |
197 |
| - PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i); |
198 |
| - _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); |
199 |
| - if (kind & CO_FAST_HIDDEN) { |
200 |
| - if (include_hidden && value != NULL) { |
201 |
| - if (PyObject_SetItem(hidden, name, value) != 0) { |
202 |
| - goto error; |
203 |
| - } |
204 |
| - } |
205 |
| - continue; |
206 |
| - } |
207 |
| - if (value == NULL) { |
208 |
| - if (PyObject_DelItem(locals, name) != 0) { |
209 |
| - if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
210 |
| - PyErr_Clear(); |
211 |
| - } |
212 |
| - else { |
213 |
| - goto error; |
214 |
| - } |
215 |
| - } |
216 |
| - } |
217 |
| - else { |
218 |
| - if (PyObject_SetItem(locals, name, value) != 0) { |
219 |
| - goto error; |
220 |
| - } |
221 |
| - } |
222 |
| - } |
223 |
| - |
224 |
| - if (include_hidden && PyDict_Size(hidden)) { |
225 |
| - PyObject *innerlocals = PyDict_New(); |
226 |
| - if (innerlocals == NULL) { |
227 |
| - goto error; |
228 |
| - } |
229 |
| - if (PyDict_Merge(innerlocals, locals, 1) != 0) { |
230 |
| - Py_DECREF(innerlocals); |
231 |
| - goto error; |
232 |
| - } |
233 |
| - if (PyDict_Merge(innerlocals, hidden, 1) != 0) { |
234 |
| - Py_DECREF(innerlocals); |
235 |
| - goto error; |
236 |
| - } |
237 |
| - locals = innerlocals; |
238 |
| - } |
239 |
| - else { |
240 |
| - Py_INCREF(locals); |
241 |
| - } |
242 |
| - Py_CLEAR(hidden); |
243 |
| - |
244 |
| - return locals; |
245 |
| - |
246 |
| - error: |
247 |
| - Py_XDECREF(hidden); |
248 |
| - return NULL; |
249 |
| -} |
250 |
| - |
251 |
| -// https://github.com/python/cpython/blob/0325a8a8cdba6c091bcbbb3c995f3bf1d1217012/Objects/frameobject.c#L1301 |
252 | 56 | int
|
253 | 57 | THP_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame, int *free_vars_copied)
|
254 | 58 | {
|
255 |
| - PyObject *locals = THP_PyFrame_GetLocals(frame, 0, free_vars_copied); |
256 |
| - if (locals == NULL) { |
257 |
| - return -1; |
258 |
| - } |
259 |
| - Py_DECREF(locals); |
| 59 | + // functionality moved to framelocals_mapping.cpp |
260 | 60 | return 0;
|
261 | 61 | }
|
262 | 62 |
|
|
0 commit comments