Skip to content

Commit

Permalink
[3.11] pythonGH-95113: Don't use EXTENDED_ARG_QUICK in unquickened co…
Browse files Browse the repository at this point in the history
…de (pythonGH-95121) (pythonGH-95143)

(cherry picked from commit e402b26)
  • Loading branch information
brandtbucher authored Jul 22, 2022
1 parent e99496e commit 5a48ab0
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 198 deletions.
186 changes: 0 additions & 186 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def extended_arg_quick():
%3d 0 RESUME 0
%3d 2 LOAD_CONST 1 (Ellipsis)
4 EXTENDED_ARG_QUICK 1
4 EXTENDED_ARG 1
6 UNPACK_EX 256
8 STORE_FAST 0 (_)
10 STORE_FAST 0 (_)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Replace all ``EXTENDED_ARG_QUICK`` instructions with basic
:opcode:`EXTENDED_ARG` instructions in unquickened code. Consumers of
non-adaptive bytecode should be able to handle extended arguments the same
way they were handled in CPython 3.10 and older.
2 changes: 1 addition & 1 deletion Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len)
{
for (int i = 0; i < len; i++) {
_Py_CODEUNIT instruction = instructions[i];
int opcode = _PyOpcode_Original[_Py_OPCODE(instruction)];
int opcode = _PyOpcode_Deopt[_Py_OPCODE(instruction)];
int caches = _PyOpcode_Caches[opcode];
instructions[i] = _Py_MAKECODEUNIT(opcode, _Py_OPARG(instruction));
while (caches--) {
Expand Down
8 changes: 8 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5590,8 +5590,16 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
assert(oparg);
oparg <<= 8;
oparg |= _Py_OPARG(*next_instr);
// We might be tracing. To avoid breaking tracing guarantees in
// quickened instructions, always deoptimize the next opcode:
opcode = _PyOpcode_Deopt[_Py_OPCODE(*next_instr)];
PRE_DISPATCH_GOTO();
// CPython hasn't traced the following instruction historically
// (DO_TRACING would clobber our extended oparg anyways), so just
// skip our usual cframe.use_tracing check before dispatch. Also,
// make sure the next instruction isn't a RESUME, since that needs
// to trace properly (and shouldn't have an extended arg anyways):
assert(opcode != RESUME);
DISPATCH_GOTO();
}

Expand Down
6 changes: 3 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
int caches = _PyOpcode_Caches[opcode];
switch (ilen - caches) {
case 4:
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 24) & 0xFF);
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG, (oparg >> 24) & 0xFF);
/* fall through */
case 3:
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 16) & 0xFF);
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG, (oparg >> 16) & 0xFF);
/* fall through */
case 2:
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 8) & 0xFF);
*codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG, (oparg >> 8) & 0xFF);
/* fall through */
case 1:
*codestr++ = _Py_MAKECODEUNIT(opcode, oparg & 0xFF);
Expand Down
3 changes: 3 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ _PyCode_Quicken(PyCodeObject *code)
else {
assert(!_PyOpcode_Caches[opcode]);
switch (opcode) {
case EXTENDED_ARG:
_Py_SET_OPCODE(instructions[i], EXTENDED_ARG_QUICK);
break;
case JUMP_BACKWARD:
_Py_SET_OPCODE(instructions[i], JUMP_BACKWARD_QUICK);
break;
Expand Down
7 changes: 0 additions & 7 deletions Tools/scripts/generate_opcode_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna

iobj.write("\nextern const uint8_t _PyOpcode_Caches[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Deopt[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Original[256];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
write_int_array_from_ops("_PyOpcode_RelativeJump", opcode['hasjrel'], iobj)
write_int_array_from_ops("_PyOpcode_Jump", opcode['hasjrel'] + opcode['hasjabs'], iobj)
Expand All @@ -138,12 +137,6 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna
for opt, deopt in sorted(deoptcodes.items()):
iobj.write(f" [{opt}] = {deopt},\n")
iobj.write("};\n")
iobj.write("\nconst uint8_t _PyOpcode_Original[256] = {\n")
for opt, deopt in sorted(deoptcodes.items()):
if opt.startswith("EXTENDED_ARG"):
deopt = "EXTENDED_ARG_QUICK"
iobj.write(f" [{opt}] = {deopt},\n")
iobj.write("};\n")
iobj.write("#endif // NEED_OPCODE_TABLES\n")

fobj.write("\n")
Expand Down

0 comments on commit 5a48ab0

Please sign in to comment.