-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathminormax2code_trans.py
More file actions
193 lines (156 loc) · 7.17 KB
/
minormax2code_trans.py
File metadata and controls
193 lines (156 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2021-2026, Science and Technology Facilities Council
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# Authors: R. W. Ford and N. Nobre, STFC Daresbury Lab
# Modified: S. Siso, STFC Daresbury Lab
# Modified: A. B. G. Chalk, STFC Daresbury Lab
'''Module containing a class that provides functionality to transform
a PSyIR MIN or MAX intrinsics to PSyIR code. This could be useful if the
intrinsic is not supported by the back-end or if the performance of the
inline code is better than the intrinsic. This utility transformation
should not be called directly by the user, rather it provides
functionality that can be specialised by MIN and MAX-specific
transformations.
'''
from abc import ABC
import warnings
from psyclone.psyir.nodes import (
BinaryOperation, Assignment, Reference, IfBlock
)
from psyclone.psyir.symbols import DataSymbol
from psyclone.psyir.transformations.intrinsics.intrinsic2code_trans import (
Intrinsic2CodeTrans
)
from psyclone.utils import transformation_documentation_wrapper
@transformation_documentation_wrapper
class MinOrMax2CodeTrans(Intrinsic2CodeTrans, ABC):
'''Provides a utility transformation from a PSyIR MIN or MAX Intrinsic
node to equivalent code in a PSyIR tree. Validity checks are also
performed (by the parent class). This utility transformation is
not designed to be called directly by the user, rather it should
be specialised to provide MIN or MAX transformations.
The transformation replaces
.. code-block:: python
R = [MIN or MAX](A, B, C ...)
with the following logic:
.. code-block:: python
R = A
if B [< or >] R:
R = B
if C [< or >] R:
R = C
...
'''
def __init__(self):
super().__init__()
self._compare_operator = None
def validate(self, node, options=None):
'''
Check that it is safe to apply the transformation to the supplied node.
:param node: the SIGN call to transform.
:type node: :py:class:`psyclone.psyir.nodes.IntrinsicCall`
:param options: any of options for the transformation.
:type options: dict[str, Any]
'''
super().validate(node, options=options)
super()._validate_scalar_arg(node)
def apply(self, node, options=None, **kwargs):
'''Apply this utility transformation to the specified node. This node
must be a MIN or MAX IntrinsicCall. The intrinsic is converted to
equivalent inline code. This is implemented as a PSyIR transform from:
.. code-block:: python
R = ... [MIN or MAX](A, B, C ...) ...
to:
.. code-block:: python
res = A
tmp = B
IF tmp [< or >] res:
res = tmp
tmp = C
IF tmp [< or >] res:
res = tmp
...
R = ... res ...
where ``A``, ``B``, ``C`` ... could be arbitrarily complex
PSyIR expressions and the ``...`` before and after ``[MIN or
MAX](A, B, C ...)`` can be arbitrary PSyIR code.
This transformation requires the IntrinsicCall node to be a
descendant of an assignment and will raise an exception if
this is not the case.
:param node: a MIN or MAX intrinsic.
:type node: :py:class:`psyclone.psyir.nodes.IntrinsicCall`
:param options: a dictionary with options for transformations.
:type options: Optional[Dict[str, Any]]
'''
# TODO 2668: options are now deprecated:
if options:
warnings.warn(self._deprecation_warning, DeprecationWarning, 2)
# pylint: disable=too-many-locals
self.validate(node, options, **kwargs)
symbol_table = node.scope.symbol_table
assignment = node.ancestor(Assignment)
# Create two temporary variables.
result_type = node.arguments[0].datatype
res_var_symbol = symbol_table.new_symbol(
f"res_{self._intrinsic.name.lower()}",
symbol_type=DataSymbol, datatype=result_type)
tmp_var_symbol = symbol_table.new_symbol(
f"tmp_{self._intrinsic.name.lower()}",
symbol_type=DataSymbol, datatype=result_type)
# Replace intrinsic with a temporary (res_var).
node.replace_with(Reference(res_var_symbol))
# res_var=A (child[0] of node)
lhs = Reference(res_var_symbol)
new_assignment = Assignment.create(lhs, node.arguments[0].detach())
assignment.parent.children.insert(assignment.position, new_assignment)
# For each of the remaining arguments (B,C...)
for expression in node.pop_all_children()[1:]:
# tmp_var=(B or C or ...)
lhs = Reference(tmp_var_symbol)
new_assignment = Assignment.create(lhs, expression)
assignment.parent.children.insert(assignment.position,
new_assignment)
# if_condition: tmp_var [< or >] res_var
lhs = Reference(tmp_var_symbol)
rhs = Reference(res_var_symbol)
if_condition = BinaryOperation.create(
self._compare_operator, lhs, rhs)
# then_body: res_var=tmp_var
lhs = Reference(res_var_symbol)
rhs = Reference(tmp_var_symbol)
then_body = [Assignment.create(lhs, rhs)]
# if [if_condition] then [then_body]
if_stmt = IfBlock.create(if_condition, then_body)
assignment.parent.children.insert(assignment.position, if_stmt)
# For AutoAPI auto-documentation generation.
__all__ = ["MinOrMax2CodeTrans"]