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

Added support for freeing variables in the ASR symbolic pass #2268

Merged
merged 3 commits into from
Aug 13, 2023

Conversation

anutosh491
Copy link
Collaborator

The framework here is simple

  1. Anytime we realize that basic_new_stack would be required to allocate memory , that's enough information to confirm that the corresponding variable would also have to be freed at the end of the program. Hence I've declared basic_free_stack accordingly.
  2. We run a loop through symbolic_vars which has all CPtr symbols that have been added to the function's symbol table.
  3. We can loop through it and keep adding subroutine calls to the function body of main0()

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Aug 12, 2023

I have a doubt . I'm seeing some cases work, for example

def main():
   x: S = pi

Through the pass I can see this block

                                    (SubroutineCall
                                        2 basic_free_stack
                                        2 basic_free_stack
                                        [((Var 3 x))]
                                        ()
                                    )

But when I have

def main():
    print(Symbol("y"))

I get the following error

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --show-asr --pass=symbolic examples/expr2.py 
ASR verify pass error: ASR verify: The symbol table was not found in the scope of `symtab`.

ASR verify pass error: ASR verify: Var::m_v `stack0` cannot point outside of its symbol table
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/anutosh491/lpython/lpython/src/bin/lpython", in _start()
  Binary file "/lib/x86_64-linux-gnu/libc.so.6", in __libc_start_main()
  File "/home/anutosh491/lpython/lpython/src/bin/lpython.cpp", line 1797, in ??
    return emit_asr(arg_file, lpython_pass_manager, runtime_library_dir,
  File "/home/anutosh491/lpython/lpython/src/bin/lpython.cpp", line 252, in ??
    pass_manager.apply_passes(al, asr, pass_options, diagnostics);
  File "/home/anutosh491/lpython/lpython/src/libasr/pass/pass_manager.h", line 280, in LCompilers::PassManager::apply_passes(Allocator&, LCompilers::ASR::TranslationUnit_t*, LCompilers::PassOptions&, LCompilers::diag::Diagnostics&)
    apply_passes(al, asr, _user_defined_passes, pass_options,
LCompilersException: Verify failed in the pass: symbolic

cc @Thirumalai-Shaktivel , I did some debugging but not exactly sure where I might be going wrong .

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Aug 12, 2023

Here is the issue for the ASR verify failure:

ASR
symbol_set:
   (Function
      (SymbolTable
            8
            {
               s:
                  (Variable
                        8
                        s
                        []
                        In
                        ()
                        ()
                        Default
                        (Character 1 -2 ())
                        ()
                        BindC
                        Public
                        Required
                        .true.
                  ),
               x:
                  (Variable
                        8
                        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 8 x)
      (Var 8 s)]
      [(SubroutineCall 						<---------------------
            2 basic_free_stack
            2 basic_free_stack
            [((Var 3 x))]
            ()
      )]
      ()
      Public
      .false.
      .false.
      "symengine/cwrapper.h"
   )

@anutosh491 anutosh491 mentioned this pull request Aug 12, 2023
4 tasks
Thirumalai-Shaktivel

This comment was marked as duplicate.

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

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

I left one suggestion, otherwise this LGTM!

@anutosh491
Copy link
Collaborator Author

I've addressed the suggested change , should be ready now.

@anutosh491 anutosh491 requested a review from certik August 12, 2023 13:48
@certik
Copy link
Contributor

certik commented Aug 13, 2023

Thanks! I think it looks good. I rebased it to make sure it still works and I set it to merge.

@certik certik merged commit 5b945fc into lcompilers:main Aug 13, 2023
9 checks passed
@anutosh491 anutosh491 deleted the GSoC_PR_14 branch August 13, 2023 06:18
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.

None yet

3 participants