Skip to content

Pure procedures #11

Open
Open
@ppenzin

Description

@ppenzin

It is a Fortran 95 feature. PURE attribute added to a function or subroutine means that it is side-effect free, output only depends on its arguments (in case of function it is also not allowed to write to the arguments). From the standard:

... a pure subprogram shall not contain any operation
that could conceivably result in an assignment or pointer assignment to a common variable, a
variable accessed by use or host association, or an INTENT (IN) dummy argument; nor shall
a pure subprogram contain any operation that could conceivably perform any external file
I/O or STOP operation. Note the use of the word conceivably; it is not sufficient for a pure
subprogram merely to be side-effect free in practice. For example, a function that contains an
assignment to a global variable but in a block that is not executed in any invocation of the
function is nevertheless not a pure function. The exclusion of functions of this nature is
required if strict compile-time checking is to be used.

Pure functions can be used in FORALL construct; also intrinsic functions are expected to be pure.

Example:

$ cat pure.f95 
pure function f(x) result(y)
  integer, intent(in) :: x
  integer :: y
  y = x * 5
end function
$ fort -fsyntax-only pure.f95 
pure.f95:1:6: error: expected '='
pure function f(x) result(y)
     ^
pure.f95:2:10: error: expected '='
  integer, intent(in) :: x
         ^
pure.f95:3:11: error: expected '='
  integer :: y
          ^
pure.f95:5:1: error: expected 'end program'
end function
^
<unknown>:0: note: to match this 'program'
$ gfortran -fsyntax-only pure.f95 
$

Checks will be needed to catch statements not allowed inside of a pure function. Also, pure function should produce IR function with appropriate metadata (something like readonly or argmemonly).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions