Skip to content

Commit 5b3b03f

Browse files
committed
[Opcache] Add Opcache\Jit attribute example
1 parent 72aea5a commit 5b3b03f

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

ext/opcache/jit/zend_jit.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,20 +3135,8 @@ static int zend_jit_setup_hot_counters(zend_op_array *op_array)
31353135

31363136
static int zend_needs_manual_jit(const zend_op_array *op_array)
31373137
{
3138-
if (op_array->doc_comment) {
3139-
const char *s = ZSTR_VAL(op_array->doc_comment);
3140-
const char *p = strstr(s, "@jit");
3141-
3142-
if (p) {
3143-
size_t l = ZSTR_LEN(op_array->doc_comment);
3144-
3145-
if ((p == s + 3 || *(p-1) <= ' ') &&
3146-
(p + 6 == s + l || *(p+4) <= ' ')) {
3147-
return 1;
3148-
}
3149-
}
3150-
}
3151-
return 0;
3138+
return op_array->attributes &&
3139+
zend_hash_str_exists(op_array->attributes, "opcache\\jit", sizeof("opcache\\jit")-1);
31523140
}
31533141

31543142
ZEND_EXT_API int zend_jit_op_array(zend_op_array *op_array, zend_script *script)
@@ -3190,7 +3178,7 @@ ZEND_EXT_API int zend_jit_op_array(zend_op_array *op_array, zend_script *script)
31903178
return zend_jit_setup_hot_counters(op_array);
31913179
} else if (zend_jit_trigger == ZEND_JIT_ON_SCRIPT_LOAD) {
31923180
return zend_real_jit_func(op_array, script, NULL);
3193-
} else if (zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT) {
3181+
} else if (zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE) {
31943182
if (zend_needs_manual_jit(op_array)) {
31953183
return zend_real_jit_func(op_array, script, NULL);
31963184
} else {
@@ -3231,9 +3219,9 @@ ZEND_EXT_API int zend_jit_script(zend_script *script)
32313219
}
32323220
}
32333221
} else if (zend_jit_trigger == ZEND_JIT_ON_SCRIPT_LOAD ||
3234-
zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT) {
3222+
zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE) {
32353223

3236-
if (zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT) {
3224+
if (zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE) {
32373225
int do_jit = 0;
32383226
for (i = 0; i < call_graph.op_arrays_count; i++) {
32393227
if (zend_needs_manual_jit(call_graph.op_arrays[i])) {
@@ -3266,7 +3254,7 @@ ZEND_EXT_API int zend_jit_script(zend_script *script)
32663254
}
32673255

32683256
for (i = 0; i < call_graph.op_arrays_count; i++) {
3269-
if (zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT &&
3257+
if (zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE &&
32703258
!zend_needs_manual_jit(call_graph.op_arrays[i])) {
32713259
continue;
32723260
}
@@ -3281,7 +3269,7 @@ ZEND_EXT_API int zend_jit_script(zend_script *script)
32813269

32823270
if (ZCG(accel_directives).jit_debug & ZEND_JIT_DEBUG_SSA) {
32833271
for (i = 0; i < call_graph.op_arrays_count; i++) {
3284-
if (zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT &&
3272+
if (zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE &&
32853273
!zend_needs_manual_jit(call_graph.op_arrays[i])) {
32863274
continue;
32873275
}
@@ -3293,7 +3281,7 @@ ZEND_EXT_API int zend_jit_script(zend_script *script)
32933281
}
32943282

32953283
for (i = 0; i < call_graph.op_arrays_count; i++) {
3296-
if (zend_jit_trigger == ZEND_JIT_ON_DOC_COMMENT &&
3284+
if (zend_jit_trigger == ZEND_JIT_ON_ATTRIBUTE &&
32973285
!zend_needs_manual_jit(call_graph.op_arrays[i])) {
32983286
continue;
32993287
}
@@ -3428,9 +3416,25 @@ static int zend_jit_make_stubs(void)
34283416
return 1;
34293417
}
34303418

3419+
void zend_jit_validate_opcache_attribute(zval *attribute, int target)
3420+
{
3421+
if (target != ZEND_ATTRIBUTE_TARGET_METHOD && target != ZEND_ATTRIBUTE_TARGET_FUNCTION) {
3422+
zend_error(E_COMPILE_ERROR, "<<Opcache\\Jit>> attribute can only be declared on methods or functions.");
3423+
}
3424+
}
3425+
34313426
ZEND_EXT_API int zend_jit_startup(zend_long jit, void *buf, size_t size, zend_bool reattached)
34323427
{
34333428
int ret;
3429+
zend_class_entry ce;
3430+
zend_attributes_internal_validator cb;
3431+
3432+
INIT_NS_CLASS_ENTRY(ce, "Opcache\\Jit", NULL);
3433+
zend_ce_opcache_jit_attribute = zend_register_internal_class(&ce);
3434+
zend_ce_opcache_jit_attribute->ce_flags |= ZEND_ACC_FINAL;
3435+
3436+
cb = zend_jit_validate_opcache_attribute;
3437+
zend_compiler_attribute_register(zend_ce_opcache_jit_attribute, &cb);
34343438

34353439
zend_jit_level = ZEND_JIT_LEVEL(jit);
34363440
zend_jit_trigger = ZEND_JIT_TRIGGER(jit);

ext/opcache/jit/zend_jit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define ZEND_JIT_ON_FIRST_EXEC 1
3333
#define ZEND_JIT_ON_PROF_REQUEST 2 /* compile the most frequently caled on first request functions */
3434
#define ZEND_JIT_ON_HOT_COUNTERS 3 /* compile functions after N calls or loop iterations */
35-
#define ZEND_JIT_ON_DOC_COMMENT 4 /* compile functions with "@jit" tag in doc-comments */
35+
#define ZEND_JIT_ON_ATTRIBUTE 4 /* compile functions with "<<Opcache\Jit>>" attribute */
3636

3737
#define ZEND_JIT_TRIGGER(n) (((n) / 10) % 10)
3838

@@ -106,4 +106,6 @@ struct _zend_lifetime_interval {
106106
zend_lifetime_interval *list_next;
107107
};
108108

109+
zend_class_entry *zend_ce_opcache_jit_attribute;
110+
109111
#endif /* HAVE_JIT_H */

0 commit comments

Comments
 (0)