pan/mdg: Remove redundant redundancy
[mesa.git] / src / panfrost / midgard / midgard_nir_algebraic.py
1 #
2 # Copyright (C) 2018 Alyssa Rosenzweig
3 #
4 # Copyright (C) 2016 Intel Corporation
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
24
25 import argparse
26 import sys
27 import math
28
29 a = 'a'
30 b = 'b'
31 c = 'c'
32
33 algebraic_late = [
34 # ineg must be lowered late, but only for integers; floats will try to
35 # have modifiers attached... hence why this has to be here rather than
36 # a more standard lower_negate approach
37
38 (('ineg', a), ('isub', 0, a)),
39
40 # Likewise we want fsub lowered but not isub
41 (('fsub', a, b), ('fadd', a, ('fneg', b))),
42
43 # These two special-cases save space/an op than the actual csel op +
44 # scheduler flexibility
45
46 (('b32csel', a, 'b@32', 0), ('iand', a, b)),
47 (('b32csel', a, 0, 'b@32'), ('iand', ('inot', a), b)),
48
49 # Fuse sat_signed. This should probably be shared with Bifrost
50 (('~fmin', ('fmax', a, -1.0), 1.0), ('fsat_signed', a)),
51 (('~fmax', ('fmin', a, 1.0), -1.0), ('fsat_signed', a)),
52
53 # Fuse clamp_positive. This should probably be shared with Utgard/bifrost
54 (('fmax', a, 0.0), ('fclamp_pos', a)),
55 ]
56
57
58 # Midgard is able to type convert down by only one "step" per instruction; if
59 # NIR wants more than one step, we need to break up into multiple instructions
60
61 converts = []
62
63 for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'):
64 srcsz_max = 64
65 dstsz_max = 64
66 # 8 bit float doesn't exist
67 srcsz_min = 8 if op[0] != 'f' else 16
68 dstsz_min = 8 if op[2] != 'f' else 16
69 dstsz = dstsz_min
70 # Iterate over all possible destination and source sizes
71 while dstsz <= dstsz_max:
72 srcsz = srcsz_min
73 while srcsz <= srcsz_max:
74 # Size converter lowering is only needed if src and dst sizes are
75 # spaced by a factor > 2.
76 # Type converter lowering is needed as soon as src_size != dst_size
77 if srcsz != dstsz and ((srcsz * 2 != dstsz and srcsz != dstsz * 2) or op[0] != op[2]):
78 cursz = srcsz
79 rule = a
80 # When converting down we first do the type conversion followed
81 # by one or more size conversions. When converting up, we do
82 # the type conversion at the end. This way we don't have to
83 # deal with the fact that f2f8 doesn't exists.
84 sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2]
85 if srcsz > dstsz and op[0] != op[2]:
86 rule = (op + str(int(cursz)), rule)
87 while cursz != dstsz:
88 cursz = cursz / 2 if dstsz < srcsz else cursz * 2
89 rule = (sizeconvop + str(int(cursz)), rule)
90 if srcsz < dstsz and op[0] != op[2]:
91 rule = (op + str(int(cursz)), rule)
92 converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)]
93 srcsz *= 2
94 dstsz *= 2
95
96 # Midgard outputs fp32 for specials. The f2f32 will be folded in later.
97 SPECIAL = ['fexp2', 'flog2', 'fsin', 'fcos', 'frcp', 'frsq']
98
99 for op in SPECIAL:
100 converts += [((op + '@16', a), ('f2f16', (op, ('f2f32', a))))]
101
102 # Try to force constants to the right
103 constant_switch = [
104 # fge gets flipped to fle, so we invert to keep the order
105 (('fge', 'a', '#b'), (('inot', ('flt', a, b)))),
106 (('fge32', 'a', '#b'), (('inot', ('flt32', a, b)))),
107 (('ige32', 'a', '#b'), (('inot', ('ilt32', a, b)))),
108 (('uge32', 'a', '#b'), (('inot', ('ult32', a, b)))),
109
110 # fge gets mapped to fle with a flip
111 (('flt32', '#a', 'b'), ('inot', ('fge32', a, b))),
112 (('ilt32', '#a', 'b'), ('inot', ('ige32', a, b))),
113 (('ult32', '#a', 'b'), ('inot', ('uge32', a, b)))
114 ]
115
116 # ..since the above switching happens after algebraic stuff is done
117 cancel_inot = [
118 (('inot', ('inot', a)), a)
119 ]
120
121 # Midgard scales fsin/fcos arguments by pi.
122 # Pass must be run only once, after the main loop
123
124 scale_trig = [
125 (('fsin', a), ('fsin', ('fdiv', a, math.pi))),
126 (('fcos', a), ('fcos', ('fdiv', a, math.pi))),
127 ]
128
129 def main():
130 parser = argparse.ArgumentParser()
131 parser.add_argument('-p', '--import-path', required=True)
132 args = parser.parse_args()
133 sys.path.insert(0, args.import_path)
134 run()
135
136
137 def run():
138 import nir_algebraic # pylint: disable=import-error
139
140 print('#include "midgard_nir.h"')
141
142 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_late",
143 algebraic_late + converts + constant_switch).render())
144
145 print(nir_algebraic.AlgebraicPass("midgard_nir_scale_trig",
146 scale_trig).render())
147
148 print(nir_algebraic.AlgebraicPass("midgard_nir_cancel_inot",
149 cancel_inot).render())
150
151
152 if __name__ == '__main__':
153 main()