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

Initial setup for test_gruntz #2423

Closed
wants to merge 1 commit into from
Closed

Conversation

anutosh491
Copy link
Collaborator

This is the first piece of code that we should support through LPython.

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Nov 14, 2023

We have the ASR correctly in place (I think so). Pasting the whole ASR below. Would be great if somebody could verify it !

;; ASR after applying the pass: insert_deallocate
(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_stmts:
                                (Function
                                    (SymbolTable
                                        5
                                        {
                                            
                                        })
                                    __main__global_stmts
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    [test_mrv]
                                    []
                                    [(SubroutineCall
                                        2 test_mrv
                                        2 test_mrv
                                        []
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            basic_free_stack:
                                (Function
                                    (SymbolTable
                                        9
                                        {
                                            x:
                                                (Variable
                                                    9
                                                    x
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                )
                                        })
                                    basic_free_stack
                                    (FunctionType
                                        [(CPtr)]
                                        ()
                                        BindC
                                        Interface
                                        "basic_free_stack"
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    [(Var 9 x)]
                                    []
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    "symengine/cwrapper.h"
                                ),
                            basic_has_symbol:
                                (Function
                                    (SymbolTable
                                        7
                                        {
                                            _lpython_return_variable:
                                                (Variable
                                                    7
                                                    _lpython_return_variable
                                                    []
                                                    ReturnVar
                                                    ()
                                                    ()
                                                    Default
                                                    (Logical 4)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            x:
                                                (Variable
                                                    7
                                                    x
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                ),
                                            y:
                                                (Variable
                                                    7
                                                    y
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                )
                                        })
                                    basic_has_symbol
                                    (FunctionType
                                        [(CPtr)
                                        (CPtr)]
                                        (Logical 4)
                                        BindC
                                        Interface
                                        "basic_has_symbol"
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    [(Var 7 x)
                                    (Var 7 y)]
                                    []
                                    (Var 7 _lpython_return_variable)
                                    Public
                                    .false.
                                    .false.
                                    "symengine/cwrapper.h"
                                ),
                            basic_new_stack:
                                (Function
                                    (SymbolTable
                                        8
                                        {
                                            x:
                                                (Variable
                                                    8
                                                    x
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                )
                                        })
                                    basic_new_stack
                                    (FunctionType
                                        [(CPtr)]
                                        ()
                                        BindC
                                        Interface
                                        "basic_new_stack"
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    [(Var 8 x)]
                                    []
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    "symengine/cwrapper.h"
                                ),
                            mrv:
                                (Function
                                    (SymbolTable
                                        3
                                        {
                                            _lpython_return_variable:
                                                (Variable
                                                    3
                                                    _lpython_return_variable
                                                    []
                                                    Out
                                                    ()
                                                    ()
                                                    Default
                                                    (Tuple
                                                        [(Dict
                                                            (CPtr)
                                                            (CPtr)
                                                        )
                                                        (CPtr)]
                                                    )
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            e:
                                                (Variable
                                                    3
                                                    e
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            empty_dict:
                                                (Variable
                                                    3
                                                    empty_dict
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Dict
                                                        (CPtr)
                                                        (CPtr)
                                                    )
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            x:
                                                (Variable
                                                    3
                                                    x
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    mrv
                                    (FunctionType
                                        [(CPtr)
                                        (CPtr)
                                        (Tuple
                                            [(Dict
                                                (CPtr)
                                                (CPtr)
                                            )
                                            (CPtr)]
                                        )]
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    [basic_has_symbol]
                                    [(Var 3 e)
                                    (Var 3 x)
                                    (Var 3 _lpython_return_variable)]
                                    [(If
                                        (LogicalNot
                                            (FunctionCall
                                                2 basic_has_symbol
                                                2 basic_has_symbol
                                                [((Var 3 e))
                                                ((Var 3 x))]
                                                (Logical 4)
                                                ()
                                                ()
                                            )
                                            (Logical 4)
                                            ()
                                        )
                                        [(=
                                            (Var 3 empty_dict)
                                            (DictConstant
                                                []
                                                []
                                                (Dict
                                                    (CPtr)
                                                    (CPtr)
                                                )
                                            )
                                            ()
                                        )
                                        (=
                                            (Var 3 _lpython_return_variable)
                                            (TupleConstant
                                                [(Var 3 empty_dict)
                                                (Var 3 x)]
                                                (Tuple
                                                    [(Dict
                                                        (CPtr)
                                                        (CPtr)
                                                    )
                                                    (CPtr)]
                                                )
                                            )
                                            ()
                                        )
                                        (Return)]
                                        [(ErrorStop
                                            ()
                                        )]
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            symbol_set:
                                (Function
                                    (SymbolTable
                                        10
                                        {
                                            s:
                                                (Variable
                                                    10
                                                    s
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (Character 1 -2 ())
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                ),
                                            x:
                                                (Variable
                                                    10
                                                    x
                                                    []
                                                    In
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .true.
                                                )
                                        })
                                    symbol_set
                                    (FunctionType
                                        [(CPtr)
                                        (Character 1 -2 ())]
                                        ()
                                        BindC
                                        Interface
                                        "symbol_set"
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    [(Var 10 x)
                                    (Var 10 s)]
                                    []
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    "symengine/cwrapper.h"
                                ),
                            test_mrv:
                                (Function
                                    (SymbolTable
                                        4
                                        {
                                            __libasr__created__var__0__func_call_res:
                                                (Variable
                                                    4
                                                    __libasr__created__var__0__func_call_res
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Tuple
                                                        [(Dict
                                                            (CPtr)
                                                            (CPtr)
                                                        )
                                                        (CPtr)]
                                                    )
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            _x:
                                                (Variable
                                                    4
                                                    _x
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 8)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            _y:
                                                (Variable
                                                    4
                                                    _y
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 8)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            ans:
                                                (Variable
                                                    4
                                                    ans
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Tuple
                                                        [(Dict
                                                            (CPtr)
                                                            (CPtr)
                                                        )
                                                        (CPtr)]
                                                    )
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            x:
                                                (Variable
                                                    4
                                                    x
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            y:
                                                (Variable
                                                    4
                                                    y
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (CPtr)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    test_mrv
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    [basic_new_stack
                                    symbol_set
                                    mrv
                                    basic_free_stack]
                                    []
                                    [(=
                                        (Var 4 _x)
                                        (Cast
                                            (IntegerConstant 0 (Integer 4))
                                            IntegerToInteger
                                            (Integer 8)
                                            (IntegerConstant 0 (Integer 8))
                                        )
                                        ()
                                    )
                                    (=
                                        (Var 4 x)
                                        (PointerNullConstant
                                            (CPtr)
                                        )
                                        ()
                                    )
                                    (=
                                        (Var 4 x)
                                        (PointerToCPtr
                                            (GetPointer
                                                (Var 4 _x)
                                                (Pointer
                                                    (Integer 8)
                                                )
                                                ()
                                            )
                                            (CPtr)
                                            ()
                                        )
                                        ()
                                    )
                                    (SubroutineCall
                                        2 basic_new_stack
                                        2 basic_new_stack
                                        [((Var 4 x))]
                                        ()
                                    )
                                    (=
                                        (Var 4 _y)
                                        (Cast
                                            (IntegerConstant 0 (Integer 4))
                                            IntegerToInteger
                                            (Integer 8)
                                            (IntegerConstant 0 (Integer 8))
                                        )
                                        ()
                                    )
                                    (=
                                        (Var 4 y)
                                        (PointerNullConstant
                                            (CPtr)
                                        )
                                        ()
                                    )
                                    (=
                                        (Var 4 y)
                                        (PointerToCPtr
                                            (GetPointer
                                                (Var 4 _y)
                                                (Pointer
                                                    (Integer 8)
                                                )
                                                ()
                                            )
                                            (CPtr)
                                            ()
                                        )
                                        ()
                                    )
                                    (SubroutineCall
                                        2 basic_new_stack
                                        2 basic_new_stack
                                        [((Var 4 y))]
                                        ()
                                    )
                                    (SubroutineCall
                                        2 symbol_set
                                        2 symbol_set
                                        [((Var 4 x))
                                        ((StringConstant
                                            "x"
                                            (Character 1 1 ())
                                        ))]
                                        ()
                                    )
                                    (SubroutineCall
                                        2 symbol_set
                                        2 symbol_set
                                        [((Var 4 y))
                                        ((StringConstant
                                            "y"
                                            (Character 1 1 ())
                                        ))]
                                        ()
                                    )
                                    (SubroutineCall
                                        2 mrv
                                        2 mrv
                                        [((Var 4 y))
                                        ((Var 4 x))
                                        ((Var 4 __libasr__created__var__0__func_call_res))]
                                        ()
                                    )
                                    (=
                                        (Var 4 ans)
                                        (Var 4 __libasr__created__var__0__func_call_res)
                                        ()
                                    )
                                    (SubroutineCall
                                        2 basic_free_stack
                                        2 basic_free_stack
                                        [((Var 4 x))]
                                        ()
                                    )
                                    (SubroutineCall
                                        2 basic_free_stack
                                        2 basic_free_stack
                                        [((Var 4 y))]
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                ),
            main_program:
                (Program
                    (SymbolTable
                        6
                        {
                            __main__global_stmts:
                                (ExternalSymbol
                                    6
                                    __main__global_stmts
                                    2 __main__global_stmts
                                    __main__
                                    []
                                    __main__global_stmts
                                    Public
                                )
                        })
                    main_program
                    [__main__]
                    [(SubroutineCall
                        6 __main__global_stmts
                        6 __main__global_stmts
                        []
                        ()
                    )]
                )
        })
    []
)

@anutosh491
Copy link
Collaborator Author

There are few things we need to discuss though. Also I'll give some context for anyone who would like to understand more (maybe cc @Thirumalai-Shaktivel @Shaikh-Ubaid )

  1. SymPy's mrv function works as follows. It returns a Subset and a rewritten expression
  • A Subset is nothing but an inherited dictionary that stores the most rapidly varying subexpressions. For eg in the example below for the expression exp(x), exp(x) itself is the most rapidly varying subexpression.
  • Once we have the most rapidly varying subexpression out of the all (there can be multiple) , we replace the original expression with a dummy value that we have provided in our dictionary and replace the original expression. A Dummy variable for now can be thought of as a placeholder for a Symbol.
>>> mrv(exp(x), x)
({exp(x): _Dummy_40}, {}, _Dummy_40)

To start with I think we might only be interested in capturing the mrv (most rapidly varying) subexpression which in our case is exp(x). SymPy does a bit more but I would say those would only be required for the complex cases.

A most rapidly varying subexpression for an expression can be thought of a subexpression that grows the most rapidly in the expression. The general order would be const < log(x) < x**n < 2**x or exp(x) < factorial(x) < x**x.
So when we say mrv(exp(x) + x, x), we know exp(x) grows faster than x. Hence we have

 >>> mrv(exp(x) + x, x)
({exp(x): _Dummy_51}, {}, _Dummy_51 + x)

@certik
Copy link
Contributor

certik commented Nov 14, 2023

Does the test that you added work? If so, let's register it.

If not, let's also add a test that fails in master, but now works.

@certik certik marked this pull request as draft November 14, 2023 15:09
@anutosh491
Copy link
Collaborator Author

These are the errors I am getting through both the concerned backends

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=c integration_tests/test_gruntz.py 
code generation error: Type dict[CPtr, CPtr] not supported yet.

Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=llvm integration_tests/test_gruntz.py 
The stack address was not found in any shared library or the main program, the stack is probably corrupted. Aborting.
The stack address was not found in any shared library or the main program, the stack is probably corrupted. Aborting.
The stack address was not found in any shared library or the main program, the stack is probably corrupted. Aborting.

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Nov 16, 2023

We could also actually start with something like the mmrv function as I would say we should only be concerned with the the set of most rapidly varying subexpressions to start with. We can focus on the rewrites once we are able to extract the subexpressions. Something like this https://github.com/sympy/sympy/blob/b6e81e7c3f3860c7e826018e587531a8622fcc4e/sympy/series/tests/test_gruntz.py#L249

This would change the example to be a bit more easier. Something like this I reckon

from lpython import S
from sympy import Symbol

def mmrv(e: S, x: S) -> set[S]:
    if not e.has(x):
        empty_set: set[S]
        return empty_set
    else:
        raise

def test_mmrv():
    x: S = Symbol("x")
    y: S = Symbol("y")
    ans: set[S] = mmrv(y, x)

test_mmrv()

So we could deal with something like set[S] rather than tuple[dict[S, S], S]
I would expect the same errors here as above as I don't think we are supporting set[CPtr] as of now through the C backend and I see us getting the same error through the llvm backend too (trying to figure that out with Thirumalai. Hopefully we can figure out something )

@certik
Copy link
Contributor

certik commented Nov 17, 2023

Perfect. Yes, start with something that works. Then report issues for features that we need, and we'll keep fixing them, one by one, and iteratively improve the capability.

@certik
Copy link
Contributor

certik commented Nov 29, 2023

To get started, instead of set[S], let's use list[S] and then write auxiliary functions like set_add(s : InOut[list[S]], e: S) -> None, that would go over the list s one by one, compare against e and if it is not there, it will append it at the end.

@anutosh491
Copy link
Collaborator Author

Closing as this was addressed through #2437

@anutosh491 anutosh491 closed this Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants