Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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
iritkatriel merged 16 commits intopython:mainfromiritkatriel:reduce_dependencty_on_compilerJun 21, 2022
Uh oh!
There was an error while loading.Please reload this page.
Merged
GH-93678: refactor compiler so that optimizer does not need the assembler and compiler structs#93842
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*
iritkatrieldb0be36 optimize_cfg does not require a struct assembler*
iritkatriel82be9a5 do not pass the whole assembler to functions that need only a_entry
iritkatriela2da370 Create struct assember just before assemble_emit. The other optimizer…
iritkatrielc26a2bb assemble_jump_offsets doesn't need the compiler
iritkatriel36d2410 duplicate_exits_without_lineno can use b_next links to iterate
iritkatriel7f06d70 stackdepth doesn't need the compiler
iritkatriel5cd8fc9 push_cold_blocks_to_end and duplicate_exits_without_lineno do not nee…
iritkatrielb1e9f7c remove 3 unused fields from struct assembler
iritkatrielb5f58d1 remove obsolete comment
iritkatrielc4a125e code review followup
iritkatriel39f03c0 entry --> entryblock for consistency
iritkatrielc233570 📜🤖 Added by blurb_it.
blurb-it[bot]482742a Merge remote-tracking branch 'upstream/main' into reduce_dependencty_…
iritkatriel28ba5d9 Merge remote-tracking branch 'upstream/main' into reduce_dependencty_…
iritkatriel51b78f7 Merge branch 'main' into reduce_dependencty_on_compiler
iritkatrielFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
optimize_cfg does not require a struct compiler*
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit6d38e85571ea97b570c70cd9de2fc28c03a1c91c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(PyObject *const_cache, PyObject *o) | ||
| { | ||
| PyDict_CheckExact(const_cache); | ||
iritkatriel marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // None and Ellipsis are singleton, and key is the singleton. | ||
| // No need to merge object and key. | ||
| if (o == Py_None || o == Py_Ellipsis) { | ||
| @@ -1355,22 +1356,22 @@ merge_consts_recursive(struct compiler *c, PyObject *o) | ||
| } | ||
| // t is borrowed reference | ||
| PyObject *t = PyDict_SetDefault(const_cache, key, key); | ||
| if (t != key) { | ||
| // o is registered inconst_cache. Just use it. | ||
| Py_XINCREF(t); | ||
| Py_DECREF(key); | ||
| return t; | ||
| } | ||
| // 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(const_cache, item); | ||
| if (u == NULL) { | ||
| Py_DECREF(key); | ||
| return NULL; | ||
| @@ -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(const_cache, item); | ||
| if (k == NULL) { | ||
| Py_DECREF(tuple); | ||
| Py_DECREF(key); | ||
| @@ -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->c_const_cache, o); | ||
| if (key == NULL) { | ||
| return -1; | ||
| } | ||
| @@ -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(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(const_cache, key, key); | ||
| Py_DECREF(key); | ||
| if (t == NULL) { | ||
| return 0; | ||
| @@ -8125,15 +8127,15 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist, | ||
| if (!names) { | ||
| goto error; | ||
| } | ||
| 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->c_const_cache, &consts)) { | ||
| goto error; | ||
| } | ||
| @@ -8184,7 +8186,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist, | ||
| goto error; | ||
| } | ||
| if (!merge_const_one(c->c_const_cache, &localsplusnames)) { | ||
| goto error; | ||
| } | ||
| con.localsplusnames = localsplusnames; | ||
| @@ -8249,7 +8251,7 @@ static int | ||
| normalize_basic_block(basicblock *bb); | ||
| static int | ||
| optimize_cfg(PyObject *const_cache, struct assembler *a, PyObject *consts); | ||
| static int | ||
| trim_unused_consts(struct assembler *a, PyObject *consts); | ||
| @@ -8582,7 +8584,7 @@ assemble(struct compiler *c, int addNone) | ||
| goto error; | ||
| } | ||
| if (optimize_cfg(c->c_const_cache, &a, consts)) { | ||
| goto error; | ||
| } | ||
| if (duplicate_exits_without_lineno(c)) { | ||
| @@ -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->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->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->c_const_cache, &a.a_bytecode)) { | ||
| goto error; | ||
| } | ||
| @@ -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(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); | ||
| @@ -8732,7 +8735,7 @@ fold_tuple_on_constants(struct compiler *c, | ||
| } | ||
| PyTuple_SET_ITEM(newconst, i, constant); | ||
| } | ||
| if (merge_const_one(const_cache, &newconst) == 0) { | ||
| Py_DECREF(newconst); | ||
| return -1; | ||
| } | ||
| @@ -8955,8 +8958,9 @@ jump_thread(struct instr *inst, struct instr *target, int opcode) | ||
| /* Optimization */ | ||
| static int | ||
| 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; | ||
| @@ -9063,7 +9067,7 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) | ||
| } | ||
| } | ||
| if (i >= oparg) { | ||
| if (fold_tuple_on_constants(const_cache, inst-oparg, oparg, consts)) { | ||
| goto error; | ||
| } | ||
| } | ||
| @@ -9415,16 +9419,17 @@ propagate_line_numbers(struct assembler *a) { | ||
| */ | ||
| static int | ||
| 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(const_cache, b, consts)) { | ||
| return -1; | ||
| } | ||
| clean_basic_block(b); | ||
| assert(b->b_predecessors == 0); | ||
| } | ||
| for (basicblock *b =a->a_entry; b != NULL; b = b->b_next) { | ||
| if (extend_block(b)) { | ||
| return -1; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.