Reference Guide  2.5.0
create_nemo_invoke_schedule_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: R. W. Ford, S. Siso and N. Nobre, STFC Daresbury Lab
36 
37 '''
38 Module providing a transformation from a generic PSyIR routine into a
39 NEMO InvokeSchedule.
40 '''
41 
42 from psyclone.transformations import Transformation, TransformationError
43 from psyclone.psyir.nodes import Routine
44 from psyclone.nemo import NemoInvokeSchedule
45 
46 
47 class CreateNemoInvokeScheduleTrans(Transformation):
48  """
49  Transform a generic PSyIR Routine into a NEMO InvokeSchedule.
50  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 \
55  CreateNemoInvokeScheduleTrans
56  >>> code = '''
57  ... subroutine sub()
58  ... integer :: ji
59  ... real :: tmp(10)
60  ... do ji=1, 10
61  ... tmp(ji) = 2.0*ji
62  ... end do
63  ... end subroutine sub'''
64  >>> psyir = FortranReader().psyir_from_source(code)
65  >>> loop = psyir.walk(Loop)[0]
66  >>> trans = CreateNemoInvokeScheduleTrans()
67  >>> trans.apply(psyir.children[0])
68  >>> print(psyir.view(colour=False))
69  FileContainer[]
70  NemoInvokeSchedule[invoke='sub']
71  0: Loop[variable='ji']
72  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
73  Literal[value:'10', Scalar<INTEGER, UNDEFINED>]
74  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
75  Schedule[]
76  0: Assignment[]
77  ArrayReference[name:'tmp']
78  Reference[name:'ji']
79  BinaryOperation[operator:'MUL']
80  Literal[value:'2.0', Scalar<REAL, UNDEFINED>]
81  Reference[name:'ji']
82  <BLANKLINE>
83 
84  The root node of this example has been transformed from a Routine into a
85  NemoInvokeSchedule.
86 
87  """
88  @property
89  def name(self):
90  '''
91  :returns: the name of the transformation.
92  :rtype: str
93 
94  TODO #1214 remove this method.
95 
96  '''
97  return type(self).__name__
98 
99  def validate(self, node, options=None):
100  '''
101  Check that the supplied node is a valid target for this transformation.
102 
103  :param node: the target of the transformation.
104  :type node: :py:class:`psyclone.psyir.nodes.Node`
105  :param options: a dictionary with options for \
106  transformations. No options are used in this \
107  transformation. This is an optional argument that defaults \
108  to None.
109  :type options: Optional[Dict[str, Any]]
110 
111  :raises TransformationError: if the supplied node is not a Routine.
112 
113  '''
114  super(CreateNemoInvokeScheduleTrans, self).validate(node,
115  options=options)
116 
117  if not isinstance(node, Routine):
118  raise TransformationError(
119  f"Error in NemoInvokeTrans transformation. The supplied node "
120  f"should be a PSyIR Routine but found '{type(node).__name__}'")
121 
122  def apply(self, node, options=None):
123  '''
124  Takes a generic PSyIR Routine and replaces it with a
125  NemoInvokeSchedule (in-place). Note that this may mean replacing
126  the top-level node itself and therefore this routine returns the
127  root of the modified tree.
128 
129  :param node: the routine node to be transformed.
130  :type node: :py:class:`psyclone.psyir.nodes.Routine`
131  :param options: a dictionary with options for \
132  transformations. No options are used in this \
133  transformation. This is an optional argument that defaults \
134  to None.
135  :type options: Optional[Dict[str, Any]]
136 
137  '''
138  self.validatevalidate(node, options=options)
139 
140  # If it's a function we need to provide the return_symbol_name to the
141  # create method
142  return_symbol_name = None
143  if node.return_symbol:
144  return_symbol_name = node.return_symbol.name
145 
146  new_node = NemoInvokeSchedule.create(
147  node.name, node.symbol_table.detach(), node.pop_all_children(),
148  is_program=node.is_program,
149  return_symbol_name=return_symbol_name)
150 
151  # We need to replace the top node in the (possibly sub-) PSyIR
152  # tree that we've been passed.
153  if node.parent:
154  node.replace_with(new_node)
155 
156 
157 # For AutoAPI documentation generation
158 __all__ = ['CreateNemoInvokeScheduleTrans']