Path: blob/21.2-virgl/src/microsoft/compiler/dxil_nir_algebraic.py
4564 views
#1# Copyright (C) 2020 Microsoft Corporation2#3# Copyright (C) 2018 Alyssa Rosenzweig4#5# Copyright (C) 2016 Intel Corporation6#7# Permission is hereby granted, free of charge, to any person obtaining a8# copy of this software and associated documentation files (the "Software"),9# to deal in the Software without restriction, including without limitation10# the rights to use, copy, modify, merge, publish, distribute, sublicense,11# and/or sell copies of the Software, and to permit persons to whom the12# Software is furnished to do so, subject to the following conditions:13#14# The above copyright notice and this permission notice (including the next15# paragraph) shall be included in all copies or substantial portions of the16# Software.17#18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL21# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS24# IN THE SOFTWARE.2526import argparse27import sys28import math2930a = 'a'3132# The nir_lower_bit_size() pass gets rid of all 8bit ALUs but insert new u2u833# and i2i8 operations to convert the result back to the original type after the34# arithmetic operation is done. Those u2u8 and i2i8 operations, as any other35# 8bit operations, are not supported by DXIL and needs to be discarded. The36# dxil_nir_lower_8bit_conv() pass is here for that.37# Similarly, some hardware doesn't support 16bit values3839no_8bit_conv = []40no_16bit_conv = []4142def remove_unsupported_casts(arr, bit_size, mask, max_unsigned_float, min_signed_float, max_signed_float):43for outer_op_type in ('u2u', 'i2i', 'u2f', 'i2f'):44for outer_op_sz in (16, 32, 64):45if outer_op_sz == bit_size:46continue47outer_op = outer_op_type + str(int(outer_op_sz))48for inner_op_type in ('u2u', 'i2i'):49inner_op = inner_op_type + str(int(bit_size))50for src_sz in (16, 32, 64):51if (src_sz == bit_size):52continue53# Coming from integral, truncate appropriately54orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz))))55if (outer_op[0] == 'u'):56new_seq = ('iand', a, mask)57else:58shift = src_sz - bit_size59new_seq = ('ishr', ('ishl', a, shift), shift)60# Make sure the destination is the right type/size61if outer_op_sz != src_sz or outer_op[2] != inner_op[0]:62new_seq = (outer_op, new_seq)63arr += [(orig_seq, new_seq)]64for inner_op_type in ('f2u', 'f2i'):65inner_op = inner_op_type + str(int(bit_size))66if (outer_op[2] == 'f'):67# From float and to float, just truncate via min/max, and ensure the right float size68for src_sz in (16, 32, 64):69if (src_sz == bit_size):70continue71orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz))))72if (outer_op[0] == 'u'):73new_seq = ('fmin', ('fmax', a, 0.0), max_unsigned_float)74else:75new_seq = ('fmin', ('fmax', a, min_signed_float), max_signed_float)76if outer_op_sz != src_sz:77new_seq = ('f2f' + str(int(outer_op_sz)), new_seq)78arr += [(orig_seq, new_seq)]79else:80# From float to integral, convert to integral type first, then truncate81orig_seq = (outer_op, (inner_op, a))82float_conv = ('f2' + inner_op[2] + str(int(outer_op_sz)), a)83if (outer_op[0] == 'u'):84new_seq = ('iand', float_conv, mask)85else:86shift = outer_op_sz - bit_size87new_seq = ('ishr', ('ishl', float_conv, shift), shift)88arr += [(orig_seq, new_seq)]8990remove_unsupported_casts(no_8bit_conv, 8, 0xff, 255.0, -128.0, 127.0)91remove_unsupported_casts(no_16bit_conv, 16, 0xffff, 65535.0, -32768.0, 32767.0)9293lower_x2b = [94(('b2b32', 'a'), ('b2i32', 'a')),95(('b2b1', 'a'), ('i2b1', 'a')),96(('i2b1', 'a'), ('ine', a, 0)),97(('f2b1', 'a'), ('fneu', a, 0)),98]99100no_16bit_conv += [101(('f2f32', ('u2u16', 'a@32')), ('unpack_half_2x16_split_x', 'a')),102(('u2u32', ('f2f16_rtz', 'a@32')), ('pack_half_2x16_split', 'a', 0)),103]104105lower_inot = [106(('inot', a), ('ixor', a, -1)),107]108109def main():110parser = argparse.ArgumentParser()111parser.add_argument('-p', '--import-path', required=True)112args = parser.parse_args()113sys.path.insert(0, args.import_path)114run()115116117def run():118import nir_algebraic # pylint: disable=import-error119120print('#include "dxil_nir.h"')121122print(nir_algebraic.AlgebraicPass("dxil_nir_lower_8bit_conv",123no_8bit_conv).render())124print(nir_algebraic.AlgebraicPass("dxil_nir_lower_16bit_conv",125no_16bit_conv).render())126print(nir_algebraic.AlgebraicPass("dxil_nir_lower_x2b",127lower_x2b).render())128print(nir_algebraic.AlgebraicPass("dxil_nir_lower_inot",129lower_inot).render())130131if __name__ == '__main__':132main()133134135