Skip to content

Commit 193a03d

Browse files
committed
Update IR
IR commit: d60d92516dc5f89b93cdf1df7a54141e83226b07
1 parent 487b22f commit 193a03d

File tree

10 files changed

+161
-144
lines changed

10 files changed

+161
-144
lines changed

ext/opcache/jit/ir/ir.c

Lines changed: 38 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
144144
if (isnan(insn->val.f)) {
145145
fprintf(f, "nan");
146146
} else {
147-
fprintf(f, "%f", insn->val.f);
147+
fprintf(f, "%g", insn->val.f);
148148
}
149149
break;
150150
default:
@@ -301,58 +301,17 @@ void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limi
301301
IR_ASSERT(consts_limit >= IR_CONSTS_LIMIT_MIN);
302302
IR_ASSERT(insns_limit >= IR_INSNS_LIMIT_MIN);
303303

304+
memset(ctx, 0, sizeof(ir_ctx));
305+
304306
ctx->insns_count = IR_UNUSED + 1;
305307
ctx->insns_limit = insns_limit;
306308
ctx->consts_count = -(IR_TRUE - 1);
307309
ctx->consts_limit = consts_limit;
308310
ctx->fold_cse_limit = IR_UNUSED + 1;
309311
ctx->flags = flags;
310-
ctx->mflags = 0;
311-
ctx->status = 0;
312-
313-
ctx->binding = NULL;
314-
315-
ctx->use_lists = NULL;
316-
ctx->use_edges = NULL;
317-
ctx->use_edges_count = 0;
318-
319-
ctx->cfg_blocks_count = 0;
320-
ctx->cfg_edges_count = 0;
321-
ctx->cfg_blocks = NULL;
322-
ctx->cfg_edges = NULL;
323-
ctx->cfg_map = NULL;
324-
ctx->rules = NULL;
325-
ctx->vregs = NULL;
326-
ctx->vregs_count = 0;
312+
327313
ctx->spill_base = -1;
328-
ctx->fixed_stack_red_zone = 0;
329314
ctx->fixed_stack_frame_size = -1;
330-
ctx->fixed_call_stack_size = 0;
331-
ctx->fixed_regset = 0;
332-
ctx->fixed_save_regset = 0;
333-
ctx->live_intervals = NULL;
334-
ctx->arena = NULL;
335-
ctx->unused_ranges = NULL;
336-
ctx->regs = NULL;
337-
ctx->prev_ref = NULL;
338-
ctx->data = NULL;
339-
ctx->snapshot_create = NULL;
340-
ctx->entries_count = 0;
341-
ctx->entries = NULL;
342-
ctx->osr_entry_loads = NULL;
343-
344-
ctx->code_buffer = NULL;
345-
ctx->code_buffer_size = 0;
346-
347-
#if defined(IR_TARGET_AARCH64)
348-
ctx->deoptimization_exits = 0;
349-
ctx->veneers_size = 0;
350-
ctx->get_exit_addr = NULL;
351-
ctx->get_veneer = NULL;
352-
ctx->set_veneer = NULL;
353-
#endif
354-
355-
ctx->strtab.data = NULL;
356315

357316
buf = ir_mem_malloc((consts_limit + insns_limit) * sizeof(ir_insn));
358317
ctx->ir_base = buf + consts_limit;
@@ -364,9 +323,6 @@ void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limi
364323
ctx->ir_base[IR_FALSE].val.u64 = 0;
365324
ctx->ir_base[IR_TRUE].optx = IR_OPT(IR_C_BOOL, IR_BOOL);
366325
ctx->ir_base[IR_TRUE].val.u64 = 1;
367-
368-
memset(ctx->prev_insn_chain, 0, sizeof(ctx->prev_insn_chain));
369-
memset(ctx->prev_const_chain, 0, sizeof(ctx->prev_const_chain));
370326
}
371327

372328
void ir_free(ir_ctx *ctx)
@@ -1991,13 +1947,21 @@ void _ir_UNREACHABLE(ir_ctx *ctx)
19911947
void _ir_TAILCALL(ir_ctx *ctx, ir_type type, ir_ref func)
19921948
{
19931949
IR_ASSERT(ctx->control);
1950+
if (ctx->ret_type == (ir_type)-1) {
1951+
ctx->ret_type = type;
1952+
}
1953+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
19941954
ctx->control = ir_emit2(ctx, IR_OPTX(IR_TAILCALL, type, 2), ctx->control, func);
19951955
_ir_UNREACHABLE(ctx);
19961956
}
19971957

19981958
void _ir_TAILCALL_1(ir_ctx *ctx, ir_type type, ir_ref func, ir_ref arg1)
19991959
{
20001960
IR_ASSERT(ctx->control);
1961+
if (ctx->ret_type == (ir_type)-1) {
1962+
ctx->ret_type = type;
1963+
}
1964+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20011965
ctx->control = ir_emit3(ctx, IR_OPTX(IR_TAILCALL, type, 3), ctx->control, func, arg1);
20021966
_ir_UNREACHABLE(ctx);
20031967
}
@@ -2007,6 +1971,10 @@ void _ir_TAILCALL_2(ir_ctx *ctx, ir_type type, ir_ref func, ir_ref arg1, ir_ref
20071971
ir_ref call;
20081972

20091973
IR_ASSERT(ctx->control);
1974+
if (ctx->ret_type == (ir_type)-1) {
1975+
ctx->ret_type = type;
1976+
}
1977+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20101978
call = ir_emit_N(ctx, IR_OPT(IR_TAILCALL, type), 4);
20111979
ir_set_op(ctx, call, 1, ctx->control);
20121980
ir_set_op(ctx, call, 2, func);
@@ -2021,6 +1989,10 @@ void _ir_TAILCALL_3(ir_ctx *ctx, ir_type type, ir_ref func, ir_ref arg1, ir_ref
20211989
ir_ref call;
20221990

20231991
IR_ASSERT(ctx->control);
1992+
if (ctx->ret_type == (ir_type)-1) {
1993+
ctx->ret_type = type;
1994+
}
1995+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20241996
call = ir_emit_N(ctx, IR_OPT(IR_TAILCALL, type), 5);
20251997
ir_set_op(ctx, call, 1, ctx->control);
20261998
ir_set_op(ctx, call, 2, func);
@@ -2036,6 +2008,10 @@ void _ir_TAILCALL_4(ir_ctx *ctx, ir_type type, ir_ref func, ir_ref arg1, ir_ref
20362008
ir_ref call;
20372009

20382010
IR_ASSERT(ctx->control);
2011+
if (ctx->ret_type == (ir_type)-1) {
2012+
ctx->ret_type = type;
2013+
}
2014+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20392015
call = ir_emit_N(ctx, IR_OPT(IR_TAILCALL, type), 6);
20402016
ir_set_op(ctx, call, 1, ctx->control);
20412017
ir_set_op(ctx, call, 2, func);
@@ -2052,6 +2028,10 @@ void _ir_TAILCALL_5(ir_ctx *ctx, ir_type type, ir_ref func, ir_ref arg1, ir_ref
20522028
ir_ref call;
20532029

20542030
IR_ASSERT(ctx->control);
2031+
if (ctx->ret_type == (ir_type)-1) {
2032+
ctx->ret_type = type;
2033+
}
2034+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20552035
call = ir_emit_N(ctx, IR_OPT(IR_TAILCALL, type), 7);
20562036
ir_set_op(ctx, call, 1, ctx->control);
20572037
ir_set_op(ctx, call, 2, func);
@@ -2070,6 +2050,10 @@ void _ir_TAILCALL_N(ir_ctx *ctx, ir_type type, ir_ref func, uint32_t count, ir_r
20702050
uint32_t i;
20712051

20722052
IR_ASSERT(ctx->control);
2053+
if (ctx->ret_type == (ir_type)-1) {
2054+
ctx->ret_type = type;
2055+
}
2056+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
20732057
call = ir_emit_N(ctx, IR_OPT(IR_TAILCALL, type), count + 2);
20742058
ir_set_op(ctx, call, 1, ctx->control);
20752059
ir_set_op(ctx, call, 2, func);
@@ -2104,7 +2088,13 @@ void _ir_CASE_DEFAULT(ir_ctx *ctx, ir_ref switch_ref)
21042088

21052089
void _ir_RETURN(ir_ctx *ctx, ir_ref val)
21062090
{
2091+
ir_type type = (val != IR_UNUSED) ? ctx->ir_base[val].type : IR_VOID;
2092+
21072093
IR_ASSERT(ctx->control);
2094+
if (ctx->ret_type == (ir_type)-1) {
2095+
ctx->ret_type = type;
2096+
}
2097+
IR_ASSERT(ctx->ret_type == type && "conflicting return type");
21082098
ctx->control = ir_emit3(ctx, IR_RETURN, ctx->control, val, ctx->ir_base[1].op1);
21092099
ctx->ir_base[1].op1 = ctx->control;
21102100
ctx->control = IR_UNUSED;
@@ -2330,52 +2320,3 @@ void _ir_STORE(ir_ctx *ctx, ir_ref addr, ir_ref val)
23302320
}
23312321
ctx->control = ir_emit3(ctx, IR_STORE, ctx->control, addr, val);
23322322
}
2333-
2334-
ir_type ir_get_return_type(ir_ctx *ctx)
2335-
{
2336-
ir_ref ref;
2337-
ir_insn *insn;
2338-
uint8_t ret_type = 255;
2339-
ir_type type;
2340-
2341-
/* Check all RETURN nodes */
2342-
ref = ctx->ir_base[1].op1;
2343-
while (ref) {
2344-
insn = &ctx->ir_base[ref];
2345-
if (insn->op == IR_RETURN) {
2346-
type = ctx->ir_base[insn->op2].type;
2347-
check_type:
2348-
if (ret_type == 255) {
2349-
if (insn->op2) {
2350-
ret_type = type;
2351-
} else {
2352-
ret_type = IR_VOID;
2353-
}
2354-
} else if (insn->op2) {
2355-
if (ret_type != type) {
2356-
IR_ASSERT(0 && "conflicting return types");
2357-
return IR_VOID;
2358-
}
2359-
} else {
2360-
if (ret_type != IR_VOID) {
2361-
IR_ASSERT(0 && "conflicting return types");
2362-
return IR_VOID;
2363-
}
2364-
}
2365-
} else if (insn->op == IR_UNREACHABLE) {
2366-
insn = &ctx->ir_base[insn->op1];
2367-
if (insn->op == IR_TAILCALL) {
2368-
type = insn->type;
2369-
if (type != IR_VOID) {
2370-
goto check_type;
2371-
}
2372-
}
2373-
}
2374-
ref = ctx->ir_base[ref].op3;
2375-
}
2376-
2377-
if (ret_type == 255) {
2378-
ret_type = IR_VOID;
2379-
}
2380-
return (ir_type)ret_type;
2381-
}

ext/opcache/jit/ir/ir.h

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -465,27 +465,32 @@ void ir_strtab_free(ir_strtab *strtab);
465465
/* IR Context Flags */
466466
#define IR_FUNCTION (1<<0) /* Generate a function. */
467467
#define IR_FASTCALL_FUNC (1<<1) /* Generate a function with fastcall calling convention, x86 32-bit only. */
468-
#define IR_SKIP_PROLOGUE (1<<2) /* Don't generate function prologue. */
469-
#define IR_USE_FRAME_POINTER (1<<3)
470-
#define IR_PREALLOCATED_STACK (1<<4)
471-
#define IR_HAS_ALLOCA (1<<5)
472-
#define IR_HAS_CALLS (1<<6)
473-
#define IR_NO_STACK_COMBINE (1<<7)
474-
#define IR_START_BR_TARGET (1<<8)
475-
#define IR_ENTRY_BR_TARGET (1<<9)
476-
#define IR_GEN_ENDBR (1<<10)
477-
#define IR_MERGE_EMPTY_ENTRIES (1<<11)
478-
479-
#define IR_CFG_HAS_LOOPS (1<<14)
480-
#define IR_IRREDUCIBLE_CFG (1<<15)
481-
482-
#define IR_OPT_FOLDING (1<<16)
483-
#define IR_OPT_CFG (1<<17) /* merge BBs, by remove END->BEGIN nodes during CFG construction */
484-
#define IR_OPT_CODEGEN (1<<18)
485-
#define IR_OPT_IN_SCCP (1<<19)
486-
#define IR_LINEAR (1<<20)
487-
#define IR_GEN_NATIVE (1<<21)
488-
#define IR_GEN_CODE (1<<22) /* C or LLVM */
468+
#define IR_VARARG_FUNC (1<<2)
469+
#define IR_STATIC (1<<3)
470+
#define IR_EXTERN (1<<4)
471+
#define IR_CONST (1<<5)
472+
473+
#define IR_SKIP_PROLOGUE (1<<6) /* Don't generate function prologue. */
474+
#define IR_USE_FRAME_POINTER (1<<7)
475+
#define IR_PREALLOCATED_STACK (1<<8)
476+
#define IR_HAS_ALLOCA (1<<9)
477+
#define IR_HAS_CALLS (1<<10)
478+
#define IR_NO_STACK_COMBINE (1<<11)
479+
#define IR_START_BR_TARGET (1<<12)
480+
#define IR_ENTRY_BR_TARGET (1<<13)
481+
#define IR_GEN_ENDBR (1<<14)
482+
#define IR_MERGE_EMPTY_ENTRIES (1<<15)
483+
484+
#define IR_CFG_HAS_LOOPS (1<<16)
485+
#define IR_IRREDUCIBLE_CFG (1<<17)
486+
487+
#define IR_OPT_FOLDING (1<<18)
488+
#define IR_OPT_CFG (1<<19) /* merge BBs, by remove END->BEGIN nodes during CFG construction */
489+
#define IR_OPT_CODEGEN (1<<20)
490+
#define IR_OPT_IN_SCCP (1<<21)
491+
#define IR_LINEAR (1<<22)
492+
#define IR_GEN_NATIVE (1<<23)
493+
#define IR_GEN_CODE (1<<24) /* C or LLVM */
489494

490495
/* Temporary: SCCP -> CFG */
491496
#define IR_SCCP_DONE (1<<25)
@@ -514,6 +519,7 @@ typedef struct _ir_block ir_block;
514519
typedef struct _ir_arena ir_arena;
515520
typedef struct _ir_live_interval ir_live_interval;
516521
typedef struct _ir_live_range ir_live_range;
522+
typedef struct _ir_loader ir_loader;
517523
typedef int8_t ir_regs[4];
518524

519525
typedef void (*ir_snapshot_create_t)(ir_ctx *ctx, ir_ref addr);
@@ -531,6 +537,7 @@ struct _ir_ctx {
531537
ir_ref consts_count; /* number of constants stored in constants buffer */
532538
ir_ref consts_limit; /* size of allocated constants buffer (it's extended when overflow) */
533539
uint32_t flags; /* IR context flags (see IR_* defines above) */
540+
ir_type ret_type; /* Function return type */
534541
uint32_t mflags; /* CPU specific flags (see IR_X86_... macros below) */
535542
int32_t status; /* non-zero error code (see IR_ERROR_... macros), app may use negative codes */
536543
ir_ref fold_cse_limit; /* CSE finds identical insns backward from "insn_count" to "fold_cse_limit" */
@@ -588,6 +595,7 @@ struct _ir_ctx {
588595
ir_get_veneer_t get_veneer;
589596
ir_set_veneer_t set_veneer;
590597
#endif
598+
ir_loader *loader;
591599
ir_strtab strtab;
592600
ir_ref prev_insn_chain[IR_LAST_FOLDABLE_OP + 1];
593601
ir_ref prev_const_chain[IR_LAST_TYPE];
@@ -748,23 +756,25 @@ void ir_gdb_unregister_all(void);
748756
bool ir_gdb_present(void);
749757

750758
/* IR load API (implementation in ir_load.c) */
751-
752-
typedef struct _ir_loader ir_loader;
753-
754759
struct _ir_loader {
755760
uint32_t default_func_flags;
756761
bool (*init_module) (ir_loader *loader, const char *name, const char *filename, const char *target);
757-
bool (*external_sym_dcl) (ir_loader *loader, const char *name, bool is_const);
758-
bool (*sym_dcl) (ir_loader *loader, const char *name, bool is_const, bool is_static, size_t size, const void *data);
759-
bool (*external_func_dcl) (ir_loader *loader, const char *name);
760-
bool (*forward_func_dcl) (ir_loader *loader, const char *name, bool is_static);
761-
bool (*init_func) (ir_loader *loader, ir_ctx *ctx, const char *name);
762-
bool (*process_func) (ir_loader *loader, ir_ctx *ctx, const char *name);
762+
bool (*external_sym_dcl) (ir_loader *loader, const char *name, uint32_t flags);
763+
bool (*external_func_dcl) (ir_loader *loader, const char *name,
764+
uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types);
765+
bool (*forward_func_dcl) (ir_loader *loader, const char *name,
766+
uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types);
767+
bool (*sym_dcl) (ir_loader *loader, const char *name, uint32_t flags, size_t size, bool has_data);
768+
bool (*sym_data) (ir_loader *loader, ir_type type, uint32_t count, const void *data);
769+
bool (*sym_data_end) (ir_loader *loader);
770+
bool (*func_init) (ir_loader *loader, ir_ctx *ctx, const char *name);
771+
bool (*func_process) (ir_loader *loader, ir_ctx *ctx, const char *name);
772+
void*(*resolve_sym_name) (ir_loader *loader, const char *name);
763773
};
764774

765775
void ir_loader_init(void);
766776
void ir_loader_free(void);
767-
int ir_load(ir_ctx *ctx, FILE *f);
777+
int ir_load(ir_loader *loader, FILE *f);
768778

769779
/* IR LLVM load API (implementation in ir_load_llvm.c) */
770780
int ir_load_llvm_bitcode(ir_loader *loader, const char *filename);
@@ -784,9 +794,11 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f);
784794

785795
/* IR to C conversion (implementation in ir_emit_c.c) */
786796
int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f);
797+
void ir_emit_c_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types, FILE *f);
787798

788799
/* IR to LLVM conversion (implementation in ir_emit_llvm.c) */
789800
int ir_emit_llvm(ir_ctx *ctx, const char *name, FILE *f);
801+
void ir_emit_llvm_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types, FILE *f);
790802

791803
/* IR verification API (implementation in ir_check.c) */
792804
bool ir_check(const ir_ctx *ctx);

0 commit comments

Comments
 (0)