Skip to content

Commit bb8bfe7

Browse files
authored
Merge pull request #1796 from Thirumalai-Shaktivel/lpython_decorator
Fixes for lpython decorator
2 parents 20d9536 + 0154fc3 commit bb8bfe7

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ inst/bin/*
9090
*_lines.dat.txt
9191
*__tmp__generated__.c
9292
visualize*.html
93+
lpython_decorator*/
9394
a.c
9495
a.h
9596
a.py

integration_tests/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ RUN(NAME callback_01 LABELS cpython llvm)
545545
# Intrinsic Functions
546546
RUN(NAME intrinsics_01 LABELS cpython llvm) # any
547547

548-
COMPILE(NAME import_order_01 LABELS cpython llvm c) # any
548+
# lpython decorator
549+
RUN(NAME lpython_decorator_01 LABELS cpython)
549550

550-
# Jit
551-
RUN(NAME test_lpython_decorator LABELS cpython)
551+
COMPILE(NAME import_order_01 LABELS cpython llvm c) # any

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,8 +3603,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
36033603
is_inline = true;
36043604
} else if (name == "static") {
36053605
is_static = true;
3606-
} else if (name == "jit") {
3607-
throw SemanticError("`@lpython.jit` decorator must be "
3606+
} else if (name == "lpython") {
3607+
throw SemanticError("`@lpython` decorator must be "
36083608
"run from CPython, not compiled using LPython",
36093609
dec->base.loc);
36103610
} else {

src/runtime/lpython/lpython.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import platform
55
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass
66
from goto import with_goto
7-
from numpy import get_include
8-
from distutils.sysconfig import get_python_inc
97

108
# TODO: this does not seem to restrict other imports
119
__slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "c32", "c64", "CPtr",
@@ -572,11 +570,13 @@ def get_data_type(t):
572570
source_code = getsource(function)
573571
source_code = source_code[source_code.find('\n'):]
574572

575-
# TODO: Create a filename based on the function name
576-
# filename = function.__name__ + ".py"
573+
dir_name = "./lpython_decorator_" + self.fn_name
574+
if not os.path.exists(dir_name):
575+
os.mkdir(dir_name)
576+
filename = dir_name + "/" + self.fn_name
577577

578578
# Open the file for writing
579-
with open("a.py", "w") as file:
579+
with open(filename + ".py", "w") as file:
580580
# Write the Python source code to the file
581581
file.write("@ccallable")
582582
file.write(source_code)
@@ -682,7 +682,7 @@ def get_data_type(t):
682682
#include <numpy/ndarrayobject.h>
683683
684684
// LPython generated C code
685-
#include "a.h"
685+
#include "{self.fn_name}.h"
686686
687687
// Define the Python module and method mappings
688688
static PyObject* define_module(PyObject* self, PyObject* args) {{
@@ -700,13 +700,13 @@ def get_data_type(t):
700700
// Define the module initialization function
701701
static struct PyModuleDef module_def = {{
702702
PyModuleDef_HEAD_INIT,
703-
"lpython_jit_module",
703+
"lpython_module_{self.fn_name}",
704704
"Shared library to use LPython generated functions",
705705
-1,
706706
module_methods
707707
}};
708708
709-
PyMODINIT_FUNC PyInit_lpython_jit_module(void) {{
709+
PyMODINIT_FUNC PyInit_lpython_module_{self.fn_name}(void) {{
710710
PyObject* module;
711711
712712
// Create the module object
@@ -720,33 +720,41 @@ def get_data_type(t):
720720
"""
721721
# ----------------------------------------------------------------------
722722
# Write the C source code to the file
723-
with open("a.c", "w") as file:
723+
with open(filename + ".c", "w") as file:
724724
file.write(template)
725725

726726
# ----------------------------------------------------------------------
727727
# Generate the Shared library
728728
# TODO: Use LLVM instead of C backend
729-
r = os.system("lpython --show-c --disable-main a.py > a.h")
729+
r = os.system("lpython --show-c --disable-main "
730+
+ filename + ".py > " + filename + ".h")
730731
assert r == 0, "Failed to create C file"
732+
731733
gcc_flags = ""
732734
if platform.system() == "Linux":
733735
gcc_flags = " -shared -fPIC "
734736
elif platform.system() == "Darwin":
735737
gcc_flags = " -bundle -flat_namespace -undefined suppress "
736738
else:
737739
raise NotImplementedError("Platform not implemented")
740+
741+
from numpy import get_include
742+
from distutils.sysconfig import get_python_inc, get_python_lib
738743
python_path = "-I" + get_python_inc() + " "
739-
numpy_path = "-I" + get_include()
744+
numpy_path = "-I" + get_include() + " "
740745
rt_path_01 = "-I" + get_rtlib_dir() + "/../libasr/runtime "
741746
rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath " \
742747
+ get_rtlib_dir() + " -llpython_runtime "
743-
python_lib = "-L" "$CONDA_PREFIX/lib/ -lpython3.10 -lm"
748+
python_lib = "-L" + get_python_lib() + "/../.. -lpython3.10 -lm"
749+
744750
r = os.system("gcc -g" + gcc_flags + python_path + numpy_path +
745-
" a.c -o lpython_jit_module.so " + rt_path_01 + rt_path_02 + python_lib)
751+
filename + ".c -o lpython_module_" + self.fn_name + ".so " +
752+
rt_path_01 + rt_path_02 + python_lib)
746753
assert r == 0, "Failed to create the shared library"
747754

748755
def __call__(self, *args, **kwargs):
749756
import sys; sys.path.append('.')
750757
# import the symbol from the shared library
751-
function = getattr(__import__("lpython_jit_module"), self.fn_name)
758+
function = getattr(__import__("lpython_module_" + self.fn_name),
759+
self.fn_name)
752760
return function(*args, **kwargs)

0 commit comments

Comments
 (0)