-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add optimizer support for prototype methods #7452
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#include "zend_call_graph.h" | ||
#include "zend_func_info.h" | ||
#include "zend_inference.h" | ||
#include "zend_execute.h" | ||
#ifdef _WIN32 | ||
#include "win32/ioutil.h" | ||
#endif | ||
|
@@ -100,18 +101,20 @@ ZEND_API int zend_func_info_rid = -1; | |
|
||
uint32_t zend_get_internal_func_info( | ||
const zend_function *callee_func, const zend_call_info *call_info, const zend_ssa *ssa) { | ||
if (callee_func->common.scope) { | ||
/* This is a method, not a function. */ | ||
if (callee_func->common.scope && !call_info->is_prototype) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be |
||
/* This is a non-prototype method. */ | ||
return 0; | ||
} | ||
|
||
zend_string *name = callee_func->common.function_name; | ||
if (!name) { | ||
zend_string *name = get_function_or_method_name_or_null(callee_func); | ||
if (name == NULL) { | ||
zend_string_release(name); | ||
/* zend_pass_function has no name. */ | ||
return 0; | ||
} | ||
|
||
zval *zv = zend_hash_find_known_hash(&func_info, name); | ||
zend_string_release(name); | ||
if (!zv) { | ||
return 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3885,6 +3885,7 @@ static zend_always_inline zend_result _zend_update_type_info( | |
case ZEND_DO_ICALL: | ||
case ZEND_DO_UCALL: | ||
case ZEND_DO_FCALL_BY_NAME: | ||
case ZEND_INIT_METHOD_CALL: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong. |
||
if (ssa_op->result_def >= 0) { | ||
zend_func_info *func_info = ZEND_FUNC_INFO(op_array); | ||
zend_call_info *call_info; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -914,6 +914,28 @@ zend_function *zend_optimizer_get_called_func( | |
} | ||
return fbc; | ||
} | ||
} else if (opline->op1_type == IS_CONST | ||
&& Z_TYPE_P(CRT_CONSTANT(opline->op1)) == IS_OBJECT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Objects cannot be |
||
&& opline->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_STRING | ||
&& op_array->scope | ||
&& !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE) | ||
&& !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)) { | ||
zend_string *method_name = Z_STR_P(CRT_CONSTANT(opline->op2) + 1); | ||
zend_function *fbc = zend_hash_find_ptr( | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&op_array->scope->function_table, method_name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class here should be based on op1 though, not |
||
if (fbc) { | ||
if (fbc->type == ZEND_INTERNAL_FUNCTION | ||
|| (fbc->type == ZEND_USER_FUNCTION && fbc->common.fn_flags & ZEND_ACC_PUBLIC)) { | ||
/* 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 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
(fbc->common.scope->ce_flags & ZEND_ACC_FINAL) == 0) { | ||
*is_prototype = true; | ||
} | ||
return fbc; | ||
} | ||
} | ||
} | ||
break; | ||
case ZEND_NEW: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -364,6 +364,10 @@ protected function __construct(string $name, bool $isBuiltin) { | |
$this->isBuiltin = $isBuiltin; | ||
} | ||
|
||
public function isMixed(): bool { | ||
return $this->isBuiltin && $this->name === "mixed"; | ||
} | ||
|
||
public function isScalar(): bool { | ||
return $this->isBuiltin && in_array($this->name, ["null", "false", "true", "bool", "int", "float"], true); | ||
} | ||
|
@@ -526,7 +530,7 @@ public function toOptimizerTypeMask(): string { | |
case "iterable": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (GitHub doesn't allow commenting on the above line for Does callable also need to include MAY_BE_ARRAY_OF_REF? If I use
Not sure what the other implications of this would be for the optimizer, @nikic would know more (E.g. something with a callable type hint might easily cease to be a callable?) |
||
return "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
case "mixed": | ||
return "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY"; | ||
return "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF"; | ||
} | ||
|
||
return $this->toTypeMask(); | ||
|
@@ -634,6 +638,16 @@ private function __construct(array $types, bool $isIntersection) { | |
$this->isIntersection = $isIntersection; | ||
} | ||
|
||
public function isMixed(): bool { | ||
foreach ($this->types as $type) { | ||
if ($type->isMixed()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function isScalar(): bool { | ||
foreach ($this->types as $type) { | ||
if (!$type->isScalar()) { | ||
|
@@ -1512,7 +1526,7 @@ public function getFunctionEntry(): string { | |
} | ||
|
||
public function getOptimizerInfo(): ?string { | ||
if ($this->isMethod()) { | ||
if ($this->isMethod() && !$this->isFinalMethod()) { | ||
return null; | ||
} | ||
|
||
|
@@ -1529,6 +1543,10 @@ public function getOptimizerInfo(): ?string { | |
return null; | ||
} | ||
|
||
if ($type->isMixed()) { | ||
return null; | ||
} | ||
|
||
return "\tF" . $this->return->refcount . '("' . addslashes($this->name->__toString()) . '", ' . $type->toOptimizerTypeMask() . "),\n"; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--TEST-- | ||
Test that internal methods can be optimized based on zend_func_infos.h | ||
--INI-- | ||
opcache.enable=1 | ||
opcache.enable_cli=1 | ||
opcache.optimization_level=-1 | ||
opcache.opt_debug_level=0x20000 | ||
opcache.preload= | ||
--EXTENSIONS-- | ||
opcache | ||
zend_test | ||
--FILE-- | ||
<?php | ||
|
||
class Test extends _ZendTestTypeInference { | ||
public function test1(): int { | ||
return $this->getIntArray()[0] ?? 0; | ||
} | ||
|
||
public static function test2(): int { | ||
return $this->createIntArray()[0] ?? 0; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
$_main: | ||
; (lines=2, args=0, vars=0, tmps=0) | ||
; (after optimizer) | ||
; %s | ||
0000 DECLARE_CLASS_DELAYED string("test") string("_zendtesttypeinference") | ||
0001 RETURN int(1) | ||
|
||
Test::test1: | ||
; (lines=7, args=0, vars=0, tmps=2) | ||
; (after optimizer) | ||
; %s | ||
0000 INIT_METHOD_CALL 0 THIS string("getIntArray") | ||
0001 V0 = DO_FCALL | ||
0002 T1 = FETCH_DIM_IS V0 int(0) | ||
0003 T0 = COALESCE T1 0005 | ||
0004 T0 = QM_ASSIGN int(0) | ||
0005 VERIFY_RETURN_TYPE T0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't these be optimized away then? What does this test test? |
||
0006 RETURN T0 | ||
LIVE RANGES: | ||
0: 0005 - 0006 (tmp/var) | ||
|
||
Test::test2: | ||
; (lines=8, args=0, vars=0, tmps=2) | ||
; (after optimizer) | ||
; %s | ||
0000 V0 = FETCH_THIS | ||
0001 INIT_METHOD_CALL 0 V0 string("createIntArray") | ||
0002 V0 = DO_FCALL | ||
0003 T1 = FETCH_DIM_IS V0 int(0) | ||
0004 T0 = COALESCE T1 0006 | ||
0005 T0 = QM_ASSIGN int(0) | ||
0006 VERIFY_RETURN_TYPE T0 | ||
0007 RETURN T0 | ||
LIVE RANGES: | ||
0: 0006 - 0007 (tmp/var) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ public function shuffleArray(array $array): array {} | |
|
||
public function shuffleBytes(string $bytes): string {} | ||
|
||
/** @return array<int, string> */ | ||
public function pickArrayKeys(array $array, int $num): array {} | ||
Comment on lines
+151
to
152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct, the returned array keys might be integers. The correct type would be |
||
|
||
public function __serialize(): array {} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.