Skip to content

Commit 86bc821

Browse files
committed
py/emitnative: Optimise native code unwinding jumps.
This commit introduces an optimisation for emitting unwinding jump blocks in native code. Currently the native emitter will emit a jump to the block's unwinding code even though that jump is located right before the unwinding sequence itself. So the unwind code sequence would look like this (as emitted by mpy-cross using "-march=debug"): ... jump(label_0) label(label_0) ... EXIT(0) or: ... jump(label_0) dead_code ... label(label_0) ... EXIT(0) With this change, the native emitter only emits jumps to the exit label (label_0 in the examples above) when unwinding if both the exception stack and the emitter stack are empty, as it means the jump is at the very end of the code block. This fixes issue #16604. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 6db2997 commit 86bc821

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

py/emitnative.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,20 @@ static void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label)
20822082
}
20832083

20842084
static void emit_native_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
2085+
#if !MICROPY_PY_MICROPYTHON_HEAP_LOCKED
2086+
// Skip the jump only if the exception stack and the emitter stack are
2087+
// empty. An unwind request with an empty exception stack and an empty
2088+
// emitter stack only occurs at the end of a code block and it is the
2089+
// last reference to the exit label from an unwind block. In that case
2090+
// the unwind block is right before the exit label anyway, so the jump
2091+
// can be omitted.
2092+
if (except_depth == 0 && label == emit->exit_label && emit->stack_info->vtype == VTYPE_PTR_NONE) {
2093+
// Simulate emit_native_jump's side effects.
2094+
mp_asm_base_suppress_code(&emit->as->base);
2095+
return;
2096+
}
2097+
#endif
2098+
20852099
if (except_depth > 0) {
20862100
exc_stack_entry_t *first_finally = NULL;
20872101
exc_stack_entry_t *prev_finally = NULL;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy