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