Skip to content

Commit 4d6dde5

Browse files
derickriluuu1994
andauthored
Emit EXT_STMT after each pipe stage, and attach the TMP var that holds the intermediary result (#19377)
* Emit EXT_STMT after each pipe stage, and attach the TMP var that holds the intermediary result * Add ZEND_EXT_STMT to keeps_op1_alive as per review * Fix leak with EXT_STMT when pipe result is unused Co-authored-by: Ilija Tovilo <[email protected]>
1 parent 07a9c25 commit 4d6dde5

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Zend/Optimizer/block_pass.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
420420
}
421421
break;
422422

423+
case ZEND_EXT_STMT:
424+
if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
425+
/* Variable will be deleted later by FREE, so we can't optimize it */
426+
Tsource[VAR_NUM(opline->op1.var)] = NULL;
427+
break;
428+
}
429+
break;
430+
423431
case ZEND_CASE:
424432
case ZEND_CASE_STRICT:
425433
case ZEND_COPY_TMP:

Zend/zend_compile.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ static void zend_do_free(znode *op1) /* {{{ */
821821
} else {
822822
while (opline >= CG(active_op_array)->opcodes) {
823823
if ((opline->opcode == ZEND_FETCH_LIST_R ||
824-
opline->opcode == ZEND_FETCH_LIST_W) &&
824+
opline->opcode == ZEND_FETCH_LIST_W ||
825+
opline->opcode == ZEND_EXT_STMT) &&
825826
opline->op1_type == IS_VAR &&
826827
opline->op1.var == op1->u.op.var) {
827828
zend_emit_op(NULL, ZEND_FREE, op1, NULL);
@@ -1920,7 +1921,7 @@ static void zend_add_to_list(void *result, void *item) /* {{{ */
19201921
}
19211922
/* }}} */
19221923

1923-
static void zend_do_extended_stmt(void) /* {{{ */
1924+
static void zend_do_extended_stmt(znode* result) /* {{{ */
19241925
{
19251926
zend_op *opline;
19261927

@@ -1931,6 +1932,9 @@ static void zend_do_extended_stmt(void) /* {{{ */
19311932
opline = get_next_op();
19321933

19331934
opline->opcode = ZEND_EXT_STMT;
1935+
if (result) {
1936+
SET_NODE(opline->op1, result);
1937+
}
19341938
}
19351939
/* }}} */
19361940

@@ -6050,7 +6054,7 @@ static void zend_compile_for(zend_ast *ast) /* {{{ */
60506054

60516055
zend_update_jump_target_to_next(opnum_jmp);
60526056
zend_compile_for_expr_list(&result, cond_ast);
6053-
zend_do_extended_stmt();
6057+
zend_do_extended_stmt(NULL);
60546058

60556059
zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
60566060

@@ -6171,7 +6175,7 @@ static void zend_compile_if(zend_ast *ast) /* {{{ */
61716175

61726176
if (i > 0) {
61736177
CG(zend_lineno) = cond_ast->lineno;
6174-
zend_do_extended_stmt();
6178+
zend_do_extended_stmt(NULL);
61756179
}
61766180

61776181
zend_compile_expr(&cond_node, cond_ast);
@@ -6505,6 +6509,8 @@ static void zend_compile_pipe(znode *result, zend_ast *ast)
65056509
}
65066510

65076511
zend_compile_expr(result, fcall_ast);
6512+
CG(zend_lineno) = fcall_ast->lineno;
6513+
zend_do_extended_stmt(result);
65086514
}
65096515

65106516
static void zend_compile_match(znode *result, zend_ast *ast)
@@ -8537,7 +8543,7 @@ static zend_op_array *zend_compile_func_decl_ex(
85378543
/* put the implicit return on the really last line */
85388544
CG(zend_lineno) = decl->end_lineno;
85398545

8540-
zend_do_extended_stmt();
8546+
zend_do_extended_stmt(NULL);
85418547
zend_emit_final_return(0);
85428548

85438549
pass_two(CG(active_op_array));
@@ -11607,7 +11613,7 @@ static void zend_compile_stmt(zend_ast *ast) /* {{{ */
1160711613
CG(zend_lineno) = ast->lineno;
1160811614

1160911615
if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11610-
zend_do_extended_stmt();
11616+
zend_do_extended_stmt(NULL);
1161111617
}
1161211618

1161311619
switch (ast->kind) {

Zend/zend_opcode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ static bool keeps_op1_alive(zend_op *opline) {
903903
|| opline->opcode == ZEND_MATCH_ERROR
904904
|| opline->opcode == ZEND_FETCH_LIST_R
905905
|| opline->opcode == ZEND_FETCH_LIST_W
906-
|| opline->opcode == ZEND_COPY_TMP) {
906+
|| opline->opcode == ZEND_COPY_TMP
907+
|| opline->opcode == ZEND_EXT_STMT) {
907908
return 1;
908909
}
909910
ZEND_ASSERT(opline->opcode != ZEND_FE_FETCH_R

0 commit comments

Comments
 (0)