-
Notifications
You must be signed in to change notification settings - Fork 167
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
certik
merged 5 commits into
lcompilers:main
from
Shaikh-Ubaid:llvm_init_cpython_interop_example
Jul 26, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0a7813
LLVM: Support linking exec to python library
Shaikh-Ubaid abcbc35
ASR: Support CPtr cast to bool()
Shaikh-Ubaid 5161129
CMake: Support COPY_TO_BIN in CMakelists
Shaikh-Ubaid bf75953
TEST: Define and use llvm_py as backend
Shaikh-Ubaid 2f174ec
TEST: Add cpython_interop initial example for LLVM
Shaikh-Ubaid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def my_f(): | ||
print("hello from python") | ||
return 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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**)
There was a problem hiding this comment.
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.