Alyssa Rosenzweig | 3a1baaf | 2020-03-10 08:20:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2020 Collabora, Ltd. |
| 3 | # Copyright (C) 2018 Alyssa Rosenzweig |
| 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 | |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 41 | for isz in ('8', '16', '32'): |
| 42 | for osz in ('16', '32', '64'): |
| 43 | algebraic_late += [(('b2f' + osz, 'a@' + isz), ('b' + isz + 'csel', a, 1.0, 0.0))] |
Alyssa Rosenzweig | 3a1baaf | 2020-03-10 08:20:59 -0400 | [diff] [blame] | 44 | |
| 45 | # Midgard is able to type convert down by only one "step" per instruction; if |
| 46 | # NIR wants more than one step, we need to break up into multiple instructions |
| 47 | |
| 48 | converts = [] |
| 49 | |
| 50 | for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'): |
| 51 | srcsz_max = 64 |
| 52 | dstsz_max = 64 |
| 53 | # 8 bit float doesn't exist |
| 54 | srcsz_min = 8 if op[0] != 'f' else 16 |
| 55 | dstsz_min = 8 if op[2] != 'f' else 16 |
| 56 | dstsz = dstsz_min |
| 57 | # Iterate over all possible destination and source sizes |
| 58 | while dstsz <= dstsz_max: |
| 59 | srcsz = srcsz_min |
| 60 | while srcsz <= srcsz_max: |
| 61 | # Size converter lowering is only needed if src and dst sizes are |
| 62 | # spaced by a factor > 2. |
| 63 | # Type converter lowering is needed as soon as src_size != dst_size |
| 64 | if srcsz != dstsz and ((srcsz * 2 != dstsz and srcsz != dstsz * 2) or op[0] != op[2]): |
| 65 | cursz = srcsz |
| 66 | rule = a |
| 67 | # When converting down we first do the type conversion followed |
| 68 | # by one or more size conversions. When converting up, we do |
| 69 | # the type conversion at the end. This way we don't have to |
| 70 | # deal with the fact that f2f8 doesn't exists. |
| 71 | sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2] |
| 72 | if srcsz > dstsz and op[0] != op[2]: |
| 73 | rule = (op + str(int(cursz)), rule) |
| 74 | while cursz != dstsz: |
| 75 | cursz = cursz / 2 if dstsz < srcsz else cursz * 2 |
| 76 | rule = (sizeconvop + str(int(cursz)), rule) |
| 77 | if srcsz < dstsz and op[0] != op[2]: |
| 78 | rule = (op + str(int(cursz)), rule) |
| 79 | converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)] |
| 80 | srcsz *= 2 |
| 81 | dstsz *= 2 |
| 82 | |
Alyssa Rosenzweig | c058607 | 2020-04-17 15:52:18 -0400 | [diff] [blame] | 83 | # Bifrost doesn't have fp16 for a lot of special ops |
| 84 | SPECIAL = ['fexp2', 'flog2', 'fsin', 'fcos'] |
| 85 | |
| 86 | for op in SPECIAL: |
| 87 | converts += [((op + '@16', a), ('f2f16', (op, ('f2f32', a))))] |
| 88 | |
Karol Herbst | e5899c1 | 2020-08-18 19:51:57 +0200 | [diff] [blame] | 89 | converts += [(('f2b32', a), ('fneu32', a, 0.0)), |
Chris Forbes | ef78188 | 2020-07-26 10:43:33 -0700 | [diff] [blame] | 90 | (('i2b32', a), ('ine32', a, 0)), |
| 91 | (('b2i32', a), ('iand', 'a@32', 1))] |
Chris Forbes | 0ffefad | 2020-07-25 20:11:11 -0700 | [diff] [blame] | 92 | |
Alyssa Rosenzweig | 3a1baaf | 2020-03-10 08:20:59 -0400 | [diff] [blame] | 93 | def main(): |
| 94 | parser = argparse.ArgumentParser() |
| 95 | parser.add_argument('-p', '--import-path', required=True) |
| 96 | args = parser.parse_args() |
| 97 | sys.path.insert(0, args.import_path) |
| 98 | run() |
| 99 | |
| 100 | |
| 101 | def run(): |
| 102 | import nir_algebraic # pylint: disable=import-error |
| 103 | |
| 104 | print('#include "bifrost_nir.h"') |
| 105 | |
| 106 | print(nir_algebraic.AlgebraicPass("bifrost_nir_lower_algebraic_late", |
| 107 | algebraic_late + converts).render()) |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | main() |