1bf215546Sopenharmony_ci# 2bf215546Sopenharmony_ci# Copyright (C) 2020 Microsoft Corporation 3bf215546Sopenharmony_ci# 4bf215546Sopenharmony_ci# Copyright (C) 2018 Alyssa Rosenzweig 5bf215546Sopenharmony_ci# 6bf215546Sopenharmony_ci# Copyright (C) 2016 Intel Corporation 7bf215546Sopenharmony_ci# 8bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a 9bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"), 10bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation 11bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense, 12bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the 13bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions: 14bf215546Sopenharmony_ci# 15bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next 16bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the 17bf215546Sopenharmony_ci# Software. 18bf215546Sopenharmony_ci# 19bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 25bf215546Sopenharmony_ci# IN THE SOFTWARE. 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ciimport argparse 28bf215546Sopenharmony_ciimport sys 29bf215546Sopenharmony_ciimport math 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_cia = 'a' 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci# The nir_lower_bit_size() pass gets rid of all 8bit ALUs but insert new u2u8 34bf215546Sopenharmony_ci# and i2i8 operations to convert the result back to the original type after the 35bf215546Sopenharmony_ci# arithmetic operation is done. Those u2u8 and i2i8 operations, as any other 36bf215546Sopenharmony_ci# 8bit operations, are not supported by DXIL and needs to be discarded. The 37bf215546Sopenharmony_ci# dxil_nir_lower_8bit_conv() pass is here for that. 38bf215546Sopenharmony_ci# Similarly, some hardware doesn't support 16bit values 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cino_8bit_conv = [] 41bf215546Sopenharmony_cino_16bit_conv = [] 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_cidef remove_unsupported_casts(arr, bit_size, mask, max_unsigned_float, min_signed_float, max_signed_float): 44bf215546Sopenharmony_ci for outer_op_type in ('u2u', 'i2i', 'u2f', 'i2f'): 45bf215546Sopenharmony_ci for outer_op_sz in (16, 32, 64): 46bf215546Sopenharmony_ci if outer_op_sz == bit_size: 47bf215546Sopenharmony_ci continue 48bf215546Sopenharmony_ci outer_op = outer_op_type + str(int(outer_op_sz)) 49bf215546Sopenharmony_ci for inner_op_type in ('u2u', 'i2i'): 50bf215546Sopenharmony_ci inner_op = inner_op_type + str(int(bit_size)) 51bf215546Sopenharmony_ci for src_sz in (16, 32, 64): 52bf215546Sopenharmony_ci if (src_sz == bit_size): 53bf215546Sopenharmony_ci continue 54bf215546Sopenharmony_ci # Coming from integral, truncate appropriately 55bf215546Sopenharmony_ci orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz)))) 56bf215546Sopenharmony_ci if (outer_op[0] == 'u'): 57bf215546Sopenharmony_ci new_seq = ('iand', a, mask) 58bf215546Sopenharmony_ci else: 59bf215546Sopenharmony_ci shift = src_sz - bit_size 60bf215546Sopenharmony_ci new_seq = ('ishr', ('ishl', a, shift), shift) 61bf215546Sopenharmony_ci # Make sure the destination is the right type/size 62bf215546Sopenharmony_ci if outer_op_sz != src_sz or outer_op[2] != inner_op[0]: 63bf215546Sopenharmony_ci new_seq = (outer_op, new_seq) 64bf215546Sopenharmony_ci arr += [(orig_seq, new_seq)] 65bf215546Sopenharmony_ci for inner_op_type in ('f2u', 'f2i'): 66bf215546Sopenharmony_ci inner_op = inner_op_type + str(int(bit_size)) 67bf215546Sopenharmony_ci if (outer_op[2] == 'f'): 68bf215546Sopenharmony_ci # From float and to float, just truncate via min/max, and ensure the right float size 69bf215546Sopenharmony_ci for src_sz in (16, 32, 64): 70bf215546Sopenharmony_ci if (src_sz == bit_size): 71bf215546Sopenharmony_ci continue 72bf215546Sopenharmony_ci orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz)))) 73bf215546Sopenharmony_ci if (outer_op[0] == 'u'): 74bf215546Sopenharmony_ci new_seq = ('fmin', ('fmax', a, 0.0), max_unsigned_float) 75bf215546Sopenharmony_ci else: 76bf215546Sopenharmony_ci new_seq = ('fmin', ('fmax', a, min_signed_float), max_signed_float) 77bf215546Sopenharmony_ci if outer_op_sz != src_sz: 78bf215546Sopenharmony_ci new_seq = ('f2f' + str(int(outer_op_sz)), new_seq) 79bf215546Sopenharmony_ci arr += [(orig_seq, new_seq)] 80bf215546Sopenharmony_ci else: 81bf215546Sopenharmony_ci # From float to integral, convert to integral type first, then truncate 82bf215546Sopenharmony_ci orig_seq = (outer_op, (inner_op, a)) 83bf215546Sopenharmony_ci float_conv = ('f2' + inner_op[2] + str(int(outer_op_sz)), a) 84bf215546Sopenharmony_ci if (outer_op[0] == 'u'): 85bf215546Sopenharmony_ci new_seq = ('iand', float_conv, mask) 86bf215546Sopenharmony_ci else: 87bf215546Sopenharmony_ci shift = outer_op_sz - bit_size 88bf215546Sopenharmony_ci new_seq = ('ishr', ('ishl', float_conv, shift), shift) 89bf215546Sopenharmony_ci arr += [(orig_seq, new_seq)] 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ciremove_unsupported_casts(no_8bit_conv, 8, 0xff, 255.0, -128.0, 127.0) 92bf215546Sopenharmony_ciremove_unsupported_casts(no_16bit_conv, 16, 0xffff, 65535.0, -32768.0, 32767.0) 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_cilower_x2b = [ 95bf215546Sopenharmony_ci (('b2b32', 'a'), ('b2i32', 'a')), 96bf215546Sopenharmony_ci (('b2b1', 'a'), ('i2b1', 'a')), 97bf215546Sopenharmony_ci (('i2b1', 'a'), ('ine', a, 0)), 98bf215546Sopenharmony_ci (('f2b1', 'a'), ('fneu', a, 0)), 99bf215546Sopenharmony_ci] 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_cino_16bit_conv += [ 102bf215546Sopenharmony_ci (('f2f32', ('u2u16', 'a@32')), ('unpack_half_2x16_split_x', 'a')), 103bf215546Sopenharmony_ci (('u2u32', ('f2f16_rtz', 'a@32')), ('pack_half_2x16_split', 'a', 0)), 104bf215546Sopenharmony_ci] 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_cidef main(): 107bf215546Sopenharmony_ci parser = argparse.ArgumentParser() 108bf215546Sopenharmony_ci parser.add_argument('-p', '--import-path', required=True) 109bf215546Sopenharmony_ci args = parser.parse_args() 110bf215546Sopenharmony_ci sys.path.insert(0, args.import_path) 111bf215546Sopenharmony_ci run() 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_cidef run(): 115bf215546Sopenharmony_ci import nir_algebraic # pylint: disable=import-error 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci print('#include "dxil_nir.h"') 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci print(nir_algebraic.AlgebraicPass("dxil_nir_lower_8bit_conv", 120bf215546Sopenharmony_ci no_8bit_conv).render()) 121bf215546Sopenharmony_ci print(nir_algebraic.AlgebraicPass("dxil_nir_lower_16bit_conv", 122bf215546Sopenharmony_ci no_16bit_conv).render()) 123bf215546Sopenharmony_ci print(nir_algebraic.AlgebraicPass("dxil_nir_lower_x2b", 124bf215546Sopenharmony_ci lower_x2b).render()) 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ciif __name__ == '__main__': 127bf215546Sopenharmony_ci main() 128