diff options
author | Tomas R. <tomas.roun8@gmail.com> | 2025-04-26 20:47:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-27 02:47:55 +0800 |
commit | 5e96e4fca80a8cd25da6b469b25f8f5a514de8be (patch) | |
tree | 76c746c85b45b12c93d72a145d87c234593f4042 /Python | |
parent | 56c88e4e8d4b183147ca9a7609dfdea96cb03e18 (diff) | |
download | cpython-5e96e4fca80a8cd25da6b469b25f8f5a514de8be.tar.gz cpython-5e96e4fca80a8cd25da6b469b25f8f5a514de8be.zip |
gh-131798: JIT: Propagate the result in `_BINARY_OP_SUBSCR_TUPLE_INT` (GH-133003)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/optimizer_bytecodes.c | 21 | ||||
-rw-r--r-- | Python/optimizer_cases.c.h | 22 |
2 files changed, 42 insertions, 1 deletions
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 040e54479b7..f862c9c8c6a 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -370,6 +370,27 @@ dummy_func(void) { res = sym_new_type(ctx, &PyUnicode_Type); } + op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) { + assert(sym_matches_type(tuple_st, &PyTuple_Type)); + if (sym_is_const(ctx, sub_st)) { + assert(PyLong_CheckExact(sym_get_const(ctx, sub_st))); + long index = PyLong_AsLong(sym_get_const(ctx, sub_st)); + assert(index >= 0); + int tuple_length = sym_tuple_length(tuple_st); + if (tuple_length == -1) { + // Unknown length + res = sym_new_not_null(ctx); + } + else { + assert(index < tuple_length); + res = sym_tuple_getitem(ctx, tuple_st, index); + } + } + else { + res = sym_new_not_null(ctx); + } + } + op(_TO_BOOL, (value -- res)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res); if (!already_bool) { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 9a5a362ec19..c92b036eb56 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -617,8 +617,28 @@ } case _BINARY_OP_SUBSCR_TUPLE_INT: { + JitOptSymbol *sub_st; + JitOptSymbol *tuple_st; JitOptSymbol *res; - res = sym_new_not_null(ctx); + sub_st = stack_pointer[-1]; + tuple_st = stack_pointer[-2]; + assert(sym_matches_type(tuple_st, &PyTuple_Type)); + if (sym_is_const(ctx, sub_st)) { + assert(PyLong_CheckExact(sym_get_const(ctx, sub_st))); + long index = PyLong_AsLong(sym_get_const(ctx, sub_st)); + assert(index >= 0); + int tuple_length = sym_tuple_length(tuple_st); + if (tuple_length == -1) { + res = sym_new_not_null(ctx); + } + else { + assert(index < tuple_length); + res = sym_tuple_getitem(ctx, tuple_st, index); + } + } + else { + res = sym_new_not_null(ctx); + } stack_pointer[-2] = res; stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); |