36 '''Module providing a transformation that transforms all constant
37 index accesses to an array (i.e. ones that do not contain loop
38 iterators) to single-trip loops.
47 import TransformationError
51 '''Provides a transformation from a PSyIR Assignment containing
52 constant index accesses to an array into single-trip loops. For
55 >>> from psyclone.psyir.transformations import AllArrayAccess2LoopTrans
56 >>> from psyclone.psyir.backend.fortran import FortranWriter
57 >>> from psyclone.psyir.frontend.fortran import FortranReader
58 >>> from psyclone.psyir.nodes import Assignment
59 >>> code = ("program example\\n"
60 ... " real a(10,10), b(10,10)\\n"
61 ... " integer :: n\\n"
62 ... " a(1,n-1) = b(1,n-1)\\n"
63 ... "end program example\\n")
64 >>> psyir = FortranReader().psyir_from_source(code)
65 >>> assignment = psyir.walk(Assignment)[0]
66 >>> AllArrayAccess2LoopTrans().apply(assignment)
67 >>> print(FortranWriter()(psyir))
69 real, dimension(10,10) :: a
70 real, dimension(10,10) :: b
76 do idx_1 = n - 1, n - 1, 1
77 a(idx,idx_1) = b(idx,idx_1)
85 def apply(self, node, options=None):
86 '''Apply the AllArrayAccess2Loop transformation if the supplied
87 node is an Assignment with an Array Reference on its
88 left-hand-side. Each constant array index access (i.e. one not
89 containing a loop iterator or a range) is then transformed into
90 an iterator and the assignment placed within a single-trip loop,
91 subject to any constraints in the ArrayAccess2Loop transformation.
93 If any of the AllArrayAccess2Loop constraints are not
94 satisfied for a loop index then this transformation does
95 nothing for that index and silently moves to the next.
97 :param node: an assignment.
98 :type node: :py:class:`psyclone.psyir.nodes.Assignment`
99 :param options: a dictionary with options for transformations.
100 This is an optional argument that defaults to None.
101 :type options: Optional[Dict[str, Any]]
108 for index
in node.lhs.children:
111 except TransformationError:
115 '''Perform any checks to ensure that it is valid to apply the
116 AllArrayAccess2LoopTrans transformation to the supplied
119 :param node: the node that is being checked.
120 :type node: :py:class:`psyclone.psyir.nodes.Node`
121 :param options: a dictionary with options for transformations.
122 This is an optional argument that defaults to None.
123 :type options: Optional[Dict[str, Any]]
127 if not isinstance(node, Assignment):
128 raise TransformationError(
129 f
"Error in AllArrayAccess2LoopTrans transformation. The "
130 f
"supplied node argument should be a PSyIR Assignment, "
131 f
"but found '{type(node).__name__}'.")
135 "Convert the constant indices of a PSyIR array-element assignment "
136 "into single-trip Loops.")
141 :returns: the name of the transformation as a string.
145 return type(self).__name__
149 __all__ = [
'AllArrayAccess2LoopTrans']