aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-12-23 23:19:54 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-12-27 16:00:53 +0100
commit84a00109ff526e1663c2d8aa260c6065b8f142f0 (patch)
tree53413359f65a5f0f14bd4142d9c8b873f054cbdc
parent69a789a78d4e64052628307f25310e195a50f5ee (diff)
downloadsparse-dev-84a00109ff526e1663c2d8aa260c6065b8f142f0.tar.gz
llvm: fix: previous function ref MUST be reused
In sparse-llvm, when a reference to a function is made via get_sym_value(), for example for a call, the reference is created with LLVMAddFunction() and if needed later, the existing reference is returned via LLVMGetNamedFunction(). This is the correct way to do it. However, when emitting the code for a function definition, a fresh reference is always made. If a previous reference to this function already existed, the second one will have a slightly different name: the given name suffixed by ".<somenumber>". LLVM does this for every created references, to disambiguate them. As consequence, the compiled function will not be name "<functionname>", as expected, but "<functionname>.<somenumber>". Fix this by always using get_sym_value() when emitting the code for the function definition as this will return the reference for the given function name if it already exist. This has the added bonus to remove some code duplication. CC: Dibyendu Majumdar <mobile@majumdar.org.uk> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--sparse-llvm.c25
-rw-r--r--validation/backend/fn-ref.c32
2 files changed, 34 insertions, 23 deletions
diff --git a/sparse-llvm.c b/sparse-llvm.c
index a8186df5..3ee014da 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -40,12 +40,6 @@ static LLVMTypeRef sym_func_type(struct symbol *sym)
struct symbol *arg;
int n_arg = 0;
- /* to avoid strangeness with varargs [for now], we build
- * the function and type anew, for each call. This
- * is probably wrong. We should look up the
- * symbol declaration info.
- */
-
ret_type = func_return_type(sym);
/* count args, build argument type information */
@@ -1128,34 +1122,19 @@ static void output_fn(LLVMModuleRef module, struct entrypoint *ep)
{
struct symbol *sym = ep->name;
struct symbol *base_type = sym->ctype.base_type;
- LLVMTypeRef arg_types[MAX_ARGS];
- LLVMTypeRef ret_type = symbol_type(base_type->ctype.base_type);
- LLVMTypeRef fun_type;
struct function function = { .module = module };
struct basic_block *bb;
- struct symbol *arg;
- const char *name;
int nr_args = 0;
int i;
- FOR_EACH_PTR(base_type->arguments, arg) {
- struct symbol *arg_base_type = arg->ctype.base_type;
-
- arg_types[nr_args++] = symbol_type(arg_base_type);
- } END_FOR_EACH_PTR(arg);
-
- name = show_ident(sym->ident);
-
- fun_type = LLVMFunctionType(ret_type, arg_types, nr_args, base_type->variadic);
-
- function.fn = LLVMAddFunction(module, name, fun_type);
+ function.fn = get_sym_value(&function, sym);
LLVMSetFunctionCallConv(function.fn, LLVMCCallConv);
-
LLVMSetLinkage(function.fn, function_linkage(sym));
function.builder = LLVMCreateBuilder();
/* give a name to each argument */
+ nr_args = symbol_list_size(base_type->arguments);
for (i = 0; i < nr_args; i++) {
char name[MAX_PSEUDO_NAME];
LLVMValueRef arg;
diff --git a/validation/backend/fn-ref.c b/validation/backend/fn-ref.c
new file mode 100644
index 00000000..d987a28b
--- /dev/null
+++ b/validation/backend/fn-ref.c
@@ -0,0 +1,32 @@
+extern int fun0(int a);
+extern int fun1(int a);
+
+int foo(int a);
+int foo(int a)
+{
+ int v = fun0(a);
+ return v;
+}
+
+void *bar(int a)
+{
+ return fun1;
+}
+
+int fun0(int a)
+{
+ return a + 0;
+}
+
+int fun1(int a)
+{
+ return a + 1;
+}
+
+/*
+ * check-name: llvm function reference
+ * check-command: sparse-llvm-dis -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: fun[0-9]\.[1-9]
+ */