From 37827ef8c896b872326a96f0e90ed25788f4d766 Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Fri, 12 May 2023 10:54:52 +0530 Subject: [PATCH 1/5] Rename `jit` to `lpython` --- integration_tests/CMakeLists.txt | 6 +++--- .../{test_lpython_decorator.py => lpython_decorator_01.py} | 0 src/lpython/semantics/python_ast_to_asr.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename integration_tests/{test_lpython_decorator.py => lpython_decorator_01.py} (100%) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 91e371b8dd..43c7d63f73 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -541,7 +541,7 @@ RUN(NAME callback_01 LABELS cpython llvm) # Intrinsic Functions RUN(NAME intrinsics_01 LABELS cpython llvm) # any -COMPILE(NAME import_order_01 LABELS cpython llvm c) # any +# lpython decorator +RUN(NAME lpython_decorator_01 LABELS cpython) -# Jit -RUN(NAME test_lpython_decorator LABELS cpython) +COMPILE(NAME import_order_01 LABELS cpython llvm c) # any diff --git a/integration_tests/test_lpython_decorator.py b/integration_tests/lpython_decorator_01.py similarity index 100% rename from integration_tests/test_lpython_decorator.py rename to integration_tests/lpython_decorator_01.py diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index e1f1cd26e3..0655af2e92 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3589,8 +3589,8 @@ class SymbolTableVisitor : public CommonVisitor { is_inline = true; } else if (name == "static") { is_static = true; - } else if (name == "jit") { - throw SemanticError("`@lpython.jit` decorator must be " + } else if (name == "lpython") { + throw SemanticError("`@lpython` decorator must be " "run from CPython, not compiled using LPython", dec->base.loc); } else { From a4df75c0e11d52db394be11b8ed1acbc54f6dcb3 Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Fri, 12 May 2023 10:56:39 +0530 Subject: [PATCH 2/5] Move import statements within the lpython class --- src/runtime/lpython/lpython.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py index f1fb0136fc..4f4b418c17 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -4,8 +4,6 @@ import platform from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass from goto import with_goto -from numpy import get_include -from distutils.sysconfig import get_python_inc # TODO: this does not seem to restrict other imports __slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "c32", "c64", "CPtr", @@ -728,6 +726,7 @@ def get_data_type(t): # TODO: Use LLVM instead of C backend r = os.system("lpython --show-c --disable-main a.py > a.h") assert r == 0, "Failed to create C file" + gcc_flags = "" if platform.system() == "Linux": gcc_flags = " -shared -fPIC " @@ -735,12 +734,16 @@ def get_data_type(t): gcc_flags = " -bundle -flat_namespace -undefined suppress " else: raise NotImplementedError("Platform not implemented") + + from numpy import get_include + from distutils.sysconfig import get_python_inc python_path = "-I" + get_python_inc() + " " numpy_path = "-I" + get_include() rt_path_01 = "-I" + get_rtlib_dir() + "/../libasr/runtime " rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath " \ + get_rtlib_dir() + " -llpython_runtime " python_lib = "-L" "$CONDA_PREFIX/lib/ -lpython3.10 -lm" + r = os.system("gcc -g" + gcc_flags + python_path + numpy_path + " a.c -o lpython_jit_module.so " + rt_path_01 + rt_path_02 + python_lib) assert r == 0, "Failed to create the shared library" From a9e9a33cf8c6cc24f01332ba8b3e288506617e0c Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Fri, 12 May 2023 12:22:50 +0530 Subject: [PATCH 3/5] Use get_python_lib to get lib path --- src/runtime/lpython/lpython.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py index 4f4b418c17..9cd901c319 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -736,13 +736,13 @@ def get_data_type(t): raise NotImplementedError("Platform not implemented") from numpy import get_include - from distutils.sysconfig import get_python_inc + from distutils.sysconfig import get_python_inc, get_python_lib python_path = "-I" + get_python_inc() + " " numpy_path = "-I" + get_include() rt_path_01 = "-I" + get_rtlib_dir() + "/../libasr/runtime " rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath " \ + get_rtlib_dir() + " -llpython_runtime " - python_lib = "-L" "$CONDA_PREFIX/lib/ -lpython3.10 -lm" + python_lib = "-L" + get_python_lib() + "/../.. -lpython3.10 -lm" r = os.system("gcc -g" + gcc_flags + python_path + numpy_path + " a.c -o lpython_jit_module.so " + rt_path_01 + rt_path_02 + python_lib) From d48b5235a60272364b98bcddc20a542642c0fa62 Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Fri, 12 May 2023 12:54:12 +0530 Subject: [PATCH 4/5] Use unique name for the lpython decorator generated files --- .gitignore | 1 + src/runtime/lpython/lpython.py | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e237284a56..a96d3ff80c 100644 --- a/.gitignore +++ b/.gitignore @@ -90,6 +90,7 @@ inst/bin/* *_lines.dat.txt *__tmp__generated__.c visualize*.html +lpython_decorator*/ a.c a.h a.py diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py index 9cd901c319..0d48c007a0 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -570,11 +570,13 @@ def get_data_type(t): source_code = getsource(function) source_code = source_code[source_code.find('\n'):] - # TODO: Create a filename based on the function name - # filename = function.__name__ + ".py" + dir_name = "./lpython_decorator_" + self.fn_name + if not os.path.exists(dir_name): + os.mkdir(dir_name) + filename = dir_name + "/" + self.fn_name # Open the file for writing - with open("a.py", "w") as file: + with open(filename + ".py", "w") as file: # Write the Python source code to the file file.write("@ccallable") file.write(source_code) @@ -680,7 +682,7 @@ def get_data_type(t): #include // LPython generated C code -#include "a.h" +#include "{self.fn_name}.h" // Define the Python module and method mappings static PyObject* define_module(PyObject* self, PyObject* args) {{ @@ -718,13 +720,14 @@ def get_data_type(t): """ # ---------------------------------------------------------------------- # Write the C source code to the file - with open("a.c", "w") as file: + with open(filename + ".c", "w") as file: file.write(template) # ---------------------------------------------------------------------- # Generate the Shared library # TODO: Use LLVM instead of C backend - r = os.system("lpython --show-c --disable-main a.py > a.h") + r = os.system("lpython --show-c --disable-main " + + filename + ".py > " + filename + ".h") assert r == 0, "Failed to create C file" gcc_flags = "" @@ -738,14 +741,15 @@ def get_data_type(t): from numpy import get_include from distutils.sysconfig import get_python_inc, get_python_lib python_path = "-I" + get_python_inc() + " " - numpy_path = "-I" + get_include() + numpy_path = "-I" + get_include() + " " rt_path_01 = "-I" + get_rtlib_dir() + "/../libasr/runtime " rt_path_02 = "-L" + get_rtlib_dir() + " -Wl,-rpath " \ + get_rtlib_dir() + " -llpython_runtime " python_lib = "-L" + get_python_lib() + "/../.. -lpython3.10 -lm" r = os.system("gcc -g" + gcc_flags + python_path + numpy_path + - " a.c -o lpython_jit_module.so " + rt_path_01 + rt_path_02 + python_lib) + filename + ".c -o lpython_jit_module.so " + + rt_path_01 + rt_path_02 + python_lib) assert r == 0, "Failed to create the shared library" def __call__(self, *args, **kwargs): From 0154fc30b334b7baf21ba778c939459137b51f15 Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Fri, 12 May 2023 12:57:43 +0530 Subject: [PATCH 5/5] Use unique name for shared library --- src/runtime/lpython/lpython.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py index 0d48c007a0..ce49c86506 100644 --- a/src/runtime/lpython/lpython.py +++ b/src/runtime/lpython/lpython.py @@ -700,13 +700,13 @@ def get_data_type(t): // Define the module initialization function static struct PyModuleDef module_def = {{ PyModuleDef_HEAD_INIT, - "lpython_jit_module", + "lpython_module_{self.fn_name}", "Shared library to use LPython generated functions", -1, module_methods }}; -PyMODINIT_FUNC PyInit_lpython_jit_module(void) {{ +PyMODINIT_FUNC PyInit_lpython_module_{self.fn_name}(void) {{ PyObject* module; // Create the module object @@ -748,12 +748,13 @@ def get_data_type(t): python_lib = "-L" + get_python_lib() + "/../.. -lpython3.10 -lm" r = os.system("gcc -g" + gcc_flags + python_path + numpy_path + - filename + ".c -o lpython_jit_module.so " + + filename + ".c -o lpython_module_" + self.fn_name + ".so " + rt_path_01 + rt_path_02 + python_lib) assert r == 0, "Failed to create the shared library" def __call__(self, *args, **kwargs): import sys; sys.path.append('.') # import the symbol from the shared library - function = getattr(__import__("lpython_jit_module"), self.fn_name) + function = getattr(__import__("lpython_module_" + self.fn_name), + self.fn_name) return function(*args, **kwargs)