Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
43df9b3
compiler uses instruction stream to create codeobjects
iritkatriel Feb 25, 2023
2b43f88
remove unused code
iritkatriel Feb 26, 2023
52a1d1d
use newg all the way
iritkatriel Feb 26, 2023
9b737f1
remove double processing
iritkatriel Feb 26, 2023
083b8b1
split cfg_builder_use_label/cfg_builder_addop to separate versions fo…
iritkatriel Feb 26, 2023
a56bafe
codegen functions take an instr_stream
iritkatriel Feb 27, 2023
8be1b28
free isntr_stream. Attach it to compiluer_unit (not cfg_builder)
iritkatriel Feb 27, 2023
741f773
move things around
iritkatriel Feb 27, 2023
fb07525
labels generated by instr_stream instead of cfg_builder
iritkatriel Feb 27, 2023
1b06bf8
add implicit RETURN NONE to the stream
iritkatriel Feb 27, 2023
142a7f1
remove u_cfg_builder
iritkatriel Feb 27, 2023
f9f8443
init the cfg_builder in instr_stream_to_cfg
iritkatriel Feb 27, 2023
f6fcde9
stream->sequence
iritkatriel Feb 28, 2023
84f5a9d
instr->cfg_instr, codegen_instr->instr
iritkatriel Feb 28, 2023
cd0225d
make sure we always emit something to jump to at the end
iritkatriel Feb 28, 2023
b8f4acf
INSTR_STREAM --> INSTR_SEQUENCE
iritkatriel Feb 28, 2023
258f142
free the cfg_builder in the test harness
iritkatriel Mar 1, 2023
e5653ee
Merge branch 'main' into instruction-stream
iritkatriel Mar 1, 2023
788da74
Merge branch 'main' into instruction-stream
iritkatriel Mar 2, 2023
fd7f2b3
#ifndef NDEBUG around cfg_builder_check
iritkatriel Mar 2, 2023
57fbe36
create shared helper function for resizing arrays
iritkatriel Mar 3, 2023
dc9f1f5
remove obsolete comment
iritkatriel Mar 3, 2023
f20e480
Merge branch 'main' into instruction-stream
iritkatriel Mar 6, 2023
fa1c66a
address code review
iritkatriel Mar 7, 2023
3528176
tweak comments
iritkatriel Mar 7, 2023
fb13e36
index -> idx to avoid github's hilighting
iritkatriel Mar 7, 2023
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
Prev Previous commit
Next Next commit
remove double processing
  • Loading branch information
iritkatriel committed Feb 28, 2023
commit 9b737f1d6e8abbeab6ce5a7db535908cc1d9b96a
106 changes: 32 additions & 74 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8696,37 +8696,37 @@ prepare_localsplus(struct compiler* c, int code_flags, cfg_builder *newg)
assert(INT_MAX - nlocals - ncellvars > 0);
assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
int nlocalsplus = nlocals + ncellvars + nfreevars;
int* cellfixedoffsets = build_cellfixedoffsets(c);
if (cellfixedoffsets == NULL) {
return ERROR;
}
// int* cellfixedoffsets = build_cellfixedoffsets(c);
// if (cellfixedoffsets == NULL) {
// return ERROR;
// }
int* newcellfixedoffsets = build_cellfixedoffsets(c);
if (newcellfixedoffsets == NULL) {
return ERROR;
}

cfg_builder* g = CFG_BUILDER(c);
// cfg_builder* g = CFG_BUILDER(c);

// This must be called before fix_cell_offsets().
if (insert_prefix_instructions(c, g->g_entryblock, cellfixedoffsets, nfreevars, code_flags)) {
PyMem_Free(cellfixedoffsets);
return ERROR;
}
// if (insert_prefix_instructions(c, g->g_entryblock, cellfixedoffsets, nfreevars, code_flags)) {
// PyMem_Free(cellfixedoffsets);
// return ERROR;
// }
if (insert_prefix_instructions(c, newg->g_entryblock, newcellfixedoffsets, nfreevars, code_flags)) {
PyMem_Free(newcellfixedoffsets);
return ERROR;
}

int numdropped = fix_cell_offsets(c, g->g_entryblock, cellfixedoffsets);
PyMem_Free(cellfixedoffsets); // At this point we're done with it.
cellfixedoffsets = NULL;
if (numdropped < 0) {
return ERROR;
}
// int numdropped = fix_cell_offsets(c, g->g_entryblock, cellfixedoffsets);
// PyMem_Free(cellfixedoffsets); // At this point we're done with it.
// cellfixedoffsets = NULL;
// if (numdropped < 0) {
// return ERROR;
// }
int newnumdropped = fix_cell_offsets(c, newg->g_entryblock, newcellfixedoffsets);
PyMem_Free(newcellfixedoffsets); // At this point we're done with it.
newcellfixedoffsets = NULL;
assert(newnumdropped == numdropped);
int numdropped = newnumdropped;

nlocalsplus -= numdropped;
return nlocalsplus;
Expand Down Expand Up @@ -8764,18 +8764,27 @@ assemble(struct compiler *c, int addNone)
return NULL;
}

/** Preprocessing **/
memset(&newg, 0, sizeof(cfg_builder));
if (cfg_builder_init(&newg) < 0) {
goto error;
}
newg.g_build_instr_stream = false;
if (instr_stream_to_cfg(&CFG_BUILDER(c)->g_instr_stream, &newg) < 0) {
goto error;
}

cfg_builder *g = &newg;
int nblocks = 0;
for (basicblock *b = CFG_BUILDER(c)->g_block_list; b != NULL; b = b->b_list) {

for (basicblock *b = g->g_block_list; b != NULL; b = b->b_list) {
nblocks++;
}
if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
PyErr_NoMemory();
goto error;
}

cfg_builder *g = CFG_BUILDER(c);
assert(g->g_entryblock != NULL);

/* Set firstlineno if it wasn't explicitly set. */
if (!c->u->u_firstlineno) {
if (g->g_entryblock->b_instr && g->g_entryblock->b_instr->i_loc.lineno) {
Expand All @@ -8786,87 +8795,46 @@ assemble(struct compiler *c, int addNone)
}
}

/** Preprocessing **/
memset(&newg, 0, sizeof(cfg_builder));
if (cfg_builder_init(&newg) < 0) {
goto error;
}
newg.g_build_instr_stream = false;
if (instr_stream_to_cfg(&g->g_instr_stream, &newg) < 0) {
goto error;
}
/* Map labels to targets and mark exception handlers */
if (translate_jump_labels_to_targets(g->g_entryblock) < 0) {
goto error;
}
if (translate_jump_labels_to_targets(newg.g_entryblock) < 0) {
goto error;
}
if (mark_except_handlers(g->g_entryblock) < 0) {
goto error;
}
if (mark_except_handlers(newg.g_entryblock) < 0) {
goto error;
}

if (label_exception_targets(g->g_entryblock)) {
goto error;
}
if (label_exception_targets(newg.g_entryblock)) {
goto error;
}

/** Optimization **/
consts = consts_dict_keys_inorder(c->u->u_consts);
if (consts == NULL) {
goto error;
}
newconsts = consts_dict_keys_inorder(c->u->u_consts);
if (consts == NULL) {
goto error;
}
if (optimize_cfg(g, consts, c->c_const_cache)) {
goto error;
}
if (optimize_cfg(&newg, newconsts, c->c_const_cache)) {
goto error;
}

if (remove_unused_consts(g->g_entryblock, consts) < 0) {
goto error;
}
if (remove_unused_consts(newg.g_entryblock, newconsts) < 0) {
goto error;
}
if (add_checks_for_loads_of_uninitialized_variables(g->g_entryblock, c) < 0) {
goto error;
}
if (add_checks_for_loads_of_uninitialized_variables(newg.g_entryblock, c) < 0) {
goto error;
}

/** line numbers (TODO: move this before optimization stage) */
if (duplicate_exits_without_lineno(g) < 0) {
goto error;
}

/** line numbers (TODO: move this before optimization stage) */
if (duplicate_exits_without_lineno(&newg) < 0) {
goto error;
}

propagate_line_numbers(g->g_entryblock);
propagate_line_numbers(newg.g_entryblock);

guarantee_lineno_for_exits(g->g_entryblock, c->u->u_firstlineno);
guarantee_lineno_for_exits(newg.g_entryblock, c->u->u_firstlineno);

if (push_cold_blocks_to_end(g, code_flags) < 0) {
goto error;
}
if (push_cold_blocks_to_end(&newg, code_flags) < 0) {
goto error;
}

/** Assembly **/
int nlocalsplus = prepare_localsplus(c, code_flags, &newg);
Expand All @@ -8878,38 +8846,28 @@ assemble(struct compiler *c, int addNone)
if (maxdepth < 0) {
goto error;
}
int newmaxdepth = stackdepth(newg.g_entryblock, code_flags);
assert(maxdepth == newmaxdepth);

/* TO DO -- For 3.12, make sure that `maxdepth <= MAX_ALLOWED_STACK_USE` */

convert_exception_handlers_to_nops(g->g_entryblock);
convert_exception_handlers_to_nops(newg.g_entryblock);

/* Order of basic blocks must have been determined by now */
if (normalize_jumps(g) < 0) {
goto error;
}

if (normalize_jumps(&newg) < 0) {
goto error;
}
assert(no_redundant_jumps(g));
assert(opcode_metadata_is_sane(g));
assert(no_redundant_jumps(&newg));
assert(opcode_metadata_is_sane(&newg));

/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(g->g_entryblock);
assemble_jump_offsets(newg.g_entryblock);

/* Create assembler */
if (assemble_init(&a, c->u->u_firstlineno) < 0) {
goto error;
}

/* Emit code. */
for (basicblock *b = newg.g_entryblock; b != NULL; b = b->b_next) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int j = 0; j < b->b_iused; j++) {
if (assemble_emit(&a, &b->b_instr[j]) < 0) {
goto error;
Expand All @@ -8921,7 +8879,7 @@ assemble(struct compiler *c, int addNone)
a.a_lineno = c->u->u_firstlineno;
location loc = NO_LOCATION;
int size = 0;
for (basicblock *b = newg.g_entryblock; b != NULL; b = b->b_next) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int j = 0; j < b->b_iused; j++) {
if (!same_location(loc, b->b_instr[j].i_loc)) {
if (assemble_emit_location(&a, loc, size)) {
Expand All @@ -8937,7 +8895,7 @@ assemble(struct compiler *c, int addNone)
goto error;
}

if (assemble_exception_table(&a, newg.g_entryblock) < 0) {
if (assemble_exception_table(&a, g->g_entryblock) < 0) {
goto error;
}
if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
Expand Down