Skip to content

Commit

Permalink
Mark replaced uops in expansion table
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon committed Dec 14, 2023
1 parent 436ad9b commit e219a26
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
9 changes: 9 additions & 0 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def f():
with temporary_optimizer(opt):
f()
exe = get_first_executor(f)
self.assertIsNotNone(exe)
self.assertTrue(exe.is_valid())
_testinternalcapi.invalidate_executors(f.__code__)
self.assertFalse(exe.is_valid())
Expand All @@ -196,7 +197,7 @@ def testfunc(x):
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
self.assertIn("_SET_IP", uops)
self.assertIn("LOAD_FAST", uops)
self.assertIn("_LOAD_FAST", uops)

def test_extended_arg(self):
"Check EXTENDED_ARG handling in superblock creation"
Expand Down
18 changes: 9 additions & 9 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,6 @@ translate_bytecode_to_trace(
oparg += extras;
}
}
if (_PyUOp_Replacements[uop]) {
uop = _PyUOp_Replacements[uop];
if (uop == _FOR_ITER_TIER_TWO) {
target += 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1;
assert(_PyCode_CODE(code)[target-1].op.code == END_FOR ||
_PyCode_CODE(code)[target-1].op.code == INSTRUMENTED_END_FOR);
}
}
break;
case OPARG_CACHE_1:
operand = read_u16(&instr[offset].cache);
Expand All @@ -661,7 +653,15 @@ translate_bytecode_to_trace(
oparg = offset;
assert(uop == _SAVE_RETURN_OFFSET);
break;

case OPARG_REPLACED:
uop = _PyUOp_Replacements[uop];
assert(uop != 0);
if (uop == _FOR_ITER_TIER_TWO) {
target += 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1;
assert(_PyCode_CODE(code)[target-1].op.code == END_FOR ||
_PyCode_CODE(code)[target-1].op.code == INSTRUMENTED_END_FOR);
}
break;
default:
fprintf(stderr,
"opcode=%d, oparg=%d; nuops=%d, i=%d; size=%d, offset=%d\n",
Expand Down
8 changes: 7 additions & 1 deletion Tools/cases_generator/opcode_metadata_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"OPARG_TOP": 5,
"OPARG_BOTTOM": 6,
"OPARG_SAVE_RETURN_OFFSET": 7,
# Skip 8 as the other powers of 2 are sizes
"OPARG_REPLACED": 9
}


Expand Down Expand Up @@ -244,6 +246,8 @@ def generate_expansion_table(
# Skip specializations
if "specializing" in part.annotations:
continue
if "replaced" in part.annotations:
size = OPARG_SIZES["OPARG_REPLACED"]
expansions.append((part.name, size, offset if size else 0))
offset += part.size
expansions_table[inst.name] = expansions
Expand All @@ -267,9 +271,11 @@ def is_viable_expansion(inst: Instruction) -> bool:
"An instruction can be expanded if all its parts are viable for tier 2"
for part in inst.parts:
if isinstance(part, Uop):
# Skip specializations
# Skip specializing and replaced uops
if "specializing" in part.annotations:
continue
if "replaced" in part.annotations:
continue
if part.properties.tier_one_only or not part.is_viable():
return False
return True
Expand Down

0 comments on commit e219a26

Please sign in to comment.