psyclone.psyir.transformations.replace_induction_variables_trans

Module providing a transformation that removes induction variables from a loop.

Classes

class psyclone.psyir.transformations.replace_induction_variables_trans.ReplaceInductionVariablesTrans

Move all supported induction variables out of the loop, and replace their usage inside the loop. For example:

>>> from psyclone.psyir.frontend.fortran import FortranReader
>>> from psyclone.psyir.nodes import Loop
>>> from psyclone.psyir.transformations import             ReplaceInductionVariablesTrans
>>> from psyclone.psyir.backend.fortran import FortranWriter
>>> psyir = FortranReader().psyir_from_source("""
... subroutine sub()
...     integer :: i, im, ic, tmp(100)
...     do i=1, 100
...         im = i - 1
...         ic = 2
...         tmp(i) = ic * im
...     enddo
... end subroutine sub""")
>>> loop = psyir.walk(Loop)[0]
>>> ReplaceInductionVariablesTrans().apply(loop)
>>> print(FortranWriter()(psyir))
subroutine sub()
  integer :: i
  integer :: im
  integer :: ic
  integer, dimension(100) :: tmp

  do i = 1, 100, 1
    tmp(i) = 2 * (i - 1)
  enddo
  ic = 2
  im = i - 1 - 1

end subroutine sub

The replaced induction variables assignments are added after the loop, so these variables will have the correct value if they are used elsewhere.

The following restrictions apply for the assignment to an induction variable:

  • the variable must be a scalar (i.e. no array access at all, not even a constant like a(3) or a%b(3)%c)

  • none of variables on the right-hand side can be written in the loop body (the loop variable is written in the Loop statement, not in the body, so it can be used).

  • Only intrinsic function calls are allowed on the RHS (since they are known to be elemental)

  • the assigned variable must not be read before the assignment.

  • the assigned variable cannot occur on the right-hand side (e.g. k = k + 3).

  • there must be only one assignment to this induction variable.

Inheritance

Inheritance diagram of ReplaceInductionVariablesTrans
apply(node, options=None)

Apply the ReplaceInductionVariablesTrans transformation to the specified node. The node must be a loop. In case of nested loops, the transformation might need to be applied several times, from the inner-most loop outwards.

Parameters:

node (psyclone.psyir.nodes.Loop) – a Loop node.

validate(node, options=None)

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

Parameters:

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

Raises:

TransformationError – if the node argument is not a Loop.