From 18a48c4f0c62e214dc9d097017b0020418e84d10 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sun, 12 Jan 2020 01:21:29 +0000 Subject: Add some LLVM attributes to functions Some of the functions generated by sinc can have attributes added to them that might be difficult for the LLVM toolchain to infer. Attribute generation can be turned off with `-O no-llvm-attributes`. --- llvm_codegen.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++--------- llvm_codegen.h | 1 + sinc.c | 14 +++++++++++-- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/llvm_codegen.c b/llvm_codegen.c index 7e790a2..deb75b9 100644 --- a/llvm_codegen.c +++ b/llvm_codegen.c @@ -335,6 +335,21 @@ int make_trmc_inner_name(char *buf, int n, char *outer_id, char *mod) { return snprintf(buf, n, ".%s.inner.%s", outer_id, mod); } +void add_enum_attr(LLVMValueRef f, uint index, char *id, size_t n, int val) { + if (!opts->llvm_attributes) return; + if (!n) n = strlen(id); + + unsigned kind = LLVMGetEnumAttributeKindForName(id, n); + if (!kind) error(GENERAL_ERROR, "no enum attribute %s", id); + + LLVMAttributeRef attr = LLVMCreateEnumAttribute(global_context, kind, val); + LLVMAddAttributeAtIndex(f, index, attr); +} + +void add_func_enum_attr(LLVMValueRef f, char *id, size_t n, int val) { + add_enum_attr(f, LLVMAttributeFunctionIndex, id, n, val); +} + LLVMValueRef codegen_type_definition(sexpr *se) { assert(se); assert(se->type == BRANCH); @@ -432,6 +447,17 @@ LLVMValueRef codegen_type_definition(sexpr *se) { ctor_e->filler.type = filler_t; ctor_e->filler.constructs = struct_t; + add_func_enum_attr(filler_p, "alwaysinline", 0, 1); + add_func_enum_attr(filler_p, "norecurse", 0, 1); + add_func_enum_attr(filler_p, "nounwind", 0, 1); + add_func_enum_attr(filler_p, "writeonly", 0, 1); + add_func_enum_attr(filler_p, "argmemonly", 0, 1); + add_enum_attr(filler_p, filler_param_c, "nonnull", 0, 1); + + add_func_enum_attr(ctor_p, "alwaysinline", 0, 1); + add_func_enum_attr(ctor_p, "norecurse", 0, 1); + add_func_enum_attr(ctor_p, "nounwind", 0, 1); + /* * Make functions to retreive each property of the struct. */ @@ -457,6 +483,13 @@ LLVMValueRef codegen_type_definition(sexpr *se) { debug("adding %s to scope\n", prop_func_id); scope_add_entry(sc, prop_func_id, prop_func_p, prop_func_t); + + add_func_enum_attr(prop_func_p, "alwaysinline", 0, 1); + add_func_enum_attr(prop_func_p, "norecurse", 0, 1); + add_func_enum_attr(prop_func_p, "nounwind", 0, 1); + add_func_enum_attr(prop_func_p, "readonly", 0, 1); + add_func_enum_attr(prop_func_p, "argmemonly", 0, 1); + add_enum_attr(prop_func_p, 1, "nonnull", 0, 1); } LLVMDisposeBuilder(util_b); @@ -892,6 +925,9 @@ void codegen_trmc_inner(char *outer_id, char **outer_param_ids, LLVMValueRef LLVMBuildRetVoid(builder); } + + add_func_enum_attr(inner_p, "nounwind", 0, 1); + add_enum_attr(inner_p, inner_arg_c, "nonnull", 0, 1); } void codegen_trmc(scope_entry *outer_e, uint outer_param_c, sexpr *body, @@ -1153,6 +1189,8 @@ void codegen_trmc(scope_entry *outer_e, uint outer_param_c, sexpr *body, if (else_p) codegen_trmc_inner(outer_id, param_names, else_p, then_p, inner_t, else_p, inner_t, body, else_ast); + add_func_enum_attr(outer_p, "nounwind", 0, 1); + /* * It would be nice if we could consider these TRMC functions similarly to * filler functions, e.g. flatten would be TRMC with concat being its cons. @@ -1225,10 +1263,18 @@ void codegen_constructor(scope_entry *outer_e, scope_entry *base_e, uint outer_e->filler.value = filler_p; outer_e->filler.type = filler_t; outer_e->filler.constructs = base_e->filler.constructs; + + add_func_enum_attr(filler_p, "nounwind", 0, 1); + add_enum_attr(filler_p, filler_param_c, "nonnull", 0, 1); + + add_func_enum_attr(outer_p, "nounwind", 0, 1); } -LLVMValueRef codegen_basic_function(sexpr *body, int tail_position) { - return LLVMBuildRet(builder, _codegen(body, tail_position)); +void codegen_basic_function(LLVMValueRef f, sexpr *body, int + tail_position) { + + add_func_enum_attr(f, "nounwind", 0, 1); + LLVMBuildRet(builder, _codegen(body, tail_position)); } LLVMValueRef codegen_definition(sexpr *se) { @@ -1313,11 +1359,11 @@ LLVMValueRef codegen_definition(sexpr *se) { codegen_constructor(entry, constructor_base, param_c, body); } else { - codegen_basic_function(body, 1); + codegen_basic_function(func_p, body, 1); } } else { - codegen_basic_function(body, 1); + codegen_basic_function(func_p, body, 1); } scope_pop_layer(&sc); @@ -1602,12 +1648,10 @@ int interpret(LLVMModuleRef module) { } int llvm_codegen_epilogue(char *output_filename, format_t format) { - // char *error_str = 0; - - // LLVMDumpModule(module); - - // LLVMVerifyModule(module, LLVMAbortProcessAction, &error_str); - // LLVMDisposeMessage(error_str); + /* char *error_str = 0; */ + /* LLVMDumpModule(module); */ + /* LLVMVerifyModule(module, LLVMAbortProcessAction, &error_str); */ + /* LLVMDisposeMessage(error_str); */ LLVMDIBuilderFinalise(di_builder); diff --git a/llvm_codegen.h b/llvm_codegen.h index 39624db..4911168 100644 --- a/llvm_codegen.h +++ b/llvm_codegen.h @@ -15,6 +15,7 @@ typedef struct { boxing_rule_t boxing_rule; uint default_bit_width; bool tail_recursive_mod_cons; + bool llvm_attributes; } optimisation_t; void llvm_codegen_prologue(char *filename, optimisation_t *optimisations); diff --git a/sinc.c b/sinc.c index 82dd164..fa9a1ba 100644 --- a/sinc.c +++ b/sinc.c @@ -137,7 +137,8 @@ int main(int argc, char **argv) { optimisation_t llvm_info = { .boxing_rule = ALWAYS_BOX, .default_bit_width = 0, - .tail_recursive_mod_cons = false + .tail_recursive_mod_cons = true, + .llvm_attributes = true }; verbose = 0; @@ -187,7 +188,16 @@ int main(int argc, char **argv) { case 'O': if (!strcmp(optarg, "tail-recursive-mod-cons")) { - llvm_info.tail_recursive_mod_cons = 1; + llvm_info.tail_recursive_mod_cons = true; + + } else if (!strcmp(optarg, "no-tail-recursive-mod-cons")) { + llvm_info.tail_recursive_mod_cons = false; + + } else if (!strcmp(optarg, "llvm-attributes")) { + llvm_info.llvm_attributes = true; + + } else if (!strcmp(optarg, "no-llvm-attributes")) { + llvm_info.llvm_attributes = false; } else { error(INVALID_ARGUMENTS, -- cgit v1.2.1