Skip to content

Improve the optimizer's check if a function is a prototype or not #10467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
13 changes: 8 additions & 5 deletions Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,16 +891,19 @@ zend_function *zend_optimizer_get_called_func(
&op_array->scope->function_table, method_name);
if (fbc) {
bool is_private = (fbc->common.fn_flags & ZEND_ACC_PRIVATE) != 0;
bool is_final = (fbc->common.fn_flags & ZEND_ACC_FINAL) != 0;
bool same_scope = fbc->common.scope == op_array->scope;
if (is_private) {
/* Only use private method if in the same scope. We can't even use it
* as a prototype, as it may be overridden with changed signature. */
bool same_scope = fbc->common.scope == op_array->scope;
return same_scope ? fbc : NULL;
}
/* If the method is non-final, it may be overridden,
* but only with a compatible method signature. */
*is_prototype = !is_final;
/* Prototype methods are potentially overridden. fbc still contains useful type information.
* Some optimizations may not be applied, like inlining or inferring the send-mode of superfluous args.
* A method cannot be overridden if the class or method is final. */
if ((fbc->common.fn_flags & ZEND_ACC_FINAL) == 0 &&
(fbc->common.scope->ce_flags & ZEND_ACC_FINAL) == 0) {
*is_prototype = true;
}
return fbc;
}
}
Expand Down
42 changes: 42 additions & 0 deletions ext/opcache/tests/opt/type_inference_final_class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Type inference test with final class
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.opt_debug_level=0x20000
opcache.preload=
--EXTENSIONS--
opcache
--FILE--
<?php

final class Test {
public function getInt(): int {
return 42;
}
public function getInt2(): int {
return $this->getInt();
}
}

?>
--EXPECTF--
$_main:
; (lines=1, args=0, vars=0, tmps=0)
; (after optimizer)
; %s
0000 RETURN int(1)

Test::getInt:
; (lines=1, args=0, vars=0, tmps=0)
; (after optimizer)
; %s
0000 RETURN int(42)

Test::getInt2:
; (lines=2, args=0, vars=0, tmps=1)
; (after optimizer)
; %s
0000 V0 = QM_ASSIGN int(42)
0001 RETURN V0