Skip to content

Conversation

anutosh491
Copy link
Collaborator

As I've been working on #1607 (comment) , which requires LPython to support IntrinsicFunction as expr . It seems it would also help us out at other places like #1606 (review) .

@anutosh491 anutosh491 marked this pull request as draft March 24, 2023 07:24
@Thirumalai-Shaktivel
Copy link
Collaborator

@czgdp1807
Copy link
Collaborator

@Thirumalai-Shaktivel Please go ahead and push all the necessary changes here. Please add LPython tests also if necessary.

@Thirumalai-Shaktivel Thirumalai-Shaktivel self-assigned this Mar 24, 2023
@Thirumalai-Shaktivel
Copy link
Collaborator

I will try to complete this tomorrow. @anutosh491 thanks for opening a PR for this.

@anutosh491
Copy link
Collaborator Author

I will try to complete this tomorrow. @anutosh491 thanks for opening a PR for this.

Sure, would be a good addition to have here !

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Mar 26, 2023

I implemented IntrinsicFunction for the built-in abs.
We create the IntrinsicFunction in AST->ASR and intrinsic_function pass we import the function from lpython_builtin.py and use that function to compute the value.

Review this commit: 80c59fd and bc505b3

What are your thoughts on this? @certik @czgdp1807 @Smit-create

@Thirumalai-Shaktivel Thirumalai-Shaktivel force-pushed the Add_Intrinsic branch 2 times, most recently from dd498f9 to 4783466 Compare March 26, 2023 14:07
@anutosh491
Copy link
Collaborator Author

I am just curious about a few things , which I would like to point out here .

  1. If we generate the ASR for something like the following
def main0():
    print(sin(0.5))

main0()

It goes like this


                                        [(IntrinsicFunction
                                             Sin
                                            [(RealConstant
                                                0.500000
                                                (Real 8 [])
                                            )]
                                            0
                                            (Real 8 [])
                                            (RealConstant
                                                0.479426
                                                (Real 8 [])
                                            )
                                        )]

Here do we expect Sin to be displayed too ? I mean I just thought we would need the intrinsic_id to be displayed according to IntrinsicFunction(int intrinsic_id, expr* args, int overload_id, ttype type, expr? value)

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Mar 27, 2023

  1. Also something like the following gives an error
def main0():
    sin(0.5)     # whereas calling through the print function works !

main0()

or just

sin(0.5)

gives .

(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython --show-asr examples/expr2.py --indent
semantic error: Function 'sin' is not declared
 --> examples/expr2.py:2:5
  |
2 |     sin(0.5)
  |     ^^^^^^^^

It's counterpart, LFortran works perfectly in this case !

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Mar 27, 2023

Here do we expect Sin to be displayed too ?

Yup, intrinsic_id is converted to display Sin
See:

https://github.com/lfortran/lfortran/blob/36f604cce47f879594b3f1852274409257850222/src/libasr/pass/intrinsic_function_registry.h#L298-L313

Intrinsic_id is just an integral value, But I think Sin makes more sense instead of just a number.

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Mar 27, 2023

#1616 (comment)

I'm not sure about this, but I think the intrinsicFunction is designed for built-in function. So, functions like abs, range, list, ... can be represented in intrinsicfunction. Whereas, sin is imported from the math Module (from math import sin). So, I think there is no need for intrinsicfunction.

@certik @czgdp1807 Am I correct?

@Thirumalai-Shaktivel
Copy link
Collaborator

def main0():
   sin(0.5)     # whereas calling through the print function works !

main0()

print(sin(0.5)), I think this should not work, if it works then we have to raise an error.

$ python examples/expr2.py
Traceback (most recent call last):
  File "/Users/thirumalai/Open_Source/lpython/examples/expr2.py", line 4, in <module>
    main0()
  File "/Users/thirumalai/Open_Source/lpython/examples/expr2.py", line 2, in main0
    print(sin(0.5))
NameError: name 'sin' is not defined. Did you mean: 'bin'?

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Mar 27, 2023

Ohhh so print(sin(0.5)) doesn't work for you , works for me though !

(lf) anutosh491@spbhat68:~/lpython/lpython$ cat examples/expr2.py
def main0():
    print(sin(0.5))

main0()

(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython --show-asr examples/expr2.py
(TranslationUnit (SymbolTable 1 {_global_symbols: (Module (SymbolTable 108 {_lpython_main_program: (Function (SymbolTable 107 {}) _lpython_main_program (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [main0] [] [(SubroutineCall 108 main0 () [] ())] () Public .false. .false.), main0: (Function (SymbolTable 2 {}) main0 (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [] [] [(Print () [(IntrinsicFunction Sin [(RealConstant 0.500000 (Real 8 []))] 0 (Real 8 []) (RealConstant 0.479426 (Real 8 [])))] () ())] () Public .false. .false.)}) _global_symbols [] .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 106 {_lpython_main_program: (ExternalSymbol 106 _lpython_main_program 108 _lpython_main_program _global_symbols [] _lpython_main_program Public)}) main_program [_global_symbols] [(SubroutineCall 106 _lpython_main_program () [] ())])}) [])

(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython examples/expr2.py
4.79425538604203005e-01

Edit : Ahh my bad , I realize that's the error which we should be returning , what you've pointed out !

@certik
Copy link
Contributor

certik commented Mar 27, 2023

I think we have to intercept math.sin (for just scalars) as well as numpy.sin (for both scalars and arrays) and create IntrinsicFunction for them.

@certik
Copy link
Contributor

certik commented Mar 27, 2023

I see, but because currently the math ones are defined in our LPython runtime library in Python, then we can leave those I guess for now, and port them to IntrinsicFunction later.

};

static inline bool is_intrinsic_function(const std::string& name) {
return intrinsic_function_by_name_db.find(name) != intrinsic_function_by_name_db.end();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put a check for "sin" here, and ignore everything else for now, and use existing LPython mechanism for those. Let's get "sin" working in this PR, merge it. Then we can add more functions, one by one.

@@ -6106,7 +6123,20 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}

if (!s) {
if (intrinsic_procedures.is_intrinsic(call_name)) {
if (ASRUtils::IntrinsicFunctionRegistry::is_intrinsic_function(call_name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just do it here instead, that way libasr stays the same.

"If sin, then something, otherwise original."

@certik
Copy link
Contributor

certik commented Mar 29, 2023

LPython doesn't have sin yet, so I would do "abs", as you did, but let's do it in LFortran first, get everything working there, that should be relatively easy. Then port it here, and use it here.

@Thirumalai-Shaktivel
Copy link
Collaborator

I'm currently working on this: #1616 (comment). After completing that I will finish this PR.
I will try to complete everything by today.

@czgdp1807
Copy link
Collaborator

Great. After that I will do the sync (on Tuesday).

@certik
Copy link
Contributor

certik commented Apr 3, 2023

@Thirumalai-Shaktivel so abs (lfortran/lfortran#1480) is in. This PR can now be finished.

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Apr 4, 2023

Almost completed, @czgdp1807 after that you can review and merge this.

@Thirumalai-Shaktivel
Copy link
Collaborator

This PR is ready!
@certik @czgdp1807 kindly review and share your feedback.

There are some changes in libasr here, I will submit another PR for it there.

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as ready for review April 4, 2023 15:49
@czgdp1807
Copy link
Collaborator

There are some changes in libasr here, I will submit another PR for it there.

Please do it right away. We will merge both PRs together. I hope they aren't big changes?

@Thirumalai-Shaktivel
Copy link
Collaborator

Thanks, for the review.
I created a PR in LFortran: lfortran/lfortran#1493

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

I think that this is fine.

@czgdp1807 czgdp1807 enabled auto-merge (squash) April 5, 2023 12:32
@czgdp1807 czgdp1807 merged commit b35e744 into lcompilers:main Apr 5, 2023
virendrakabra14 pushed a commit to virendrakabra14/lpython that referenced this pull request Apr 8, 2023
Co-authored-by: Thirumalai-Shaktivel <thirumalaishaktivel@gmail.com>
@anutosh491 anutosh491 deleted the Add_Intrinsic branch May 20, 2023 04:30
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.

4 participants