Skip to content

Commit 866510f

Browse files
Address review (add long functions to allowlist)
1 parent e88b71a commit 866510f

File tree

9 files changed

+47
-43
lines changed

9 files changed

+47
-43
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_generated_cases.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,9 +2225,11 @@ def test_validate_uop_unused_size_mismatch(self):
22252225
self.run_cases_test(input, input2, output)
22262226

22272227
def test_pure_uop_body_copied_in(self):
2228+
# Note: any non-escaping call works.
2229+
# In this case, we use _PyLong_Add.
22282230
input = """
22292231
pure op(OP, (foo -- res)) {
2230-
res = body(foo);
2232+
res = _PyLong_Add(foo);
22312233
}
22322234
"""
22332235
input2 = """
@@ -2248,15 +2250,14 @@ def test_pure_uop_body_copied_in(self):
22482250
_PyStackRef foo = sym_get_const_as_stackref(ctx, foo_sym);
22492251
_PyStackRef res_stackref;
22502252
/* Start of uop copied from bytecodes for constant evaluation */
2251-
res_stackref = body(foo);
2253+
res_stackref = _PyLong_Add(foo);
22522254
/* End of uop copied from bytecodes for constant evaluation */
22532255
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
22542256
stack_pointer[-1] = res;
2257+
break;
22552258
}
2256-
else {
2257-
res = sym_new_known(ctx, foo);
2258-
stack_pointer[-1] = res;
2259-
}
2259+
res = sym_new_known(ctx, foo);
2260+
stack_pointer[-1] = res;
22602261
break;
22612262
}
22622263
"""
@@ -2266,7 +2267,7 @@ def test_pure_uop_body_copied_in_complex(self):
22662267
input = """
22672268
pure op(OP, (foo -- res)) {
22682269
if (foo) {
2269-
res = body(foo);
2270+
res = _PyLong_Add(foo);
22702271
}
22712272
else {
22722273
res = 1;
@@ -2292,19 +2293,18 @@ def test_pure_uop_body_copied_in_complex(self):
22922293
_PyStackRef res_stackref;
22932294
/* Start of uop copied from bytecodes for constant evaluation */
22942295
if (foo) {
2295-
res_stackref = body(foo);
2296+
res_stackref = _PyLong_Add(foo);
22962297
}
22972298
else {
22982299
res_stackref = 1;
22992300
}
23002301
/* End of uop copied from bytecodes for constant evaluation */
23012302
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
23022303
stack_pointer[-1] = res;
2304+
break;
23032305
}
2304-
else {
2305-
res = sym_new_known(ctx, foo);
2306-
stack_pointer[-1] = res;
2307-
}
2306+
res = sym_new_known(ctx, foo);
2307+
stack_pointer[-1] = res;
23082308
break;
23092309
}
23102310
"""

Python/executor_cases.c.h

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_cases.c.h

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/cases_generator/analyzer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
635635
"_PyLong_IsNegative",
636636
"_PyLong_IsNonNegativeCompact",
637637
"_PyLong_IsZero",
638+
"_PyLong_Add",
639+
"_PyLong_Multiply",
640+
"_PyLong_Subtract",
638641
"_PyManagedDictPointer_IsValues",
639642
"_PyObject_GC_IS_SHARED",
640643
"_PyObject_GC_IS_TRACKED",

Tools/cases_generator/generators_common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def __init__(self, out: CWriter, labels: dict[str, Label]):
138138
}
139139
self.out = out
140140
self.labels = labels
141-
self.is_abstract = False
142141

143142
def emit_to_with_replacement(
144143
self,
@@ -512,7 +511,7 @@ def emit_SimpleStmt(
512511
reachable = True
513512
tkn = stmt.contents[-1]
514513
try:
515-
if stmt in uop.properties.escaping_calls and not self.is_abstract:
514+
if stmt in uop.properties.escaping_calls:
516515
escape = uop.properties.escaping_calls[stmt]
517516
if escape.kills is not None:
518517
self.stackref_kill(escape.kills, storage, True)
@@ -549,7 +548,7 @@ def emit_SimpleStmt(
549548
self.out.emit(tkn)
550549
else:
551550
self.out.emit(tkn)
552-
if stmt in uop.properties.escaping_calls and not self.is_abstract:
551+
if stmt in uop.properties.escaping_calls:
553552
self.emit_reload(storage)
554553
return reachable, None, storage
555554
except StackError as ex:

Tools/cases_generator/optimizer_generator.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class OptimizerEmitter(Emitter):
146146
def __init__(self, out: CWriter, labels: dict[str, Label], original_uop: Uop, stack: Stack):
147147
super().__init__(out, labels)
148148
self._replacers["REPLACE_OPCODE_IF_EVALUATES_PURE"] = self.replace_opcode_if_evaluates_pure
149-
self.is_abstract = True
150149
self.original_uop = original_uop
151150
self.stack = stack
152151

@@ -169,6 +168,12 @@ def replace_opcode_if_evaluates_pure(
169168
inst: Instruction | None,
170169
) -> bool:
171170
skip_to(tkn_iter, "SEMI")
171+
172+
if self.original_uop.properties.escapes:
173+
raise analysis_error(
174+
f"REPLACE_OPCODE_IF_EVALUATES_PURE cannot be used with an escaping uop {self.original_uop.properties.escaping_calls}",
175+
self.original_uop.body.open
176+
)
172177
emitter = OptimizerConstantEmitter(self.out, {}, self.original_uop, copy.deepcopy(self.stack))
173178
emitter.emit("if (\n")
174179
input_identifiers = replace_opcode_if_evaluates_pure_identifiers(uop)
@@ -256,11 +261,12 @@ def write_uop(
256261
override: Uop | None,
257262
uop: Uop,
258263
out: CWriter,
264+
stack: Stack,
259265
debug: bool,
266+
skip_inputs: bool,
260267
) -> None:
261268
locals: dict[str, Local] = {}
262269
prototype = override if override else uop
263-
stack = Stack()
264270
try:
265271
out.start_line()
266272
if override:
@@ -285,8 +291,8 @@ def write_uop(
285291
# No reference management of inputs needed.
286292
for var in storage.inputs: # type: ignore[possibly-undefined]
287293
var.in_local = False
294+
_, storage = emitter.emit_tokens(override, storage, None, False)
288295
out.start_line()
289-
_, storage = emitter.emit_tokens(override, storage, inst=None, emit_braces=False)
290296
storage.flush(out)
291297
out.start_line()
292298
else:
@@ -335,7 +341,8 @@ def generate_abstract_interpreter(
335341
declare_variables(override, out, skip_inputs=False)
336342
else:
337343
declare_variables(uop, out, skip_inputs=True)
338-
write_uop(override, uop, out, debug)
344+
stack = Stack()
345+
write_uop(override, uop, out, stack, debug, skip_inputs=(override is None))
339346
out.start_line()
340347
out.emit("break;\n")
341348
out.emit("}")

0 commit comments

Comments
 (0)