Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make backend compulsory if optimisations are provided
  • Loading branch information
czgdp1807 committed Jul 22, 2023
commit 5cff2f61c8942d801b5fa3098e166b2c40a51c33
2 changes: 1 addition & 1 deletion integration_tests/lpython_decorator_01.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from numpy import array
from lpython import i32, f64, lpython

@lpython(optimisation_flags=["-ffast-math", "-funroll-loops", "-O3"])
@lpython(backend="c", backend_optimisation_flags=["-ffast-math", "-funroll-loops", "-O3"])
def fast_sum(n: i32, x: f64[:]) -> f64:
s: f64 = 0.0
i: i32
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/lpython_decorator_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

n = TypeVar("n")

@lpython(optimisation_flags=["-ffast-math", "-funroll-loops"])
@lpython(backend="c", backend_optimisation_flags=["-ffast-math", "-funroll-loops"])
def multiply_01(n: i32, x: f64[:]) -> f64[n]:
i: i32
for i in range(n):
Expand Down
18 changes: 16 additions & 2 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,12 @@ def compile(self, function, backend, optimisation_flags):
if function in self.pyfunc2compiledfunc:
return self.pyfunc2compiledfunc[function]

if optimisation_flags is not None and backend is None:
raise ValueError("backend must be specified if backend_optimisation_flags are provided.")

if backend is None:
backend = "c"

def get_rtlib_dir():
current_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(current_dir, "..")
Expand Down Expand Up @@ -726,13 +732,21 @@ def get_rtlib_dir():
lpython_jit_cache = LpythonJITCache()

# Taken from https://stackoverflow.com/a/24617244
def lpython(original_function=None, backend="c", optimisation_flags=None):
def lpython(original_function=None, backend=None, backend_optimisation_flags=None):
"""
The @lpython decorator compiles a given function using LPython.

The decorator should be used from CPython mode, i.e., when the module is
being run using CPython. When possible, it is recommended to use LPython
for the main program, and use the @cpython decorator from the LPython mode
to access CPython features that are not supported by LPython.
"""
def _lpython(function):
@functools.wraps(function)
def __lpython(*args, **kwargs):
import sys; sys.path.append('.')
lib_name, fn_name = lpython_jit_cache.compile(
function, backend, optimisation_flags)
function, backend, backend_optimisation_flags)
return getattr(__import__(lib_name), fn_name)(*args, **kwargs)
return __lpython

Expand Down