Itroduce a reference ir_jit_compile() and use it in examples

This commit is contained in:
Dmitry Stogov 2023-05-23 10:52:59 +03:00
parent 9296ff3b75
commit 46fd86acb9
4 changed files with 61 additions and 24 deletions

View File

@ -31,18 +31,12 @@ int main(int argc, char **argv)
ir_consistency_check(); ir_consistency_check();
ir_init(&ctx, IR_FUNCTION, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN); ir_init(&ctx, IR_FUNCTION | IR_OPT_FOLDING, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN);
gen_myfunc(&ctx); gen_myfunc(&ctx);
ir_build_def_use_lists(&ctx);
ir_build_cfg(&ctx);
ir_match(&ctx);
ir_assign_virtual_registers(&ctx);
ir_compute_dessa_moves(&ctx);
size_t size; size_t size;
void *entry = ir_emit_code(&ctx, &size); void *entry = ir_jit_compile(&ctx, 2, &size);
if (entry) { if (entry) {
printf("42 - 24 = %d\n", ((myfunc_t)entry)(42, 24)); printf("42 - 24 = %d\n", ((myfunc_t)entry)(42, 24));
} }

View File

@ -41,18 +41,12 @@ int main(int argc, char **argv)
ir_consistency_check(); ir_consistency_check();
ir_init(&ctx, IR_FUNCTION, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN); ir_init(&ctx, IR_FUNCTION | IR_OPT_FOLDING, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN);
gen_myfunc(&ctx); gen_myfunc(&ctx);
ir_build_def_use_lists(&ctx);
ir_build_cfg(&ctx);
ir_match(&ctx);
ir_assign_virtual_registers(&ctx);
ir_compute_dessa_moves(&ctx);
size_t size; size_t size;
void *entry = ir_emit_code(&ctx, &size); void *entry = ir_jit_compile(&ctx, 2, &size);
if (entry) { if (entry) {
printf("%d\n", ((myfunc_t)entry)()); printf("%d\n", ((myfunc_t)entry)());
} }

View File

@ -47,18 +47,12 @@ int main(int argc, char **argv)
ir_consistency_check(); ir_consistency_check();
ir_init(&ctx, IR_FUNCTION, 256, 1024); ir_init(&ctx, IR_FUNCTION | IR_OPT_FOLDING, 256, 1024);
gen_myfunc(&ctx); gen_myfunc(&ctx);
ir_build_def_use_lists(&ctx);
ir_build_cfg(&ctx);
ir_match(&ctx);
ir_assign_virtual_registers(&ctx);
ir_compute_dessa_moves(&ctx);
size_t size; size_t size;
void *entry = ir_emit_code(&ctx, &size); void *entry = ir_jit_compile(&ctx, 2, &size);
if (entry) { if (entry) {
run(entry); run(entry);
} }

55
ir.h
View File

@ -757,6 +757,61 @@ uint32_t ir_cpuinfo(void);
/* Deoptimization helpers */ /* Deoptimization helpers */
const void *ir_emit_exitgroup(uint32_t first_exit_point, uint32_t exit_points_per_group, const void *exit_addr, void *code_buffer, size_t code_buffer_size, size_t *size_ptr); const void *ir_emit_exitgroup(uint32_t first_exit_point, uint32_t exit_points_per_group, const void *exit_addr, void *code_buffer, size_t code_buffer_size, size_t *size_ptr);
/* A reference IR JIT compiler */
IR_ALWAYS_INLINE void *ir_jit_compile(ir_ctx *ctx, int opt_level, size_t *size)
{
if (opt_level == 0) {
if (ctx->flags & IR_OPT_FOLDING) {
// IR_ASSERT(0 && "IR_OPT_FOLDING is incompatible with -O0");
return NULL;
}
ctx->flags &= ~(IR_OPT_CFG | IR_OPT_CODEGEN);
ir_build_def_use_lists(ctx);
if (!ir_build_cfg(ctx)
|| !ir_match(ctx)
|| !ir_assign_virtual_registers(ctx)
|| !ir_compute_dessa_moves(ctx)) {
return NULL;
}
return ir_emit_code(ctx, size);
} else if (opt_level == 1 || opt_level == 2) {
if (!(ctx->flags & IR_OPT_FOLDING)) {
// IR_ASSERT(0 && "IR_OPT_FOLDING must be set in ir_init() for -O1 and -O2");
return NULL;
}
ctx->flags |= IR_OPT_CFG | IR_OPT_CODEGEN;
ir_build_def_use_lists(ctx);
if (opt_level == 2
&& !ir_sccp(ctx)) {
return NULL;
}
if (!ir_build_cfg(ctx)
|| !ir_build_dominators_tree(ctx)
|| !ir_find_loops(ctx)
|| !ir_gcm(ctx)
|| !ir_schedule(ctx)
|| !ir_match(ctx)
|| !ir_assign_virtual_registers(ctx)
|| !ir_compute_live_ranges(ctx)
|| !ir_coalesce(ctx)
|| !ir_reg_alloc(ctx)
|| !ir_schedule_blocks(ctx)) {
return NULL;
}
return ir_emit_code(ctx, size);
} else {
// IR_ASSERT(0 && "wrong optimization level");
return NULL;
}
}
/* IR Memmory Allocation */ /* IR Memmory Allocation */
#ifndef ir_mem_malloc #ifndef ir_mem_malloc
# define ir_mem_malloc malloc # define ir_mem_malloc malloc