Reference Guide  2.5.0
allarrayaccess2loop_trans.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2021-2024, Science and Technology Facilities Council.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # -----------------------------------------------------------------------------
34 # Authors: R. W. Ford, N. Nobre and S. Siso, STFC Daresbury Lab
35 
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.
39 
40 '''
41 
43  ArrayAccess2LoopTrans
44 from psyclone.psyGen import Transformation
45 from psyclone.psyir.nodes import Assignment
47  import TransformationError
48 
49 
51  '''Provides a transformation from a PSyIR Assignment containing
52  constant index accesses to an array into single-trip loops. For
53  example:
54 
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))
68  program example
69  real, dimension(10,10) :: a
70  real, dimension(10,10) :: b
71  integer :: n
72  integer :: idx
73  integer :: idx_1
74  <BLANKLINE>
75  do idx = 1, 1, 1
76  do idx_1 = n - 1, n - 1, 1
77  a(idx,idx_1) = b(idx,idx_1)
78  enddo
79  enddo
80  <BLANKLINE>
81  end program example
82  <BLANKLINE>
83 
84  '''
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.
92 
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.
96 
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]]
102 
103  '''
104  self.validatevalidatevalidate(node, options)
105 
106  trans = ArrayAccess2LoopTrans()
107 
108  for index in node.lhs.children:
109  try:
110  trans.apply(index)
111  except TransformationError:
112  pass
113 
114  def validate(self, node, options=None):
115  '''Perform any checks to ensure that it is valid to apply the
116  AllArrayAccess2LoopTrans transformation to the supplied
117  PSyIR Node.
118 
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]]
124 
125  '''
126  # Not a PSyIR Assignment node
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__}'.")
132 
133  def __str__(self):
134  return (
135  "Convert the constant indices of a PSyIR array-element assignment "
136  "into single-trip Loops.")
137 
138  @property
139  def name(self):
140  '''
141  :returns: the name of the transformation as a string.
142  :rtype: str
143 
144  '''
145  return type(self).__name__
146 
147 
148 # For automatic document generation
149 __all__ = ['AllArrayAccess2LoopTrans']
def validate(self, node, options=None)
Definition: psyGen.py:2783