Skip to content

Commit 98e785d

Browse files
authored
gh-87092: in compiler, move the detection of exception handlers before the CFG optimization stage (GH-96935)
1 parent fc05107 commit 98e785d

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

Python/compile.c

+32-16
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,8 @@ typedef struct basicblock_ {
258258
int b_iused;
259259
/* length of instruction array (b_instr) */
260260
int b_ialloc;
261-
/* Number of predecssors that a block has. */
261+
/* Number of predecessors that a block has. */
262262
int b_predecessors;
263-
/* Number of predecssors that a block has as an exception handler. */
264-
int b_except_predecessors;
265263
/* depth of stack upon entry of block, computed by stackdepth() */
266264
int b_startdepth;
267265
/* instruction offset for block, computed by assemble_jump_offsets() */
@@ -270,6 +268,8 @@ typedef struct basicblock_ {
270268
unsigned b_preserve_lasti : 1;
271269
/* Used by compiler passes to mark whether they have visited a basic block. */
272270
unsigned b_visited : 1;
271+
/* b_except_handler is used by the cold-detection algorithm to mark exception targets */
272+
unsigned b_except_handler : 1;
273273
/* b_cold is true if this block is not perf critical (like an exception handler) */
274274
unsigned b_cold : 1;
275275
/* b_warm is used by the cold-detection algorithm to mark blocks which are definitely not cold */
@@ -7296,6 +7296,25 @@ label_exception_targets(basicblock *entryblock) {
72967296
return -1;
72977297
}
72987298

7299+
7300+
static int
7301+
mark_except_handlers(basicblock *entryblock) {
7302+
#ifndef NDEBUG
7303+
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7304+
assert(!b->b_except_handler);
7305+
}
7306+
#endif
7307+
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7308+
for (int i=0; i < b->b_iused; i++) {
7309+
struct instr *instr = &b->b_instr[i];
7310+
if (is_block_push(instr)) {
7311+
instr->i_target->b_except_handler = 1;
7312+
}
7313+
}
7314+
}
7315+
return 0;
7316+
}
7317+
72997318
static int
73007319
mark_warm(basicblock *entryblock) {
73017320
basicblock **stack = make_cfg_traversal_stack(entryblock);
@@ -7308,7 +7327,7 @@ mark_warm(basicblock *entryblock) {
73087327
entryblock->b_visited = 1;
73097328
while (sp > stack) {
73107329
basicblock *b = *(--sp);
7311-
assert(!b->b_except_predecessors);
7330+
assert(!b->b_except_handler);
73127331
b->b_warm = 1;
73137332
basicblock *next = b->b_next;
73147333
if (next && BB_HAS_FALLTHROUGH(b) && !next->b_visited) {
@@ -7343,8 +7362,7 @@ mark_cold(basicblock *entryblock) {
73437362

73447363
basicblock **sp = stack;
73457364
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7346-
if (b->b_except_predecessors) {
7347-
assert(b->b_except_predecessors == b->b_predecessors);
7365+
if (b->b_except_handler) {
73487366
assert(!b->b_warm);
73497367
*sp++ = b;
73507368
b->b_visited = 1;
@@ -8257,8 +8275,8 @@ static void
82578275
dump_basicblock(const basicblock *b)
82588276
{
82598277
const char *b_return = basicblock_returns(b) ? "return " : "";
8260-
fprintf(stderr, "%d: [%d %d %d %p] used: %d, depth: %d, offset: %d %s\n",
8261-
b->b_label, b->b_cold, b->b_warm, BB_NO_FALLTHROUGH(b), b, b->b_iused,
8278+
fprintf(stderr, "%d: [EH=%d CLD=%d WRM=%d NO_FT=%d %p] used: %d, depth: %d, offset: %d %s\n",
8279+
b->b_label, b->b_except_handler, b->b_cold, b->b_warm, BB_NO_FALLTHROUGH(b), b, b->b_iused,
82628280
b->b_startdepth, b->b_offset, b_return);
82638281
if (b->b_instr) {
82648282
int i;
@@ -8616,6 +8634,12 @@ assemble(struct compiler *c, int addNone)
86168634
if (calculate_jump_targets(g->g_entryblock)) {
86178635
goto error;
86188636
}
8637+
if (mark_except_handlers(g->g_entryblock) < 0) {
8638+
goto error;
8639+
}
8640+
if (label_exception_targets(g->g_entryblock)) {
8641+
goto error;
8642+
}
86198643
if (optimize_cfg(g, consts, c->c_const_cache)) {
86208644
goto error;
86218645
}
@@ -8634,9 +8658,6 @@ assemble(struct compiler *c, int addNone)
86348658
}
86358659
/* TO DO -- For 3.12, make sure that `maxdepth <= MAX_ALLOWED_STACK_USE` */
86368660

8637-
if (label_exception_targets(g->g_entryblock)) {
8638-
goto error;
8639-
}
86408661
convert_exception_handlers_to_nops(g->g_entryblock);
86418662

86428663
if (push_cold_blocks_to_end(g, code_flags) < 0) {
@@ -9353,11 +9374,6 @@ mark_reachable(basicblock *entryblock) {
93539374
*sp++ = target;
93549375
}
93559376
target->b_predecessors++;
9356-
if (is_block_push(instr)) {
9357-
target->b_except_predecessors++;
9358-
}
9359-
assert(target->b_except_predecessors == 0 ||
9360-
target->b_except_predecessors == target->b_predecessors);
93619377
}
93629378
}
93639379
}

0 commit comments

Comments
 (0)