Skip to content
Prev Previous commit
Next Next commit
optimize code struct
  • Loading branch information
cocolato committed Jan 12, 2026
commit 25797cf308d8d5497f61bd45099bdc34ff9e3e88
14 changes: 10 additions & 4 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ optimize_uops(
}
#endif

int out_len_before = ctx->tracer->out_len;
switch (opcode) {

#include "optimizer_cases.c.h"
Expand All @@ -428,7 +427,7 @@ optimize_uops(
Py_UNREACHABLE();
}
// If no ADD_OP was called during this iteration, copy the original instruction
if (ctx->tracer->out_len == out_len_before) {
if (ctx->tracer->out_len == i) {
ctx->tracer->out_buffer[ctx->tracer->out_len++] = *this_instr;
}
assert(ctx->frame != NULL);
Expand Down Expand Up @@ -461,8 +460,15 @@ optimize_uops(
_Py_uop_abstractcontext_fini(ctx);
// Check that the trace ends with a proper terminator
if (ctx->tracer->out_len > 0) {
if (!is_terminator_uop(&ctx->tracer->out_buffer[ctx->tracer->out_len - 1])) {
return 0;
_PyUOpInstruction *last_uop = &ctx->tracer->out_buffer[ctx->tracer->out_len - 1];
if (!is_terminator_uop(last_uop)) {
// Copy remaining uops from original trace until we find a terminator
for (int i = ctx->tracer->out_len; i < trace_len; i++) {
ctx->tracer->out_buffer[ctx->tracer->out_len++] = trace[i];
if (is_terminator_uop(&trace[i])) {
break;
}
}
}
}

Expand Down