Skip to content
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

gh-102860: improve performance of compiler's instr_sequence_to_cfg #102861

Merged
merged 2 commits into from
Mar 21, 2023
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
50 changes: 44 additions & 6 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,52 @@ static int
instr_sequence_to_cfg(instr_sequence *seq, cfg_builder *g) {
memset(g, 0, sizeof(cfg_builder));
RETURN_IF_ERROR(cfg_builder_init(g));
/* Note: there can be more than one label for the same offset */

/* There can be more than one label for the same offset. The
* offset2lbl maping selects one of them which we use consistently.
*/

int *offset2lbl = PyMem_Malloc(seq->s_used * sizeof(int));
if (offset2lbl == NULL) {
PyErr_NoMemory();
return ERROR;
}
for (int i = 0; i < seq->s_used; i++) {
for (int j=0; j < seq->s_labelmap_size; j++) {
if (seq->s_labelmap[j] == i) {
jump_target_label lbl = {j};
RETURN_IF_ERROR(cfg_builder_use_label(g, lbl));
offset2lbl[i] = -1;
}
for (int lbl=0; lbl < seq->s_labelmap_size; lbl++) {
int offset = seq->s_labelmap[lbl];
if (offset >= 0) {
assert(offset < seq->s_used);
offset2lbl[offset] = lbl;
}
}

for (int i = 0; i < seq->s_used; i++) {
int lbl = offset2lbl[i];
if (lbl >= 0) {
assert (lbl < seq->s_labelmap_size);
jump_target_label lbl_ = {lbl};
if (cfg_builder_use_label(g, lbl_) < 0) {
goto error;
}
}
instruction *instr = &seq->s_instrs[i];
RETURN_IF_ERROR(cfg_builder_addop(g, instr->i_opcode, instr->i_oparg, instr->i_loc));
int opcode = instr->i_opcode;
int oparg = instr->i_oparg;
if (HAS_TARGET(opcode)) {
int offset = seq->s_labelmap[oparg];
assert(offset >= 0 && offset < seq->s_used);
int lbl = offset2lbl[offset];
assert(lbl >= 0 && lbl < seq->s_labelmap_size);
oparg = lbl;
}
Comment on lines +631 to +637
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is required now because we only call cfg_builder_use_label for the one canonical label for an offset, rather than for every label? So we have to ensure we use the canonical label for jump targets also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

if (cfg_builder_addop(g, opcode, oparg, instr->i_loc) < 0) {
goto error;
}
}
PyMem_Free(offset2lbl);

int nblocks = 0;
for (basicblock *b = g->g_block_list; b != NULL; b = b->b_list) {
nblocks++;
Expand All @@ -615,6 +650,9 @@ instr_sequence_to_cfg(instr_sequence *seq, cfg_builder *g) {
return ERROR;
}
return SUCCESS;
error:
PyMem_Free(offset2lbl);
return ERROR;
}


Expand Down