Reference Guide  2.5.0
create_nemo_loop_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 # Author A. R. Porter, STFC Daresbury Lab
35 # Modified by J. Henrichs, Bureau of Meteorology
36 # Modified by R. W. Ford, S. Siso and N. Nobre, STFC Daresbury Lab
37 
38 '''
39 Module providing a transformation from a generic PSyIR Loop into a
40 NEMO Loop.
41 
42 '''
43 from psyclone.transformations import Transformation, TransformationError
44 from psyclone.psyir.nodes import Loop
45 from psyclone.nemo import NemoLoop
46 
47 
48 class CreateNemoLoopTrans(Transformation):
49  """
50  Transform a generic PSyIR Loop into a NemoLoop. For example:
51 
52  >>> from psyclone.psyir.frontend.fortran import FortranReader
53  >>> from psyclone.psyir.nodes import Loop
54  >>> from psyclone.domain.nemo.transformations import CreateNemoLoopTrans
55  >>> code = '''
56  ... subroutine sub()
57  ... integer :: ji, tmp(10)
58  ... do ji=1, 10
59  ... tmp(ji) = 2*ji
60  ... end do
61  ... end subroutine sub'''
62  >>> psyir = FortranReader().psyir_from_source(code)
63  >>> loops = psyir.walk(Loop)
64  >>> trans = CreateNemoLoopTrans()
65  >>> trans.apply(loops[0])
66  >>> print(psyir.view(colour=False))
67  FileContainer[]
68  Routine[name:'sub']
69  0: Loop[type='lon', field_space='None', it_space='None']
70  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
71  Literal[value:'10', Scalar<INTEGER, UNDEFINED>]
72  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
73  Schedule[]
74  0: Assignment[]
75  ArrayReference[name:'tmp']
76  Reference[name:'ji']
77  BinaryOperation[operator:'MUL']
78  Literal[value:'2', Scalar<INTEGER, UNDEFINED>]
79  Reference[name:'ji']
80  <BLANKLINE>
81 
82  As shown above, the resulting Schedule now contains a NemoLoop, indicated
83  by the "type='lon'" (for 'longitude') annotation for the Loop node.
84 
85  """
86  @property
87  def name(self):
88  '''
89  :returns: the name of the transformation.
90  :rtype: str
91 
92  TODO #1214 remove this method.
93 
94  '''
95  return type(self).__name__
96 
97  def validate(self, node, options=None):
98  '''
99  Check that the supplied node is a valid target for this transformation.
100 
101  :param node: the target of the transformation.
102  :type node: :py:class:`psyclone.psyir.nodes.Node`
103  :param options: a dictionary with options for \
104  transformations. No options are used in this \
105  transformation. This is an optional argument that defaults \
106  to None.
107  :type options: Optional[Dict[str, Any]]
108 
109  :raises TransformationError: if the supplied node is not a Routine.
110 
111  '''
112  super(CreateNemoLoopTrans, self).validate(node, options=options)
113 
114  if not isinstance(node, Loop):
115  raise TransformationError(
116  f"Error in CreateNemoLoopTrans transformation. The supplied "
117  f"node should be a PSyIR Loop but found "
118  f"'{type(node).__name__}'")
119 
120  def apply(self, loop, options=None):
121  '''
122  Takes a generic PSyIR Loop node and replaces it with a NemoLoop.
123 
124  :param loop: the Loop node to be transformed.
125  :type loop: :py:class:`psyclone.psyir.nodes.Loop`
126  :param options: a dictionary with options for \
127  transformations. No options are used in this \
128  transformation. This is an optional argument that defaults \
129  to None.
130  :type options: Optional[Dict[str, Any]]
131 
132  '''
133  self.validatevalidate(loop, options=options)
134 
135  # Convert a generic loop into a NEMO Loop by creating a new
136  # NemoLoop object and inserting it into the PSyIR.
137  table = loop.loop_body.symbol_table.detach()
138  nodes = loop.pop_all_children()
139  new_loop = NemoLoop.create(loop.variable,
140  nodes[0], nodes[1], nodes[2],
141  nodes[3].pop_all_children())
142  # TODO #1377 the NemoLoop.create() interface needs extending to accept
143  # a SymbolTable.
144  new_loop.loop_body.symbol_table.detach()
145  table.attach(new_loop.loop_body)
146 
147  loop.replace_with(new_loop)
148 
149 
150 # For AutoAPI documentation generation
151 __all__ = ['CreateNemoLoopTrans']