psyclone.psyir.transformations.replace_reference_by_literal_trans#

Module providing a transformation that replace PsyIR Node representing a static, constant value with a Literal Node when possible.

Classes#

class psyclone.psyir.transformations.replace_reference_by_literal_trans.ReplaceReferenceByLiteralTrans[source]#

This transformation takes a psyir Routine and replace all Reference psyir Nodes by Literal if the corresponding symbol from the symbol table is constant. That is to say the symbol is a Fortran parameter. NOTE: this transformation does not handle parent scopes recursively yet. For example:

>>> from psyclone.psyir.backend.fortran import FortranWriter
>>> from psyclone.psyir.symbols import INTEGER_TYPE
>>> from psyclone.psyir.transformations import (
    ReplaceReferenceByLiteralTrans)
>>> source = """program test
... use mymod
... type(my_type):: t1, t2, t3, t4
... integer, parameter :: x=3, y=12, z=13
... integer, parameter :: u1=1, u2=2, u3=3, u4=4
... integer i, invariant, ic1, ic2, ic3
... real, dimension(10) :: a
... invariant = 1
... do i = 1, 10
...     t1%a = z
...     a(ic1) = u1+(ic1+x)*ic1
...     a(ic2) = u2+(ic2+y)*ic2
...     a(ic3) = u3+(ic3+z)*ic3
...     a(t1%a) = u4+(t1%a+u4*z)*t1%a
... end do
... end program test"""
>>> fortran_writer = FortranWriter()
>>> fortran_reader = FortranReader()
>>> psyir = fortran_reader.psyir_from_source(source)
>>> routine = psyir.walk(Routine)[0]
>>> rrbl = ReplaceReferenceByLiteralTrans()
>>> rrbl.apply(routine)
>>> written_code = fortran_writer(routine)
>>> print(written_code)
program test
  use mymod
  integer, parameter :: x = 3
  integer, parameter :: y = 12
  integer, parameter :: z = 13
  integer, parameter :: u1 = 1
  integer, parameter :: u2 = 2
  integer, parameter :: u3 = 3
  integer, parameter :: u4 = 4
  type(my_type) :: t1
  type(my_type) :: t2
  type(my_type) :: t3
  type(my_type) :: t4
  integer :: i
  integer :: invariant
  integer :: ic1
  integer :: ic2
  integer :: ic3
  real, dimension(10) :: a

  invariant = 1
  do i = 1, 10, 1
    t1%a = 13
    a(ic1) = 1 + (ic1 + 3) * ic1
    a(ic2) = 2 + (ic2 + 12) * ic2
    a(ic3) = 3 + (ic3 + 13) * ic3
    a(t1%a) = 4 + (t1%a + 4 * 13) * t1%a
  enddo

end program test

Inheritance

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

Applies the transformation to a Routine node: * First update a dictionary (param_table) with the Literal of constant (parameter) symbol from node.parent symbol_table, and from node.symbol_table. * Second, use this updated param_table to replace reference in node psyir_tree with the corresponsing Literal. * Third, use this updated param_table to replace reference in node symbol_table DataSymbol array’s dimensions with the corresponding Literal.

Note: If an update cannot be performed for any reason then the substitution is skipped and a comment starting with ‘Psyclone(ReplaceReferenceByLiteral):’ is added to the generated code.

Parameters:
  • node (Routine) – node on which the transformation is applied

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

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

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

Parameters:
  • node – the node that is being checked.

  • options (_type_, optional) – not used, defaults to None

Raises:

TransformationError – if the node argument is not a Routine.