Skip to content

gh-87092: compiler's CFG construction moved to after codegen stage #102320

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 26 commits into from
Mar 7, 2023
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
146 changes: 76 additions & 70 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,27 +431,73 @@ typedef struct instr_sequence_ {
#define INITIAL_INSTR_SEQUENCE_SIZE 100
#define INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE 10

/*
* Resize the array if index is out of range.
*
* index: the index we want to access
* arr pointer to the array
* alloc: pointer to the capacity of the array
* default_alloc: initial number of items
* item_size: size of each item
*
*/
static int
instr_sequence_next_inst(instr_sequence *seq) {
if (seq->s_instrs == NULL) {
assert(seq->s_allocated == 0);
assert(seq->s_used == 0);
seq->s_instrs = (instruction *)PyObject_Calloc(
INITIAL_INSTR_SEQUENCE_SIZE, sizeof(instruction));
if (seq->s_instrs == NULL) {
ensure_array_large_enough(int index, void **arr_, int *alloc, int default_alloc, size_t item_size)
{
void *arr = *arr_;
if (arr == NULL) {
int new_alloc = default_alloc;
if (index >= new_alloc) {
new_alloc = index + default_alloc;
}
arr = PyObject_Calloc(new_alloc, item_size);
if (arr == NULL) {
PyErr_NoMemory();
return ERROR;
}
seq->s_allocated = INITIAL_INSTR_SEQUENCE_SIZE;
*alloc = new_alloc;
}
if (seq->s_used == seq->s_allocated) {
instruction *tmp = (instruction *)PyObject_Realloc(
seq->s_instrs, 2 * seq->s_allocated * sizeof(instruction));
else if (index >= *alloc) {
size_t oldsize = *alloc * item_size;
int new_alloc = *alloc << 1;
if (index >= new_alloc) {
new_alloc = index + default_alloc;
}
size_t newsize = new_alloc * item_size;

if (oldsize > (SIZE_MAX >> 1)) {
PyErr_NoMemory();
return ERROR;
}

if (newsize == 0) {
PyErr_NoMemory();
return ERROR;
}
void *tmp = PyObject_Realloc(arr, newsize);
if (tmp == NULL) {
PyErr_NoMemory();
return ERROR;
}
seq->s_instrs = tmp;
seq->s_allocated *= 2;
*alloc = new_alloc;
arr = tmp;
memset((char *)arr + oldsize, 0, newsize - oldsize);
}

*arr_ = arr;
return SUCCESS;
}

static int
instr_sequence_next_inst(instr_sequence *seq) {
assert(seq->s_instrs != NULL || seq->s_used == 0);

RETURN_IF_ERROR(
ensure_array_large_enough(seq->s_used + 1,
(void**)&seq->s_instrs,
&seq->s_allocated,
INITIAL_INSTR_SEQUENCE_SIZE,
sizeof(instruction)));
assert(seq->s_used < seq->s_allocated);
return seq->s_used++;
}
Expand All @@ -465,31 +511,16 @@ instr_sequence_new_label(instr_sequence *seq)

static int
instr_sequence_use_label(instr_sequence *seq, int lbl) {
if (seq->s_labelmap_size <= lbl) {
int old_size, new_size;
int *tmp = NULL;
if (seq->s_labelmap == NULL) {
old_size = 0;
new_size = INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE;
if (new_size < 2 * lbl) {
new_size = 2 * lbl;
}
tmp = (int*)PyObject_Calloc(new_size, sizeof(int));
}
else {
old_size = seq->s_labelmap_size;
new_size = 2 * lbl;
tmp = (int*)PyObject_Realloc(seq->s_labelmap,
new_size * sizeof(int));
}
if (tmp == NULL) {
return ERROR;
}
for(int i = old_size; i < new_size; i++) {
tmp[i] = -111; /* something weird, for debugging */
}
seq->s_labelmap = tmp;
seq->s_labelmap_size = new_size;
int old_size = seq->s_labelmap_size;
RETURN_IF_ERROR(
ensure_array_large_enough(lbl,
(void**)&seq->s_labelmap,
&seq->s_labelmap_size,
INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE,
sizeof(int)));

for(int i = old_size; i < seq->s_labelmap_size; i++) {
seq->s_labelmap[i] = -111; /* something weird, for debugging */
}
seq->s_labelmap[lbl] = seq->s_used; /* label refers to the next instruction */
return SUCCESS;
Expand Down Expand Up @@ -1177,40 +1208,15 @@ static int
basicblock_next_instr(basicblock *b)
{
assert(b != NULL);
if (b->b_instr == NULL) {
b->b_instr = (struct cfg_instr *)PyObject_Calloc(
DEFAULT_BLOCK_SIZE, sizeof(struct cfg_instr));
if (b->b_instr == NULL) {
PyErr_NoMemory();
return ERROR;
}
b->b_ialloc = DEFAULT_BLOCK_SIZE;
}
else if (b->b_iused == b->b_ialloc) {
struct cfg_instr *tmp;
size_t oldsize, newsize;
oldsize = b->b_ialloc * sizeof(struct cfg_instr);
newsize = oldsize << 1;

if (oldsize > (SIZE_MAX >> 1)) {
PyErr_NoMemory();
return ERROR;
}
RETURN_IF_ERROR(
ensure_array_large_enough(
b->b_iused + 1,
(void**)&b->b_instr,
&b->b_ialloc,
DEFAULT_BLOCK_SIZE,
sizeof(struct cfg_instr)));

if (newsize == 0) {
PyErr_NoMemory();
return ERROR;
}
b->b_ialloc <<= 1;
tmp = (struct cfg_instr *)PyObject_Realloc(
(void *)b->b_instr, newsize);
if (tmp == NULL) {
PyErr_NoMemory();
return ERROR;
}
b->b_instr = tmp;
memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
}
return b->b_iused++;
}

Expand Down