-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
math.pi causes linker errors #2129
Comments
This was caused by the (correct) fix not to use C macros but global variables. But they now clash. |
I think we need to make the names unique to a translation unit, similarly to what we did in LFortran recently. We need to port the solution from here: lfortran/lfortran#1898 and use it for these global variables. Update: implemented in #2138. |
I think in the C backend we need to use a global map (using a unique hash) where we save the actual (unique) name, and look it up. Similarly how we handle similar things in the LLVM backend. |
Can you please add a small reproducible example? Is this something related to #2076? |
I am probably able to reproduce it: $ cat a.py
from lpython import f64
from math import pi
def area_of_circle(r: f64) -> f64:
return pi * r * r
|
@Shaikh-Ubaid yes, exactly. I was going to post the same thing. We use this to compile a larger project, file by file, to C, then we link everything at the end. The solution is to make these global variables have a unique name, using LFortran does this for some of the generated functions, but only when |
This would also fail for functions and subroutines:
|
I think we need a |
A quick workaround: diff --git a/src/runtime/lpython_intrinsic_numpy.py b/src/runtime/lpython_intrinsic_numpy.py
index 17243dd99..5d5aebcbd 100644
--- a/src/runtime/lpython_intrinsic_numpy.py
+++ b/src/runtime/lpython_intrinsic_numpy.py
@@ -1,7 +1,5 @@
from lpython import i32, i64, f64, f32, ccall, vectorize, overload
-pi_64: f64 = f64(3.141592653589793238462643383279502884197)
-pi_32: f32 = f32(3.141592653589793238462643383279502884197)
########## sin ##########
@@ -271,27 +269,7 @@ def arctan(x: f32) -> f32:
########## degrees ##########
-@overload
-@vectorize
-def degrees(x: f64) -> f64:
- return x*180.0/pi_64
-
-@overload
-@vectorize
-def degrees(x: f32) -> f32:
- return x*f32(f32(180)/pi_32)
-
-########## radians ##########
-@overload
-@vectorize
-def radians(x: f64) -> f64:
- return x*pi_64/180.0
-
-@overload
-@vectorize
-def radians(x: f32) -> f32:
- return x*f32(pi_32/f32(180))
########## arcsinh ##########
This works. |
Due to lcompilers#2129, the global variables do not always work well. Until that issue is fixed, a simple workaround is to move these global variables into local variables. Reference tests updated.
@Smit-create how do I use the PR #2149 here? Here is an example: from math import pi
def f():
print(pi) And I tried: lpython --separate-compilation --global-mangling --show-c a.py
lpython --separate-compilation --show-c a.py
lpython --module-mangling --show-c a.py But it doesn't seem to mangle the symbols, so things still fail at link time when compiling separately two files like the above. |
This works for me:
The reason that global-mangling doesn't work is that I think we wrap all the global symbols in a module named
|
@Smit-create awesome, I think it works!! Here is what I tried: $ cat a.py
from math import pi
def f():
print(pi)
$ cat b.py
from math import pi
def f():
print(pi)
f()
$ lpython --separate-compilation --all-mangling --show-c --disable-main a.py > a.c
$ lpython --separate-compilation --all-mangling --show-c b.py > b.c
$ clang -I$(lpython --get-rtl-header-dir) b.c a.c -Wl,-rpath -Wl,$(lpython --get-rtl-dir) -L$(lpython --get-rtl-dir) -llpython_runtime
$ ./a.out
3.141593 Thank you for this. I think this is good enough and we can improve it later. Great job! |
This global variables in math.py:
Cause the following linker errors using the C backend if compiling and linking more than one file:
The text was updated successfully, but these errors were encountered: