-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
267 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "opt.h" | ||
#include "ir.h" | ||
|
||
vx_OptConfig vx_g_optconfig = { | ||
.max_total_cmov_inline_cost = 4, | ||
.consteval_iterations = 6, | ||
.loop_simplify = true, | ||
.if_eval = true, | ||
}; | ||
|
||
static void opt_pre(vx_IrBlock *block) { | ||
// TODO: re-enable | ||
(void) block; | ||
/* | ||
// place immediates into params | ||
vx_opt_inline_vars(vx_IrView_of_all(block), block); | ||
for (size_t i = 0; i < vx_g_optconfig.consteval_iterations; i ++) { | ||
// evaluate constants | ||
vx_opt_constant_eval(vx_IrView_of_all(block), block); | ||
// place results into params | ||
vx_opt_inline_vars(vx_IrView_of_all(block), block); | ||
} | ||
if (block->is_root) | ||
vx_opt_remove_vars(block); | ||
vx_opt_comparisions(vx_IrView_of_all(block), block); | ||
*/ | ||
} | ||
|
||
void opt(vx_IrBlock *block) { | ||
// TODO: remove opt() calls from opt pass bodies | ||
|
||
for (size_t i = 0; i < block->ops_len; i ++) { | ||
vx_IrOp *op = &block->ops[i]; | ||
|
||
for (size_t i = 0; i < op->params_len; i ++) | ||
if (op->params[i].val.type == VX_IR_VAL_BLOCK) | ||
opt(op->params[i].val.block); | ||
} | ||
|
||
opt_pre(block); | ||
vx_opt_join_compute(vx_IrView_of_all(block), block); | ||
if (vx_g_optconfig.if_eval) { | ||
vx_opt_reduce_if(vx_IrView_of_all(block), block); | ||
} | ||
vx_opt_cmov(vx_IrView_of_all(block), block); | ||
opt_pre(block); | ||
if (vx_g_optconfig.loop_simplify) { | ||
vx_opt_reduce_loops(vx_IrView_of_all(block), block); | ||
vx_opt_loop_simplify(vx_IrView_of_all(block), block); | ||
} | ||
|
||
for (size_t i = 0; i < block->ops_len; i ++) { | ||
vx_IrOp *op = &block->ops[i]; | ||
|
||
if (op->id == VX_IR_OP_FLATTEN_PLEASE) { | ||
vx_IrBlock *body = vx_IrOp_param(op, VX_IR_NAME_BLOCK)->block; | ||
opt(body); | ||
} | ||
} | ||
} | ||
|
||
void opt_ll(vx_IrBlock *block) { | ||
opt_pre(block); | ||
vx_opt_ll_jumps(vx_IrView_of_all(block), block); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "../opt.h" | ||
|
||
static vx_IrVar block_res_as_var(vx_IrBlock *parent, vx_IrBlock *block) { | ||
vx_IrOp *op = vx_IrBlock_add_op_building(parent); | ||
vx_IrOp_init(op, VX_IR_OP_FLATTEN_PLEASE, parent); | ||
vx_IrVar dest = vx_IrBlock_new_var(parent, op); | ||
vx_IrOp_add_param_s(op, VX_IR_NAME_BLOCK, (vx_IrValue) { .type = VX_IR_VAL_BLOCK, .block = block }); | ||
vx_IrOp_add_out(op, dest, vx_IrBlock_typeof_var(block, block->outs[0])); | ||
return dest; | ||
} | ||
|
||
// convert: | ||
// DEST = if cond=COND, then={ | ||
// ^ THEN: non volatile | ||
// }, else={ | ||
// ^ ELSE: non volatile | ||
// } | ||
// | ||
// to: | ||
// V0 < THEN | ||
// V1 < ELSE | ||
// DEST = cmov cond=COND, then=V0, else=V1 | ||
// | ||
// if: | ||
// cost of then + else is small enough | ||
// exactly one out | ||
|
||
void vx_opt_cmov(vx_IrView view, vx_IrBlock *block) | ||
{ | ||
for (size_t i = view.start; i < view.end; i ++) { | ||
vx_IrOp *op = &block->ops[i]; | ||
|
||
if (op->id != VX_IR_OP_IF) | ||
continue; | ||
|
||
if (op->outs_len != 1) | ||
continue; | ||
|
||
vx_IrValue *pthen = vx_IrOp_param(op, VX_IR_NAME_COND_THEN); | ||
if (!pthen) | ||
continue; | ||
|
||
vx_IrValue *pelse = vx_IrOp_param(op, VX_IR_NAME_COND_ELSE); | ||
if (!pelse) | ||
continue; | ||
|
||
vx_IrBlock *then = pthen->block; | ||
if (vx_IrBlock_is_volatile(then)) | ||
continue; | ||
|
||
vx_IrBlock *els = pelse->block; | ||
if (vx_IrBlock_is_volatile(els)) | ||
continue; | ||
|
||
size_t total_cost = vx_IrBlock_inline_cost(then) + vx_IrBlock_inline_cost(els); | ||
if (total_cost > vx_g_optconfig.max_total_cmov_inline_cost) | ||
continue; | ||
|
||
vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block; | ||
|
||
|
||
vx_IrBlock *body = vx_IrBlock_init_heap(block, i); | ||
|
||
vx_IrVar v0 = block_res_as_var(body, then); | ||
vx_IrVar v1 = block_res_as_var(body, els); | ||
|
||
vx_IrOp *cmov = vx_IrBlock_add_op_building(body); | ||
vx_IrOp_init(cmov, VX_IR_OP_CMOV, body); | ||
vx_IrVar dest = vx_IrBlock_new_var(body, cmov); | ||
vx_IrOp_add_param_s(cmov, VX_IR_NAME_COND, (vx_IrValue) { .type = VX_IR_VAL_BLOCK, .block = cond }); | ||
vx_IrOp_add_param_s(cmov, VX_IR_NAME_COND_THEN, (vx_IrValue) { .type = VX_IR_VAL_VAR, .var = v0 }); | ||
vx_IrOp_add_param_s(cmov, VX_IR_NAME_COND_ELSE, (vx_IrValue) { .type = VX_IR_VAL_VAR, .var = v1 }); | ||
vx_IrOp_add_out(cmov, dest, vx_IrBlock_typeof_var(then, then->outs[0])); | ||
|
||
vx_IrBlock_add_out(body, dest); | ||
|
||
|
||
vx_IrTypedVar out = op->outs[0]; | ||
|
||
free(op->params); | ||
op->params = NULL; | ||
op->params_len = 0; | ||
vx_IrOp_destroy(op); | ||
vx_IrOp_init(op, VX_IR_OP_FLATTEN_PLEASE, block); | ||
|
||
vx_IrOp_add_param_s(op, VX_IR_NAME_BLOCK, (vx_IrValue) { .type = VX_IR_VAL_BLOCK, .block = body }); | ||
vx_IrOp_add_out(op, out.var, out.type); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.