panfrost/midgard: Add missing lowering passes for type/size conversion ops
[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 # These two special-cases save space/an op than the actual csel op +
41 # scheduler flexibility
42
43 (('b32csel', a, 'b@32', 0), ('iand', a, b)),
44 (('b32csel', a, 0, 'b@32'), ('iand', ('inot', a), b)),
45 ]
46
47
48 # Midgard is able to type convert down by only one "step" per instruction; if
49 # NIR wants more than one step, we need to break up into multiple instructions
50
51 converts = []
52
53 for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'):
54 srcsz_max = 64
55 dstsz_max = 64
56 # 8 bit float doesn't exist
57 srcsz_min = 8 if op[0] != 'f' else 16
58 dstsz_min = 8 if op[2] != 'f' else 16
59 dstsz = dstsz_min
60 # Iterate over all possible destination and source sizes
61 while dstsz <= dstsz_max:
62 srcsz = srcsz_min
63 while srcsz <= srcsz_max:
64 # Size converter lowering is only needed if src and dst sizes are
65 # spaced by a factor > 2.
66 # Type converter lowering is needed as soon as src_size != dst_size
67 if srcsz != dstsz and ((srcsz * 2 != dstsz and srcsz != dstsz * 2) or op[0] != op[2]):
68 cursz = srcsz
69 rule = a
70 # When converting down we first do the type conversion followed
71 # by one or more size conversions. When converting up, we do
72 # the type conversion at the end. This way we don't have to
73 # deal with the fact that f2f8 doesn't exists.
74 sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2]
75 if srcsz > dstsz and op[0] != op[2]:
76 rule = (op + str(int(cursz)), rule)
77 while cursz != dstsz:
78 cursz = cursz / 2 if dstsz < srcsz else cursz * 2
79 rule = (sizeconvop + str(int(cursz)), rule)
80 if srcsz < dstsz and op[0] != op[2]:
81 rule = (op + str(int(cursz)), rule)
82 converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)]
83 srcsz *= 2
84 dstsz *= 2
85
86 # Midgard scales fsin/fcos arguments by pi.
87 # Pass must be run only once, after the main loop
88
89 scale_trig = [
90 (('fsin', a), ('fsin', ('fdiv', a, math.pi))),
91 (('fcos', a), ('fcos', ('fdiv', a, math.pi))),
92 ]
93
94 def main():
95 parser = argparse.ArgumentParser()
96 parser.add_argument('-p', '--import-path', required=True)
97 args = parser.parse_args()
98 sys.path.insert(0, args.import_path)
99 run()
100
101
102 def run():
103 import nir_algebraic # pylint: disable=import-error
104
105 print('#include "midgard_nir.h"')
106
107 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_late",
108 algebraic_late + converts).render())
109
110 print(nir_algebraic.AlgebraicPass("midgard_nir_scale_trig",
111 scale_trig).render())
112
113
114 if __name__ == '__main__':
115 main()