Skip to content

Commit bb9a1a2

Browse files
committed
Add target field to uop IR
1 parent 69cee8d commit bb9a1a2

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

Include/internal/pycore_uops.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ extern "C" {
1313
#define _Py_UOP_MAX_TRACE_LENGTH 128
1414

1515
typedef struct {
16-
uint32_t opcode;
17-
uint32_t oparg;
16+
uint16_t opcode;
17+
uint16_t oparg;
18+
uint32_t target;
1819
uint64_t operand; // A cache entry
1920
} _PyUOpInstruction;
2021

Python/optimizer.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ translate_bytecode_to_trace(
446446
#define DPRINTF(level, ...)
447447
#endif
448448

449-
#define ADD_TO_TRACE(OPCODE, OPARG, OPERAND) \
449+
450+
#define ADD_TO_TRACE(OPCODE, OPARG, OPERAND, TARGET) \
450451
DPRINTF(2, \
451452
" ADD_TO_TRACE(%s, %d, %" PRIu64 ")\n", \
452453
uop_name(OPCODE), \
@@ -458,6 +459,7 @@ translate_bytecode_to_trace(
458459
trace[trace_length].opcode = (OPCODE); \
459460
trace[trace_length].oparg = (OPARG); \
460461
trace[trace_length].operand = (OPERAND); \
462+
trace[trace_length].target = (TARGET); \
461463
trace_length++;
462464

463465
#define INSTR_IP(INSTR, CODE) \
@@ -493,7 +495,7 @@ translate_bytecode_to_trace(
493495
if (trace_stack_depth >= TRACE_STACK_SIZE) { \
494496
DPRINTF(2, "Trace stack overflow\n"); \
495497
OPT_STAT_INC(trace_stack_overflow); \
496-
ADD_TO_TRACE(_SET_IP, 0, 0); \
498+
ADD_TO_TRACE(_SET_IP, 0, 0, 0); \
497499
goto done; \
498500
} \
499501
trace_stack[trace_stack_depth].code = code; \
@@ -517,18 +519,22 @@ translate_bytecode_to_trace(
517519
top: // Jump here after _PUSH_FRAME or likely branches
518520
for (;;) {
519521
RESERVE_RAW(3, "epilogue"); // Always need space for _SET_IP, _CHECK_VALIDITY and _EXIT_TRACE
520-
ADD_TO_TRACE(_SET_IP, INSTR_IP(instr, code), 0);
521-
ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0);
522+
ADD_TO_TRACE(_SET_IP, INSTR_IP(instr, code), 0, 0);
523+
ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0, INSTR_IP(instr, code));
522524

523525
uint32_t opcode = instr->op.code;
524526
uint32_t oparg = instr->op.arg;
525527
uint32_t extras = 0;
526528

527-
while (opcode == EXTENDED_ARG) {
529+
if (opcode == EXTENDED_ARG) {
528530
instr++;
529531
extras += 1;
530532
opcode = instr->op.code;
531533
oparg = (oparg << 8) | instr->op.arg;
534+
if (opcode == EXTENDED_ARG) {
535+
instr--;
536+
goto done;
537+
}
532538
}
533539

534540
if (opcode == ENTER_EXECUTOR) {
@@ -554,7 +560,7 @@ translate_bytecode_to_trace(
554560
DPRINTF(4, "%s(%d): counter=%x, bitcount=%d, likely=%d, uopcode=%s\n",
555561
uop_name(opcode), oparg,
556562
counter, bitcount, jump_likely, uop_name(uopcode));
557-
ADD_TO_TRACE(uopcode, max_length, 0);
563+
ADD_TO_TRACE(uopcode, max_length, 0, INSTR_IP(instr, code));
558564
if (jump_likely) {
559565
_Py_CODEUNIT *target_instr = next_instr + oparg;
560566
DPRINTF(2, "Jump likely (%x = %d bits), continue at byte offset %d\n",
@@ -569,7 +575,7 @@ translate_bytecode_to_trace(
569575
{
570576
if (instr + 2 - oparg == initial_instr && code == initial_code) {
571577
RESERVE(1, 0);
572-
ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0);
578+
ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0, 0);
573579
}
574580
else {
575581
OPT_STAT_INC(inner_loop);
@@ -653,7 +659,7 @@ translate_bytecode_to_trace(
653659
expansion->uops[i].offset);
654660
Py_FatalError("garbled expansion");
655661
}
656-
ADD_TO_TRACE(uop, oparg, operand);
662+
ADD_TO_TRACE(uop, oparg, operand, INSTR_IP(instr, code));
657663
if (uop == _POP_FRAME) {
658664
TRACE_STACK_POP();
659665
DPRINTF(2,
@@ -682,15 +688,15 @@ translate_bytecode_to_trace(
682688
PyUnicode_AsUTF8(new_code->co_filename),
683689
new_code->co_firstlineno);
684690
OPT_STAT_INC(recursive_call);
685-
ADD_TO_TRACE(_SET_IP, 0, 0);
691+
ADD_TO_TRACE(_SET_IP, 0, 0, 0);
686692
goto done;
687693
}
688694
if (new_code->co_version != func_version) {
689695
// func.__code__ was updated.
690696
// Perhaps it may happen again, so don't bother tracing.
691697
// TODO: Reason about this -- is it better to bail or not?
692698
DPRINTF(2, "Bailing because co_version != func_version\n");
693-
ADD_TO_TRACE(_SET_IP, 0, 0);
699+
ADD_TO_TRACE(_SET_IP, 0, 0, 0);
694700
goto done;
695701
}
696702
// Increment IP to the return address
@@ -707,7 +713,7 @@ translate_bytecode_to_trace(
707713
2 * INSTR_IP(instr, code));
708714
goto top;
709715
}
710-
ADD_TO_TRACE(_SET_IP, 0, 0);
716+
ADD_TO_TRACE(_SET_IP, 0, 0, 0);
711717
goto done;
712718
}
713719
}
@@ -732,7 +738,7 @@ translate_bytecode_to_trace(
732738
assert(code == initial_code);
733739
// Skip short traces like _SET_IP, LOAD_FAST, _SET_IP, _EXIT_TRACE
734740
if (trace_length > 4) {
735-
ADD_TO_TRACE(_EXIT_TRACE, 0, 0);
741+
ADD_TO_TRACE(_EXIT_TRACE, 0, 0, INSTR_IP(instr, code));
736742
DPRINTF(1,
737743
"Created a trace for %s (%s:%d) at byte offset %d -- length %d+%d\n",
738744
PyUnicode_AsUTF8(code->co_qualname),

0 commit comments

Comments
 (0)