Debian Codegraph @

src/panfrost/compiler/midgard/midgard_nir_algebraic.py

raw
1 # Copyright (C) 2016 Intel Corporation
2 # Copyright (C) 2018 Alyssa Rosenzweig
3 # Copyright (C) 2019-2020 Collabora, Ltd.
4 # SPDX-License-Identifier: MIT
5
6 import argparse
7 import sys
8 import math
9
10 a = 'a'
11 b = 'b'
12 c = 'c'
13
14 algebraic = [
15 # Allows us to schedule as a multiply by 2
16 (('~fadd', ('fadd', a, b), a), ('fadd', ('fadd', a, a), b)),
17
18 # Midgard scales fsin/fcos arguments by pi.
19 (('fsin', a), ('fsin_mdg', ('fdiv', a, math.pi))),
20 (('fcos', a), ('fcos_mdg', ('fdiv', a, math.pi))),
21 ]
22
23 algebraic_late = [
24 # Likewise we want fsub lowered but not isub
25 (('fsub', a, b), ('fadd', a, ('fneg', b))),
26
27 # These two special-cases save space/an op than the actual csel op +
28 # scheduler flexibility
29
30 (('b32csel', a, 'b@32', 0), ('iand', a, b)),
31 (('b32csel', a, 0, 'b@32'), ('iand', ('inot', a), b)),
32
33 # Fuse sat_signed. This should probably be shared with Bifrost
34 (('~fmin', ('fmax', a, -1.0), 1.0), ('fsat_signed', a)),
35 (('~fmax', ('fmin', a, 1.0), -1.0), ('fsat_signed', a)),
36
37 # Fuse clamp_positive. This should probably be shared with Utgard/bifrost
38 (('fmax', a, 0.0), ('fclamp_pos', a)),
39
40 (('ishl', 'a@16', b), ('u2u16', ('ishl', ('u2u32', a), b))),
41 (('ishr', 'a@16', b), ('i2i16', ('ishr', ('i2i32', a), b))),
42 (('ushr', 'a@16', b), ('u2u16', ('ushr', ('u2u32', a), b))),
43
44 (('ishl', 'a@8', b), ('u2u8', ('u2u16', ('ishl', ('u2u32', ('u2u16', a)), b)))),
45 (('ishr', 'a@8', b), ('i2i8', ('i2i16', ('ishr', ('i2i32', ('i2i16', a)), b)))),
46 (('ushr', 'a@8', b), ('u2u8', ('u2u16', ('ushr', ('u2u32', ('u2u16', a)), b)))),
47
48 # Canonical form. The scheduler will convert back if it makes sense.
49 (('fmul', a, 2.0), ('fadd', a, a))
50 ]
51
52 # Size conversion is redundant to Midgard but needed for NIR, and writing this
53 # lowering in MIR would be painful without a competent builder, so eat the
54 # extra instruction
55 for sz in ('8', '16', '32'):
56 converted = ('u2u32', a) if sz != '32' else a
57 algebraic_late += [(('ufind_msb', 'a@' + sz), ('isub', 31, ('uclz', converted)))]
58
59 # Midgard is able to type convert down by only one "step" per instruction; if
60 # NIR wants more than one step, we need to break up into multiple instructions.
61 # Nevertheless, we can do both a size step and a floating/int step at once.
62
63 converts = []
64
65 for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'):
66 srcsz_max = 64
67 dstsz_max = 64
68 # 8 bit float doesn't exist
69 srcsz_min = 8 if op[0] != 'f' else 16
70 dstsz_min = 8 if op[2] != 'f' else 16
71 dstsz = dstsz_min
72 # Iterate over all possible destination and source sizes
73 while dstsz <= dstsz_max:
74 srcsz = srcsz_min
75 while srcsz <= srcsz_max:
76 # Size converter lowering is only needed if src and dst sizes are
77 # spaced by a factor > 2.
78 if srcsz != dstsz and (srcsz * 2 != dstsz and srcsz != dstsz * 2):
79 cursz = srcsz
80 rule = a
81 # When converting down we first do the type conversion followed
82 # by one or more size conversions. When converting up, we do
83 # the type conversion at the end. This way we don't have to
84 # deal with the fact that f2f8 doesn't exists.
85 sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2]
86 if srcsz > dstsz and op[0] != op[2]:
87 rule = (op + str(int(cursz)), rule)
88 while cursz != dstsz:
89 cursz = cursz / 2 if dstsz < srcsz else cursz * 2
90 rule = (sizeconvop + str(int(cursz)), rule)
91 if srcsz < dstsz and op[0] != op[2]:
92 rule = (op + str(int(cursz)), rule)
93 converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)]
94 srcsz *= 2
95 dstsz *= 2
96
97 # Try to force constants to the right
98 constant_switch = [
99 # fge gets flipped to fle, so we invert to keep the order
100 (('fge', 'a', '#b'), (('inot', ('flt', a, b)))),
101 (('fge32', 'a', '#b'), (('inot', ('flt32', a, b)))),
102 (('ige32', 'a', '#b'), (('inot', ('ilt32', a, b)))),
103 (('uge32', 'a', '#b'), (('inot', ('ult32', a, b)))),
104
105 # fge gets mapped to fle with a flip
106 (('flt32', '#a', 'b'), ('inot', ('fge32', a, b))),
107 (('ilt32', '#a', 'b'), ('inot', ('ige32', a, b))),
108 (('ult32', '#a', 'b'), ('inot', ('uge32', a, b)))
109 ]
110
111 # ..since the above switching happens after algebraic stuff is done
112 cancel_inot = [
113 (('inot', ('inot', a)), a),
114 (('b32csel', ('inot', a), b, c), ('b32csel', a, c, b)),
115 ]
116
117 def main():
118 parser = argparse.ArgumentParser()
119 parser.add_argument('-p', '--import-path', required=True)
120 args = parser.parse_args()
121 sys.path.insert(0, args.import_path)
122 run()
123
124
125 def run():
126 import nir_algebraic # pylint: disable=import-error
127
128 print('#include "midgard_nir.h"')
129
130 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_early",
131 algebraic).render())
132
133 print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_late",
134 algebraic_late + converts + constant_switch).render())
135
136 print(nir_algebraic.AlgebraicPass("midgard_nir_cancel_inot",
137 cancel_inot).render())
138
139
140 if __name__ == '__main__':
141 main()