Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2181,12 +2181,37 @@ def test_family_member_needs_transform_only_when_shape_changes(self):
output = self.generate_tables(input)
self.assert_slot_map_lines(
output,
"[OP_RAW] = {1, 1, {0}}",
"[OP_RAW_SPECIALIZED] = {1, 0, {0}}",
"[OP_RAW] = {1, 0, {0}}",
"[OP_RAW_SPECIALIZED] = {1, 1, {0}}",
"[OP_TYPED] = {1, 0, {0}}",
"[OP_TYPED_SPECIALIZED] = {1, 0, {0}}",
)

def test_record_transform_generated_from_recording_uop(self):
input = """
tier2 op(_RECORD_TOS, (tos -- tos)) {
RECORD_VALUE(PyStackRef_AsPyObjectBorrow(tos));
}
tier2 op(_RECORD_TOS_TYPE, (tos -- tos)) {
RECORD_VALUE(Py_TYPE(PyStackRef_AsPyObjectBorrow(tos)));
}
op(_DO_STUFF, (tos -- res)) {
res = tos;
}
macro(OP) = _RECORD_TOS + _DO_STUFF;
macro(OP_SPECIALIZED) = _RECORD_TOS_TYPE + _DO_STUFF;
family(OP, INLINE_CACHE_ENTRIES_OP) = { OP_SPECIALIZED };
"""
output = self.generate_tables(input)
self.assertIn("_PyOpcode_RecordTransform_TOS_TYPE", output)
self.assertIn("tos = PyStackRef_FromPyObjectBorrow(recorded_value);", output)
self.assertIn(
"transformed_value = (PyObject *)Py_TYPE(PyStackRef_AsPyObjectBorrow(tos));",
output,
)
self.assertIn("return _PyOpcode_RecordTransform_TOS_TYPE(value);", output)
self.assertNotIn("record_trace_transform_to_type", output)

def test_family_member_maps_positional_recorders_to_family_slots(self):
input = """
tier2 op(_RECORD_TOS, (sub -- sub)) {
Expand Down Expand Up @@ -2227,8 +2252,8 @@ def test_family_member_maps_non_positional_recorders_by_stack_shape(self):
output = self.generate_tables(input)
self.assert_slot_map_lines(
output,
"[OP] = {1, 1, {0}}",
"[OP_SPECIALIZED] = {1, 0, {0}}",
"[OP] = {1, 0, {0}}",
"[OP_SPECIALIZED] = {1, 1, {0}}",
)

def test_family_head_records_union_of_member_recorders(self):
Expand All @@ -2243,7 +2268,12 @@ def test_family_head_records_union_of_member_recorders(self):
macro(OP_SPECIALIZED) = _RECORD_TOS + _DO_STUFF;
family(OP, INLINE_CACHE_ENTRIES_OP) = { OP_SPECIALIZED };
"""
analysis = self.analyze_input(input)
output = self.generate_tables(input)
self.assertEqual(
analysis.families["OP"].member_record_names,
("_RECORD_TOS",),
)
self.assertIn("[OP] = {1, {_RECORD_TOS_INDEX}}", output)
self.assertIn("[OP_SPECIALIZED] = {1, {_RECORD_TOS_INDEX}}", output)
self.assert_slot_map_lines(output, "[OP_SPECIALIZED] = {1, 0, {0}}")
Expand Down Expand Up @@ -2277,12 +2307,12 @@ def test_family_detects_base_and_specialized_recording_difference(self):
),
["_RECORD_TOS_TYPE"],
)
self.assertIn("[OP] = {1, {_RECORD_TOS_TYPE_INDEX}}", output)
self.assertIn("[OP_SPECIALIZED] = {1, {_RECORD_TOS_TYPE_INDEX}}", output)
self.assertIn("[OP] = {1, {_RECORD_TOS_INDEX}}", output)
self.assertIn("[OP_SPECIALIZED] = {1, {_RECORD_TOS_INDEX}}", output)
self.assert_slot_map_lines(
output,
"[OP] = {1, 1, {0}}",
"[OP_SPECIALIZED] = {1, 0, {0}}",
"[OP] = {1, 0, {0}}",
"[OP_SPECIALIZED] = {1, 1, {0}}",
)

def test_family_head_falls_back_for_missing_member_slots(self):
Expand Down
38 changes: 0 additions & 38 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,44 +660,6 @@ is_terminator(const _PyUOpInstruction *uop)
);
}

static PyObject *
record_trace_transform_to_type(PyObject *value)
{
PyObject *tp = Py_NewRef((PyObject *)Py_TYPE(value));
Py_DECREF(value);
return tp;
}

/* _RECORD_NOS_GEN_FUNC and _RECORD_3OS_GEN_FUNC record the raw receiver.
* If it is a generator, return its function object; otherwise return NULL.
*/
static PyObject *
record_trace_transform_gen_func(PyObject *value)
{
PyObject *func = NULL;
if (PyGen_Check(value)) {
_PyStackRef f = ((PyGenObject *)value)->gi_iframe.f_funcobj;
if (!PyStackRef_IsNull(f)) {
func = Py_NewRef(PyStackRef_AsPyObjectBorrow(f));
}
}
Py_DECREF(value);
return func;
}

/* _RECORD_BOUND_METHOD records the raw callable.
* Keep it only for bound methods; otherwise return NULL.
*/
static PyObject *
record_trace_transform_bound_method(PyObject *value)
{
if (Py_TYPE(value) == &PyMethod_Type) {
return value;
}
Py_DECREF(value);
return NULL;
}

/* Returns 1 on success (added to trace), 0 on trace end.
*/
// gh-142543: inlining this function causes stack overflows
Expand Down
49 changes: 43 additions & 6 deletions Python/record_functions.c.h

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

11 changes: 11 additions & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ class Family:
size: str
members: list[Instruction]

@property
def member_record_names(self) -> tuple[str, ...]:
seen: set[str] = set()
names: list[str] = []
for member in self.members:
for part in member.parts:
if part.properties.records_value and part.name not in seen:
seen.add(part.name)
names.append(part.name)
return tuple(names)

def dump(self, indent: str) -> None:
print(indent, self.name, "= ", ", ".join([m.name for m in self.members]))

Expand Down
Loading
Loading