xref: /aosp_15_r20/external/mesa3d/src/microsoft/compiler/dxil_nir_algebraic.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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'
32b = 'b'
33c = 'c'
34
35# The nir_lower_bit_size() pass gets rid of all 8bit ALUs but insert new u2u8
36# and i2i8 operations to convert the result back to the original type after the
37# arithmetic operation is done. Those u2u8 and i2i8 operations, as any other
38# 8bit operations, are not supported by DXIL and needs to be discarded. The
39# dxil_nir_lower_8bit_conv() pass is here for that.
40# Similarly, some hardware doesn't support 16bit values
41
42no_8bit_conv = []
43no_16bit_conv = []
44
45def remove_unsupported_casts(arr, bit_size, mask, max_unsigned_float, min_signed_float, max_signed_float):
46    for outer_op_type in ('u2u', 'i2i', 'u2f', 'i2f'):
47        for outer_op_sz in (16, 32, 64):
48            if outer_op_sz == bit_size:
49                continue
50            outer_op = outer_op_type + str(int(outer_op_sz))
51            for inner_op_type in ('u2u', 'i2i'):
52                inner_op = inner_op_type + str(int(bit_size))
53                for src_sz in (16, 32, 64):
54                    if (src_sz == bit_size):
55                        continue
56                    # Coming from integral, truncate appropriately
57                    orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz))))
58                    if (outer_op[0] == 'u'):
59                        new_seq = ('iand', a, mask)
60                    else:
61                        shift = src_sz - bit_size
62                        new_seq = ('ishr', ('ishl', a, shift), shift)
63                    # Make sure the destination is the right type/size
64                    if outer_op_sz != src_sz or outer_op[2] != inner_op[0]:
65                        new_seq = (outer_op, new_seq)
66                    arr += [(orig_seq, new_seq)]
67            for inner_op_type in ('f2u', 'f2i'):
68                inner_op = inner_op_type + str(int(bit_size))
69                if (outer_op[2] == 'f'):
70                    # From float and to float, just truncate via min/max, and ensure the right float size
71                    for src_sz in (16, 32, 64):
72                        if (src_sz == bit_size):
73                            continue
74                        orig_seq = (outer_op, (inner_op, 'a@' + str(int(src_sz))))
75                        if (outer_op[0] == 'u'):
76                            new_seq = ('fmin', ('fmax', a, 0.0), max_unsigned_float)
77                        else:
78                            new_seq = ('fmin', ('fmax', a, min_signed_float), max_signed_float)
79                        if outer_op_sz != src_sz:
80                            new_seq = ('f2f' + str(int(outer_op_sz)), new_seq)
81                        arr += [(orig_seq, new_seq)]
82                else:
83                    # From float to integral, convert to integral type first, then truncate
84                    orig_seq = (outer_op, (inner_op, a))
85                    float_conv = ('f2' + inner_op[2] + str(int(outer_op_sz)), a)
86                    if (outer_op[0] == 'u'):
87                        new_seq = ('iand', float_conv, mask)
88                    else:
89                        shift = outer_op_sz - bit_size
90                        new_seq = ('ishr', ('ishl', float_conv, shift), shift)
91                    arr += [(orig_seq, new_seq)]
92
93remove_unsupported_casts(no_8bit_conv, 8, 0xff, 255.0, -128.0, 127.0)
94remove_unsupported_casts(no_16bit_conv, 16, 0xffff, 65535.0, -32768.0, 32767.0)
95
96algebraic_ops = [
97  (('b2b32', 'a'), ('b2i32', 'a')),
98  (('b2b1', 'a'), ('ine', ('b2i32', 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_algebraic",
124                                      algebraic_ops).render())
125
126if __name__ == '__main__':
127    main()
128