From 84a00109ff526e1663c2d8aa260c6065b8f142f0 Mon Sep 17 00:00:00 2001
From: Luc Van Oostenryck
Date: Sat, 23 Dec 2017 23:19:54 +0100
Subject: 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 ".". LLVM does this for every
created references, to disambiguate them. As consequence, the
compiled function will not be name "", as expected,
but ".".
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
Signed-off-by: Luc Van Oostenryck
---
validation/backend/fn-ref.c | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 validation/backend/fn-ref.c
(limited to 'validation/backend')
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]
+ */
--
cgit 1.2.3-korg