Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to handle deallocatable function return values #494

Open
@certik

Description

@certik

An example:

def g() -> list[i32]:
    ...
 
def f() -> list[i32]:
    a = [1, 2, 3]
    z = a+g()
    return z

Here, we need to deallocate the temporary result of the function g().

One general way is to assign the result of a function to a temporary variable first (either always do it, or only when the result is allocatable, or possibly an array). Like this:

def g() -> list[i32]:
    ...
 
def f() -> list[i32]:
    a = [1, 2, 3]
    tmp1 = g()
    z = a+tmp1
    Deallocate([tmp1, a])
    return z

It's a question whether we should do this for all functions all the time, or only for functions that return allocatable arrays/lists.

For arrays, this approach also allows to write an ASR transformation that transforms functions into subroutines:

def g() -> list[i32]:
    ...
 
def f() -> list[i32]:
    a = [1, 2, 3]
    tmp1: Allocatable[i32[:]]
    g2(tmp1)
    z = a+tmp1
    Deallocate([tmp1,a])
    return z

Which makes it easy on the LLVM backend to implement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions