pan/mdg: eliminate references to ins->alu.reg_mode
[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 = [
34 (('pack_unorm_4x8', a), ('pack_32_4x8', ('f2u8', ('fround_even', ('fmul', ('fsat', a), 255.0))))),
35
36 # Allows us to schedule as a multiply by 2
37 (('~fadd', ('fadd', a, b), a), ('fadd', ('fadd', a, a), b)),
38 ]
39
40 algebraic_late = [
41 # ineg must be lowered late, but only for integers; floats will try to
42 # have modifiers attached... hence why this has to be here rather than
43 # a more standard lower_negate approach
44
45 (('ineg', a), ('isub', 0, a)),
46
47 # Likewise we want fsub lowered but not isub
48 (('fsub', a, b), ('fadd', a, ('fneg', b))),
49
50 # These two special-cases save space/an op than the actual csel op +
51 # scheduler flexibility
52
53 (('b32csel', a, 'b@32', 0), ('iand', a, b)),
54 (('b32csel', a, 0, 'b@32'), ('iand', ('inot', a), b)),
55
56 # Fuse sat_signed. This should probably be shared with Bifrost
57 (('~fmin', ('fmax', a, -1.0), 1.0), ('fsat_signed', a)),
58 (('~fmax', ('fmin', a, 1.0), -1.0), ('fsat_signed', a)),
59
60 # Fuse clamp_positive. This should probably be shared with Utgard/bifrost
61 (('fmax', a, 0.0), ('fclamp_pos', a)),
62
63 (('ishl', 'a@16', b), ('u2u16', ('ishl', ('u2u32', a), b))),
64 (('ishr', 'a@16', b), ('i2i16', ('ishr', ('i2i32', a), b))),
65 (('ushr', 'a@16', b), ('u2u16', ('ushr', ('u2u32', a), b))),
66
67 (('ishl', 'a@8', b), ('u2u8', ('u2u16', ('ishl', ('u2u32', ('u2u16', a)), b)))),
68 (('ishr', 'a@8', b), ('i2i8', ('i2i16', ('ishr', ('i2i32', ('i2i16', a)), b)))),
69 (('ushr', 'a@8', b), ('u2u8', ('u2u16', ('ushr', ('u2u32', ('u2u16', a)), b)))),
70
71 # Canonical form. The scheduler will convert back if it makes sense.
72 (('fmul', a, 2.0), ('fadd', a, a))
73 ]
74
75
76 # Midgard is able to type convert down by only one "step" per instruction; if
77 # NIR wants more than one step, we need to break up into multiple instructions.
78 # Nevertheless, we can do both a size step and a floating/int step at once.
79
80 converts = []
81
82 for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'):
83 srcsz_max = 64
84 dstsz_max = 64
85 # 8 bit float doesn't exist
86 srcsz_min = 8 if op[0] != 'f' else 16
87 dstsz_min = 8 if op[2] != 'f' else 16
88 dstsz = dstsz_min
89 # Iterate over all possible destination and source sizes
90 while dstsz <= dstsz_max:
91 srcsz = srcsz_min
92 while srcsz <= srcsz_max:
93 # Size converter lowering is only needed if src and dst sizes are
94 # spaced by a factor > 2.
95 if srcsz != dstsz and (srcsz * 2 != dstsz and srcsz != dstsz * 2):
96 cursz = srcsz
97 rule = a
98 # When converting down we first do the type conversion followed
99 # by one or more size conversions. When converting up, we do
100 # the type conversion at the end. This way we don't have to
101 # deal with the fact that f2f8 doesn't exists.
102 sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2]
103 if srcsz > dstsz and op[0] != op[2]:
104 rule = (op + str(int(cursz)), rule)
105 while cursz != dstsz:
106 cursz = cursz / 2 if dstsz < srcsz else cursz * 2
107 rule = (sizeconvop + str(int(cursz)), rule)
108 if srcsz < dstsz and op[0] != op[2]:
109 rule = (op + str(int(cursz)), rule)
110 converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)]
111 srcsz *= 2
112 dstsz *= 2
113
114 # Try to force constants to the right
115 constant_switch = [
116 # fge gets flipped to fle, so we invert to keep the order
117 (('fge', 'a', '#b'), (('inot', ('flt', a, b)))),
118 (('fge32', 'a', '#b'), (('inot', ('flt32', a, b)))),
119 (('ige32', 'a', '#b'), (('inot', ('ilt32', a, b)))),
120 (('uge32', 'a', '#b'), (('inot', ('ult32', a, b)))),
121
122 # fge gets mapped to fle with a flip
123 (('flt32', '#a', 'b'), ('inot', ('fge32', a, b))),
124 (('ilt32', '#a', 'b'), ('inot', ('ige32', a, b))),
125 (('ult32', '#a', 'b'), ('inot', ('uge32', a, b)))
126 ]
127
128 # ..since the above switching happens after algebraic stuff is done
129 cancel_inot = [
130 (('inot', ('inot', a)), a)
131 ]
132
133 # Midgard scales fsin/fcos arguments by pi.
134 # Pass must be run only once, after the main loop
135
136 scale_trig = [
137 (('fsin', a), ('fsin', ('fdiv', a, math.pi))),
138 (('fcos', a), ('fcos', ('fdiv', a, math.pi))),
139 ]
140
141 def main():
142 parser = argparse.ArgumentParser()
143 parser.add_argument('-p', '--import-path', required=True)
144 args = parser.parse_args()
145 sys.path.insert(0, args.import_path)
146 run()
147
148
149 def run():
150 import nir_algebraic # pylint: disable=import-error
151
152 print('#include "midgard_nir.h"')
153
154 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_early",
155 algebraic).render())
156
157 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_late",
158 algebraic_late + converts + constant_switch).render())
159
160 print(nir_algebraic.AlgebraicPass("midgard_nir_scale_trig",
161 scale_trig).render())
162
163 print(nir_algebraic.AlgebraicPass("midgard_nir_cancel_inot",
164 cancel_inot).render())
165
166
167 if __name__ == '__main__':
168 main()