Path: blob/21.2-virgl/src/panfrost/midgard/midgard_nir_algebraic.py
4564 views
#1# Copyright (C) 2018 Alyssa Rosenzweig2# Copyright (C) 2019-2020 Collabora, Ltd.3#4# Copyright (C) 2016 Intel Corporation5#6# Permission is hereby granted, free of charge, to any person obtaining a7# copy of this software and associated documentation files (the "Software"),8# to deal in the Software without restriction, including without limitation9# the rights to use, copy, modify, merge, publish, distribute, sublicense,10# and/or sell copies of the Software, and to permit persons to whom the11# Software is furnished to do so, subject to the following conditions:12#13# The above copyright notice and this permission notice (including the next14# paragraph) shall be included in all copies or substantial portions of the15# Software.16#17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS23# IN THE SOFTWARE.2425import argparse26import sys27import math2829a = 'a'30b = 'b'31c = 'c'3233algebraic = [34# Allows us to schedule as a multiply by 235(('~fadd', ('fadd', a, b), a), ('fadd', ('fadd', a, a), b)),36]3738algebraic_late = [39# ineg must be lowered late, but only for integers; floats will try to40# have modifiers attached... hence why this has to be here rather than41# a more standard lower_negate approach4243(('ineg', a), ('isub', 0, a)),4445# Likewise we want fsub lowered but not isub46(('fsub', a, b), ('fadd', a, ('fneg', b))),4748# These two special-cases save space/an op than the actual csel op +49# scheduler flexibility5051(('b32csel', a, 'b@32', 0), ('iand', a, b)),52(('b32csel', a, 0, 'b@32'), ('iand', ('inot', a), b)),5354# Fuse sat_signed. This should probably be shared with Bifrost55(('~fmin', ('fmax', a, -1.0), 1.0), ('fsat_signed_mali', a)),56(('~fmax', ('fmin', a, 1.0), -1.0), ('fsat_signed_mali', a)),5758# Fuse clamp_positive. This should probably be shared with Utgard/bifrost59(('fmax', a, 0.0), ('fclamp_pos_mali', a)),6061(('ishl', 'a@16', b), ('u2u16', ('ishl', ('u2u32', a), b))),62(('ishr', 'a@16', b), ('i2i16', ('ishr', ('i2i32', a), b))),63(('ushr', 'a@16', b), ('u2u16', ('ushr', ('u2u32', a), b))),6465(('ishl', 'a@8', b), ('u2u8', ('u2u16', ('ishl', ('u2u32', ('u2u16', a)), b)))),66(('ishr', 'a@8', b), ('i2i8', ('i2i16', ('ishr', ('i2i32', ('i2i16', a)), b)))),67(('ushr', 'a@8', b), ('u2u8', ('u2u16', ('ushr', ('u2u32', ('u2u16', a)), b)))),6869# Canonical form. The scheduler will convert back if it makes sense.70(('fmul', a, 2.0), ('fadd', a, a))71]7273# Size conversion is redundant to Midgard but needed for NIR, and writing this74# lowering in MIR would be painful without a competent builder, so eat the75# extra instruction76for sz in ('8', '16', '32'):77converted = ('u2u32', a) if sz != '32' else a78algebraic_late += [(('ufind_msb', 'a@' + sz), ('isub', 31, ('uclz', converted)))]7980# Midgard is able to type convert down by only one "step" per instruction; if81# NIR wants more than one step, we need to break up into multiple instructions.82# Nevertheless, we can do both a size step and a floating/int step at once.8384converts = []8586for op in ('u2u', 'i2i', 'f2f', 'i2f', 'u2f', 'f2i', 'f2u'):87srcsz_max = 6488dstsz_max = 6489# 8 bit float doesn't exist90srcsz_min = 8 if op[0] != 'f' else 1691dstsz_min = 8 if op[2] != 'f' else 1692dstsz = dstsz_min93# Iterate over all possible destination and source sizes94while dstsz <= dstsz_max:95srcsz = srcsz_min96while srcsz <= srcsz_max:97# Size converter lowering is only needed if src and dst sizes are98# spaced by a factor > 2.99if srcsz != dstsz and (srcsz * 2 != dstsz and srcsz != dstsz * 2):100cursz = srcsz101rule = a102# When converting down we first do the type conversion followed103# by one or more size conversions. When converting up, we do104# the type conversion at the end. This way we don't have to105# deal with the fact that f2f8 doesn't exists.106sizeconvop = op[0] + '2' + op[0] if srcsz < dstsz else op[2] + '2' + op[2]107if srcsz > dstsz and op[0] != op[2]:108rule = (op + str(int(cursz)), rule)109while cursz != dstsz:110cursz = cursz / 2 if dstsz < srcsz else cursz * 2111rule = (sizeconvop + str(int(cursz)), rule)112if srcsz < dstsz and op[0] != op[2]:113rule = (op + str(int(cursz)), rule)114converts += [((op + str(int(dstsz)), 'a@' + str(int(srcsz))), rule)]115srcsz *= 2116dstsz *= 2117118# Try to force constants to the right119constant_switch = [120# fge gets flipped to fle, so we invert to keep the order121(('fge', 'a', '#b'), (('inot', ('flt', a, b)))),122(('fge32', 'a', '#b'), (('inot', ('flt32', a, b)))),123(('ige32', 'a', '#b'), (('inot', ('ilt32', a, b)))),124(('uge32', 'a', '#b'), (('inot', ('ult32', a, b)))),125126# fge gets mapped to fle with a flip127(('flt32', '#a', 'b'), ('inot', ('fge32', a, b))),128(('ilt32', '#a', 'b'), ('inot', ('ige32', a, b))),129(('ult32', '#a', 'b'), ('inot', ('uge32', a, b)))130]131132# ..since the above switching happens after algebraic stuff is done133cancel_inot = [134(('inot', ('inot', a)), a)135]136137# Midgard scales fsin/fcos arguments by pi.138# Pass must be run only once, after the main loop139140scale_trig = [141(('fsin', a), ('fsin', ('fdiv', a, math.pi))),142(('fcos', a), ('fcos', ('fdiv', a, math.pi))),143]144145def main():146parser = argparse.ArgumentParser()147parser.add_argument('-p', '--import-path', required=True)148args = parser.parse_args()149sys.path.insert(0, args.import_path)150run()151152153def run():154import nir_algebraic # pylint: disable=import-error155156print('#include "midgard_nir.h"')157158print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_early",159algebraic).render())160161print(nir_algebraic.AlgebraicPass("midgard_nir_lower_algebraic_late",162algebraic_late + converts + constant_switch).render())163164print(nir_algebraic.AlgebraicPass("midgard_nir_scale_trig",165scale_trig).render())166167print(nir_algebraic.AlgebraicPass("midgard_nir_cancel_inot",168cancel_inot).render())169170171if __name__ == '__main__':172main()173174175