| 1 |
# Copyright (C) 2021 Collabora, Ltd. |
| 2 |
# Copyright (C) 2016 Intel Corporation |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import argparse |
| 6 |
import sys |
| 7 |
import math |
| 8 |
|
| 9 |
a = 'a' |
| 10 |
b = 'b' |
| 11 |
c = 'c' |
| 12 |
d = 'd' |
| 13 |
|
| 14 |
# In general, bcsel is cheaper than bitwise arithmetic on Mali. On |
| 15 |
# Bifrost, we can implement bcsel as either CSEL or MUX to schedule to either |
| 16 |
# execution unit. On Valhall, bitwise arithmetic may be on the SFU whereas MUX |
| 17 |
# is on the higher throughput CVT unit. We get a zero argument for free relative |
| 18 |
# to the bitwise op, which would be LSHIFT_* internally taking a zero anyway. |
| 19 |
# |
| 20 |
# As such, it's beneficial to reexpress bitwise arithmetic of booleans as bcsel. |
| 21 |
opt_bool_bitwise = [ |
| 22 |
(('iand', 'a@1', 'b@1'), ('bcsel', a, b, False)), |
| 23 |
(('ior', 'a@1', 'b@1'), ('bcsel', a, a, b)), |
| 24 |
(('iand', 'a@1', ('inot', 'b@1')), ('bcsel', b, 0, a)), |
| 25 |
(('ior', 'a@1', ('inot', 'b@1')), ('bcsel', b, a, True)), |
| 26 |
] |
| 27 |
|
| 28 |
algebraic_late = [ |
| 29 |
# Canonical form. The scheduler will convert back if it makes sense. |
| 30 |
(('fmul', a, 2.0), ('fadd', a, a)), |
| 31 |
|
| 32 |
# Fuse Mali-specific clamps |
| 33 |
(('fmin', ('fmax', a, -1.0), 1.0), ('fsat_signed', a)), |
| 34 |
(('fmax', ('fmin', a, 1.0), -1.0), ('fsat_signed', a)), |
| 35 |
(('fmax', a, 0.0), ('fclamp_pos', a)), |
| 36 |
|
| 37 |
(('b32csel', 'b@32', ('iadd', 'a@32', 1), a), ('iadd', a, ('b2i32', b))), |
| 38 |
|
| 39 |
# We don't have an 8-bit CSEL, so this is the best we can do. |
| 40 |
# Note that we use 8-bit booleans internally to preserve vectorization. |
| 41 |
(('imin', 'a@8', 'b@8'), ('b8csel', ('ilt8', a, b), a, b)), |
| 42 |
(('imax', 'a@8', 'b@8'), ('b8csel', ('ilt8', a, b), b, a)), |
| 43 |
(('umin', 'a@8', 'b@8'), ('b8csel', ('ult8', a, b), a, b)), |
| 44 |
(('umax', 'a@8', 'b@8'), ('b8csel', ('ult8', a, b), b, a)), |
| 45 |
|
| 46 |
# Floats are at minimum 16-bit, which means when converting to an 8-bit |
| 47 |
# integer, the vectorization changes. So there's no one-shot hardware |
| 48 |
# instruction for f2i8. Instead, lower to two NIR instructions that map |
| 49 |
# directly to the hardware. |
| 50 |
(('f2i8', a), ('i2i8', ('f2i16', a))), |
| 51 |
(('f2u8', a), ('u2u8', ('f2u16', a))), |
| 52 |
|
| 53 |
# XXX: Duplicate of nir_lower_pack |
| 54 |
(('unpack_64_2x32', a), ('vec2', ('unpack_64_2x32_split_x', a), |
| 55 |
('unpack_64_2x32_split_y', a))), |
| 56 |
|
| 57 |
# On v11+, all non integer variant to convert to F32 are gone except for S32_TO_F32. |
| 58 |
(('i2f32', 'a@8'), ('i2f32', ('i2i32', a)), 'gpu_arch >= 11'), |
| 59 |
(('i2f32', 'a@16'), ('i2f32', ('i2i32', a)), 'gpu_arch >= 11'), |
| 60 |
(('u2f32', 'a@8'), ('u2f32', ('u2u32', a)), 'gpu_arch >= 11'), |
| 61 |
(('u2f32', 'a@16'), ('u2f32', ('u2u32', a)), 'gpu_arch >= 11'), |
| 62 |
|
| 63 |
# On v11+, all non integer variant to convert to F16 are gone except for S32_TO_F32. |
| 64 |
(('i2f16', 'a'), ('f2f16', ('i2f32', ('i2i32', a))), 'gpu_arch >= 11'), |
| 65 |
(('u2f16', 'a'), ('f2f16', ('u2f32', ('u2u32', a))), 'gpu_arch >= 11'), |
| 66 |
|
| 67 |
# We don't have S32_TO_F16 on any arch |
| 68 |
(('i2f16', 'a@32'), ('f2f16', ('i2f32', a))), |
| 69 |
(('u2f16', 'a@32'), ('f2f16', ('u2f32', a))), |
| 70 |
|
| 71 |
# On v11+, V2F16_TO_V2S16 / V2F16_TO_V2U16 are gone |
| 72 |
(('f2i16', 'a@16'), ('f2i16', ('f2f32', a)), 'gpu_arch >= 11'), |
| 73 |
(('f2u16', 'a@16'), ('f2u16', ('f2f32', a)), 'gpu_arch >= 11'), |
| 74 |
|
| 75 |
# On v11+, V2F32_TO_V2S16 is gone |
| 76 |
(('pack_half_2x16_split', a, b), ('pack_32_2x16_split', ('f2f16', a), ('f2f16', b)), 'gpu_arch >= 11'), |
| 77 |
|
| 78 |
# On v11+, F16_TO_S32/F16_TO_U32 is gone but we still have F32_TO_S32/F32_TO_U32 |
| 79 |
(('f2i32', 'a@16'), ('f2i32', ('f2f32', a)), 'gpu_arch >= 11'), |
| 80 |
(('f2u32', 'a@16'), ('f2u32', ('f2f32', a)), 'gpu_arch >= 11'), |
| 81 |
|
| 82 |
# On v11+, because FROUND.v2f16 is gone we end up with precision issues. |
| 83 |
# We lower ffract here instead to ensure lower_bit_size has been performed. |
| 84 |
(('ffract', a), ('fadd', a, ('fneg', ('ffloor', a))), 'gpu_arch >= 11'), |
| 85 |
] |
| 86 |
|
| 87 |
# nir_lower_bool_to_bitsize can generate needless conversions. |
| 88 |
for bits in [8, 16, 32]: |
| 89 |
algebraic_late += [ |
| 90 |
((f'i2i{bits}', f'a@{bits}'), a) |
| 91 |
] |
| 92 |
|
| 93 |
# On v11+, ICMP_OR.v4u8 was removed |
| 94 |
for cond in ['ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']: |
| 95 |
convert_8bit = 'u2u8' |
| 96 |
convert_16bit = 'u2u16' |
| 97 |
|
| 98 |
if cond[0] == 'i': |
| 99 |
convert_8bit = 'i2i8' |
| 100 |
convert_16bit = 'i2i16' |
| 101 |
|
| 102 |
algebraic_late += [ |
| 103 |
((f'{cond}8', a, b), (convert_8bit, (f'{cond}16', (convert_16bit, a), (convert_16bit, b))), 'gpu_arch >= 11'), |
| 104 |
] |
| 105 |
|
| 106 |
# Handling all combinations of boolean and float sizes for b2f is nontrivial. |
| 107 |
# bcsel has the same problem in more generality; lower b2f to bcsel in NIR to |
| 108 |
# reuse the efficient implementations of bcsel. This includes special handling |
| 109 |
# to allow vectorization in places the hardware does not directly. |
| 110 |
# |
| 111 |
# Because this lowering must happen late, NIR won't squash inot in |
| 112 |
# automatically. Do so explicitly. (The more specific pattern must be first.) |
| 113 |
for bsz in [8, 16, 32]: |
| 114 |
for fsz in [16, 32]: |
| 115 |
if bsz == fsz: |
| 116 |
a_fsz = 'a' |
| 117 |
else: |
| 118 |
a_fsz = (f'i2i{fsz}', a) |
| 119 |
|
| 120 |
algebraic_late += [ |
| 121 |
((f'b2f{fsz}', ('inot', f'a@{bsz}')), (f'b{fsz}csel', a_fsz, 0.0, 1.0)), |
| 122 |
((f'b2f{fsz}', f'a@{bsz}'), (f'b{fsz}csel', a_fsz, 1.0, 0.0)), |
| 123 |
] |
| 124 |
|
| 125 |
# Bifrost LDEXP.v2f16 takes i16 exponent, while nir_op_ldexp takes i32. Lower |
| 126 |
# to nir_op_ldexp16_pan. |
| 127 |
# |
| 128 |
# From the GLSL 4.60 spec (section 8.3): |
| 129 |
# "If exp is greater than +128 (single-precision) or +1024 |
| 130 |
# (double-precision), the value returned is undefined. If exp is less |
| 131 |
# than -126 (single- precision) or -1022 (double-precision), the value |
| 132 |
# returned may be flushed to zero." |
| 133 |
# |
| 134 |
# So we can't just truncate the exponent. Overflow is undefined behavior, but |
| 135 |
# we need to return signed zero on underflow. If exp32 < INT16_MIN, we can use |
| 136 |
# any 16-bit exponent that's sufficiently small to send all f16 values to zero. |
| 137 |
# |
| 138 |
# If we test exp32 < INT16_MIN directly, the comparison could not be |
| 139 |
# vectorized, so instead we test the upper half. |
| 140 |
# TODO: some possible values for -127 can be encoded as small |
| 141 |
# immediates on valhall. None of the usable immediates have replicated |
| 142 |
# i16 lanes, but for example 0xFAFCFDFE would be {-1284,-514}, both of |
| 143 |
# which are small enough. |
| 144 |
algebraic_late += [ |
| 145 |
(('ldexp', 'a@16', b), ('ldexp16_pan', a, ('b16csel', ('ilt16', ('unpack_32_2x16_split_y', b), -1), -127, ('i2i16', b)))) |
| 146 |
] |
| 147 |
|
| 148 |
|
| 149 |
def main(): |
| 150 |
parser = argparse.ArgumentParser() |
| 151 |
parser.add_argument('-p', '--import-path', required=True) |
| 152 |
args = parser.parse_args() |
| 153 |
sys.path.insert(0, args.import_path) |
| 154 |
run() |
| 155 |
|
| 156 |
|
| 157 |
def run(): |
| 158 |
import nir_algebraic # pylint: disable=import-error |
| 159 |
|
| 160 |
print('#include "bifrost_nir.h"') |
| 161 |
|
| 162 |
print(nir_algebraic.AlgebraicPass("bifrost_nir_opt_boolean_bitwise", |
| 163 |
opt_bool_bitwise).render()) |
| 164 |
print(nir_algebraic.AlgebraicPass("bifrost_nir_lower_algebraic_late", |
| 165 |
algebraic_late, |
| 166 |
[ |
| 167 |
("unsigned ", "gpu_arch") |
| 168 |
]).render()) |
| 169 |
|
| 170 |
if __name__ == '__main__': |
| 171 |
main() |