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

Conversation

Shaikh-Ubaid
Copy link
Collaborator

@Shaikh-Ubaid Shaikh-Ubaid commented Jul 25, 2023

@Shaikh-Ubaid Shaikh-Ubaid force-pushed the llvm_init_cpython_interop_example branch 2 times, most recently from f249925 to f23a8d8 Compare July 25, 2023 20:26
@Shaikh-Ubaid Shaikh-Ubaid requested a review from certik July 25, 2023 22:29
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.

@Shaikh-Ubaid Shaikh-Ubaid force-pushed the llvm_init_cpython_interop_example branch 3 times, most recently from a649e3d to 8d769b6 Compare July 26, 2023 05:37
@Shaikh-Ubaid Shaikh-Ubaid force-pushed the llvm_init_cpython_interop_example branch from 8d769b6 to 2f174ec Compare July 26, 2023 06:20
@certik certik merged commit 5905252 into lcompilers:main Jul 26, 2023
@Shaikh-Ubaid Shaikh-Ubaid deleted the llvm_init_cpython_interop_example branch July 26, 2023 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants