psyclone.psyir.transformations.arrayassignment2loops_trans#

Module providing a transformation from a PSyIR Array Assignment to explicit PSyIR Loops. This could be useful for e.g. performance reasons, to enable further transformations e.g. loop fusion or if the back-end does not support array ranges.

Classes#

class psyclone.psyir.transformations.arrayassignment2loops_trans.ArrayAssignment2LoopsTrans[source]#

Provides a transformation from a PSyIR Array Range to a PSyIR Loop. For example:

>>> from psyclone.psyir.frontend.fortran import FortranReader
>>> from psyclone.psyir.nodes import Assignment
>>> from psyclone.psyir.transformations import ArrayAssignment2LoopsTrans
>>> code = """
... subroutine sub()
...   real :: tmp(10)
...   tmp(:) = tmp(:) + 3
... end subroutine sub"""
>>> psyir = FortranReader().psyir_from_source(code)
>>> assignment = psyir.walk(Assignment)[0]
>>> trans = ArrayAssignment2LoopsTrans()
>>> trans.apply(assignment)
>>> print(psyir.debug_string())
subroutine sub()
  real, dimension(10) :: tmp
  integer :: idx

  do idx = LBOUND(tmp, dim=1), UBOUND(tmp, dim=1), 1
    tmp(idx) = tmp(idx) + 3
  enddo

end subroutine sub

By default the transformation will reject character arrays, though this can be overriden by setting the ‘allow_string’ option to True. Note that PSyclone expresses syntax such as character(LEN=100) as UnsupportedFortranType, and this transformation will convert unknown or unsupported types to loops.

Inheritance

Inheritance diagram of ArrayAssignment2LoopsTrans
apply(node, options=None)[source]#

Apply the transformation to the specified array assignment node. Each range node within the assignment is replaced with an explicit loop. The bounds of the loop are determined from the bounds of the array range on the left hand side of the assignment.

Parameters:
  • node (psyclone.psyir.nodes.Assignment) – an Assignment node.

  • options["allow_string"] (bool) – whether to allow the transformation on a character type array range. Defaults to False.

  • options["verbose"] (bool) – log the reason the validation failed, at the moment with a comment in the provided PSyIR node.

validate(node, options=None)[source]#

Perform various checks to ensure that it is valid to apply the ArrayAssignment2LoopsTrans transformation to the supplied PSyIR Node.

By default the validate function will throw an TransofmrationError on character arrays, though this can be overriden by setting the allow_string option to True. Note that PSyclone expresses syntax such as character(LEN=100) as UnsupportedFortranType, and this transformation will convert unknown or unsupported types to loops.

Parameters:
  • node (psyclone.psyir.nodes.Assignment) – the node that is being checked.

  • options (Optional[Dict[str, Any]]) – a dictionary with options for transformations

  • options["allow_string"] (bool) – whether to allow the transformation on a character type array range. Defaults to False.

  • options["verbose"] (bool) – log the reason the validation failed, at the moment with a comment in the provided PSyIR node.

Raises:
  • TransformationError – if the node argument is not an Assignment.

  • TransformationError – if the node argument is an Assignment whose left hand side is not an ArrayReference.

  • TransformationError – if the node argument is an Assignment whose left hand side is an ArrayReference that does not have Range specifying the access to at least one of its dimensions.

  • TransformationError – if two or more of the loop ranges in the assignment are different or are not known to be the same.

  • TransformationError – if node contains a character type child and the allow_strings option is not set.

static validate_no_char(node, message, options)[source]#

Check that there is no character variable accessed in the sub-tree with the supplied node at its root.

Parameters:
  • node (Node) – the root node to check for character assignments.

  • message (str) – the message to use if a character assignment is found.

  • options (dict) – any options that apply to this check.

  • options["verbose"] (bool) – log the reason the validation failed, at the moment with a comment in the provided PSyIR node.

Raises:

TransformationError – if the supplied node contains a child of character type.

Return type:

None