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-120367: fix removal of redundant NOPs and jumps after reordering hot-cold blocks#120425

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 2 commits intopython:mainfromiritkatriel:gh-120367
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
gh-120367: fix removal of redundant NOPs and jumps after reordering h…
…ot-cold blocks
  • Loading branch information
@iritkatriel
iritkatriel committedJun 12, 2024
commit955ab46b1fdfeda3634acbf60c1fefc875eabc66
25 changes: 25 additions & 0 deletionsLib/test/test_compile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -502,6 +502,31 @@ def test_compile_invalid_namedexpr(self):
with self.assertRaisesRegex(TypeError, "NamedExpr target must be a Name"):
compile(ast.fix_missing_locations(m), "<file>", "exec")

def test_compile_redundant_jumps_and_nops_after_moving_cold_blocks(self):
# See gh-120367
code=textwrap.dedent("""
try:
pass
except:
pass
else:
match name_2:
case b'':
pass
finally:
something
""")

tree = ast.parse(code)
for node in ast.walk(tree):
if hasattr(node,"lineno"):
del node.lineno
del node.end_lineno
del node.col_offset
del node.end_col_offset
Comment on lines +524 to +528
Copy link
Member

Choose a reason for hiding this comment

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

why is it important for this test to remove node locations and then useast.fix_missing_locations? maybe worth a comment?


compile(ast.fix_missing_locations(tree), "<file>", "exec")

def test_compile_ast(self):
fname = __file__
if fname.lower().endswith('pyc'):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fix crash in compiler on code with redundant NOPs and JUMPs which show up
after exception handlers are moved to the end of the code.
30 changes: 18 additions & 12 deletionsPython/flowgraph.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1857,6 +1857,22 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)

static int resolve_line_numbers(cfg_builder *g, int firstlineno);

static int
remove_redundant_nops_and_jumps(cfg_builder *g)
{
int removed_nops, removed_jumps;
do {
/* Convergence is guaranteed because the number of
* redundant jumps and nops only decreases.
*/
removed_nops = remove_redundant_nops(g);
RETURN_IF_ERROR(removed_nops);
removed_jumps = remove_redundant_jumps(g);
RETURN_IF_ERROR(removed_jumps);
} while(removed_nops + removed_jumps > 0);
return SUCCESS;
}

/* Perform optimizations on a control flow graph.
The consts object should still be in list form to allow new constants
to be appended.
Expand All@@ -1878,17 +1894,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache, int firstl
}
RETURN_IF_ERROR(remove_redundant_nops_and_pairs(g->g_entryblock));
RETURN_IF_ERROR(remove_unreachable(g->g_entryblock));

int removed_nops, removed_jumps;
do {
/* Convergence is guaranteed because the number of
* redundant jumps and nops only decreases.
*/
removed_nops = remove_redundant_nops(g);
RETURN_IF_ERROR(removed_nops);
removed_jumps = remove_redundant_jumps(g);
RETURN_IF_ERROR(removed_jumps);
} while(removed_nops + removed_jumps > 0);
RETURN_IF_ERROR(remove_redundant_nops_and_jumps(g));
assert(no_redundant_jumps(g));
return SUCCESS;
}
Expand DownExpand Up@@ -2358,7 +2364,7 @@ push_cold_blocks_to_end(cfg_builder *g) {
b->b_next = cold_blocks;

if (cold_blocks != NULL) {
RETURN_IF_ERROR(remove_redundant_jumps(g));
RETURN_IF_ERROR(remove_redundant_nops_and_jumps(g));
}
return SUCCESS;
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp