Skip to content

Commit

Permalink
aaaaaaaaaaaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed May 27, 2024
1 parent 1d830db commit 0db36f9
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 6 deletions.
8 changes: 7 additions & 1 deletion ir/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,16 @@ bool vx_IrBlock_is_volatile(const vx_IrBlock *block)

vx_IrType *vx_IrBlock_typeof_var(vx_IrBlock *block, vx_IrVar var) {
vx_IrOp *decl = vx_IrBlock_root_get_var_decl(vx_IrBlock_root(block), var);
if (decl == NULL)
goto warn;

for (size_t i = 0; i < decl->outs_len; i ++)
if (decl->outs[i].var == var)
return decl->outs[i].type;
assert(false);

warn:
fprintf(stderr, "VARIABLE %%%zu DECL ERROR (typeof_var)\n", var);
return NULL;
}

static size_t cost_lut[VX_IR_OP____END] = {
Expand Down
14 changes: 14 additions & 0 deletions ir/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,22 @@ void vx_IrOp_add_param_s(vx_IrOp *op,
vx_IrOp_add_param(op, vx_IrNamedValue_create(name, val));
}

void vx_IrOp_undeclare(vx_IrOp *op)
{
vx_IrBlock *root = (vx_IrBlock*) vx_IrBlock_root(op->parent);
if (root) {
for (size_t i = 0; i < op->outs_len; i ++) {
vx_IrVar var = op->outs[i].var;
if (var < root->as_root.vars_len && vx_IrBlock_root_get_var_decl(root, var) == op) {
root->as_root.vars[var].decl_parent = NULL;
}
}
}
}

void vx_IrOp_destroy(vx_IrOp *op)
{
// BEFORE CHANGING NOTE THAT MOST PASSES MISUSE THIS!!!
vx_IrOp_remove_params(op);
free(op->outs);
free(op->states);
Expand Down
2 changes: 1 addition & 1 deletion ir/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void vx_IrOp_dump(const vx_IrOp *op, FILE *out, size_t indent) {
if (j > 0)
fputc(',', out);
const vx_IrTypedVar var = op->outs[j];
fprintf(out, "%s %%%zu", var.type->debugName, var.var);
fprintf(out, "%s %%%zu", var.type ? var.type->debugName : "NULLTYPE", var.var);
}

if (op->outs_len > 0)
Expand Down
33 changes: 33 additions & 0 deletions ir/ir.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
#include "ir.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>


void vx_IrBlock_llir_fix_decl(vx_IrBlock *root) {
assert(root->is_root);

// TODO: WHY THE FUCK IS THIS EVEN REQUIRED????

memset(root->as_root.vars, 0, sizeof(*root->as_root.vars) * root->as_root.vars_len);

size_t total = 0;
for (size_t i = 0; i < root->ops_len; i ++) {
vx_IrOp *op = &root->ops[i];

for (size_t j = 0; j < op->outs_len; j ++) {
vx_IrTypedVar out = op->outs[j];
if (root->as_root.vars[out.var].decl_parent == NULL) {
vx_IrBlock_root_set_var_decl(root, out.var, op);
root->as_root.vars[out.var].ll_type = out.type;
total ++;
}
}
}
}

void vx_IrOp_warn(vx_IrOp *op, const char *optMsg0, const char *optMsg1) {
vx_IrBlock *parent = op ? op->parent : NULL;
if (parent) {
fprintf(stderr, "WARN: in op idx %zu: %s %s\n", parent->ops - op, optMsg0 ? optMsg0 : "", optMsg1 ? optMsg1 : "");
} else {
fprintf(stderr, "WARN: trying to warn() for non existant op!\n");
}
}

// TODO: add boolean to stop traverse
void vx_IrView_deep_traverse(vx_IrView top, void (*callback)(vx_IrOp *op, void *data), void *data) {
for (size_t i = top.start; i < top.end; i ++) {
Expand Down
9 changes: 8 additions & 1 deletion ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ typedef enum {
/** false for nop and label true for everything else */
bool vx_IrOpType_has_effect(vx_IrOpType type);

void vx_IrOp_undeclare(vx_IrOp *op);

#define SSAOPTYPE_LEN (VX_IR_OP____END - VX_IR_OP_NOP)

extern const char *vx_IrOpType_names[SSAOPTYPE_LEN];
Expand All @@ -335,6 +337,8 @@ struct vx_IrOp_s {
vx_IrOpType id;
};

void vx_IrOp_warn(vx_IrOp *op, const char *optMsg0, const char *optMsg1);

void vx_IrOp_dump(const vx_IrOp *op, FILE *out, size_t indent);

typedef struct {
Expand Down Expand Up @@ -409,7 +413,10 @@ static vx_IrOp *vx_IrBlock_root_get_var_decl(const vx_IrBlock *root, vx_IrVar va
vx_IrBlock *parent = root->as_root.vars[var].decl_parent;
if (parent == NULL)
return NULL;
return parent->ops + root->as_root.vars[var].decl_idx;
size_t idx = root->as_root.vars[var].decl_idx;
if (idx >= parent->ops_len)
return NULL;
return parent->ops + idx;
}

struct IrStaticIncrement {
Expand Down
12 changes: 12 additions & 0 deletions ir/lifetimes.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ void vx_IrBlock_lifetimes(vx_IrBlock *block) {
assert(block->is_root);

for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
vx_IrOp *decl = vx_IrBlock_root_get_var_decl(block, var);
if (decl == NULL) {
block->as_root.vars[var].ll_lifetime.first = NULL;
continue;
}

// llir shouldn't be nested

vx_IrOp *start = (void*) -1;
Expand All @@ -21,6 +27,12 @@ void vx_IrBlock_lifetimes(vx_IrBlock *block) {
}
}

if (start == (void*)-1)
start = decl;

if (end == 0)
end = decl;

lifetime lt;
lt.first = start;
lt.last = end;
Expand Down
10 changes: 9 additions & 1 deletion ir/llir.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

void vx_IrBlock_llir_lower(vx_IrBlock *block);

/** block needs to be 100% fat */
void vx_IrBlock_llir_fix_decl(vx_IrBlock *root);

/** block needs to be 100% flat */
void vx_IrBlock_lifetimes(vx_IrBlock *block);

// block needs to be 100% flat, decl of vars must be known, decl won't be known after this fn anymore; adds type info to vars
void vx_IrBlock_ll_share_slots(vx_IrBlock *block);

static void llir_prep_lower(vx_IrBlock *block) {
vx_IrBlock_llir_fix_decl(block);
vx_IrBlock_lifetimes(block);
vx_IrBlock_ll_share_slots(block);
}
20 changes: 18 additions & 2 deletions ir/transform/share_slots.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
// if start of lifetime A == end of lifetime B, we can still merge

// block needs to be 100% flat, decl of vars must be known, decl won't be known after this fn anymore

// TODO: figure out why it doesn't apply for overlapping*? lifetimes

void vx_IrBlock_ll_share_slots(vx_IrBlock *block) {
for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
if (block->as_root.vars[var].decl_parent == NULL)
continue;

// TODO: only search in lifetime: IrView of lifetime
vx_IrView view = vx_IrView_of_all(block);
bool get_rid = false;
while (vx_IrView_find(&view, VX_IR_OP_PLACE)) {
Expand All @@ -33,10 +40,11 @@ void vx_IrBlock_ll_share_slots(vx_IrBlock *block) {

for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
lifetime *lt = &block->as_root.vars[var].ll_lifetime;
vx_IrType *type = block->as_root.vars[var].ll_type;

if (lt->first == NULL)
continue;

vx_IrType *type = block->as_root.vars[var].ll_type;

for (vx_IrVar var2 = 0; var < block->as_root.vars_len; var ++) {
if (var == var2)
Expand All @@ -58,7 +66,15 @@ void vx_IrBlock_ll_share_slots(vx_IrBlock *block) {
cmp_high = *lt;
}

// TODO
if (cmp_low.last > cmp_high.first)
continue;

// it's okay if end0 = start1

vx_IrView view = vx_IrView_of_all(block);
vx_IrView_rename_var(view, block, var, var2);

break;
}
}
}
6 changes: 6 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ static int cir_test(void) {
printf("After LL IR opt:\n");
vx_IrBlock_dump(block, stdout, 0);

llir_prep_lower(block);

printf("After LL IR lower prepare:\n");
vx_IrBlock_dump(block, stdout, 0);

vx_IrBlock_destroy(block);

return 0;
}

int main(void) {
// TODO: figure out why these break things
vx_g_optconfig.loop_simplify = false;
vx_g_optconfig.if_eval = false;

Expand Down

0 comments on commit 0db36f9

Please sign in to comment.