Skip to content

Commit

Permalink
#77 simple optimization phase to replace upvalues that reference lite…
Browse files Browse the repository at this point in the history
…rals with those literals
  • Loading branch information
dibyendumajumdar committed Mar 19, 2022
1 parent 81cb5d6 commit c24e1eb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/ravi_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ RAVICOMP_EXPORT LinearizerState *raviX_init_linearizer(CompilerState *compiler_s
* Returns 0 on success.
*/
RAVICOMP_EXPORT int raviX_ast_linearize(LinearizerState *linearizer);

/*
* Attempts to replace use of upvalues with constants where the upvalue
* points to a local that was initialized with a literal and the local
* has no other assignments.
*/
RAVICOMP_EXPORT void raviX_optimize_upvalues(LinearizerState *linearizer);

/* Prints out the content of the linear IR */
RAVICOMP_EXPORT void raviX_output_linearizer(LinearizerState *linearizer, FILE *fp);
/* Cleanup the linearizer */
Expand Down
4 changes: 4 additions & 0 deletions src/linearizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3001,6 +3001,10 @@ static void replace_literal_upvalues(Proc *proc)
END_FOR_EACH_PTR(childproc)
}

void raviX_optimize_upvalues(LinearizerState *linearizer) {
replace_literal_upvalues(linearizer->main_proc);
}

////////////// end of optimization of upvalues

static const char *op_codenames[] = {
Expand Down
1 change: 1 addition & 0 deletions src/ravi_binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int raviX_compile(struct Ravi_CompilerInterface *compiler_interface)
}
raviX_construct_cfg(linearizer->main_proc);
raviX_remove_unreachable_blocks(linearizer);
raviX_optimize_upvalues(linearizer);

TextBuffer buf;
raviX_buffer_init(&buf, 4096);
Expand Down
3 changes: 3 additions & 0 deletions tests/tcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void parse_arguments(struct arguments *args, int argc, const char *argv[])
args->simplify_ast = 0;
args->remove_unreachable_blocks = 0;
args->gen_C = 0;
args->opt_upvalue = 0;
args->mainfunc = "setup";
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--notypecheck") == 0) {
Expand All @@ -60,6 +61,8 @@ void parse_arguments(struct arguments *args, int argc, const char *argv[])
args->remove_unreachable_blocks = 1;
} else if (strcmp(argv[i], "--gen-C") == 0) {
args->gen_C = 1;
} else if (strcmp(argv[i], "--opt-upvalues") == 0) {
args->opt_upvalue = 1;
} else if (strcmp(argv[i], "-main") == 0) {
if (i < argc - 1) {
i++;
Expand Down
2 changes: 1 addition & 1 deletion tests/tcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct arguments {
const char *filename;
const char *code;
unsigned typecheck : 1, linearize : 1, astdump : 1, irdump : 1, cfgdump : 1, codump : 1, simplify_ast : 1,
remove_unreachable_blocks: 1, gen_C: 1;
remove_unreachable_blocks: 1, gen_C: 1, opt_upvalue: 1;
const char *mainfunc; /* name of the main function in generated code, only applies if gen_C is on */
};
extern void parse_arguments(struct arguments *args, int argc, const char *argv[]);
Expand Down
6 changes: 6 additions & 0 deletions tests/trun.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ static int do_code(C_MemoryAllocator *allocator, const char *code, const struct
raviX_output_cfg(linearizer->main_proc, stdout);
}
}
if (args->opt_upvalue) {
raviX_optimize_upvalues(linearizer);
if (args->irdump) {
raviX_output_linearizer(linearizer, stdout);
}
}
if (args->gen_C) {
fprintf(stdout, "\n#endif\n");
raviX_generate_C_tofile(linearizer, args->mainfunc, stdout);
Expand Down

0 comments on commit c24e1eb

Please sign in to comment.