Skip to content
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

Add optimizer support for prototype methods #7452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kocsismate
Copy link
Member

@kocsismate kocsismate commented Sep 2, 2021

No description provided.

@kocsismate kocsismate force-pushed the optimizer-method branch 2 times, most recently from b390a1a to c7e5496 Compare September 2, 2021 15:22
@kocsismate kocsismate changed the title Add optimizer support for methods Add optimizer support for prototype methods Sep 2, 2021
@kocsismate kocsismate force-pushed the optimizer-method branch 2 times, most recently from c483aec to 610c6ad Compare September 2, 2021 16:47
@@ -387,7 +391,7 @@ public function toOptimizerTypeMask(): string {
case "iterable":
return "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think MAY_BE_ARRAY_OF_REF would also make sense here?

@@ -387,7 +391,7 @@ public function toOptimizerTypeMask(): string {
case "iterable":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(GitHub doesn't allow commenting on the above line for case "callable":)

Does callable also need to include MAY_BE_ARRAY_OF_REF? If I use callable as a parameter type hint, I can still modify fields of an array callable by reference (and see them get modified)

php > class B { public function main() { echo "in main\n"; } }
php > $class = new B();
php > $method = 'main';
php > $callable = [&$class, &$method];
php > $callable();
in main
php > function example(callable $c) { $c(); $c[1] = 'changed'; }
php > example($callable);
in main
php > var_dump($callable);
array(2) {
  [0]=>
  &object(B)#1 (0) {
  }
  [1]=>
  &string(7) "changed"
}
php > 

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?)

zend_string *name = callee_func->common.function_name;
if (!name) {
zend_string *name = get_function_or_method_name(callee_func);
if (zend_string_equals_literal_ci(name, "main")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't main already a valid name for a internal function or an instance method <?php function main() {}? "(main)" would at least avoid conflicts, though I'm not sure where else get_function_or_method_name is used

Is there a reason

Zend/Optimizer/zend_func_info.c Show resolved Hide resolved
@kocsismate kocsismate changed the base branch from master to PHP-8.1 September 5, 2021 18:53
@kocsismate kocsismate force-pushed the optimizer-method branch 2 times, most recently from c9f8b3b to 71e152d Compare September 6, 2021 15:21
@kocsismate kocsismate changed the base branch from PHP-8.1 to master March 7, 2024 07:23
Comment on lines +151 to 152
/** @return array<int, string> */
public function pickArrayKeys(array $array, int $num): array {}
Copy link
Member

Choose a reason for hiding this comment

The 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 list<int|string>.

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be callee_func->common.scope && call_info->is_prototype? is_prototype indicates we might be calling an overridden method.

@@ -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:
Copy link
Member

@iluuu1994 iluuu1994 Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. ZEND_INIT_METHOD_CALL does not define a result. Methods are called using DO_FCALL, so this is already handled.

0002 T1 = FETCH_DIM_IS V0 int(0)
0003 T0 = COALESCE T1 0005
0004 T0 = QM_ASSIGN int(0)
0005 VERIFY_RETURN_TYPE T0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be optimized away then? What does this test test?

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects cannot be IS_CONST. I don't think this branch is hit. You should look at the inferred SSA variable instead.

Zend/Optimizer/zend_optimizer.c Show resolved Hide resolved
/* 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 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: && on next line.

&& !(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(
&op_array->scope->function_table, method_name);
Copy link
Member

@iluuu1994 iluuu1994 Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class here should be based on op1 though, not op_array->scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants