Skip to content

unbroken_consts #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
@@ -220,6 +220,13 @@ _PyFrame_PushUnchecked(PyThreadState *tstate, PyFunctionObject *func)
tstate->datastack_top += code->co_framesize;
assert(tstate->datastack_top < tstate->datastack_limit);
_PyFrame_InitializeSpecials(new_frame, func, NULL, code);
int nconsts = (int)PyTuple_Size(code->co_consts);
if (nconsts > 0) {
PyObject **const_regs = new_frame->localsplus + (code->co_nlocalsplus +
code->co_stacksize);
PyObject **consts = &PyTuple_GET_ITEM(code->co_consts, 0);
memcpy(const_regs, consts, sizeof(PyObject*) * nconsts);
}
return new_frame;
}

16 changes: 8 additions & 8 deletions Include/internal/pycore_opcode.h

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

40 changes: 21 additions & 19 deletions Include/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/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
@@ -438,7 +438,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3569).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3572).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

2 changes: 2 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
@@ -162,6 +162,8 @@ def pseudo_op(name, op, real_ops):
jrel_op('JUMP_FORWARD', 110) # Number of words to skip
jrel_op('JUMP_IF_FALSE_OR_POP', 111) # Number of words to skip
jrel_op('JUMP_IF_TRUE_OR_POP', 112) # ""
def_op('LOAD_CONST_R', 113) # Index in const list
hasconst.append(113)
jrel_op('POP_JUMP_IF_FALSE', 114)
jrel_op('POP_JUMP_IF_TRUE', 115)
name_op('LOAD_GLOBAL', 116) # Index in name list
5 changes: 5 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
@@ -156,6 +156,11 @@ dummy_func(
Py_INCREF(value);
}

inst(LOAD_CONST_R, (-- value)) {
value = REG(oparg1);
Py_INCREF(value);
}

inst(STORE_FAST, (value --)) {
SETLOCAL(oparg, value);
}
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
@@ -2015,7 +2015,7 @@ _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
PyObject **const_regs = localsarray + (code->co_nlocalsplus +
code->co_stacksize);
PyObject **consts = &PyTuple_GET_ITEM(code->co_consts, 0);
Py_MEMCPY(const_regs, consts, sizeof(PyObject*) * nconsts);
memcpy(const_regs, consts, sizeof(PyObject*) * nconsts);
}
return frame;
fail:
37 changes: 29 additions & 8 deletions Python/compile.c
Original file line number Diff line number Diff line change
@@ -304,7 +304,7 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
int oparg3 = instruction->i_oparg3.final;

if (0) {
if (opcode == LOAD_FAST_R || opcode == STORE_FAST_R)
if (opcode == LOAD_CONST_R || opcode == LOAD_FAST_R || opcode == STORE_FAST_R)
{
fprintf(stderr,
"write_instr [%d]: oparg = %d oparg1 = %d oparg2 = %d oparg3 = %d\n",
@@ -710,9 +710,10 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
c->c_regcode = false;
}
else {
c->c_regcode = !strstr(f, "import"); // strstr(f, "test_");
c->c_regcode = !strstr(f, "import") && !strstr(f, "frozen") && !strstr(f, "freeze") && !strstr(f, "encodings");
c->c_regcode = strstr(f, "mytest");
}
c->c_regcode = true;
//c->c_regcode = true;

c->c_arena = arena;
if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
@@ -1223,6 +1224,7 @@ stack_effect(int opcode, int oparg, int jump)
case DELETE_GLOBAL:
return 0;
case LOAD_CONST:
case LOAD_CONST_R:
return 1;
case LOAD_NAME:
return 1;
@@ -1636,7 +1638,13 @@ compiler_addop_load_const(struct compiler *c, location loc, PyObject *o)
if (arg < 0) {
return ERROR;
}
return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc);
if (c->c_regcode) {
return cfg_builder_addop(CFG_BUILDER(c), LOAD_CONST_R, arg, loc,
CONST_OPARG(arg), UNUSED_OPARG, UNUSED_OPARG);
}
else {
return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc);
}
}

static int
@@ -8873,9 +8881,7 @@ resolve_register(oparg_t *oparg, int nlocalsplus, PyObject *varnames,
break;
case CONST_REG:
assert(oparg->value >= 0 && oparg->value < nconsts);
oparg->final = (nlocalsplus +
stacksize +
oparg->value);
oparg->final = (nlocalsplus + stacksize + oparg->value);
break;
case NAME_REG:
assert(oparg->value >= 0 && oparg->value < nlocalsplus);
@@ -9092,7 +9098,7 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
{
PyObject *constant = NULL;
assert(HAS_CONST(opcode));
if (opcode == LOAD_CONST) {
if (opcode == LOAD_CONST || opcode == LOAD_CONST_R) {
constant = PyList_GET_ITEM(co_consts, oparg);
}

@@ -9385,10 +9391,15 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
switch (inst->i_opcode) {
/* Remove LOAD_CONST const; conditional jump */
case LOAD_CONST:
case LOAD_CONST_R:
{
PyObject* cnt;
int is_true;
int jump_if_true;
if (inst->i_opcode == LOAD_CONST_R) {
oparg = inst->i_oparg1.value;
}

switch(nextop) {
case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
@@ -9926,6 +9937,10 @@ remove_unused_consts(basicblock *entryblock, PyObject *consts)
int index = b->b_instr[i].i_oparg;
index_map[index] = index;
}
if (b->b_instr[i].i_opcode == LOAD_CONST_R) {
int index = b->b_instr[i].i_oparg1.value;
index_map[index] = index;
}
}
}
/* now index_map[i] == i if consts[i] is used, -1 otherwise */
@@ -9985,6 +10000,12 @@ remove_unused_consts(basicblock *entryblock, PyObject *consts)
assert(reverse_index_map[index] < n_used_consts);
b->b_instr[i].i_oparg = (int)reverse_index_map[index];
}
if (b->b_instr[i].i_opcode == LOAD_CONST_R) {
int index = b->b_instr[i].i_oparg1.value;
assert(reverse_index_map[index] >= 0);
assert(reverse_index_map[index] < n_used_consts);
b->b_instr[i].i_oparg1.value = (int)reverse_index_map[index];
}
}
}

9 changes: 9 additions & 0 deletions Python/generated_cases.c.h

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

14 changes: 7 additions & 7 deletions Python/opcode_targets.h

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

5 changes: 1 addition & 4 deletions Tools/build/deepfreeze.py
Original file line number Diff line number Diff line change
@@ -262,10 +262,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
self.field(code, "co_argcount")
self.field(code, "co_posonlyargcount")
self.field(code, "co_kwonlyargcount")
num_registers = (len(localsplusnames) +
code.co_stacksize +
len(co_consts))
self.write(f".co_framesize = {num_registers} + FRAME_SPECIALS_SIZE,")
self.write(f".co_framesize = {len(localsplusnames)} + {code.co_stacksize} + {len(code.co_consts)} + FRAME_SPECIALS_SIZE,")
self.field(code, "co_stacksize")
self.field(code, "co_firstlineno")
self.write(f".co_nlocalsplus = {len(localsplusnames)},")