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