Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-93678: refactor compiler so that optimizer does not need the assembler and compiler structs#93842

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes from1 commit
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
6d38e85
optimize_cfg does not require a struct compiler*
iritkatrielJun 14, 2022
db0be36
optimize_cfg does not require a struct assembler*
iritkatrielJun 14, 2022
82be9a5
do not pass the whole assembler to functions that need only a_entry
iritkatrielJun 14, 2022
a2da370
Create struct assember just before assemble_emit. The other optimizer…
iritkatrielJun 14, 2022
c26a2bb
assemble_jump_offsets doesn't need the compiler
iritkatrielJun 14, 2022
36d2410
duplicate_exits_without_lineno can use b_next links to iterate
iritkatrielJun 14, 2022
7f06d70
stackdepth doesn't need the compiler
iritkatrielJun 14, 2022
5cd8fc9
push_cold_blocks_to_end and duplicate_exits_without_lineno do not nee…
iritkatrielJun 15, 2022
b1e9f7c
remove 3 unused fields from struct assembler
iritkatrielJun 15, 2022
b5f58d1
remove obsolete comment
iritkatrielJun 15, 2022
c4a125e
code review followup
iritkatrielJun 15, 2022
39f03c0
entry --> entryblock for consistency
iritkatrielJun 15, 2022
c233570
📜🤖 Added by blurb_it.
blurb-it[bot]Jun 15, 2022
482742a
Merge remote-tracking branch 'upstream/main' into reduce_dependencty_…
iritkatrielJun 17, 2022
28ba5d9
Merge remote-tracking branch 'upstream/main' into reduce_dependencty_…
iritkatrielJun 20, 2022
51b78f7
Merge branch 'main' into reduce_dependencty_on_compiler
iritkatrielJun 20, 2022
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
NextNext commit
optimize_cfg does not require a struct compiler*
  • Loading branch information
@iritkatriel
iritkatriel committedJun 14, 2022
commit6d38e85571ea97b570c70cd9de2fc28c03a1c91c
53 changes: 29 additions & 24 deletionsPython/compile.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1340,8 +1340,9 @@ compiler_add_o(PyObject *dict, PyObject *o)

// Merge const *o* recursively and return constant key object.
static PyObject*
merge_consts_recursive(struct compiler *c, PyObject *o)
merge_consts_recursive(PyObject *const_cache, PyObject *o)
{
PyDict_CheckExact(const_cache);
// None and Ellipsis are singleton, and key is the singleton.
// No need to merge object and key.
if (o == Py_None || o == Py_Ellipsis) {
Expand All@@ -1355,22 +1356,22 @@ merge_consts_recursive(struct compiler *c, PyObject *o)
}

// t is borrowed reference
PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
PyObject *t = PyDict_SetDefault(const_cache, key, key);
if (t != key) {
// o is registered inc_const_cache. Just use it.
// o is registered inconst_cache. Just use it.
Py_XINCREF(t);
Py_DECREF(key);
return t;
}

// We registered o inc_const_cache.
// We registered o inconst_cache.
// When o is a tuple or frozenset, we want to merge its
// items too.
if (PyTuple_CheckExact(o)) {
Py_ssize_t len = PyTuple_GET_SIZE(o);
for (Py_ssize_t i = 0; i < len; i++) {
PyObject *item = PyTuple_GET_ITEM(o, i);
PyObject *u = merge_consts_recursive(c, item);
PyObject *u = merge_consts_recursive(const_cache, item);
if (u == NULL) {
Py_DECREF(key);
return NULL;
Expand DownExpand Up@@ -1413,7 +1414,7 @@ merge_consts_recursive(struct compiler *c, PyObject *o)
PyObject *item;
Py_hash_t hash;
while (_PySet_NextEntry(o, &pos, &item, &hash)) {
PyObject *k = merge_consts_recursive(c, item);
PyObject *k = merge_consts_recursive(const_cache, item);
if (k == NULL) {
Py_DECREF(tuple);
Py_DECREF(key);
Expand DownExpand Up@@ -1451,7 +1452,7 @@ merge_consts_recursive(struct compiler *c, PyObject *o)
static Py_ssize_t
compiler_add_const(struct compiler *c, PyObject *o)
{
PyObject *key = merge_consts_recursive(c, o);
PyObject *key = merge_consts_recursive(c->c_const_cache, o);
if (key == NULL) {
return -1;
}
Expand DownExpand Up@@ -8034,15 +8035,16 @@ compute_code_flags(struct compiler *c)
// Merge *obj* with constant cache.
// Unlike merge_consts_recursive(), this function doesn't work recursively.
static int
merge_const_one(struct compiler *c, PyObject **obj)
merge_const_one(PyObject *const_cache, PyObject **obj)
{
PyDict_CheckExact(const_cache);
PyObject *key = _PyCode_ConstantKey(*obj);
if (key == NULL) {
return 0;
}

// t is borrowed reference
PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
PyObject *t = PyDict_SetDefault(const_cache, key, key);
Py_DECREF(key);
if (t == NULL) {
return 0;
Expand DownExpand Up@@ -8125,15 +8127,15 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
if (!names) {
goto error;
}
if (!merge_const_one(c, &names)) {
if (!merge_const_one(c->c_const_cache, &names)) {
goto error;
}

consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */
if (consts == NULL) {
goto error;
}
if (!merge_const_one(c, &consts)) {
if (!merge_const_one(c->c_const_cache, &consts)) {
goto error;
}

Expand DownExpand Up@@ -8184,7 +8186,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
goto error;
}

if (!merge_const_one(c, &localsplusnames)) {
if (!merge_const_one(c->c_const_cache, &localsplusnames)) {
goto error;
}
con.localsplusnames = localsplusnames;
Expand DownExpand Up@@ -8249,7 +8251,7 @@ static int
normalize_basic_block(basicblock *bb);

static int
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
optimize_cfg(PyObject *const_cache, struct assembler *a, PyObject *consts);

static int
trim_unused_consts(struct assembler *a, PyObject *consts);
Expand DownExpand Up@@ -8582,7 +8584,7 @@ assemble(struct compiler *c, int addNone)
goto error;
}

if (optimize_cfg(c, &a, consts)) {
if (optimize_cfg(c->c_const_cache, &a, consts)) {
goto error;
}
if (duplicate_exits_without_lineno(c)) {
Expand DownExpand Up@@ -8650,21 +8652,21 @@ assemble(struct compiler *c, int addNone)
if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
goto error;
}
if (!merge_const_one(c, &a.a_except_table)) {
if (!merge_const_one(c->c_const_cache, &a.a_except_table)) {
goto error;
}

if (_PyBytes_Resize(&a.a_linetable, a.a_location_off) < 0) {
goto error;
}
if (!merge_const_one(c, &a.a_linetable)) {
if (!merge_const_one(c->c_const_cache, &a.a_linetable)) {
goto error;
}

if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
goto error;
}
if (!merge_const_one(c, &a.a_bytecode)) {
if (!merge_const_one(c->c_const_cache, &a.a_bytecode)) {
goto error;
}

Expand DownExpand Up@@ -8703,11 +8705,12 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
Called with codestr pointing to the first LOAD_CONST.
*/
static int
fold_tuple_on_constants(struct compiler *c,
fold_tuple_on_constants(PyObject *const_cache,
struct instr *inst,
int n, PyObject *consts)
{
/* Pre-conditions */
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
assert(inst[n].i_opcode == BUILD_TUPLE);
assert(inst[n].i_oparg == n);
Expand All@@ -8732,7 +8735,7 @@ fold_tuple_on_constants(struct compiler *c,
}
PyTuple_SET_ITEM(newconst, i, constant);
}
if (merge_const_one(c, &newconst) == 0) {
if (merge_const_one(const_cache, &newconst) == 0) {
Py_DECREF(newconst);
return -1;
}
Expand DownExpand Up@@ -8955,8 +8958,9 @@ jump_thread(struct instr *inst, struct instr *target, int opcode)

/* Optimization */
static int
optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
struct instr nop;
nop.i_opcode = NOP;
Expand DownExpand Up@@ -9063,7 +9067,7 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
}
}
if (i >= oparg) {
if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
if (fold_tuple_on_constants(const_cache, inst-oparg, oparg, consts)) {
goto error;
}
}
Expand DownExpand Up@@ -9415,16 +9419,17 @@ propagate_line_numbers(struct assembler *a) {
*/

static int
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
optimize_cfg(PyObject *const_cache, struct assembler *a, PyObject *consts)
{
assert(PyDict_CheckExact(const_cache));
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
if (optimize_basic_block(c, b, consts)) {
if (optimize_basic_block(const_cache, b, consts)) {
return -1;
}
clean_basic_block(b);
assert(b->b_predecessors == 0);
}
for (basicblock *b =c->u->u_blocks; b != NULL; b = b->b_list) {
for (basicblock *b =a->a_entry; b != NULL; b = b->b_next) {
if (extend_block(b)) {
return -1;
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp