Skip to content
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

LLVM: Initial CPython Interop example #2211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ jobs:
shell: bash -e -l {0}
run: |
cd integration_tests
./run_tests.py -b cpython c_py
./run_tests.py -b cpython c_py -f
./run_tests.py -b cpython c_py llvm_py
./run_tests.py -b cpython c_py llvm_py -f

sympy:
name: Run SymPy tests
Expand Down
43 changes: 32 additions & 11 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ message("LPYTHON_RTLIB_DIR: ${LPYTHON_RTLIB_DIR}")
message("LPYTHON_RTLIB_LIBRARY: ${LPYTHON_RTLIB_LIBRARY}")


macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS)
macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN)
set(fail ${${RUN_FAIL}})
set(name ${${RUN_NAME}})
set(file_name ${${RUN_FILE_NAME}})
set(labels ${${RUN_LABELS}})
set(extra_files ${${RUN_EXTRAFILES}})
set(extra_args ${${RUN_EXTRA_ARGS}})
set(copy_to_bin ${${RUN_COPY_TO_BIN}})

if (NOT name)
message(FATAL_ERROR "Must specify the NAME argument")
Expand All @@ -105,6 +106,23 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT
if (${fail})
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
endif()
elseif (KIND STREQUAL "llvm_py")
add_custom_command(
OUTPUT ${name}.o
COMMAND ${LPYTHON} -c ${extra_args} ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}.py -o ${name}.o
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}.py
VERBATIM)
add_executable(${name} ${name}.o ${extra_files})
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR} ${NUMPY_INCLUDE_DIR})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(${name} lpython_rtlib Python::Python)
add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name})
if (labels)
set_tests_properties(${name} PROPERTIES LABELS "${labels}")
endif()
if (${fail})
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
endif()
elseif(KIND STREQUAL "llvm_sym")
add_custom_command(
OUTPUT ${name}.o
Expand Down Expand Up @@ -153,9 +171,6 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR} ${NUMPY_INCLUDE_DIR})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(${name} lpython_rtlib Python::Python)
if (extra_files)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${extra_files} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif()
add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name})
if (labels)
set_tests_properties(${name} PROPERTIES LABELS "${labels}")
Expand Down Expand Up @@ -281,12 +296,17 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
endif()
endif()

if (copy_to_bin)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${copy_to_bin} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif()

endif()
endmacro(RUN_UTIL)

macro(RUN)
set(options FAIL NOFAST ENABLE_CPYTHON LINK_NUMPY)
set(oneValueArgs NAME IMPORT_PATH)
set(oneValueArgs NAME IMPORT_PATH COPY_TO_BIN)
set(multiValueArgs LABELS EXTRAFILES)
cmake_parse_arguments(RUN "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )
Expand All @@ -309,14 +329,14 @@ macro(RUN)
endif()

if (NOT FAST)
RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS)
RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN)
endif()

if ((FAST) AND (NOT RUN_NOFAST))
set(RUN_EXTRA_ARGS ${RUN_EXTRA_ARGS} --fast)
set(RUN_NAME "${RUN_NAME}_FAST")
list(REMOVE_ITEM RUN_LABELS cpython cpython_sym) # remove cpython, cpython_sym, from --fast test
RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS)
RUN_UTIL(RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXTRA_ARGS RUN_COPY_TO_BIN)
endif()
endmacro(RUN)

Expand Down Expand Up @@ -575,10 +595,11 @@ RUN(NAME bindc_05 LABELS llvm c
EXTRAFILES bindc_05b.c)
RUN(NAME bindc_06 LABELS llvm c
EXTRAFILES bindc_06b.c)
RUN(NAME bindpy_01 LABELS cpython c_py ENABLE_CPYTHON NOFAST EXTRAFILES bindpy_01_module.py)
RUN(NAME bindpy_02 LABELS cpython c_py LINK_NUMPY EXTRAFILES bindpy_02_module.py)
RUN(NAME bindpy_03 LABELS cpython c_py LINK_NUMPY NOFAST EXTRAFILES bindpy_03_module.py)
RUN(NAME bindpy_04 LABELS cpython c_py LINK_NUMPY NOFAST EXTRAFILES bindpy_04_module.py)
RUN(NAME bindpy_01 LABELS cpython c_py ENABLE_CPYTHON NOFAST COPY_TO_BIN bindpy_01_module.py)
RUN(NAME bindpy_02 LABELS cpython c_py LINK_NUMPY COPY_TO_BIN bindpy_02_module.py)
RUN(NAME bindpy_03 LABELS cpython c_py LINK_NUMPY NOFAST COPY_TO_BIN bindpy_03_module.py)
RUN(NAME bindpy_04 LABELS cpython c_py LINK_NUMPY NOFAST COPY_TO_BIN bindpy_04_module.py)
RUN(NAME bindpy_05 LABELS llvm_py c_py ENABLE_CPYTHON COPY_TO_BIN bindpy_05_module.py)
RUN(NAME test_generics_01 LABELS cpython llvm c NOFAST)
RUN(NAME test_cmath LABELS cpython llvm c NOFAST)
RUN(NAME test_complex_01 LABELS cpython llvm c wasm wasm_x64)
Expand Down
79 changes: 79 additions & 0 deletions integration_tests/bindpy_05.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a lot of CPython C-APIs being defined as interfaces directly in this file. That's a lot of exposure I feel. Its good that it works, but I would like all of it happen automatically. In other words, what I am trying to convey is that its great that calling CPython C-APIs work with LPython but that shouldn't be how our end users should interoperate with CPython. Instead, they should be using @pythoncall.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, all of this is for LLVM interoperability? So if I want to make LLVM interoperate with CPython then I will have to declare all of these functions (Py_Initialize, Py_DecodeLocale, etc.) in my .py file? That's not very user friendly. Other members of the team might have a different opinion, but considering that in C backend its as easy as @pythoncall decoration and in LLVM its about declaring so many functions, I would always use C backend for interoperating with CPython.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show the LLVM code generated for bindpy_05.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@czgdp1807 Thank you so much for your views. You are completely right. This is not the approach to use CPython interoperability with the LLVM Backend. This PR is just the starting point or foundation towards it. This PR is towards #2050 (comment).

The example added in this PR establishes the basics we need for the CPython Interoperability pass. That is, the changes in this PR demonstrate that in the CPython interoperability pass we would need to produce an ASR equivalent of the code added in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show the LLVM code generated for bindpy_05.py?

Please find below.

bindpy_05.ll ; ModuleID = 'LFortran' source_filename = "LFortran"

@0 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@1 = private unnamed_addr constant [17 x i8] c"AssertionError: \00", align 1
@2 = private unnamed_addr constant [43 x i8] c"BindPython: Unknown Error in FinalizeEx()\0A\00", align 1
@3 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@4 = private unnamed_addr constant [7 x i8] c"%s%s%s\00", align 1
@5 = private unnamed_addr constant [17 x i8] c"bindpy_05_module\00", align 1
@6 = private unnamed_addr constant [17 x i8] c"AssertionError: \00", align 1
@7 = private unnamed_addr constant [54 x i8] c"Failed to convert to unicode string bindpy_05_module\0A\00", align 1
@8 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@9 = private unnamed_addr constant [7 x i8] c"%s%s%s\00", align 1
@10 = private unnamed_addr constant [17 x i8] c"AssertionError: \00", align 1
@11 = private unnamed_addr constant [47 x i8] c"Failed to load python module bindpy_05_module\0A\00", align 1
@12 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@13 = private unnamed_addr constant [7 x i8] c"%s%s%s\00", align 1
@14 = private unnamed_addr constant [5 x i8] c"my_f\00", align 1
@15 = private unnamed_addr constant [17 x i8] c"AssertionError: \00", align 1
@16 = private unnamed_addr constant [27 x i8] c"Cannot find function my_f\0A\00", align 1
@17 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@18 = private unnamed_addr constant [7 x i8] c"%s%s%s\00", align 1
@19 = private unnamed_addr constant [17 x i8] c"AssertionError: \00", align 1
@20 = private unnamed_addr constant [21 x i8] c"Call to my_f failed\0A\00", align 1
@21 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@22 = private unnamed_addr constant [7 x i8] c"%s%s%s\00", align 1
@23 = private unnamed_addr constant [2 x i8] c" \00", align 1
@24 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@25 = private unnamed_addr constant [7 x i8] c"Ans is\00", align 1
@26 = private unnamed_addr constant [9 x i8] c"%s%s%d%s\00", align 1
@27 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1
@O_RDONLY = global i32 0

declare void* @PyImport_Import(void*)

declare i32 @PyLong_AsLongLong(void*)

declare void* @PyObject_CallObject(void*, void*)

declare void* @PyObject_GetAttrString(void*, i8*)

declare void @PySys_SetArgv(i32, void**)

declare void* @PyTuple_New(i32)

declare void* @PyUnicode_FromString(i8*)

declare void* @Py_DecodeLocale(i8*, void*)

declare i32 @Py_FinalizeEx()

declare void @Py_Initialize()

declare void @_Py_DecRef(void*)

define void @__module___main_____main____global_statements() {
.entry:
call void @__module___main___main0()
br label %return

return: ; preds = %.entry
ret void
}

define void @__module___main___main0() {
.entry:
%argv1 = alloca void*, align 8
call void @Py_Initialize()
%0 = call void* @Py_DecodeLocale(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @0, i32 0, i32 0), void* null)
store void* %0, void** %argv1, align 8
call void @PySys_SetArgv(i32 1, void** %argv1)
call void @__module___main___my_f()
%1 = call i32 @Py_FinalizeEx()
%2 = icmp sge i32 %1, 0
br i1 %2, label %then, label %else

then: ; preds = %.entry
br label %ifcont

else: ; preds = %.entry
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @4, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @1, i32 0, i32 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @2, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @3, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont

ifcont: ; preds = %else, %then
br label %return

return: ; preds = %ifcont
ret void
}

define void @__module___main___my_f() {
.entry:
%ans = alloca i32, align 4
%pArgs = alloca void*, align 8
%pFunc = alloca void*, align 8
%pModule = alloca void*, align 8
%pName = alloca void*, align 8
%pValue = alloca void*, align 8
%0 = call void* @PyUnicode_FromString(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @5, i32 0, i32 0))
store void* %0, void** %pName, align 8
%1 = load void*, void** %pName, align 8
%2 = ptrtoint void* %1 to i64
%3 = icmp ne i64 %2, 0
br i1 %3, label %then, label %else

then: ; preds = %.entry
br label %ifcont

else: ; preds = %.entry
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @9, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @6, i32 0, i32 0), i8* getelementptr inbounds ([54 x i8], [54 x i8]* @7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @8, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont

ifcont: ; preds = %else, %then
%4 = load void*, void** %pName, align 8
%5 = call void* @PyImport_Import(void* %4)
store void* %5, void** %pModule, align 8
%6 = load void*, void** %pName, align 8
call void @_Py_DecRef(void* %6)
%7 = load void*, void** %pModule, align 8
%8 = ptrtoint void* %7 to i64
%9 = icmp ne i64 %8, 0
br i1 %9, label %then1, label %else2

then1: ; preds = %ifcont
br label %ifcont3

else2: ; preds = %ifcont
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @13, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @10, i32 0, i32 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @11, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @12, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont3

ifcont3: ; preds = %else2, %then1
%10 = load void*, void** %pModule, align 8
%11 = call void* @PyObject_GetAttrString(void* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @14, i32 0, i32 0))
store void* %11, void** %pFunc, align 8
%12 = load void*, void** %pFunc, align 8
%13 = ptrtoint void* %12 to i64
%14 = icmp ne i64 %13, 0
br i1 %14, label %then4, label %else5

then4: ; preds = %ifcont3
br label %ifcont6

else5: ; preds = %ifcont3
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @18, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @15, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @16, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @17, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont6

ifcont6: ; preds = %else5, %then4
%15 = call void* @PyTuple_New(i32 0)
store void* %15, void** %pArgs, align 8
%16 = load void*, void** %pFunc, align 8
%17 = load void*, void** %pArgs, align 8
%18 = call void* @PyObject_CallObject(void* %16, void* %17)
store void* %18, void** %pValue, align 8
%19 = load void*, void** %pArgs, align 8
call void @_Py_DecRef(void* %19)
%20 = load void*, void** %pValue, align 8
%21 = ptrtoint void* %20 to i64
%22 = icmp ne i64 %21, 0
br i1 %22, label %then7, label %else8

then7: ; preds = %ifcont6
br label %ifcont9

else8: ; preds = %ifcont6
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @22, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @19, i32 0, i32 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @20, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @21, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont9

ifcont9: ; preds = %else8, %then7
%23 = load void*, void** %pValue, align 8
%24 = call i32 @PyLong_AsLongLong(void* %23)
store i32 %24, i32* %ans, align 4
%25 = load i32, i32* %ans, align 4
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @26, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @25, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @23, i32 0, i32 0), i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @24, i32 0, i32 0))
%26 = load i32, i32* %ans, align 4
%27 = icmp eq i32 %26, 5
br i1 %27, label %then10, label %else11

then10: ; preds = %ifcont9
br label %ifcont12

else11: ; preds = %ifcont9
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @27, i32 0, i32 0))
call void @EXIT(i32 1)
br label %ifcont12

ifcont12: ; preds = %else11, %then10
br label %return

return: ; preds = %ifcont12
ret void
}

declare void @_lcompilers_print_error(i8*, ...)

declare void @EXIT(i32)

declare void @_lfortran_printf(i8*, ...)

declare void @_lpython_close(i64)

declare i64 @_lpython_open(i8*, i8*)

declare i8* @_lpython_read(i64, i64)

define i32 @main(i32 %0, i8** %1) {
.entry:
call void @_lpython_set_argv(i32 %0, i8** %1)
call void @__module___main_____main____global_statements()
ret i32 0
}

declare void @_lpython_set_argv(i32, i8**)

Copy link
Contributor

@certik certik Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect!

@czgdp1807, the idea is to get something that works with all backends first. This does it. Then the next step is to write a pass that generates the same ASR as this very test, from @pythoncall.

We did exactly that with the symbolic backend.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from lpython import ccall, Pointer, i32, i64, empty_c_void_p, CPtr, pointer

@ccall(header="Python.h")
def Py_Initialize():
pass

@ccall(header="Python.h")
def Py_DecodeLocale(s: str, p: CPtr) -> CPtr:
pass

@ccall(header="Python.h")
def PySys_SetArgv(n: i32, args: Pointer[CPtr]):
pass

@ccall(header="Python.h")
def Py_FinalizeEx() -> i32:
pass

@ccall(header="Python.h")
def PyUnicode_FromString(s: str) -> CPtr:
pass

@ccall(header="Python.h")
def PyImport_Import(name: CPtr) -> CPtr:
pass

@ccall(header="Python.h")
def _Py_DecRef(name: CPtr):
pass

@ccall(header="Python.h")
def PyObject_GetAttrString(m: CPtr, s: str) -> CPtr:
pass

@ccall(header="Python.h")
def PyTuple_New(n: i32) -> CPtr:
pass

@ccall(header="Python.h")
def PyObject_CallObject(a: CPtr, b: CPtr) -> CPtr:
pass

@ccall(header="Python.h")
def PyLong_AsLongLong(a: CPtr) -> i32:
pass

def my_f():
pName: CPtr; pModule: CPtr; pFunc: CPtr; pArgs: CPtr; pValue: CPtr

pName = PyUnicode_FromString("bindpy_05_module")
assert bool(pName), "Failed to convert to unicode string bindpy_05_module\n"

pModule = PyImport_Import(pName)
_Py_DecRef(pName)
assert bool(pModule), "Failed to load python module bindpy_05_module\n"

pFunc = PyObject_GetAttrString(pModule, "my_f")
assert bool(pFunc), "Cannot find function my_f\n"

pArgs = PyTuple_New(0)
pValue = PyObject_CallObject(pFunc, pArgs)
_Py_DecRef(pArgs)
assert bool(pValue), "Call to my_f failed\n"

ans: i32 = PyLong_AsLongLong(pValue)
print("Ans is", ans)
assert ans == 5


def main0():
Py_Initialize()
argv1: CPtr = Py_DecodeLocale("", empty_c_void_p())
PySys_SetArgv(1, pointer(argv1, i64))

my_f()

assert(Py_FinalizeEx() >= 0), "BindPython: Unknown Error in FinalizeEx()\n"

main0()
3 changes: 3 additions & 0 deletions integration_tests/bindpy_05_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def my_f():
print("hello from python")
return 5
4 changes: 2 additions & 2 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Initialization
DEFAULT_THREADS_TO_USE = 8 # default no of threads is 8
SUPPORTED_BACKENDS = ['llvm', 'c', 'wasm', 'cpython', 'x86', 'wasm_x86', 'wasm_x64', 'c_py', 'c_sym', 'cpython_sym', 'llvm_sym']
SUPPORTED_BACKENDS = ['llvm', 'c', 'wasm', 'cpython', 'x86', 'wasm_x86', 'wasm_x64', 'c_py', 'c_sym', 'cpython_sym', 'llvm_sym', 'llvm_py']
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
LPYTHON_PATH = f"{BASE_DIR}/../src/bin"

Expand Down Expand Up @@ -62,7 +62,7 @@ def main():
DEFAULT_THREADS_TO_USE = args.no_of_threads or DEFAULT_THREADS_TO_USE
fast_tests = "yes" if args.fast else "no"
for backend in args.backends:
python_libs_req = "yes" if backend in ["c_py", "c_sym", "llvm_sym"] else "no"
python_libs_req = "yes" if backend in ["c_py", "c_sym", "llvm_sym", 'llvm_py'] else "no"
test_backend(backend)


Expand Down
10 changes: 10 additions & 0 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,16 @@ int link_executable(const std::vector<std::string> &infiles,
if (compiler_options.enable_symengine) {
cmd += " -L$CONDA_PREFIX/lib -Wl,-rpath -Wl,$CONDA_PREFIX/lib -lsymengine";
}

if (compiler_options.enable_cpython) {
std::string py_version = "3.10";
std::string py_flags = R"(-I $CONDA_PREFIX/include/python)" + py_version + R"( -L$CONDA_PREFIX/lib -Wl,-rpath -Wl,$CONDA_PREFIX/lib -lpython)" + py_version + R"()";
if (compiler_options.link_numpy) {
py_flags += R"( -I$CONDA_PREFIX/lib/python)" + py_version + R"(/site-packages/numpy/core/include)";
}
cmd += " " + py_flags;
}

int err = system(cmd.c_str());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
Expand Down
4 changes: 4 additions & 0 deletions src/lpython/semantics/python_intrinsic_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ struct IntrinsicNodeHandler {

} else if (ASRUtils::is_logical(*type)) {
return (ASR::asr_t *)arg;
} else if (ASR::is_a<ASR::CPtr_t>(*type)) {
ASR::expr_t* c_null_ptr = ASRUtils::EXPR(ASR::make_PointerNullConstant_t(
al, loc, ASRUtils::TYPE(ASR::make_CPtr_t(al, loc))));
return ASR::make_CPtrCompare_t(al, loc, arg, ASR::cmpopType::NotEq, c_null_ptr, to_type, nullptr);
} else {
std::string stype = ASRUtils::type_to_str_python(type);
throw SemanticError(
Expand Down