Skip to content

Commit c9853b6

Browse files
committed
JIT for FFI::sizeof() and FFI::alignof()
1 parent 3efb86c commit c9853b6

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

ext/opcache/jit/zend_jit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ void zend_jit_restart(void);
185185
#define ZREG_FFI_PTR_LOAD (1<<3)
186186
#define ZREG_FFI_ZVAL_DTOR (1<<4)
187187
#define ZREG_FFI_ZVAL_DEREF (1<<5)
188+
#define ZREG_FFI_CTYPE (1<<6)
188189

189190
#define ZREG_NONE -1
190191

ext/opcache/jit/zend_jit_ir_ffi.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ static ir_ref jit_FFI_CDATA_TYPE(zend_jit_ctx *jit, ir_ref obj_ref)
2626
return ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_ffi_cdata, type)));
2727
}
2828

29+
static ir_ref jit_FFI_CTYPE_TYPE(zend_jit_ctx *jit, ir_ref obj_ref)
30+
{
31+
return ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_ffi_ctype, type)));
32+
}
33+
2934
static int zend_jit_ffi_symbols_guard(zend_jit_ctx *jit,
3035
const zend_op *opline,
3136
zend_ssa *ssa,
@@ -119,6 +124,7 @@ static int zend_jit_ffi_send_val(zend_jit_ctx *jit,
119124
uint32_t op1_info,
120125
zend_jit_addr op1_addr,
121126
zend_jit_addr op1_def_addr,
127+
zend_class_entry *op1_ce,
122128
zend_ffi_type *op1_ffi_type)
123129
{
124130
zend_jit_trace_stack_frame *call = JIT_G(current_frame)->call;
@@ -130,8 +136,6 @@ static int zend_jit_ffi_send_val(zend_jit_ctx *jit,
130136

131137
if (TRACE_FRAME_FFI_FUNC(call)) {
132138
if (TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_ADDR
133-
|| TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_ALIGNOF
134-
|| TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_SIZEOF
135139
|| TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_TYPEOF
136140
|| TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_IS_NULL) {
137141
ZEND_ASSERT(opline->op2.num == 1);
@@ -151,6 +155,23 @@ static int zend_jit_ffi_send_val(zend_jit_ctx *jit,
151155
ref = jit_Z_PTR(jit, op1_addr);
152156
SET_STACK_TYPE(stack, 0, IS_OBJECT, 0);
153157
SET_STACK_REF_EX(stack, 0, ref, arg_flags);
158+
} else if (TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_ALIGNOF
159+
|| TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_SIZEOF) {
160+
ZEND_ASSERT(opline->op2.num == 1);
161+
162+
if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
163+
arg_flags |= ZREG_FFI_ZVAL_DTOR;
164+
}
165+
if (op1_info & MAY_BE_REF) {
166+
arg_flags |= ZREG_FFI_ZVAL_DEREF;
167+
}
168+
ZEND_ASSERT(op1_ffi_type || op1_ce == zend_ffi_api->ctype_ce);
169+
if (!op1_ffi_type && op1_ce == zend_ffi_api->ctype_ce) {
170+
arg_flags |= ZREG_FFI_CTYPE;
171+
}
172+
ref = jit_Z_PTR(jit, op1_addr);
173+
SET_STACK_TYPE(stack, 0, IS_OBJECT, 0);
174+
SET_STACK_REF_EX(stack, 0, ref, arg_flags);
154175
} else if (TRACE_FRAME_FFI_FUNC(call) == TRACE_FRAME_FFI_FUNC_STRING) {
155176
if (opline->op2.num == 1) {
156177
ZEND_ASSERT(op1_ffi_type);
@@ -564,7 +585,11 @@ static int zend_jit_ffi_do_call(zend_jit_ctx *jit,
564585
// TODO: try to remove this dereference ???
565586
ref = zend_jit_gc_deref(jit, ref);
566587
}
567-
ref = jit_FFI_CDATA_TYPE(jit, ref);
588+
if (STACK_FLAGS(stack, 0) & ZREG_FFI_CTYPE) {
589+
ref = jit_FFI_CTYPE_TYPE(jit, ref);
590+
} else {
591+
ref = jit_FFI_CDATA_TYPE(jit, ref);
592+
}
568593
// TODO: type flags ???
569594
ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_ffi_type, align)));
570595
if (sizeof(void*) == 8) {
@@ -578,7 +603,11 @@ static int zend_jit_ffi_do_call(zend_jit_ctx *jit,
578603
// TODO: try to remove this dereference ???
579604
ref = zend_jit_gc_deref(jit, ref);
580605
}
581-
ref = jit_FFI_CDATA_TYPE(jit, ref);
606+
if (STACK_FLAGS(stack, 0) & ZREG_FFI_CTYPE) {
607+
ref = jit_FFI_CTYPE_TYPE(jit, ref);
608+
} else {
609+
ref = jit_FFI_CDATA_TYPE(jit, ref);
610+
}
582611
// TODO: type flags ???
583612
ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_ffi_type, size)));
584613
if (sizeof(void*) == 8) {

ext/opcache/jit/zend_jit_trace.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5689,7 +5689,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
56895689
&& JIT_G(current_frame)->call
56905690
&& TRACE_FRAME_FFI(JIT_G(current_frame)->call)) {
56915691
if (!zend_jit_ffi_send_val(&ctx, opline, op_array, ssa, ssa_op,
5692-
op1_info, OP1_REG_ADDR(), 0, op1_ffi_type)) {
5692+
op1_info, OP1_REG_ADDR(), 0, op1_ce, op1_ffi_type)) {
56935693
goto jit_failure;
56945694
}
56955695
goto done;
@@ -5721,7 +5721,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
57215721
&& JIT_G(current_frame)->call
57225722
&& TRACE_FRAME_FFI(JIT_G(current_frame)->call)) {
57235723
if (!zend_jit_ffi_send_val(&ctx, opline, op_array, ssa, ssa_op,
5724-
op1_info, OP1_REG_ADDR(), 0, op1_ffi_type)) {
5724+
op1_info, OP1_REG_ADDR(), 0, op1_ce, op1_ffi_type)) {
57255725
goto jit_failure;
57265726
}
57275727
goto done;
@@ -5763,7 +5763,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
57635763
&& JIT_G(current_frame)->call
57645764
&& TRACE_FRAME_FFI(JIT_G(current_frame)->call)) {
57655765
if (!zend_jit_ffi_send_val(&ctx, opline, op_array, ssa, ssa_op,
5766-
op1_info, op1_addr, op1_def_addr, op1_ffi_type)) {
5766+
op1_info, op1_addr, op1_def_addr, op1_ce, op1_ffi_type)) {
57675767
goto jit_failure;
57685768
}
57695769
goto done;
@@ -7004,8 +7004,6 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
70047004
frame_ffi_func_type = NULL;
70057005
frame_ffi_func_ref = IR_UNUSED;
70067006
goto done;
7007-
#if 0
7008-
// TODO: add support for FFI::CType argument ???
70097007
} else if (Z_TYPE_P(zv) == IS_STRING
70107008
&& zend_string_equals_literal_ci(Z_STR_P(zv), "alignof")
70117009
&& opline->extended_value == 1) {
@@ -7020,7 +7018,6 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
70207018
frame_ffi_func_type = NULL;
70217019
frame_ffi_func_ref = IR_UNUSED;
70227020
goto done;
7023-
#endif
70247021
} else if (Z_TYPE_P(zv) == IS_STRING
70257022
&& zend_string_equals_literal_ci(Z_STR_P(zv), "typeof")
70267023
&& opline->extended_value == 1) {

0 commit comments

Comments
 (0)