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] 41 42late_optimizations = [ 43 (('iadd@32', ('imul(nak_is_only_used_by_iadd)', a, b), c), 44 ('imad', a, b, c), 'nak->sm >= 70'), 45] 46 47def main(): 48 parser = argparse.ArgumentParser() 49 parser.add_argument('--out', required=True, help='Output file.') 50 parser.add_argument('-p', '--import-path', required=True) 51 args = parser.parse_args() 52 sys.path.insert(0, args.import_path) 53 54 import nir_algebraic # pylint: disable=import-error 55 56 try: 57 with open(args.out, 'w', encoding='utf-8') as f: 58 f.write('#include "nak_private.h"') 59 f.write(nir_algebraic.AlgebraicPass( 60 "nak_nir_lower_algebraic_late", 61 algebraic_lowering + late_optimizations, 62 [ 63 ("const struct nak_compiler *", "nak"), 64 ]).render()) 65 except Exception: 66 sys.exit(1) 67 68if __name__ == '__main__': 69 main() 70