xref: /aosp_15_r20/external/mesa3d/src/nouveau/compiler/nak_nir_algebraic.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1# Copyright (C) 2023 Red Hat, Inc.
2# Copyright (C) 2021 Collabora, Ltd.
3# Copyright (C) 2016 Intel Corporation
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the "Software"),
7# to deal in the Software without restriction, including without limitation
8# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9# and/or sell copies of the Software, and to permit persons to whom the
10# Software is furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice (including the next
13# paragraph) shall be included in all copies or substantial portions of the
14# Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22# IN THE SOFTWARE.
23
24import argparse
25import sys
26
27a = 'a'
28b = 'b'
29c = 'c'
30
31# common conditions to improve readability
32volta = 'nak->sm >= 70 && nak->sm < 75'
33
34algebraic_lowering = [
35    # Volta doesn't have `IMNMX`
36    (('imin', 'a', 'b'), ('bcsel', ('ilt', a, b), a, b), volta),
37    (('imax', 'a', 'b'), ('bcsel', ('ilt', a, b), b, a), volta),
38    (('umin', 'a', 'b'), ('bcsel', ('ult', a, b), a, b), volta),
39    (('umax', 'a', 'b'), ('bcsel', ('ult', a, b), b, a), volta),
40    (('iadd', 'a@64', ('ineg', 'b@64')), ('isub', a, b)),
41]
42
43def main():
44    parser = argparse.ArgumentParser()
45    parser.add_argument('--out', required=True, help='Output file.')
46    parser.add_argument('-p', '--import-path', required=True)
47    args = parser.parse_args()
48    sys.path.insert(0, args.import_path)
49
50    import nir_algebraic  # pylint: disable=import-error
51
52    try:
53        with open(args.out, 'w', encoding='utf-8') as f:
54            f.write('#include "nak_private.h"')
55            f.write(nir_algebraic.AlgebraicPass(
56                "nak_nir_lower_algebraic_late",
57                algebraic_lowering,
58                [
59                    ("const struct nak_compiler *", "nak"),
60                ]).render())
61    except Exception:
62        sys.exit(1)
63
64if __name__ == '__main__':
65    main()
66